From 29298585a90106707d8f3c54e8f1c0a1f7f300eb Mon Sep 17 00:00:00 2001 From: Rui Zhao Date: Thu, 26 Nov 2026 23:44:56 +0800 Subject: [PATCH] Check the old rows after a batched cross-partition update The postgres_fdw test checks that the destination receives one INSERT for two moved rows. Also inspect the parent's OLD TABLE so the test would catch a missing row or a new partition key in the old rows. --- contrib/postgres_fdw/expected/postgres_fdw.out | 15 ++++++++++++--- contrib/postgres_fdw/sql/postgres_fdw.sql | 13 +++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index ba64a2c66a..3c213136fb 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -12279,10 +12279,18 @@ DROP VIEW batch_cp_upd_view; -- Transition tables on the parent disable batching only if the routed rows -- may have to be captured in a NEW TABLE. An OLD TABLE never contains them -- (a moved row is captured when deleting it from the source partition), so --- expect a single INSERT on batch_cp_upd_test1 for both moved rows. +-- expect a single INSERT on batch_cp_upd_test1 for both moved rows, and +-- both old partition keys in the parent's transition table. +CREATE FUNCTION log_old_keys() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + INSERT INTO public.cmdlog + SELECT 'old keys: ' || array_agg(a ORDER BY a)::text FROM oldtab; + RETURN NULL; +END; +$$; CREATE TRIGGER oldtab_trig AFTER UPDATE ON batch_cp_upd_test REFERENCING OLD TABLE AS oldtab - FOR EACH STATEMENT EXECUTE FUNCTION log_stmt(); + FOR EACH STATEMENT EXECUTE FUNCTION log_old_keys(); INSERT INTO batch_cp_upd_test VALUES (2), (2); TRUNCATE cmdlog; UPDATE batch_cp_upd_test SET a = 1 WHERE a = 2; @@ -12290,10 +12298,11 @@ SELECT * FROM cmdlog ORDER BY 1; cmd ------------------------------ INSERT on batch_cp_upd_test1 - UPDATE on batch_cp_upd_test + old keys: {2,2} (2 rows) DROP TRIGGER oldtab_trig ON batch_cp_upd_test; +DROP FUNCTION log_old_keys(); -- With a NEW TABLE, batching is disabled, and the per-row code refuses to -- collect the rows moved to the foreign partition. CREATE TRIGGER newtab_trig AFTER UPDATE ON batch_cp_upd_test diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 5d421aae54..b77cb09b28 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -4261,15 +4261,24 @@ DROP VIEW batch_cp_upd_view; -- Transition tables on the parent disable batching only if the routed rows -- may have to be captured in a NEW TABLE. An OLD TABLE never contains them -- (a moved row is captured when deleting it from the source partition), so --- expect a single INSERT on batch_cp_upd_test1 for both moved rows. +-- expect a single INSERT on batch_cp_upd_test1 for both moved rows, and +-- both old partition keys in the parent's transition table. +CREATE FUNCTION log_old_keys() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + INSERT INTO public.cmdlog + SELECT 'old keys: ' || array_agg(a ORDER BY a)::text FROM oldtab; + RETURN NULL; +END; +$$; CREATE TRIGGER oldtab_trig AFTER UPDATE ON batch_cp_upd_test REFERENCING OLD TABLE AS oldtab - FOR EACH STATEMENT EXECUTE FUNCTION log_stmt(); + FOR EACH STATEMENT EXECUTE FUNCTION log_old_keys(); INSERT INTO batch_cp_upd_test VALUES (2), (2); TRUNCATE cmdlog; UPDATE batch_cp_upd_test SET a = 1 WHERE a = 2; SELECT * FROM cmdlog ORDER BY 1; DROP TRIGGER oldtab_trig ON batch_cp_upd_test; +DROP FUNCTION log_old_keys(); -- With a NEW TABLE, batching is disabled, and the per-row code refuses to -- collect the rows moved to the foreign partition.