pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Collect ALTER PUBLICATION commands for event triggers
11+ messages / 5 participants
[nested] [flat]

* Collect ALTER PUBLICATION commands for event triggers
@ 2026-07-24 02:06  Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 3 replies; 11+ messages in thread

From: Fujii Masao @ 2026-07-24 02:06 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi,

I found two cases where ALTER PUBLICATION fires ddl_command_end,
but pg_event_trigger_ddl_commands() does not report the corresponding
ALTER PUBLICATION command.

Attached are two patches.

0001 patch: Collect ALTER PUBLICATION mapping drops

When an ALTER PUBLICATION command only removes publication membership mappings,
the publication itself is not collected as a ddl_command_end object.

For example:
    ALTER PUBLICATION p DROP TABLE t1;
    ALTER PUBLICATION p SET TABLE t2;

sql_drop reports the removed publication relation,
but pg_event_trigger_ddl_commands() reports no corresponding
ALTER PUBLICATION entry. This seems like a bug to me. Thoughts?

0001 patch fixes the issue by detecting when table or schema membership
mappings are removed and collecting the publication itself once as the
ALTER PUBLICATION command.

As a side-effect of this fix, SET commands that both remove and add
memberships may now produce both a publication-level entry and
the existing per-membership entries for additions. This is intentional,
since they represent different parts of the command.

I think this should be backpatched to all supported branches. But,
if the above behavioral change is considered too risky for backpatching,
though, I'm fine with applying it only to master. Thoughts?


0002: Collect ALTER PUBLICATION SET ALL changes

ALTER PUBLICATION ... SET ALL TABLES and SET ALL SEQUENCES have
a similar issue. These commands fire ddl_command_end,
but pg_event_trigger_ddl_commands() can return no entry.

0002 patch fixes the issue by making AlterPublicationAllFlags() report
whether the publication state changed. If it did, the publication itself is
collected as the ALTER PUBLICATION command.

As a side-effect of this fix, SET ALL TABLES ... EXCEPT (...) may now produce
both a publication-level entry and the existing per-membership entries for
added exclusions. This is intentional, since they represent different parts
of the command.

I think this should be backpatched to v19, where
ALTER PUBLICATION SET ALL TABLES and SET ALL SEQUENCES were introduced.

Regards,

-- 
Fujii Masao

Attachments: download all (2 files, .tar.gz)

v1-0001-Collect-ALTER-PUBLICATION-mapping-drops-for-event.patch (application/octet-stream, 12.8K) download From 9c49b0171450c44abf15b744b93cf850b36ae0f4 Mon Sep 17 00:00:00 2001 From: Fujii Masao <fujii@postgresql.org> Date: Fri, 24 Jul 2026 00:30:07 +0900 Subject: [PATCH v1 1/2] Collect ALTER PUBLICATION mapping drops for event triggers ALTER PUBLICATION is expected to emit its own ddl_command_end entry for event triggers. Previously, however, commands that only removed publication membership entries did not do so. As a result, sql_drop reported the removed membership entries, but pg_event_trigger_ddl_commands() did not report the corresponding ALTER PUBLICATION command. This affected ALTER PUBLICATION ... DROP TABLE(S), DROP TABLES IN SCHEMA, and SET forms whose net effect was only to remove publication memberships. Fix this by collecting the publication itself as the ALTER PUBLICATION command whenever the command removes table or schema membership entries, while preserving the existing per-membership entries for additions. As a side-effect of this fix, SET commands that both add and remove memberships may now produce both a publication-level ALTER PUBLICATION entry and the existing per-membership addition entries. This is intentional, as they represent different parts of the command. Backpatch to all supported versions. --- src/backend/commands/publicationcmds.c | 56 +++++++++++++++------ src/test/regress/expected/event_trigger.out | 24 +++++++++ src/test/regress/sql/event_trigger.sql | 20 ++++++++ 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 440adb356ad..6fa354b170c 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -66,10 +66,10 @@ static void CloseTableList(List *rels); static void LockSchemaList(List *schemalist); static void PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, AlterPublicationStmt *stmt); -static void PublicationDropTables(Oid pubid, List *rels, bool missing_ok); +static bool PublicationDropTables(Oid pubid, List *rels, bool missing_ok); static void PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, AlterPublicationStmt *stmt); -static void PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok); +static bool PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok); static char defGetGeneratedColsOption(DefElem *def); @@ -1238,7 +1238,7 @@ InvalidatePublicationRels(List *relids) /* * Add or remove table to/from publication. */ -static void +static bool AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, List *tables, const char *queryString, bool publish_schema) @@ -1246,6 +1246,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, List *rels = NIL; Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); Oid pubid = pubform->oid; + bool dropped = false; /* * Nothing to do if no objects, except in SET: for that it is quite @@ -1253,7 +1254,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, * to remove all the existing tables. */ if (!tables && stmt->action != AP_SetObjects) - return; + return false; rels = OpenTableList(tables); @@ -1269,7 +1270,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, PublicationAddTables(pubid, rels, false, stmt); } else if (stmt->action == AP_DropObjects) - PublicationDropTables(pubid, rels, false); + dropped = PublicationDropTables(pubid, rels, false); else /* AP_SetObjects */ { List *oldrelids = NIL; @@ -1404,7 +1405,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, } /* And drop them. */ - PublicationDropTables(pubid, delrels, true); + dropped = PublicationDropTables(pubid, delrels, true); /* * Don't bother calculating the difference for adding, we'll catch and @@ -1416,6 +1417,8 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, } CloseTableList(rels); + + return dropped; } /* @@ -1423,11 +1426,12 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, * * Add or remove schemas to/from publication. */ -static void +static bool AlterPublicationSchemas(AlterPublicationStmt *stmt, HeapTuple tup, List *schemaidlist) { Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); + bool dropped = false; /* * Nothing to do if no objects, except in SET: for that it is quite @@ -1435,7 +1439,7 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, * to remove all the existing schemas. */ if (!schemaidlist && stmt->action != AP_SetObjects) - return; + return false; /* * Schema lock is held until the publication is altered to prevent @@ -1478,7 +1482,7 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, PublicationAddSchemas(pubform->oid, schemaidlist, false, stmt); } else if (stmt->action == AP_DropObjects) - PublicationDropSchemas(pubform->oid, schemaidlist, false); + dropped = PublicationDropSchemas(pubform->oid, schemaidlist, false); else /* AP_SetObjects */ { List *oldschemaids = GetPublicationSchemas(pubform->oid); @@ -1494,7 +1498,7 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, LockSchemaList(delschemas); /* And drop them */ - PublicationDropSchemas(pubform->oid, delschemas, true); + dropped = PublicationDropSchemas(pubform->oid, delschemas, true); /* * Don't bother calculating the difference for adding, we'll catch and @@ -1502,6 +1506,8 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, */ PublicationAddSchemas(pubform->oid, schemaidlist, true, stmt); } + + return dropped; } /* @@ -1686,6 +1692,8 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) List *exceptrelations = NIL; List *schemaidlist = NIL; Oid pubid = pubform->oid; + bool tables_dropped; + bool schemas_dropped; ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations, &exceptrelations, &schemaidlist); @@ -1712,10 +1720,20 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) stmt->pubname)); relations = list_concat(relations, exceptrelations); - AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, - schemaidlist != NIL); - AlterPublicationSchemas(stmt, tup, schemaidlist); + tables_dropped = + AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, + schemaidlist != NIL); + schemas_dropped = AlterPublicationSchemas(stmt, tup, schemaidlist); AlterPublicationAllFlags(stmt, rel, tup); + + if (tables_dropped || schemas_dropped) + { + ObjectAddress obj; + + ObjectAddressSet(obj, PublicationRelationId, pubid); + EventTriggerCollectSimpleCommand(obj, InvalidObjectAddress, + (Node *) stmt); + } } /* Cleanup. */ @@ -2066,12 +2084,13 @@ PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, /* * Remove listed tables from the publication. */ -static void +static bool PublicationDropTables(Oid pubid, List *rels, bool missing_ok) { ObjectAddress obj; ListCell *lc; Oid prid; + bool dropped = false; foreach(lc, rels) { @@ -2105,7 +2124,10 @@ PublicationDropTables(Oid pubid, List *rels, bool missing_ok) ObjectAddressSet(obj, PublicationRelRelationId, prid); performDeletion(&obj, DROP_CASCADE, 0); + dropped = true; } + + return dropped; } /* @@ -2137,12 +2159,13 @@ PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, /* * Remove listed schemas from the publication. */ -static void +static bool PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok) { ObjectAddress obj; ListCell *lc; Oid psid; + bool dropped = false; foreach(lc, schemas) { @@ -2165,7 +2188,10 @@ PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok) ObjectAddressSet(obj, PublicationNamespaceRelationId, psid); performDeletion(&obj, DROP_CASCADE, 0); + dropped = true; } + + return dropped; } /* diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out index 86ae50ce531..960f6d002e3 100644 --- a/src/test/regress/expected/event_trigger.out +++ b/src/test/regress/expected/event_trigger.out @@ -401,6 +401,14 @@ DROP ROLE regress_evt_user; DROP EVENT TRIGGER regress_event_trigger_drop_objects; DROP EVENT TRIGGER undroppable; -- Event triggers on relations. +CREATE TABLE evttrig_pub_tbl (a int); +CREATE TABLE evttrig_pub_tbl2 (a int); +CREATE TABLE evttrig_pub_tbl3 (a int); +CREATE SCHEMA evttrig_pub_schema; +CREATE SCHEMA evttrig_pub_schema2; +CREATE PUBLICATION evttrig_pub FOR TABLE evttrig_pub_tbl, + evttrig_pub_tbl2, evttrig_pub_tbl3, TABLES IN SCHEMA evttrig_pub_schema, + evttrig_pub_schema2; CREATE OR REPLACE FUNCTION event_trigger_report_dropped() RETURNS event_trigger LANGUAGE plpgsql @@ -434,6 +442,16 @@ BEGIN END; $$; CREATE EVENT TRIGGER regress_event_trigger_report_end ON ddl_command_end EXECUTE PROCEDURE event_trigger_report_end(); +ALTER PUBLICATION evttrig_pub DROP TABLE evttrig_pub_tbl; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl in publication evttrig_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl} args={evttrig_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub +ALTER PUBLICATION evttrig_pub DROP TABLES IN SCHEMA evttrig_pub_schema; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication namespace identity=evttrig_pub_schema in publication evttrig_pub schema=<NULL> name=<NULL> addr={evttrig_pub_schema} args={evttrig_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub +ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication namespace identity=evttrig_pub_schema2 in publication evttrig_pub schema=<NULL> name=<NULL> addr={evttrig_pub_schema2} args={evttrig_pub} +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl3 in publication evttrig_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl3} args={evttrig_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -590,6 +608,12 @@ NOTICE: END: command_tag=CREATE OPERATOR FAMILY type=operator family identity=p NOTICE: END: command_tag=CREATE OPERATOR CLASS type=operator class identity=public.evttrigopclass USING btree DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; +DROP PUBLICATION evttrig_pub; +DROP TABLE evttrig_pub_tbl; +DROP TABLE evttrig_pub_tbl2; +DROP TABLE evttrig_pub_tbl3; +DROP SCHEMA evttrig_pub_schema; +DROP SCHEMA evttrig_pub_schema2; -- only allowed from within an event trigger function, should fail select pg_event_trigger_table_rewrite_oid(); ERROR: pg_event_trigger_table_rewrite_oid() can only be called in a table_rewrite event trigger function diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql index d0e6ba295fe..028ab53e408 100644 --- a/src/test/regress/sql/event_trigger.sql +++ b/src/test/regress/sql/event_trigger.sql @@ -301,6 +301,15 @@ DROP EVENT TRIGGER regress_event_trigger_drop_objects; DROP EVENT TRIGGER undroppable; -- Event triggers on relations. +CREATE TABLE evttrig_pub_tbl (a int); +CREATE TABLE evttrig_pub_tbl2 (a int); +CREATE TABLE evttrig_pub_tbl3 (a int); +CREATE SCHEMA evttrig_pub_schema; +CREATE SCHEMA evttrig_pub_schema2; +CREATE PUBLICATION evttrig_pub FOR TABLE evttrig_pub_tbl, + evttrig_pub_tbl2, evttrig_pub_tbl3, TABLES IN SCHEMA evttrig_pub_schema, + evttrig_pub_schema2; + CREATE OR REPLACE FUNCTION event_trigger_report_dropped() RETURNS event_trigger LANGUAGE plpgsql @@ -335,6 +344,10 @@ END; $$; CREATE EVENT TRIGGER regress_event_trigger_report_end ON ddl_command_end EXECUTE PROCEDURE event_trigger_report_end(); +ALTER PUBLICATION evttrig_pub DROP TABLE evttrig_pub_tbl; +ALTER PUBLICATION evttrig_pub DROP TABLES IN SCHEMA evttrig_pub_schema; +ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; + CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -417,6 +430,13 @@ CREATE OPERATOR CLASS evttrigopclass FOR TYPE int USING btree AS STORAGE int; DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; +DROP PUBLICATION evttrig_pub; +DROP TABLE evttrig_pub_tbl; +DROP TABLE evttrig_pub_tbl2; +DROP TABLE evttrig_pub_tbl3; +DROP SCHEMA evttrig_pub_schema; +DROP SCHEMA evttrig_pub_schema2; + -- only allowed from within an event trigger function, should fail select pg_event_trigger_table_rewrite_oid(); -- 2.55.0
v1-0002-Collect-ALTER-PUBLICATION-SET-ALL-commands-for-ev.patch (application/octet-stream, 5.2K) download From 25ab982601b3b3786d7372a3181ddad1d35072e5 Mon Sep 17 00:00:00 2001 From: Fujii Masao <fujii@postgresql.org> Date: Fri, 24 Jul 2026 10:41:40 +0900 Subject: [PATCH v1 2/2] Collect ALTER PUBLICATION SET ALL commands for event triggers Previously, ALTER PUBLICATION ... SET ALL TABLES and SET ALL SEQUENCES fired ddl_command_end, but pg_event_trigger_ddl_commands() could return no entry for the altered publication because none was collected. Fix this by collecting the publication itself as the ALTER PUBLICATION command whenever a SET ALL command changes the publication state. As a side-effect of this fix, SET ALL TABLES with EXCEPT entries may now produce both a publication-level ALTER PUBLICATION entry and the existing per-membership entries for added exclusions. This is intentional, since they represent different parts of the command. Backpatch to v19, where ALTER PUBLICATION SET ALL TABLES and SET ALL SEQUENCES were introduced. --- src/backend/commands/publicationcmds.c | 11 +++++++---- src/test/regress/expected/event_trigger.out | 8 ++++++++ src/test/regress/sql/event_trigger.sql | 5 +++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 6fa354b170c..5edbf1c7920 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -1607,7 +1607,7 @@ CheckAlterPublication(AlterPublicationStmt *stmt, HeapTuple tup, /* * Update FOR ALL TABLES / FOR ALL SEQUENCES flags of a publication. */ -static void +static bool AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup) { @@ -1618,7 +1618,7 @@ AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, bool dirty = false; if (!stmt->for_all_tables && !stmt->for_all_sequences) - return; + return false; pubform = (Form_pg_publication) GETSTRUCT(tup); @@ -1651,6 +1651,8 @@ AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, if (replaces[Anum_pg_publication_puballtables - 1]) CacheInvalidateRelcacheAll(); } + + return dirty; } /* @@ -1694,6 +1696,7 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) Oid pubid = pubform->oid; bool tables_dropped; bool schemas_dropped; + bool all_flags_changed; ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations, &exceptrelations, &schemaidlist); @@ -1724,9 +1727,9 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, schemaidlist != NIL); schemas_dropped = AlterPublicationSchemas(stmt, tup, schemaidlist); - AlterPublicationAllFlags(stmt, rel, tup); + all_flags_changed = AlterPublicationAllFlags(stmt, rel, tup); - if (tables_dropped || schemas_dropped) + if (tables_dropped || schemas_dropped || all_flags_changed) { ObjectAddress obj; diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out index 960f6d002e3..1d03422a97b 100644 --- a/src/test/regress/expected/event_trigger.out +++ b/src/test/regress/expected/event_trigger.out @@ -452,6 +452,14 @@ ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; NOTICE: NORMAL: orig=t normal=f istemp=f type=publication namespace identity=evttrig_pub_schema2 in publication evttrig_pub schema=<NULL> name=<NULL> addr={evttrig_pub_schema2} args={evttrig_pub} NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl3 in publication evttrig_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl3} args={evttrig_pub} NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub +CREATE PUBLICATION evttrig_all_pub; +NOTICE: END: command_tag=CREATE PUBLICATION type=publication identity=evttrig_all_pub +ALTER PUBLICATION evttrig_all_pub SET ALL TABLES; +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_all_pub +ALTER PUBLICATION evttrig_all_pub SET ALL SEQUENCES; +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_all_pub +DROP PUBLICATION evttrig_all_pub; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication identity=evttrig_all_pub schema=<NULL> name=evttrig_all_pub addr={evttrig_all_pub} args={} CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql index 028ab53e408..b09f9e3ca41 100644 --- a/src/test/regress/sql/event_trigger.sql +++ b/src/test/regress/sql/event_trigger.sql @@ -348,6 +348,11 @@ ALTER PUBLICATION evttrig_pub DROP TABLE evttrig_pub_tbl; ALTER PUBLICATION evttrig_pub DROP TABLES IN SCHEMA evttrig_pub_schema; ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; +CREATE PUBLICATION evttrig_all_pub; +ALTER PUBLICATION evttrig_all_pub SET ALL TABLES; +ALTER PUBLICATION evttrig_all_pub SET ALL SEQUENCES; +DROP PUBLICATION evttrig_all_pub; + CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) -- 2.55.0
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: Collect ALTER PUBLICATION commands for event triggers
@ 2026-07-24 07:45  Ian Lawrence Barwick <barwick@gmail.com>
  parent: Fujii Masao <masao.fujii@gmail.com>
  2 siblings, 1 reply; 11+ messages in thread

From: Ian Lawrence Barwick @ 2026-07-24 07:45 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi

2026年7月24日(金) 11:07 Fujii Masao <masao.fujii@gmail.com>:
>
> Hi,
>
> I found two cases where ALTER PUBLICATION fires ddl_command_end,
> but pg_event_trigger_ddl_commands() does not report the corresponding
> ALTER PUBLICATION command.
>
> Attached are two patches.
>
> 0001 patch: Collect ALTER PUBLICATION mapping drops
>
> When an ALTER PUBLICATION command only removes publication membership mappings,
> the publication itself is not collected as a ddl_command_end object.
>
> For example:
>     ALTER PUBLICATION p DROP TABLE t1;
>     ALTER PUBLICATION p SET TABLE t2;
>
> sql_drop reports the removed publication relation,
> but pg_event_trigger_ddl_commands() reports no corresponding
> ALTER PUBLICATION entry. This seems like a bug to me. Thoughts?

It certainly seems inconsistent with the behaviour of e.g. ALTER TABLE
... DROP COLUMN.
The documentation [*] indicates the sql_drop event should be followed by
a  ddl_command_end event for the main ALTER command, so there's no obvious
reason why it isn't happening here.

[*] https://www.postgresql.org/docs/devel/event-trigger-definition.html#EVENT-TRIGGER-SQL_DROP

> 0001 patch fixes the issue by detecting when table or schema membership
> mappings are removed and collecting the publication itself once as the
> ALTER PUBLICATION command.
>
> As a side-effect of this fix, SET commands that both remove and add
> memberships may now produce both a publication-level entry and
> the existing per-membership entries for additions. This is intentional,
> since they represent different parts of the command.

Seems reasonable.

> I think this should be backpatched to all supported branches. But,
> if the above behavioral change is considered too risky for backpatching,
> though, I'm fine with applying it only to master. Thoughts?

This seems like the kind of issue which doesn't get backpatched because
it changes behaviour and no-one has complained about it (AFAIK),
but that's just my Friday afternoon vibe opinion and more qualified
opinions may be available.

> 0002: Collect ALTER PUBLICATION SET ALL changes
>
> ALTER PUBLICATION ... SET ALL TABLES and SET ALL SEQUENCES have
> a similar issue. These commands fire ddl_command_end,
> but pg_event_trigger_ddl_commands() can return no entry.
>
> 0002 patch fixes the issue by making AlterPublicationAllFlags() report
> whether the publication state changed. If it did, the publication itself is
> collected as the ALTER PUBLICATION command.
>
> As a side-effect of this fix, SET ALL TABLES ... EXCEPT (...) may now produce
> both a publication-level entry and the existing per-membership entries for
> added exclusions. This is intentional, since they represent different parts
> of the command.
>
> I think this should be backpatched to v19, where
> ALTER PUBLICATION SET ALL TABLES and SET ALL SEQUENCES were introduced.

Seems reasonable.

Would it be an idea to add  SET ALL TABLES ... EXCEPT (...) to the regression
test as well, to clarify the expected behaviour?

Regards

Ian Barwick






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Collect ALTER PUBLICATION commands for event triggers
@ 2026-07-24 08:38  Chao Li <li.evan.chao@gmail.com>
  parent: Fujii Masao <masao.fujii@gmail.com>
  2 siblings, 1 reply; 11+ messages in thread

From: Chao Li @ 2026-07-24 08:38 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>



> On Jul 24, 2026, at 10:06, Fujii Masao <masao.fujii@gmail.com> wrote:
> 
> Hi,
> 
> I found two cases where ALTER PUBLICATION fires ddl_command_end,
> but pg_event_trigger_ddl_commands() does not report the corresponding
> ALTER PUBLICATION command.
> 
> Attached are two patches.
> 
> 0001 patch: Collect ALTER PUBLICATION mapping drops
> 
> When an ALTER PUBLICATION command only removes publication membership mappings,
> the publication itself is not collected as a ddl_command_end object.
> 
> For example:
>    ALTER PUBLICATION p DROP TABLE t1;
>    ALTER PUBLICATION p SET TABLE t2;
> 
> sql_drop reports the removed publication relation,
> but pg_event_trigger_ddl_commands() reports no corresponding
> ALTER PUBLICATION entry. This seems like a bug to me. Thoughts?
> 
> 0001 patch fixes the issue by detecting when table or schema membership
> mappings are removed and collecting the publication itself once as the
> ALTER PUBLICATION command.
> 
> As a side-effect of this fix, SET commands that both remove and add
> memberships may now produce both a publication-level entry and
> the existing per-membership entries for additions. This is intentional,
> since they represent different parts of the command.
> 
> I think this should be backpatched to all supported branches. But,
> if the above behavioral change is considered too risky for backpatching,
> though, I'm fine with applying it only to master. Thoughts?
> 
> 
> 0002: Collect ALTER PUBLICATION SET ALL changes
> 
> ALTER PUBLICATION ... SET ALL TABLES and SET ALL SEQUENCES have
> a similar issue. These commands fire ddl_command_end,
> but pg_event_trigger_ddl_commands() can return no entry.
> 
> 0002 patch fixes the issue by making AlterPublicationAllFlags() report
> whether the publication state changed. If it did, the publication itself is
> collected as the ALTER PUBLICATION command.
> 
> As a side-effect of this fix, SET ALL TABLES ... EXCEPT (...) may now produce
> both a publication-level entry and the existing per-membership entries for
> added exclusions. This is intentional, since they represent different parts
> of the command.
> 
> I think this should be backpatched to v19, where
> ALTER PUBLICATION SET ALL TABLES and SET ALL SEQUENCES were introduced.
> 
> Regards,
> 
> -- 
> Fujii Masao
> <v1-0001-Collect-ALTER-PUBLICATION-mapping-drops-for-event.patch><v1-0002-Collect-ALTER-PUBLICATION-SET-ALL-commands-for-ev.patch>

Both v1-0001 and v1-0002 look good to me.

While reviewing 0002, I noticed another suspicion. AlterPublicationAllFlags() directly updates the pg_publication tuple but doesn’t call InvokeObjectPostAlterHook(). This seems inconsistent with AlterPublicationOptions() and AlterPublicationOwner_internal(), which invoke that hook after altering the publication.

As this is a separate issue from this patch, should I start a new thread? This seems an oversight of 493f8c6439c.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/










^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* RE: Collect ALTER PUBLICATION commands for event triggers
@ 2026-07-24 12:03  Zhijie Hou (Fujitsu) <houzj.fnst@fujitsu.com>
  parent: Fujii Masao <masao.fujii@gmail.com>
  2 siblings, 1 reply; 11+ messages in thread

From: Zhijie Hou (Fujitsu) @ 2026-07-24 12:03 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Friday, July 24, 2026 10:07 AM Fujii Masao <masao.fujii@gmail.com> wrote:
> I found two cases where ALTER PUBLICATION fires ddl_command_end, but
> pg_event_trigger_ddl_commands() does not report the corresponding ALTER
> PUBLICATION command.
> 
> Attached are two patches.
> 
> 0001 patch: Collect ALTER PUBLICATION mapping drops
> 
> When an ALTER PUBLICATION command only removes publication
> membership mappings, the publication itself is not collected as a
> ddl_command_end object.
> 
> For example:
>     ALTER PUBLICATION p DROP TABLE t1;
>     ALTER PUBLICATION p SET TABLE t2;
> 
> sql_drop reports the removed publication relation, but
> pg_event_trigger_ddl_commands() reports no corresponding ALTER
> PUBLICATION entry. This seems like a bug to me. Thoughts?

I suspect this wasn't caught earlier because handling the dropped publication
relation was sufficient to deparse it into the original ALTER PUBLICATION
command (by combining multiple dropped items). I also assumed the primary use
case for the event trigger was deparsing. I'm not opposed to also catching this
command in ddl_end - I'm just curious whether it would address any additional
use cases beyond what's already covered.

> I think this should be backpatched to all supported branches. But, if the above
> behavioral change is considered too risky for backpatching, though, I'm fine
> with applying it only to master. Thoughts?
> 
> 
> 0002: Collect ALTER PUBLICATION SET ALL changes
> 
> ALTER PUBLICATION ... SET ALL TABLES and SET ALL SEQUENCES have a
> similar issue. These commands fire ddl_command_end, but
> pg_event_trigger_ddl_commands() can return no entry.
> 
> 0002 patch fixes the issue by making AlterPublicationAllFlags() report whether
> the publication state changed. If it did, the publication itself is collected as the
> ALTER PUBLICATION command.

+1 for addressing this. Without handling this command, there is no alternative
means to deparse it into the original ALTER PUBLICATION SET ALL statement.

> 
> As a side-effect of this fix, SET ALL TABLES ... EXCEPT (...) may now produce
> both a publication-level entry and the existing per-membership entries for
> added exclusions. This is intentional, since they represent different parts of
> the command.

Best Regards,
Zhijie Hou


^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Collect ALTER PUBLICATION commands for event triggers
@ 2026-07-24 13:53  Fujii Masao <masao.fujii@gmail.com>
  parent: Ian Lawrence Barwick <barwick@gmail.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Fujii Masao @ 2026-07-24 13:53 UTC (permalink / raw)
  To: Ian Lawrence Barwick <barwick@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Fri, Jul 24, 2026 at 4:45 PM Ian Lawrence Barwick <barwick@gmail.com> wrote:
> This seems like the kind of issue which doesn't get backpatched because
> it changes behaviour and no-one has complained about it (AFAIK),
> but that's just my Friday afternoon vibe opinion and more qualified
> opinions may be available.

Yeah, I see your point.


> Would it be an idea to add  SET ALL TABLES ... EXCEPT (...) to the regression
> test as well, to clarify the expected behaviour?

Thanks for the suggestion! I've updated patch 0002 accordingly.
Attached are the updated patches.

Patch 0001 is unchanged. Patch 0003 is new and invokes the post-alter
hook for ALTER PUBLICATION SET ALL, as suggested by Chao.

Regards,

-- 
Fujii Masao

Attachments: download all (3 files, .tar.gz)

v2-0001-Collect-ALTER-PUBLICATION-mapping-drops-for-event.patch (application/octet-stream, 12.8K) download From db98eb16d79ab1895369498b484ae288c4a72c2d Mon Sep 17 00:00:00 2001 From: Fujii Masao <fujii@postgresql.org> Date: Fri, 24 Jul 2026 00:30:07 +0900 Subject: [PATCH v2 1/3] Collect ALTER PUBLICATION mapping drops for event triggers ALTER PUBLICATION is expected to emit its own ddl_command_end entry for event triggers. Previously, however, commands that only removed publication membership entries did not do so. As a result, sql_drop reported the removed membership entries, but pg_event_trigger_ddl_commands() did not report the corresponding ALTER PUBLICATION command. This affected ALTER PUBLICATION ... DROP TABLE(S), DROP TABLES IN SCHEMA, and SET forms whose net effect was only to remove publication memberships. Fix this by collecting the publication itself as the ALTER PUBLICATION command whenever the command removes table or schema membership entries, while preserving the existing per-membership entries for additions. As a side-effect of this fix, SET commands that both add and remove memberships may now produce both a publication-level ALTER PUBLICATION entry and the existing per-membership addition entries. This is intentional, as they represent different parts of the command. Backpatch to all supported versions. --- src/backend/commands/publicationcmds.c | 56 +++++++++++++++------ src/test/regress/expected/event_trigger.out | 24 +++++++++ src/test/regress/sql/event_trigger.sql | 20 ++++++++ 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 440adb356ad..6fa354b170c 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -66,10 +66,10 @@ static void CloseTableList(List *rels); static void LockSchemaList(List *schemalist); static void PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, AlterPublicationStmt *stmt); -static void PublicationDropTables(Oid pubid, List *rels, bool missing_ok); +static bool PublicationDropTables(Oid pubid, List *rels, bool missing_ok); static void PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, AlterPublicationStmt *stmt); -static void PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok); +static bool PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok); static char defGetGeneratedColsOption(DefElem *def); @@ -1238,7 +1238,7 @@ InvalidatePublicationRels(List *relids) /* * Add or remove table to/from publication. */ -static void +static bool AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, List *tables, const char *queryString, bool publish_schema) @@ -1246,6 +1246,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, List *rels = NIL; Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); Oid pubid = pubform->oid; + bool dropped = false; /* * Nothing to do if no objects, except in SET: for that it is quite @@ -1253,7 +1254,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, * to remove all the existing tables. */ if (!tables && stmt->action != AP_SetObjects) - return; + return false; rels = OpenTableList(tables); @@ -1269,7 +1270,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, PublicationAddTables(pubid, rels, false, stmt); } else if (stmt->action == AP_DropObjects) - PublicationDropTables(pubid, rels, false); + dropped = PublicationDropTables(pubid, rels, false); else /* AP_SetObjects */ { List *oldrelids = NIL; @@ -1404,7 +1405,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, } /* And drop them. */ - PublicationDropTables(pubid, delrels, true); + dropped = PublicationDropTables(pubid, delrels, true); /* * Don't bother calculating the difference for adding, we'll catch and @@ -1416,6 +1417,8 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, } CloseTableList(rels); + + return dropped; } /* @@ -1423,11 +1426,12 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, * * Add or remove schemas to/from publication. */ -static void +static bool AlterPublicationSchemas(AlterPublicationStmt *stmt, HeapTuple tup, List *schemaidlist) { Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); + bool dropped = false; /* * Nothing to do if no objects, except in SET: for that it is quite @@ -1435,7 +1439,7 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, * to remove all the existing schemas. */ if (!schemaidlist && stmt->action != AP_SetObjects) - return; + return false; /* * Schema lock is held until the publication is altered to prevent @@ -1478,7 +1482,7 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, PublicationAddSchemas(pubform->oid, schemaidlist, false, stmt); } else if (stmt->action == AP_DropObjects) - PublicationDropSchemas(pubform->oid, schemaidlist, false); + dropped = PublicationDropSchemas(pubform->oid, schemaidlist, false); else /* AP_SetObjects */ { List *oldschemaids = GetPublicationSchemas(pubform->oid); @@ -1494,7 +1498,7 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, LockSchemaList(delschemas); /* And drop them */ - PublicationDropSchemas(pubform->oid, delschemas, true); + dropped = PublicationDropSchemas(pubform->oid, delschemas, true); /* * Don't bother calculating the difference for adding, we'll catch and @@ -1502,6 +1506,8 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, */ PublicationAddSchemas(pubform->oid, schemaidlist, true, stmt); } + + return dropped; } /* @@ -1686,6 +1692,8 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) List *exceptrelations = NIL; List *schemaidlist = NIL; Oid pubid = pubform->oid; + bool tables_dropped; + bool schemas_dropped; ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations, &exceptrelations, &schemaidlist); @@ -1712,10 +1720,20 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) stmt->pubname)); relations = list_concat(relations, exceptrelations); - AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, - schemaidlist != NIL); - AlterPublicationSchemas(stmt, tup, schemaidlist); + tables_dropped = + AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, + schemaidlist != NIL); + schemas_dropped = AlterPublicationSchemas(stmt, tup, schemaidlist); AlterPublicationAllFlags(stmt, rel, tup); + + if (tables_dropped || schemas_dropped) + { + ObjectAddress obj; + + ObjectAddressSet(obj, PublicationRelationId, pubid); + EventTriggerCollectSimpleCommand(obj, InvalidObjectAddress, + (Node *) stmt); + } } /* Cleanup. */ @@ -2066,12 +2084,13 @@ PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, /* * Remove listed tables from the publication. */ -static void +static bool PublicationDropTables(Oid pubid, List *rels, bool missing_ok) { ObjectAddress obj; ListCell *lc; Oid prid; + bool dropped = false; foreach(lc, rels) { @@ -2105,7 +2124,10 @@ PublicationDropTables(Oid pubid, List *rels, bool missing_ok) ObjectAddressSet(obj, PublicationRelRelationId, prid); performDeletion(&obj, DROP_CASCADE, 0); + dropped = true; } + + return dropped; } /* @@ -2137,12 +2159,13 @@ PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, /* * Remove listed schemas from the publication. */ -static void +static bool PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok) { ObjectAddress obj; ListCell *lc; Oid psid; + bool dropped = false; foreach(lc, schemas) { @@ -2165,7 +2188,10 @@ PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok) ObjectAddressSet(obj, PublicationNamespaceRelationId, psid); performDeletion(&obj, DROP_CASCADE, 0); + dropped = true; } + + return dropped; } /* diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out index 86ae50ce531..960f6d002e3 100644 --- a/src/test/regress/expected/event_trigger.out +++ b/src/test/regress/expected/event_trigger.out @@ -401,6 +401,14 @@ DROP ROLE regress_evt_user; DROP EVENT TRIGGER regress_event_trigger_drop_objects; DROP EVENT TRIGGER undroppable; -- Event triggers on relations. +CREATE TABLE evttrig_pub_tbl (a int); +CREATE TABLE evttrig_pub_tbl2 (a int); +CREATE TABLE evttrig_pub_tbl3 (a int); +CREATE SCHEMA evttrig_pub_schema; +CREATE SCHEMA evttrig_pub_schema2; +CREATE PUBLICATION evttrig_pub FOR TABLE evttrig_pub_tbl, + evttrig_pub_tbl2, evttrig_pub_tbl3, TABLES IN SCHEMA evttrig_pub_schema, + evttrig_pub_schema2; CREATE OR REPLACE FUNCTION event_trigger_report_dropped() RETURNS event_trigger LANGUAGE plpgsql @@ -434,6 +442,16 @@ BEGIN END; $$; CREATE EVENT TRIGGER regress_event_trigger_report_end ON ddl_command_end EXECUTE PROCEDURE event_trigger_report_end(); +ALTER PUBLICATION evttrig_pub DROP TABLE evttrig_pub_tbl; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl in publication evttrig_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl} args={evttrig_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub +ALTER PUBLICATION evttrig_pub DROP TABLES IN SCHEMA evttrig_pub_schema; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication namespace identity=evttrig_pub_schema in publication evttrig_pub schema=<NULL> name=<NULL> addr={evttrig_pub_schema} args={evttrig_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub +ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication namespace identity=evttrig_pub_schema2 in publication evttrig_pub schema=<NULL> name=<NULL> addr={evttrig_pub_schema2} args={evttrig_pub} +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl3 in publication evttrig_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl3} args={evttrig_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -590,6 +608,12 @@ NOTICE: END: command_tag=CREATE OPERATOR FAMILY type=operator family identity=p NOTICE: END: command_tag=CREATE OPERATOR CLASS type=operator class identity=public.evttrigopclass USING btree DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; +DROP PUBLICATION evttrig_pub; +DROP TABLE evttrig_pub_tbl; +DROP TABLE evttrig_pub_tbl2; +DROP TABLE evttrig_pub_tbl3; +DROP SCHEMA evttrig_pub_schema; +DROP SCHEMA evttrig_pub_schema2; -- only allowed from within an event trigger function, should fail select pg_event_trigger_table_rewrite_oid(); ERROR: pg_event_trigger_table_rewrite_oid() can only be called in a table_rewrite event trigger function diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql index d0e6ba295fe..028ab53e408 100644 --- a/src/test/regress/sql/event_trigger.sql +++ b/src/test/regress/sql/event_trigger.sql @@ -301,6 +301,15 @@ DROP EVENT TRIGGER regress_event_trigger_drop_objects; DROP EVENT TRIGGER undroppable; -- Event triggers on relations. +CREATE TABLE evttrig_pub_tbl (a int); +CREATE TABLE evttrig_pub_tbl2 (a int); +CREATE TABLE evttrig_pub_tbl3 (a int); +CREATE SCHEMA evttrig_pub_schema; +CREATE SCHEMA evttrig_pub_schema2; +CREATE PUBLICATION evttrig_pub FOR TABLE evttrig_pub_tbl, + evttrig_pub_tbl2, evttrig_pub_tbl3, TABLES IN SCHEMA evttrig_pub_schema, + evttrig_pub_schema2; + CREATE OR REPLACE FUNCTION event_trigger_report_dropped() RETURNS event_trigger LANGUAGE plpgsql @@ -335,6 +344,10 @@ END; $$; CREATE EVENT TRIGGER regress_event_trigger_report_end ON ddl_command_end EXECUTE PROCEDURE event_trigger_report_end(); +ALTER PUBLICATION evttrig_pub DROP TABLE evttrig_pub_tbl; +ALTER PUBLICATION evttrig_pub DROP TABLES IN SCHEMA evttrig_pub_schema; +ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; + CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -417,6 +430,13 @@ CREATE OPERATOR CLASS evttrigopclass FOR TYPE int USING btree AS STORAGE int; DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; +DROP PUBLICATION evttrig_pub; +DROP TABLE evttrig_pub_tbl; +DROP TABLE evttrig_pub_tbl2; +DROP TABLE evttrig_pub_tbl3; +DROP SCHEMA evttrig_pub_schema; +DROP SCHEMA evttrig_pub_schema2; + -- only allowed from within an event trigger function, should fail select pg_event_trigger_table_rewrite_oid(); -- 2.55.0
v2-0002-Collect-ALTER-PUBLICATION-SET-ALL-commands-for-ev.patch (application/octet-stream, 6.0K) download From b3d488c99eaec0558104992033e815fd20a6e925 Mon Sep 17 00:00:00 2001 From: Fujii Masao <fujii@postgresql.org> Date: Fri, 24 Jul 2026 10:41:40 +0900 Subject: [PATCH v2 2/3] Collect ALTER PUBLICATION SET ALL commands for event triggers Previously, ALTER PUBLICATION ... SET ALL TABLES and SET ALL SEQUENCES fired ddl_command_end, but pg_event_trigger_ddl_commands() could return no entry for the altered publication because none was collected. Fix this by collecting the publication itself as the ALTER PUBLICATION command whenever a SET ALL command changes the publication state. As a side-effect of this fix, SET ALL TABLES with EXCEPT entries may now produce both a publication-level ALTER PUBLICATION entry and the existing per-membership entries for added exclusions. This is intentional, since they represent different parts of the command. Backpatch to v19, where ALTER PUBLICATION SET ALL TABLES and SET ALL SEQUENCES were introduced. --- src/backend/commands/publicationcmds.c | 11 +++++++---- src/test/regress/expected/event_trigger.out | 9 +++++++++ src/test/regress/sql/event_trigger.sql | 5 +++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 6fa354b170c..5edbf1c7920 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -1607,7 +1607,7 @@ CheckAlterPublication(AlterPublicationStmt *stmt, HeapTuple tup, /* * Update FOR ALL TABLES / FOR ALL SEQUENCES flags of a publication. */ -static void +static bool AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup) { @@ -1618,7 +1618,7 @@ AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, bool dirty = false; if (!stmt->for_all_tables && !stmt->for_all_sequences) - return; + return false; pubform = (Form_pg_publication) GETSTRUCT(tup); @@ -1651,6 +1651,8 @@ AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, if (replaces[Anum_pg_publication_puballtables - 1]) CacheInvalidateRelcacheAll(); } + + return dirty; } /* @@ -1694,6 +1696,7 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) Oid pubid = pubform->oid; bool tables_dropped; bool schemas_dropped; + bool all_flags_changed; ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations, &exceptrelations, &schemaidlist); @@ -1724,9 +1727,9 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, schemaidlist != NIL); schemas_dropped = AlterPublicationSchemas(stmt, tup, schemaidlist); - AlterPublicationAllFlags(stmt, rel, tup); + all_flags_changed = AlterPublicationAllFlags(stmt, rel, tup); - if (tables_dropped || schemas_dropped) + if (tables_dropped || schemas_dropped || all_flags_changed) { ObjectAddress obj; diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out index 960f6d002e3..438b486aacf 100644 --- a/src/test/regress/expected/event_trigger.out +++ b/src/test/regress/expected/event_trigger.out @@ -452,6 +452,14 @@ ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; NOTICE: NORMAL: orig=t normal=f istemp=f type=publication namespace identity=evttrig_pub_schema2 in publication evttrig_pub schema=<NULL> name=<NULL> addr={evttrig_pub_schema2} args={evttrig_pub} NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl3 in publication evttrig_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl3} args={evttrig_pub} NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub +CREATE PUBLICATION evttrig_all_pub; +NOTICE: END: command_tag=CREATE PUBLICATION type=publication identity=evttrig_all_pub +ALTER PUBLICATION evttrig_all_pub SET ALL TABLES EXCEPT (TABLE evttrig_pub_tbl); +NOTICE: END: command_tag=ALTER PUBLICATION type=publication relation identity=public.evttrig_pub_tbl in publication evttrig_all_pub +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_all_pub +ALTER PUBLICATION evttrig_all_pub SET ALL SEQUENCES; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl in publication evttrig_all_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl} args={evttrig_all_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_all_pub CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -609,6 +617,7 @@ NOTICE: END: command_tag=CREATE OPERATOR CLASS type=operator class identity=pub DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; DROP PUBLICATION evttrig_pub; +DROP PUBLICATION evttrig_all_pub; DROP TABLE evttrig_pub_tbl; DROP TABLE evttrig_pub_tbl2; DROP TABLE evttrig_pub_tbl3; diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql index 028ab53e408..49291fe82d3 100644 --- a/src/test/regress/sql/event_trigger.sql +++ b/src/test/regress/sql/event_trigger.sql @@ -348,6 +348,10 @@ ALTER PUBLICATION evttrig_pub DROP TABLE evttrig_pub_tbl; ALTER PUBLICATION evttrig_pub DROP TABLES IN SCHEMA evttrig_pub_schema; ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; +CREATE PUBLICATION evttrig_all_pub; +ALTER PUBLICATION evttrig_all_pub SET ALL TABLES EXCEPT (TABLE evttrig_pub_tbl); +ALTER PUBLICATION evttrig_all_pub SET ALL SEQUENCES; + CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -431,6 +435,7 @@ DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; DROP PUBLICATION evttrig_pub; +DROP PUBLICATION evttrig_all_pub; DROP TABLE evttrig_pub_tbl; DROP TABLE evttrig_pub_tbl2; DROP TABLE evttrig_pub_tbl3; -- 2.55.0
v2-0003-Invoke-post-alter-hook-for-ALTER-PUBLICATION-SET-.patch (application/octet-stream, 1.5K) download From c705a55db8923c21be7d00c315bafea2da8f7846 Mon Sep 17 00:00:00 2001 From: Fujii Masao <fujii@postgresql.org> Date: Fri, 24 Jul 2026 20:26:02 +0900 Subject: [PATCH v2 3/3] Invoke post-alter hook for ALTER PUBLICATION SET ALL Previously, ALTER PUBLICATION ... SET ALL TABLES and SET ALL SEQUENCES updated the pg_publication catalog without invoking the post-alter hook. As a result, object access hook users could miss changes made by these commands. Fix this by invoking the post-alter hook whenever a SET ALL command changes the publication state, using the updated catalog tuple. Backpatch to v19, where ALTER PUBLICATION SET ALL TABLES and SET ALL SEQUENCES were introduced. --- src/backend/commands/publicationcmds.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 5edbf1c7920..c2ed6606314 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -1647,9 +1647,14 @@ AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, CatalogTupleUpdate(rel, &tup->t_self, tup); CommandCounterIncrement(); + pubform = (Form_pg_publication) GETSTRUCT(tup); + /* For ALL TABLES, we must invalidate all relcache entries */ if (replaces[Anum_pg_publication_puballtables - 1]) CacheInvalidateRelcacheAll(); + + InvokeObjectPostAlterHook(PublicationRelationId, + pubform->oid, 0); } return dirty; -- 2.55.0
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: Collect ALTER PUBLICATION commands for event triggers
@ 2026-07-24 13:54  Fujii Masao <masao.fujii@gmail.com>
  parent: Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Fujii Masao @ 2026-07-24 13:54 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Fri, Jul 24, 2026 at 5:38 PM Chao Li <li.evan.chao@gmail.com> wrote:
> Both v1-0001 and v1-0002 look good to me.

Thanks for the review!


> While reviewing 0002, I noticed another suspicion. AlterPublicationAllFlags() directly updates the pg_publication tuple but doesn’t call InvokeObjectPostAlterHook(). This seems inconsistent with AlterPublicationOptions() and AlterPublicationOwner_internal(), which invoke that hook after altering the publication.
>
> As this is a separate issue from this patch, should I start a new thread? This seems an oversight of 493f8c6439c.

Good catch! Although it's a separate issue, I think it's fine to
discuss it here since it's a bit related and a small change.

I put together a patch to invoke the post-alter hook for
ALTER PUBLICATION SET ALL. It's attached as patch 0003
in my reply to Ian upthread.

Regards,

-- 
Fujii Masao





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Collect ALTER PUBLICATION commands for event triggers
@ 2026-07-24 13:59  Fujii Masao <masao.fujii@gmail.com>
  parent: Zhijie Hou (Fujitsu) <houzj.fnst@fujitsu.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Fujii Masao @ 2026-07-24 13:59 UTC (permalink / raw)
  To: Zhijie Hou (Fujitsu) <houzj.fnst@fujitsu.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Fri, Jul 24, 2026 at 9:03 PM Zhijie Hou (Fujitsu)
<houzj.fnst@fujitsu.com> wrote:
> I suspect this wasn't caught earlier because handling the dropped publication
> relation was sufficient to deparse it into the original ALTER PUBLICATION
> command (by combining multiple dropped items). I also assumed the primary use
> case for the event trigger was deparsing. I'm not opposed to also catching this
> command in ddl_end - I'm just curious whether it would address any additional
> use cases beyond what's already covered.

sql_drop may be sufficient for deparsing those cases. My concern is that
ddl_command_end fires, but pg_event_trigger_ddl_commands() reports
no corresponding ALTER PUBLICATION entry, which seems inconsistent
from a command-level auditing/logging perspective. So at least for me,
collecting the publication itself seems the right approach...

Regards,

-- 
Fujii Masao





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Collect ALTER PUBLICATION commands for event triggers
@ 2026-07-24 14:12  Chao Li <li.evan.chao@gmail.com>
  parent: Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Chao Li @ 2026-07-24 14:12 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>



> On Jul 24, 2026, at 21:54, Fujii Masao <masao.fujii@gmail.com> wrote:
> 
> On Fri, Jul 24, 2026 at 5:38 PM Chao Li <li.evan.chao@gmail.com> wrote:
>> Both v1-0001 and v1-0002 look good to me.
> 
> Thanks for the review!
> 
> 
>> While reviewing 0002, I noticed another suspicion. AlterPublicationAllFlags() directly updates the pg_publication tuple but doesn’t call InvokeObjectPostAlterHook(). This seems inconsistent with AlterPublicationOptions() and AlterPublicationOwner_internal(), which invoke that hook after altering the publication.
>> 
>> As this is a separate issue from this patch, should I start a new thread? This seems an oversight of 493f8c6439c.
> 
> Good catch! Although it's a separate issue, I think it's fine to
> discuss it here since it's a bit related and a small change.
> 
> I put together a patch to invoke the post-alter hook for
> ALTER PUBLICATION SET ALL. It's attached as patch 0003
> in my reply to Ian upthread.
> 

Yeah, that’s better. Thanks for adding 0003. I will review it next Monday.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/










^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Collect ALTER PUBLICATION commands for event triggers
@ 2026-08-06 06:30  Chao Li <li.evan.chao@gmail.com>
  parent: Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Chao Li @ 2026-08-06 06:30 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>



> On Jul 24, 2026, at 22:12, Chao Li <li.evan.chao@gmail.com> wrote:
> 
> 
> 
>> On Jul 24, 2026, at 21:54, Fujii Masao <masao.fujii@gmail.com> wrote:
>> 
>> On Fri, Jul 24, 2026 at 5:38 PM Chao Li <li.evan.chao@gmail.com> wrote:
>>> Both v1-0001 and v1-0002 look good to me.
>> 
>> Thanks for the review!
>> 
>> 
>>> While reviewing 0002, I noticed another suspicion. AlterPublicationAllFlags() directly updates the pg_publication tuple but doesn’t call InvokeObjectPostAlterHook(). This seems inconsistent with AlterPublicationOptions() and AlterPublicationOwner_internal(), which invoke that hook after altering the publication.
>>> 
>>> As this is a separate issue from this patch, should I start a new thread? This seems an oversight of 493f8c6439c.
>> 
>> Good catch! Although it's a separate issue, I think it's fine to
>> discuss it here since it's a bit related and a small change.
>> 
>> I put together a patch to invoke the post-alter hook for
>> ALTER PUBLICATION SET ALL. It's attached as patch 0003
>> in my reply to Ian upthread.
>> 
> 
> Yeah, that’s better. Thanks for adding 0003. I will review it next Monday.
> 

Sorry for the delay. V2 looks good to me.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/










^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Collect ALTER PUBLICATION commands for event triggers
@ 2026-09-05 02:52  shihao zhong <zhong950419@gmail.com>
  parent: Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: shihao zhong @ 2026-09-05 02:52 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

> Sorry for the delay. V2 looks good to me.
Hi,

I applied v2 on master with asserts and measured the output.
Please commit 0003 separately and soon.  It is an unrelated five-line
fix for a v19 feature, and v19 has branched.

Do we need the following line in 003?

pubform = GETSTRUCT(tup) line;

heap_modify_tuple() does not change the OID.


0001/0002 collect when the catalog changed, but ddl_command_end means
the command ran.  Measured, all three applied:
  ADD TABLE t2                         membership row only
  SET TABLE t1,t2,t3 (same set)        zero rows
  DROP TABLE t1                        publication row
  SET (publish='insert') twice         publication row both times
  SET ALL TABLES (already all tables)  zero rows

But one command now has two rules.  And 0002's claim that it fixes "no
entry" for SET ALL TABLES is untrue for the no-op case

I think weneed to fix the commit message.

Thanks,
Shihao

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Collect ALTER PUBLICATION commands for event triggers
@ 2026-09-05 02:56  shihao zhong <zhong950419@gmail.com>
  parent: shihao zhong <zhong950419@gmail.com>
  0 siblings, 0 replies; 11+ messages in thread

From: shihao zhong @ 2026-09-05 02:56 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

The patches also need rebase.

Attachments: download all (3 files, .tar.gz)

0001-Collect-ALTER-PUBLICATION-mapping-drops-for-event-tr.patch (application/octet-stream, 13.2K) download From cf223decbd3f86ca28ea5087ce9f50d7ee46bf04 Mon Sep 17 00:00:00 2001 From: Fujii Masao <fujii@postgresql.org> Date: Fri, 24 Jul 2026 00:30:07 +0900 Subject: [PATCH v3 1/3] Collect ALTER PUBLICATION mapping drops for event triggers ALTER PUBLICATION is expected to emit its own ddl_command_end entry for event triggers. Previously, however, commands that only removed publication membership entries did not do so. As a result, sql_drop reported the removed membership entries, but pg_event_trigger_ddl_commands() did not report the corresponding ALTER PUBLICATION command. This affected ALTER PUBLICATION ... DROP TABLE(S), DROP TABLES IN SCHEMA, and SET forms whose net effect was only to remove publication memberships. Fix this by collecting the publication itself as the ALTER PUBLICATION command whenever the command removes table or schema membership entries, while preserving the existing per-membership entries for additions. As a side-effect of this fix, SET commands that both add and remove memberships may now produce both a publication-level ALTER PUBLICATION entry and the existing per-membership addition entries. This is intentional, as they represent different parts of the command. Backpatch to all supported versions. --- src/backend/commands/publicationcmds.c | 56 +++++++++++++++------ src/test/regress/expected/event_trigger.out | 24 +++++++++ src/test/regress/sql/event_trigger.sql | 20 ++++++++ 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 96838730fe1..ebb7750ee30 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -66,10 +66,10 @@ static void CloseTableList(List *rels); static void LockSchemaList(List *schemalist); static void PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, AlterPublicationStmt *stmt); -static void PublicationDropTables(Oid pubid, List *rels, bool missing_ok); +static bool PublicationDropTables(Oid pubid, List *rels, bool missing_ok); static void PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, AlterPublicationStmt *stmt); -static void PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok); +static bool PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok); static char defGetGeneratedColsOption(DefElem *def); @@ -1238,7 +1238,7 @@ InvalidatePublicationRels(List *relids) /* * Add or remove table to/from publication. */ -static void +static bool AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, List *tables, const char *queryString, bool publish_schema) @@ -1246,6 +1246,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, List *rels = NIL; Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); Oid pubid = pubform->oid; + bool dropped = false; /* * Nothing to do if no objects, except in SET: for that it is quite @@ -1253,7 +1254,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, * to remove all the existing tables. */ if (!tables && stmt->action != AP_SetObjects) - return; + return false; rels = OpenTableList(tables); @@ -1269,7 +1270,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, PublicationAddTables(pubid, rels, false, stmt); } else if (stmt->action == AP_DropObjects) - PublicationDropTables(pubid, rels, false); + dropped = PublicationDropTables(pubid, rels, false); else /* AP_SetObjects */ { List *oldrelids = NIL; @@ -1404,7 +1405,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, } /* And drop them. */ - PublicationDropTables(pubid, delrels, true); + dropped = PublicationDropTables(pubid, delrels, true); /* * Don't bother calculating the difference for adding, we'll catch and @@ -1416,6 +1417,8 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, } CloseTableList(rels); + + return dropped; } /* @@ -1423,11 +1426,12 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, * * Add or remove schemas to/from publication. */ -static void +static bool AlterPublicationSchemas(AlterPublicationStmt *stmt, HeapTuple tup, List *schemaidlist) { Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); + bool dropped = false; /* * Nothing to do if no objects, except in SET: for that it is quite @@ -1435,7 +1439,7 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, * to remove all the existing schemas. */ if (!schemaidlist && stmt->action != AP_SetObjects) - return; + return false; /* * Schema lock is held until the publication is altered to prevent @@ -1478,7 +1482,7 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, PublicationAddSchemas(pubform->oid, schemaidlist, false, stmt); } else if (stmt->action == AP_DropObjects) - PublicationDropSchemas(pubform->oid, schemaidlist, false); + dropped = PublicationDropSchemas(pubform->oid, schemaidlist, false); else /* AP_SetObjects */ { List *oldschemaids = GetPublicationSchemas(pubform->oid); @@ -1494,7 +1498,7 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, LockSchemaList(delschemas); /* And drop them */ - PublicationDropSchemas(pubform->oid, delschemas, true); + dropped = PublicationDropSchemas(pubform->oid, delschemas, true); /* * Don't bother calculating the difference for adding, we'll catch and @@ -1502,6 +1506,8 @@ AlterPublicationSchemas(AlterPublicationStmt *stmt, */ PublicationAddSchemas(pubform->oid, schemaidlist, true, stmt); } + + return dropped; } /* @@ -1686,6 +1692,8 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) List *exceptrelations = NIL; List *schemaidlist = NIL; Oid pubid = pubform->oid; + bool tables_dropped; + bool schemas_dropped; ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations, &exceptrelations, &schemaidlist); @@ -1712,10 +1720,20 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) stmt->pubname)); relations = list_concat(relations, exceptrelations); - AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, - schemaidlist != NIL); - AlterPublicationSchemas(stmt, tup, schemaidlist); + tables_dropped = + AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, + schemaidlist != NIL); + schemas_dropped = AlterPublicationSchemas(stmt, tup, schemaidlist); AlterPublicationAllFlags(stmt, rel, tup); + + if (tables_dropped || schemas_dropped) + { + ObjectAddress obj; + + ObjectAddressSet(obj, PublicationRelationId, pubid); + EventTriggerCollectSimpleCommand(obj, InvalidObjectAddress, + (Node *) stmt); + } } /* Cleanup. */ @@ -2063,12 +2081,13 @@ PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, /* * Remove listed tables from the publication. */ -static void +static bool PublicationDropTables(Oid pubid, List *rels, bool missing_ok) { ObjectAddress obj; ListCell *lc; Oid prid; + bool dropped = false; foreach(lc, rels) { @@ -2102,7 +2121,10 @@ PublicationDropTables(Oid pubid, List *rels, bool missing_ok) ObjectAddressSet(obj, PublicationRelRelationId, prid); performDeletion(&obj, DROP_CASCADE, 0); + dropped = true; } + + return dropped; } /* @@ -2134,12 +2156,13 @@ PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, /* * Remove listed schemas from the publication. */ -static void +static bool PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok) { ObjectAddress obj; ListCell *lc; Oid psid; + bool dropped = false; foreach(lc, schemas) { @@ -2162,7 +2185,10 @@ PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok) ObjectAddressSet(obj, PublicationNamespaceRelationId, psid); performDeletion(&obj, DROP_CASCADE, 0); + dropped = true; } + + return dropped; } /* diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out index 2fb925fc0ff..44503e6d0bd 100644 --- a/src/test/regress/expected/event_trigger.out +++ b/src/test/regress/expected/event_trigger.out @@ -398,6 +398,14 @@ DROP ROLE regress_evt_user; DROP EVENT TRIGGER regress_event_trigger_drop_objects; DROP EVENT TRIGGER undroppable; -- Event triggers on relations. +CREATE TABLE evttrig_pub_tbl (a int); +CREATE TABLE evttrig_pub_tbl2 (a int); +CREATE TABLE evttrig_pub_tbl3 (a int); +CREATE SCHEMA evttrig_pub_schema; +CREATE SCHEMA evttrig_pub_schema2; +CREATE PUBLICATION evttrig_pub FOR TABLE evttrig_pub_tbl, + evttrig_pub_tbl2, evttrig_pub_tbl3, TABLES IN SCHEMA evttrig_pub_schema, + evttrig_pub_schema2; CREATE OR REPLACE FUNCTION event_trigger_report_dropped() RETURNS event_trigger LANGUAGE plpgsql @@ -445,6 +453,16 @@ NOTICE: NORMAL: orig=t normal=f istemp=f type=table identity=public.tv2 schema= NOTICE: NORMAL: orig=f normal=t istemp=f type=table constraint identity=te1_b_fkey on public.te1 schema=public name=<NULL> addr={public,te1,te1_b_fkey} args={} NOTICE: NORMAL: orig=t normal=f istemp=f type=table identity=public.tv1 schema=public name=tv1 addr={public,tv1} args={} NOTICE: NORMAL: orig=f normal=t istemp=f type=table constraint identity=te1_a_fkey on public.te1 schema=public name=<NULL> addr={public,te1,te1_a_fkey} args={} +ALTER PUBLICATION evttrig_pub DROP TABLE evttrig_pub_tbl; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl in publication evttrig_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl} args={evttrig_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub +ALTER PUBLICATION evttrig_pub DROP TABLES IN SCHEMA evttrig_pub_schema; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication namespace identity=evttrig_pub_schema in publication evttrig_pub schema=<NULL> name=<NULL> addr={evttrig_pub_schema} args={evttrig_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub +ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication namespace identity=evttrig_pub_schema2 in publication evttrig_pub schema=<NULL> name=<NULL> addr={evttrig_pub_schema2} args={evttrig_pub} +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl3 in publication evttrig_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl3} args={evttrig_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -601,6 +619,12 @@ NOTICE: END: command_tag=CREATE OPERATOR FAMILY type=operator family identity=p NOTICE: END: command_tag=CREATE OPERATOR CLASS type=operator class identity=public.evttrigopclass USING btree DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; +DROP PUBLICATION evttrig_pub; +DROP TABLE evttrig_pub_tbl; +DROP TABLE evttrig_pub_tbl2; +DROP TABLE evttrig_pub_tbl3; +DROP SCHEMA evttrig_pub_schema; +DROP SCHEMA evttrig_pub_schema2; -- only allowed from within an event trigger function, should fail select pg_event_trigger_table_rewrite_oid(); ERROR: pg_event_trigger_table_rewrite_oid() can only be called in a table_rewrite event trigger function diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql index c1deac2d628..34bd0c23fa9 100644 --- a/src/test/regress/sql/event_trigger.sql +++ b/src/test/regress/sql/event_trigger.sql @@ -300,6 +300,15 @@ DROP EVENT TRIGGER regress_event_trigger_drop_objects; DROP EVENT TRIGGER undroppable; -- Event triggers on relations. +CREATE TABLE evttrig_pub_tbl (a int); +CREATE TABLE evttrig_pub_tbl2 (a int); +CREATE TABLE evttrig_pub_tbl3 (a int); +CREATE SCHEMA evttrig_pub_schema; +CREATE SCHEMA evttrig_pub_schema2; +CREATE PUBLICATION evttrig_pub FOR TABLE evttrig_pub_tbl, + evttrig_pub_tbl2, evttrig_pub_tbl3, TABLES IN SCHEMA evttrig_pub_schema, + evttrig_pub_schema2; + CREATE OR REPLACE FUNCTION event_trigger_report_dropped() RETURNS event_trigger LANGUAGE plpgsql @@ -340,6 +349,10 @@ REVOKE SELECT ON PROPERTY GRAPH gx FROM public; DROP PROPERTY GRAPH gx; DROP TABLE tv1, tv2, te1; +ALTER PUBLICATION evttrig_pub DROP TABLE evttrig_pub_tbl; +ALTER PUBLICATION evttrig_pub DROP TABLES IN SCHEMA evttrig_pub_schema; +ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; + CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -422,6 +435,13 @@ CREATE OPERATOR CLASS evttrigopclass FOR TYPE int USING btree AS STORAGE int; DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; +DROP PUBLICATION evttrig_pub; +DROP TABLE evttrig_pub_tbl; +DROP TABLE evttrig_pub_tbl2; +DROP TABLE evttrig_pub_tbl3; +DROP SCHEMA evttrig_pub_schema; +DROP SCHEMA evttrig_pub_schema2; + -- only allowed from within an event trigger function, should fail select pg_event_trigger_table_rewrite_oid(); -- 2.37.1 (Apple Git-137.1)
0002-Collect-ALTER-PUBLICATION-SET-ALL-commands-for-event.patch (application/octet-stream, 6.0K) download From 9b58975ebf44b17a887396de0722bc1d618b493f Mon Sep 17 00:00:00 2001 From: Fujii Masao <fujii@postgresql.org> Date: Fri, 24 Jul 2026 10:41:40 +0900 Subject: [PATCH v3 2/3] Collect ALTER PUBLICATION SET ALL commands for event triggers Previously, ALTER PUBLICATION ... SET ALL TABLES and SET ALL SEQUENCES fired ddl_command_end, but pg_event_trigger_ddl_commands() could return no entry for the altered publication because none was collected. Fix this by collecting the publication itself as the ALTER PUBLICATION command whenever a SET ALL command changes the publication state. As a side-effect of this fix, SET ALL TABLES with EXCEPT entries may now produce both a publication-level ALTER PUBLICATION entry and the existing per-membership entries for added exclusions. This is intentional, since they represent different parts of the command. Backpatch to v19, where ALTER PUBLICATION SET ALL TABLES and SET ALL SEQUENCES were introduced. --- src/backend/commands/publicationcmds.c | 11 +++++++---- src/test/regress/expected/event_trigger.out | 9 +++++++++ src/test/regress/sql/event_trigger.sql | 5 +++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index ebb7750ee30..601ae091e32 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -1607,7 +1607,7 @@ CheckAlterPublication(AlterPublicationStmt *stmt, HeapTuple tup, /* * Update FOR ALL TABLES / FOR ALL SEQUENCES flags of a publication. */ -static void +static bool AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup) { @@ -1618,7 +1618,7 @@ AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, bool dirty = false; if (!stmt->for_all_tables && !stmt->for_all_sequences) - return; + return false; pubform = (Form_pg_publication) GETSTRUCT(tup); @@ -1651,6 +1651,8 @@ AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, if (replaces[Anum_pg_publication_puballtables - 1]) CacheInvalidateRelcacheAll(); } + + return dirty; } /* @@ -1694,6 +1696,7 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) Oid pubid = pubform->oid; bool tables_dropped; bool schemas_dropped; + bool all_flags_changed; ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations, &exceptrelations, &schemaidlist); @@ -1724,9 +1727,9 @@ AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, schemaidlist != NIL); schemas_dropped = AlterPublicationSchemas(stmt, tup, schemaidlist); - AlterPublicationAllFlags(stmt, rel, tup); + all_flags_changed = AlterPublicationAllFlags(stmt, rel, tup); - if (tables_dropped || schemas_dropped) + if (tables_dropped || schemas_dropped || all_flags_changed) { ObjectAddress obj; diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out index 44503e6d0bd..0e57c2d4eb1 100644 --- a/src/test/regress/expected/event_trigger.out +++ b/src/test/regress/expected/event_trigger.out @@ -463,6 +463,14 @@ ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; NOTICE: NORMAL: orig=t normal=f istemp=f type=publication namespace identity=evttrig_pub_schema2 in publication evttrig_pub schema=<NULL> name=<NULL> addr={evttrig_pub_schema2} args={evttrig_pub} NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl3 in publication evttrig_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl3} args={evttrig_pub} NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_pub +CREATE PUBLICATION evttrig_all_pub; +NOTICE: END: command_tag=CREATE PUBLICATION type=publication identity=evttrig_all_pub +ALTER PUBLICATION evttrig_all_pub SET ALL TABLES EXCEPT (TABLE evttrig_pub_tbl); +NOTICE: END: command_tag=ALTER PUBLICATION type=publication relation identity=public.evttrig_pub_tbl in publication evttrig_all_pub +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_all_pub +ALTER PUBLICATION evttrig_all_pub SET ALL SEQUENCES; +NOTICE: NORMAL: orig=t normal=f istemp=f type=publication relation identity=public.evttrig_pub_tbl in publication evttrig_all_pub schema=<NULL> name=<NULL> addr={public,evttrig_pub_tbl} args={evttrig_all_pub} +NOTICE: END: command_tag=ALTER PUBLICATION type=publication identity=evttrig_all_pub CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -620,6 +628,7 @@ NOTICE: END: command_tag=CREATE OPERATOR CLASS type=operator class identity=pub DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; DROP PUBLICATION evttrig_pub; +DROP PUBLICATION evttrig_all_pub; DROP TABLE evttrig_pub_tbl; DROP TABLE evttrig_pub_tbl2; DROP TABLE evttrig_pub_tbl3; diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql index 34bd0c23fa9..85c961caef4 100644 --- a/src/test/regress/sql/event_trigger.sql +++ b/src/test/regress/sql/event_trigger.sql @@ -353,6 +353,10 @@ ALTER PUBLICATION evttrig_pub DROP TABLE evttrig_pub_tbl; ALTER PUBLICATION evttrig_pub DROP TABLES IN SCHEMA evttrig_pub_schema; ALTER PUBLICATION evttrig_pub SET TABLE evttrig_pub_tbl2; +CREATE PUBLICATION evttrig_all_pub; +ALTER PUBLICATION evttrig_all_pub SET ALL TABLES EXCEPT (TABLE evttrig_pub_tbl); +ALTER PUBLICATION evttrig_all_pub SET ALL SEQUENCES; + CREATE SCHEMA evttrig CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL) CREATE INDEX one_idx ON one (col_b) @@ -436,6 +440,7 @@ DROP EVENT TRIGGER regress_event_trigger_report_dropped; DROP EVENT TRIGGER regress_event_trigger_report_end; DROP PUBLICATION evttrig_pub; +DROP PUBLICATION evttrig_all_pub; DROP TABLE evttrig_pub_tbl; DROP TABLE evttrig_pub_tbl2; DROP TABLE evttrig_pub_tbl3; -- 2.37.1 (Apple Git-137.1)
0003-Invoke-post-alter-hook-for-ALTER-PUBLICATION-SET-ALL.patch (application/octet-stream, 1.5K) download From adc8ac29e5709e1b8c1a8c4c3230d52830287204 Mon Sep 17 00:00:00 2001 From: Fujii Masao <fujii@postgresql.org> Date: Fri, 24 Jul 2026 20:26:02 +0900 Subject: [PATCH v3 3/3] Invoke post-alter hook for ALTER PUBLICATION SET ALL Previously, ALTER PUBLICATION ... SET ALL TABLES and SET ALL SEQUENCES updated the pg_publication catalog without invoking the post-alter hook. As a result, object access hook users could miss changes made by these commands. Fix this by invoking the post-alter hook whenever a SET ALL command changes the publication state, using the updated catalog tuple. Backpatch to v19, where ALTER PUBLICATION SET ALL TABLES and SET ALL SEQUENCES were introduced. --- src/backend/commands/publicationcmds.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 601ae091e32..adbbc65cd8e 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -1647,9 +1647,14 @@ AlterPublicationAllFlags(AlterPublicationStmt *stmt, Relation rel, CatalogTupleUpdate(rel, &tup->t_self, tup); CommandCounterIncrement(); + pubform = (Form_pg_publication) GETSTRUCT(tup); + /* For ALL TABLES, we must invalidate all relcache entries */ if (replaces[Anum_pg_publication_puballtables - 1]) CacheInvalidateRelcacheAll(); + + InvokeObjectPostAlterHook(PublicationRelationId, + pubform->oid, 0); } return dirty; -- 2.37.1 (Apple Git-137.1)
^ permalink raw reply [nested|flat] 11+ messages in thread


end of thread, other threads:[~2026-09-05 02:56 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-07-24 02:06 Collect ALTER PUBLICATION commands for event triggers Fujii Masao <masao.fujii@gmail.com>
2026-07-24 07:45 ` Ian Lawrence Barwick <barwick@gmail.com>
2026-07-24 13:53   ` Fujii Masao <masao.fujii@gmail.com>
2026-07-24 08:38 ` Chao Li <li.evan.chao@gmail.com>
2026-07-24 13:54   ` Fujii Masao <masao.fujii@gmail.com>
2026-07-24 14:12     ` Chao Li <li.evan.chao@gmail.com>
2026-08-06 06:30       ` Chao Li <li.evan.chao@gmail.com>
2026-09-05 02:52         ` shihao zhong <zhong950419@gmail.com>
2026-09-05 02:56           ` shihao zhong <zhong950419@gmail.com>
2026-07-24 12:03 ` Zhijie Hou (Fujitsu) <houzj.fnst@fujitsu.com>
2026-07-24 13:59   ` Fujii Masao <masao.fujii@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox