Skip to content

Commit

Permalink
Stop using CONCURRENTLY when creating indexes
Browse files Browse the repository at this point in the history
CONCURRENT indexes are only valid if they are not created inside a
transaction block.

psycopg2 has started to automatically emit BEGIN / END transaction when
using the "with "  context manager, thus causing explicit transactions
to happen for us here.

See: psycopg/psycopg2#941

This means that we now need to stop using concurrently as we may or may
not be inside a transaction block.
  • Loading branch information
Spindel committed Jul 5, 2021
1 parent 0d06427 commit 409cb95
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 3 additions & 4 deletions housekeeper/housekeeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,19 @@ def clean_old_indexes(table="history", year=2011, month=12):
yield cleanup.format(oldindex)


def ensure_btree_index(table="history", year=2011, month=12, concurrently=True):
def ensure_btree_index(table="history", year=2011, month=12):
index = get_index_name(table=table, year=year, month=month, kind="btree")
table = get_table_name(table=table, year=year, month=month)
conc = "CONCURRENTLY" if concurrently else ""
with log_state(step="ensure_btree_index", table=table, index=index):
yield f"CREATE INDEX {conc} IF NOT EXISTS {index} on {table} using btree (itemid, clock);"
yield f"CREATE INDEX IF NOT EXISTS {index} on {table} using btree (itemid, clock);"


def ensure_brin_index(table="history", year=2011, month=12):
index = get_index_name(table=table, year=year, month=month, kind="brin")
table = get_table_name(table=table, year=year, month=month)
with log_state(step="ensure_brin_index", index=index, table=table):
yield (
f"CREATE INDEX CONCURRENTLY IF NOT EXISTS {index} on {table} "
f"CREATE INDEX IF NOT EXISTS {index} on {table} "
f"USING brin (itemid, clock) WITH (pages_per_range='16');"
)

Expand Down
2 changes: 1 addition & 1 deletion housekeeper/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def gen_partitions(table="history_part"):
)

yield from ensure_btree_index(
table=table, year=date.year, month=date.month, concurrently=False
table=table, year=date.year, month=date.month
)


Expand Down

0 comments on commit 409cb95

Please sign in to comment.