From 7a7ede3ad24c0b6c8a976e46cca99515c0e008c3 Mon Sep 17 00:00:00 2001 From: naisila Date: Thu, 4 Aug 2022 16:47:57 +0300 Subject: [PATCH] Fixes tests for ALTER TRIGGER RENAME consistency for part. tables Relevant PG commit: 80ba4bb383538a2ee846fece6a7b8da9518b6866 --- .../regress/expected/distributed_triggers.out | 18 ++--- src/test/regress/expected/pg15.out | 81 ++++++++++++++++++- src/test/regress/sql/distributed_triggers.sql | 7 +- src/test/regress/sql/pg15.sql | 56 +++++++++++++ 4 files changed, 149 insertions(+), 13 deletions(-) diff --git a/src/test/regress/expected/distributed_triggers.out b/src/test/regress/expected/distributed_triggers.out index 002c4e465..c53b91acb 100644 --- a/src/test/regress/expected/distributed_triggers.out +++ b/src/test/regress/expected/distributed_triggers.out @@ -733,33 +733,29 @@ SELECT operation_type, product_sku, state_code FROM record_sale ORDER BY 1,2,3; -- --Test ALTER TRIGGER -- +-- Pre PG15, renaming the trigger on the parent table didn't rename the same trigger on +-- the children as well. Hence, let's not print the trigger names of the children +-- In PG15, rename is consistent for all partitions of the parent +-- This is tested in pg15.sql file. CREATE VIEW sale_triggers AS SELECT tgname, tgrelid::regclass, tgenabled FROM pg_trigger - WHERE tgrelid::regclass::text like 'sale%' + WHERE tgrelid::regclass::text = 'sale' ORDER BY 1, 2; SELECT * FROM sale_triggers ORDER BY 1,2; tgname | tgrelid | tgenabled --------------------------------------------------------------------- record_sale_trigger | sale | O - record_sale_trigger | sale_newyork | O - record_sale_trigger | sale_california | O truncate_trigger_xxxxxxx | sale | O - truncate_trigger_xxxxxxx | sale_california | O - truncate_trigger_xxxxxxx | sale_newyork | O -(6 rows) +(2 rows) ALTER TRIGGER "record_sale_trigger" ON "distributed_triggers"."sale" RENAME TO "new_record_sale_trigger"; SELECT * FROM sale_triggers ORDER BY 1,2; tgname | tgrelid | tgenabled --------------------------------------------------------------------- new_record_sale_trigger | sale | O - record_sale_trigger | sale_newyork | O - record_sale_trigger | sale_california | O truncate_trigger_xxxxxxx | sale | O - truncate_trigger_xxxxxxx | sale_california | O - truncate_trigger_xxxxxxx | sale_newyork | O -(6 rows) +(2 rows) CREATE EXTENSION seg; ALTER TRIGGER "emptest_audit" ON "emptest" DEPENDS ON EXTENSION seg; diff --git a/src/test/regress/expected/pg15.out b/src/test/regress/expected/pg15.out index f7aede2e1..4c3615787 100644 --- a/src/test/regress/expected/pg15.out +++ b/src/test/regress/expected/pg15.out @@ -93,8 +93,87 @@ SELECT result FROM run_command_on_all_nodes(' (3 rows) +-- +-- In PG15, Renaming triggers on partitioned tables had two problems +-- recurses to renaming the triggers on the partitions as well. +-- Here we test that distributed triggers behave the same way. +-- Relevant PG commit: +-- 80ba4bb383538a2ee846fece6a7b8da9518b6866 +-- +SET citus.enable_unsafe_triggers TO true; +CREATE TABLE sale( + sale_date date not null, + state_code text, + product_sku text, + units integer) + PARTITION BY list (state_code); +ALTER TABLE sale ADD CONSTRAINT sale_pk PRIMARY KEY (state_code, sale_date); +CREATE TABLE sale_newyork PARTITION OF sale FOR VALUES IN ('NY'); +CREATE TABLE sale_california PARTITION OF sale FOR VALUES IN ('CA'); +CREATE TABLE record_sale( + operation_type text not null, + product_sku text, + state_code text, + units integer, + PRIMARY KEY(state_code, product_sku, operation_type, units)); +SELECT create_distributed_table('sale', 'state_code'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +SELECT create_distributed_table('record_sale', 'state_code', colocate_with := 'sale'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +CREATE OR REPLACE FUNCTION record_sale() +RETURNS trigger +AS $$ +BEGIN + INSERT INTO pg15.record_sale(operation_type, product_sku, state_code, units) + VALUES (TG_OP, NEW.product_sku, NEW.state_code, NEW.units); + RETURN NULL; +END; +$$ LANGUAGE plpgsql; +CREATE TRIGGER record_sale_trigger +AFTER INSERT OR UPDATE OR DELETE ON sale +FOR EACH ROW EXECUTE FUNCTION pg15.record_sale(); +CREATE VIEW sale_triggers AS + SELECT tgname, tgrelid::regclass, tgenabled + FROM pg_trigger + WHERE tgrelid::regclass::text like 'sale%' + ORDER BY 1, 2; +SELECT * FROM sale_triggers ORDER BY 1, 2; + tgname | tgrelid | tgenabled +--------------------------------------------------------------------- + record_sale_trigger | sale | O + record_sale_trigger | sale_newyork | O + record_sale_trigger | sale_california | O + truncate_trigger_xxxxxxx | sale | O + truncate_trigger_xxxxxxx | sale_california | O + truncate_trigger_xxxxxxx | sale_newyork | O +(6 rows) + +ALTER TRIGGER "record_sale_trigger" ON "pg15"."sale" RENAME TO "new_record_sale_trigger"; +SELECT * FROM sale_triggers ORDER BY 1, 2; + tgname | tgrelid | tgenabled +--------------------------------------------------------------------- + new_record_sale_trigger | sale | O + new_record_sale_trigger | sale_newyork | O + new_record_sale_trigger | sale_california | O + truncate_trigger_xxxxxxx | sale | O + truncate_trigger_xxxxxxx | sale_california | O + truncate_trigger_xxxxxxx | sale_newyork | O +(6 rows) + -- Clean up DROP SCHEMA pg15 CASCADE; -NOTICE: drop cascades to 2 other objects +NOTICE: drop cascades to 6 other objects DETAIL: drop cascades to collation german_phonebook_test drop cascades to collation default_provider +drop cascades to table sale +drop cascades to table record_sale +drop cascades to function record_sale() +drop cascades to view sale_triggers diff --git a/src/test/regress/sql/distributed_triggers.sql b/src/test/regress/sql/distributed_triggers.sql index e070a7e2e..2194d94d8 100644 --- a/src/test/regress/sql/distributed_triggers.sql +++ b/src/test/regress/sql/distributed_triggers.sql @@ -415,10 +415,15 @@ SELECT operation_type, product_sku, state_code FROM record_sale ORDER BY 1,2,3; -- --Test ALTER TRIGGER -- +-- Pre PG15, renaming the trigger on the parent table didn't rename the same trigger on +-- the children as well. Hence, let's not print the trigger names of the children +-- In PG15, rename is consistent for all partitions of the parent +-- This is tested in pg15.sql file. + CREATE VIEW sale_triggers AS SELECT tgname, tgrelid::regclass, tgenabled FROM pg_trigger - WHERE tgrelid::regclass::text like 'sale%' + WHERE tgrelid::regclass::text = 'sale' ORDER BY 1, 2; SELECT * FROM sale_triggers ORDER BY 1,2; diff --git a/src/test/regress/sql/pg15.sql b/src/test/regress/sql/pg15.sql index e1b1b30a8..ca47c03df 100644 --- a/src/test/regress/sql/pg15.sql +++ b/src/test/regress/sql/pg15.sql @@ -58,5 +58,61 @@ SELECT result FROM run_command_on_all_nodes(' SELECT colliculocale FROM pg_collation WHERE collname = ''default_provider''; '); +-- +-- In PG15, Renaming triggers on partitioned tables had two problems +-- recurses to renaming the triggers on the partitions as well. +-- Here we test that distributed triggers behave the same way. +-- Relevant PG commit: +-- 80ba4bb383538a2ee846fece6a7b8da9518b6866 +-- + +SET citus.enable_unsafe_triggers TO true; + +CREATE TABLE sale( + sale_date date not null, + state_code text, + product_sku text, + units integer) + PARTITION BY list (state_code); + +ALTER TABLE sale ADD CONSTRAINT sale_pk PRIMARY KEY (state_code, sale_date); + +CREATE TABLE sale_newyork PARTITION OF sale FOR VALUES IN ('NY'); +CREATE TABLE sale_california PARTITION OF sale FOR VALUES IN ('CA'); + +CREATE TABLE record_sale( + operation_type text not null, + product_sku text, + state_code text, + units integer, + PRIMARY KEY(state_code, product_sku, operation_type, units)); + +SELECT create_distributed_table('sale', 'state_code'); +SELECT create_distributed_table('record_sale', 'state_code', colocate_with := 'sale'); + +CREATE OR REPLACE FUNCTION record_sale() +RETURNS trigger +AS $$ +BEGIN + INSERT INTO pg15.record_sale(operation_type, product_sku, state_code, units) + VALUES (TG_OP, NEW.product_sku, NEW.state_code, NEW.units); + RETURN NULL; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER record_sale_trigger +AFTER INSERT OR UPDATE OR DELETE ON sale +FOR EACH ROW EXECUTE FUNCTION pg15.record_sale(); + +CREATE VIEW sale_triggers AS + SELECT tgname, tgrelid::regclass, tgenabled + FROM pg_trigger + WHERE tgrelid::regclass::text like 'sale%' + ORDER BY 1, 2; + +SELECT * FROM sale_triggers ORDER BY 1, 2; +ALTER TRIGGER "record_sale_trigger" ON "pg15"."sale" RENAME TO "new_record_sale_trigger"; +SELECT * FROM sale_triggers ORDER BY 1, 2; + -- Clean up DROP SCHEMA pg15 CASCADE;