Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pager usage is off.
- Timing is on.
- SET
- Time: 78.776 ms
- SET
- Time: 81.274 ms
- DO $$
- DECLARE
- tbl text := quote_ident('entity_associations');
- target_col text := quote_ident('target_entity_id');
- new_col text := quote_ident('tmp_promote_target_entity_id');
- BEGIN
- -- If the target column does not exist, fail.
- IF NOT EXISTS (
- SELECT 1 FROM information_schema.columns
- WHERE table_name = tbl
- AND column_name = target_col
- ) THEN
- RAISE EXCEPTION 'target column % does not exist', target_col;
- END IF;
- -- If the target column exists, but is not of type integer, fail.
- IF NOT EXISTS (
- SELECT 1 FROM information_schema.columns
- WHERE table_name = tbl
- AND column_name = target_col
- AND data_type = 'integer'
- ) THEN
- RAISE EXCEPTION 'target column % must have type integer', target_col;
- END IF;
- -- If the new column already exists, fail.
- -- NB: It is intentional that a user must deliberately delete a leftover new
- -- column from a previous, failed execution of this script. Automatic cleanup
- -- could result in catastrophe if the arguments to target_col and new_col were
- -- accidentally swapped!
- IF EXISTS (
- SELECT 1 FROM information_schema.columns
- WHERE table_name = tbl
- AND column_name = new_col
- ) THEN
- RAISE EXCEPTION
- 'Temp column % already exists! To continue, confirm that this is in fact a temp column, and remove it.',
- new_col;
- END IF;
- END
- $$;
- DO
- Time: 94.230 ms
- SELECT run_command_on_workers(
- $cmd$
- CREATE OR REPLACE FUNCTION copy_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations()
- RETURNS TRIGGER AS
- $copy_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations$
- BEGIN
- NEW.tmp_promote_target_entity_id := NEW.target_entity_id;
- RETURN NEW;
- END;
- $copy_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations$ LANGUAGE plpgsql;
- $cmd$
- );
- run_command_on_workers
- -----------------------------------------------------------------------
- (ec2-18-210-81-75.compute-1.amazonaws.com,5432,t,"CREATE FUNCTION")
- (ec2-18-232-211-178.compute-1.amazonaws.com,5432,t,"CREATE FUNCTION")
- (2 rows)
- Time: 84.638 ms
- ALTER TABLE entity_associations ADD COLUMN tmp_promote_target_entity_id bigint;
- ALTER TABLE
- Time: 90.755 ms
- SELECT run_command_on_shards(
- 'entity_associations',
- $cmd$
- DROP TRIGGER IF EXISTS backfill_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations ON %s
- $cmd$
- );
- run_command_on_shards
- ---------------------------
- (102224,t,"DROP TRIGGER")
- (102225,t,"DROP TRIGGER")
- (102226,t,"DROP TRIGGER")
- (102227,t,"DROP TRIGGER")
- (102228,t,"DROP TRIGGER")
- (102229,t,"DROP TRIGGER")
- (102230,t,"DROP TRIGGER")
- (102231,t,"DROP TRIGGER")
- (8 rows)
- Time: 91.372 ms
- SELECT run_command_on_shards('entity_associations', $cmd$ CREATE TRIGGER backfill_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations
- BEFORE INSERT OR UPDATE ON %s
- FOR EACH ROW
- EXECUTE PROCEDURE copy_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations (target_entity_id, tmp_promote_target_entity_id); $cmd$
- );
- run_command_on_shards
- -----------------------------
- (102224,t,"CREATE TRIGGER")
- (102225,t,"CREATE TRIGGER")
- (102226,t,"CREATE TRIGGER")
- (102227,t,"CREATE TRIGGER")
- (102228,t,"CREATE TRIGGER")
- (102229,t,"CREATE TRIGGER")
- (102230,t,"CREATE TRIGGER")
- (102231,t,"CREATE TRIGGER")
- (8 rows)
- Time: 86.881 ms
- Done!
Add Comment
Please, Sign In to add comment