From 0fa0a517ace813064cffd2a35c85a73c7387e929 Mon Sep 17 00:00:00 2001 From: Jim Ladd Date: Mon, 8 Mar 2021 14:00:57 -0800 Subject: [PATCH] create tmp schema to reference when creating partitioned table * if we use the actual old job events table and make tweaks to its schema namely, dropping the pkey constraint, then when we go to migrate the old job events we will be forcing postgres to do a sequential scan on the old table, which effectively causes the migration to hang --- awx/main/migrations/0130_event_partitions.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/awx/main/migrations/0130_event_partitions.py b/awx/main/migrations/0130_event_partitions.py index 1fbf7cbc08..571d06924f 100644 --- a/awx/main/migrations/0130_event_partitions.py +++ b/awx/main/migrations/0130_event_partitions.py @@ -37,21 +37,32 @@ def migrate_event_data(apps, schema_editor): f'ALTER TABLE {tblname} RENAME TO _unpartitioned_{tblname}' ) + # create a copy of the table that we will use as a reference for schema + # otherwise, the schema changes we would make on the old jobevents table + # (namely, dropping the primary key constraint) would cause the migration + # to suffer a serious performance degradation + cursor.execute( + f'CREATE TABLE tmp_{tblname} ' + f'(LIKE _unpartitioned_{tblname} INCLUDING ALL)' + ) + # drop primary key constraint; in a partioned table # constraints must include the partition key itself # TODO: do more generic search for pkey constraints # instead of hardcoding this one that applies to main_jobevent cursor.execute( - f'ALTER TABLE _unpartitioned_{tblname} DROP CONSTRAINT {tblname}_pkey1' + f'ALTER TABLE tmp_{tblname} DROP CONSTRAINT {tblname}_pkey1' ) # create parent table cursor.execute( f'CREATE TABLE {tblname} ' - f'(LIKE _unpartitioned_{tblname} INCLUDING ALL, job_created TIMESTAMP WITH TIME ZONE NOT NULL) ' + f'(LIKE tmp_{tblname} INCLUDING ALL, job_created TIMESTAMP WITH TIME ZONE NOT NULL) ' f'PARTITION BY RANGE(job_created);' ) + cursor.execute(f'DROP TABLE tmp_{tblname}') + # let's go ahead and add and subtract a few indexes while we're here cursor.execute(f'CREATE INDEX {tblname}_modified_idx ON {tblname} (modified);') cursor.execute(f'DROP INDEX IF EXISTS {tblname}_job_id_brin_idx;') @@ -64,7 +75,7 @@ def migrate_event_data(apps, schema_editor): current_time = now() - # .. as well as initial partition containing all existing events + # create initial partition containing all existing events epoch = datetime.utcfromtimestamp(0) create_partition(tblname, epoch, current_time, 'old_events')