Patch
Record extension membership for publications
- Branch
- master
- Risk
- –
- Base commit
- a9cb202e9e09fe1031e5dd03408668bebe6f98a2
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1cb98-9752-7c6c-8a70-f187fa3a5b1b | jq -r .body | git am
From af948abd82f89e5beef7d2c8cc9537ac71ec3603 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Wed, 11 Nov 2026 09:11:06 -0800
Subject: [PATCH] Record extension membership for publications.
CREATE PUBLICATION did not call recordDependencyOnCurrentExtension,
and ALTER EXTENSION rejected publications. DROP EXTENSION therefore
left the publication in place. Installing the extension again failed
with "publication already exists", and pg_dump emitted the publication
both from the extension script and as a separate object.
Record the dependency, accept ALTER EXTENSION ADD and DROP
PUBLICATION, and emit ALTER EXTENSION ... ADD PUBLICATION during
binary upgrade.
---
doc/src/sgml/ref/alter_extension.sgml | 1 +
src/backend/commands/extension.c | 1 -
src/backend/commands/publicationcmds.c | 4 +++
src/bin/pg_dump/pg_dump.c | 4 +++
src/bin/psql/tab-complete.in.c | 3 +-
src/test/modules/test_extensions/Makefile | 2 ++
.../expected/test_extensions.out | 36 +++++++++++++++++++
src/test/modules/test_extensions/meson.build | 2 ++
.../test_extensions/sql/test_extensions.sql | 20 +++++++++++
.../test_extensions/test_ext_pub--1.0.sql | 8 +++++
.../test_extensions/test_ext_pub.control | 3 ++
11 files changed, 82 insertions(+), 2 deletions(-)
create mode 100644 src/test/modules/test_extensions/test_ext_pub--1.0.sql
create mode 100644 src/test/modules/test_extensions/test_ext_pub.control
diff --git a/doc/src/sgml/ref/alter_extension.sgml b/doc/src/sgml/ref/alter_extension.sgml
index c819c7bb4e3..ca5273b9518 100644
--- a/doc/src/sgml/ref/alter_extension.sgml
+++ b/doc/src/sgml/ref/alter_extension.sgml
@@ -46,6 +46,7 @@ ALTER EXTENSION <replaceable class="parameter">name</replaceable> DROP <replacea
OPERATOR FAMILY <replaceable class="parameter">object_name</replaceable> USING <replaceable class="parameter">index_method</replaceable> |
[ PROCEDURAL ] LANGUAGE <replaceable class="parameter">object_name</replaceable> |
PROCEDURE <replaceable class="parameter">procedure_name</replaceable> [ ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] ) ] |
+ PUBLICATION <replaceable class="parameter">object_name</replaceable> |
ROUTINE <replaceable class="parameter">routine_name</replaceable> [ ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] ) ] |
SCHEMA <replaceable class="parameter">object_name</replaceable> |
SEQUENCE <replaceable class="parameter">object_name</replaceable> |
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 35443dabecf..4a0ce409a5a 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -3931,7 +3931,6 @@ ExecAlterExtensionContentsStmt(AlterExtensionContentsStmt *stmt,
case OBJECT_DATABASE:
case OBJECT_EXTENSION:
case OBJECT_INDEX:
- case OBJECT_PUBLICATION:
case OBJECT_ROLE:
case OBJECT_STATISTIC_EXT:
case OBJECT_SUBSCRIPTION:
diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c
index 301050104dd..ae269f0ce1a 100644
--- a/src/backend/commands/publicationcmds.c
+++ b/src/backend/commands/publicationcmds.c
@@ -18,6 +18,7 @@
#include "access/table.h"
#include "access/xact.h"
#include "catalog/catalog.h"
+#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
@@ -931,6 +932,9 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
ObjectAddressSet(myself, PublicationRelationId, puboid);
+ /* Depend on extension, if any. */
+ recordDependencyOnCurrentExtension(&myself, false);
+
/* Make the changes visible. */
CommandCounterIncrement();
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index a3f38f3b886..346e404a2cf 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -4730,6 +4730,10 @@ dumpPublication(Archive *fout, const PublicationInfo *pubinfo)
appendPQExpBufferStr(query, ");\n");
+ if (dopt->binary_upgrade)
+ binary_upgrade_extension_member(query, &pubinfo->dobj,
+ "PUBLICATION", qpubname, NULL);
+
if (pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId,
ARCHIVE_OPTS(.tag = pubinfo->dobj.name,
diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 96de78cd087..b38de90cbfc 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -2432,7 +2432,8 @@ match_previous_words(int pattern_id,
COMPLETE_WITH("ACCESS METHOD", "AGGREGATE", "CAST", "COLLATION",
"CONVERSION", "DOMAIN", "EVENT TRIGGER", "FOREIGN",
"FUNCTION", "MATERIALIZED VIEW", "OPERATOR",
- "LANGUAGE", "PROCEDURE", "ROUTINE", "SCHEMA",
+ "LANGUAGE", "PROCEDURE", "PUBLICATION",
+ "ROUTINE", "SCHEMA",
"SEQUENCE", "SERVER", "TABLE", "TEXT SEARCH",
"TRANSFORM FOR", "TYPE", "VIEW");
diff --git a/src/test/modules/test_extensions/Makefile b/src/test/modules/test_extensions/Makefile
index d1b0b81e5fd..431bc4f8185 100644
--- a/src/test/modules/test_extensions/Makefile
+++ b/src/test/modules/test_extensions/Makefile
@@ -10,6 +10,7 @@ EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \
test_ext_extschema \
test_ext_evttrig \
test_ext_set_schema \
+ test_ext_pub \
test_ext_req_schema1 test_ext_req_schema2 test_ext_req_schema3
OBJS = test_ext.o
@@ -26,6 +27,7 @@ DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \
test_ext_extschema--1.0.sql \
test_ext_evttrig--1.0.sql test_ext_evttrig--1.0--2.0.sql \
test_ext_set_schema--1.0.sql \
+ test_ext_pub--1.0.sql \
test_ext_req_schema1--1.0.sql \
test_ext_req_schema2--1.0.sql \
test_ext_req_schema3--1.0.sql
diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out
index 39366811b32..db814c0f8bb 100644
--- a/src/test/modules/test_extensions/expected/test_extensions.out
+++ b/src/test/modules/test_extensions/expected/test_extensions.out
@@ -724,3 +724,39 @@ SELECT test_s_dep.dep_req2();
DROP EXTENSION test_ext_req_schema1 CASCADE;
NOTICE: drop cascades to extension test_ext_req_schema2
+--
+-- Publications created by an extension script are extension members.
+--
+CREATE EXTENSION test_ext_pub;
+SELECT pg_describe_object(classid, objid, objsubid) AS obj
+ FROM pg_depend
+ WHERE refclassid = 'pg_extension'::regclass
+ AND refobjid = (SELECT oid FROM pg_extension WHERE extname = 'test_ext_pub')
+ AND deptype = 'e'
+ ORDER BY 1;
+ obj
+-------------------------
+ publication ext_pub_all
+ publication ext_pub_tbl
+ table ext_pub_t
+ type ext_pub_t
+ type ext_pub_t[]
+(5 rows)
+
+DROP EXTENSION test_ext_pub;
+SELECT pubname FROM pg_publication WHERE pubname LIKE 'ext_pub%' ORDER BY 1;
+ pubname
+---------
+(0 rows)
+
+CREATE EXTENSION test_ext_pub;
+CREATE PUBLICATION ext_pub_free;
+ALTER EXTENSION test_ext_pub ADD PUBLICATION ext_pub_free;
+ALTER EXTENSION test_ext_pub DROP PUBLICATION ext_pub_free;
+DROP PUBLICATION ext_pub_free;
+DROP EXTENSION test_ext_pub;
+SELECT pubname FROM pg_publication WHERE pubname LIKE 'ext_pub%' ORDER BY 1;
+ pubname
+---------
+(0 rows)
+
diff --git a/src/test/modules/test_extensions/meson.build b/src/test/modules/test_extensions/meson.build
index 2c7cea189e2..b24da377806 100644
--- a/src/test/modules/test_extensions/meson.build
+++ b/src/test/modules/test_extensions/meson.build
@@ -44,6 +44,8 @@ test_install_data += files(
'test_ext_req_schema3.control',
'test_ext_set_schema--1.0.sql',
'test_ext_set_schema.control',
+ 'test_ext_pub--1.0.sql',
+ 'test_ext_pub.control',
)
test_ext_sources = files('test_ext.c')
diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql
index 4d2178a4d83..5509e03ba0a 100644
--- a/src/test/modules/test_extensions/sql/test_extensions.sql
+++ b/src/test/modules/test_extensions/sql/test_extensions.sql
@@ -349,3 +349,23 @@ ALTER EXTENSION test_ext_req_schema1 SET SCHEMA test_s_dep2; -- now ok
SELECT test_s_dep2.dep_req1();
SELECT test_s_dep.dep_req2();
DROP EXTENSION test_ext_req_schema1 CASCADE;
+
+--
+-- Publications created by an extension script are extension members.
+--
+CREATE EXTENSION test_ext_pub;
+SELECT pg_describe_object(classid, objid, objsubid) AS obj
+ FROM pg_depend
+ WHERE refclassid = 'pg_extension'::regclass
+ AND refobjid = (SELECT oid FROM pg_extension WHERE extname = 'test_ext_pub')
+ AND deptype = 'e'
+ ORDER BY 1;
+DROP EXTENSION test_ext_pub;
+SELECT pubname FROM pg_publication WHERE pubname LIKE 'ext_pub%' ORDER BY 1;
+CREATE EXTENSION test_ext_pub;
+CREATE PUBLICATION ext_pub_free;
+ALTER EXTENSION test_ext_pub ADD PUBLICATION ext_pub_free;
+ALTER EXTENSION test_ext_pub DROP PUBLICATION ext_pub_free;
+DROP PUBLICATION ext_pub_free;
+DROP EXTENSION test_ext_pub;
+SELECT pubname FROM pg_publication WHERE pubname LIKE 'ext_pub%' ORDER BY 1;
diff --git a/src/test/modules/test_extensions/test_ext_pub--1.0.sql b/src/test/modules/test_extensions/test_ext_pub--1.0.sql
new file mode 100644
index 00000000000..7cc23ba2693
--- /dev/null
+++ b/src/test/modules/test_extensions/test_ext_pub--1.0.sql
@@ -0,0 +1,8 @@
+/* src/test/modules/test_extensions/test_ext_pub--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION test_ext_pub" to load this file. \quit
+
+CREATE TABLE ext_pub_t (a int);
+CREATE PUBLICATION ext_pub_tbl FOR TABLE ext_pub_t;
+CREATE PUBLICATION ext_pub_all FOR ALL TABLES;
diff --git a/src/test/modules/test_extensions/test_ext_pub.control b/src/test/modules/test_extensions/test_ext_pub.control
new file mode 100644
index 00000000000..df8605862c5
--- /dev/null
+++ b/src/test/modules/test_extensions/test_ext_pub.control
@@ -0,0 +1,3 @@
+comment = 'Test extension publication membership'
+default_version = '1.0'
+relocatable = false
base-commit: a9cb202e9e09fe1031e5dd03408668bebe6f98a2
--
2.53.0