Skip to content

Commit

Permalink
switch partitioning to column
Browse files Browse the repository at this point in the history
  • Loading branch information
bailliekova committed Feb 1, 2025
1 parent d11a400 commit 9ca3669
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
8 changes: 5 additions & 3 deletions gerrydb_meta/crud/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def create(
canonical_ref.col_id = col.col_id
db.flush()

#create partition
db.execute(create_column_value_partition_text(column_id=col.col_id))

# Create additional aliases (non-canonical references) to the column.
if obj_in.aliases:
self._add_aliases(
Expand Down Expand Up @@ -204,9 +207,8 @@ def set_values(
# Add the new column values and invalidate the old ones where present.
geo_ids = [geo.geo_id for geo, _ in values]

# make sure partitions exist for all geos
for geo_id in set(geo_ids):
db.execute(create_column_value_partition_text(geo_id=geo_id))
# make sure partition exists for column
db.execute(create_column_value_partition_text(column_id=col.col_id))

with_tuples = (
db.query(
Expand Down
3 changes: 0 additions & 3 deletions gerrydb_meta/crud/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from gerrydb_meta import models, schemas
from gerrydb_meta.crud.base import NamespacedCRBase, normalize_path
from gerrydb_meta.exceptions import BulkCreateError, BulkPatchError
from gerrydb_meta.utils import create_column_value_partition_text

log = logging.getLogger()

Expand Down Expand Up @@ -78,8 +77,6 @@ def create_bulk(
],
)
)
for geo in geos:
db.execute(create_column_value_partition_text(geo_id=geo.geo_id))

try:
geo_versions = list(
Expand Down
2 changes: 1 addition & 1 deletion gerrydb_meta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ class ColumnValue(Base):
__tablename__ = "column_value"
__table_args__ = (
UniqueConstraint("col_id", "geo_id", "valid_from"),
{"postgresql_partition_by": "LIST (geo_id)"},
{"postgresql_partition_by": "LIST (col_id)"},
)

col_id: Mapped[int] = mapped_column(
Expand Down
4 changes: 2 additions & 2 deletions gerrydb_meta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from gerrydb_meta import models


def create_column_value_partition_text(geo_id: int):
def create_column_value_partition_text(column_id: int):
table_name = models.ColumnValue.__table__.name
sql = f"CREATE TABLE IF NOT EXISTS {models.SCHEMA}.{table_name}_{geo_id} PARTITION OF {models.SCHEMA}.{table_name} FOR VALUES IN ({geo_id})"
sql = f"CREATE TABLE IF NOT EXISTS {models.SCHEMA}.{table_name}_{column_id} PARTITION OF {models.SCHEMA}.{table_name} FOR VALUES IN ({column_id})"
return text(sql)
4 changes: 2 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


def test_create_column_value_partition_text():
geo_id = 42
got = create_column_value_partition_text(geo_id=geo_id)
column_id = 42
got = create_column_value_partition_text(column_id=column_id)
wanted = text(
"CREATE TABLE IF NOT EXISTS gerrydb.column_value_42 PARTITION OF gerrydb.column_value FOR VALUES IN (42)"
)
Expand Down

0 comments on commit 9ca3669

Please sign in to comment.