From c47327949c0dee6520842904141e4a9c1e61d61d Mon Sep 17 00:00:00 2001 From: Vignesh C Date: Fri, 27 Nov 2026 07:07:18 +0530 Subject: [PATCH v2 2/2] Preserve conflict log tables and conflict_log_destination across pg_upgrade. A subscription's conflict log table name is derived from its OID, and the table is linked through pg_subscription.subconflictlogrelid. Neither was preserved across pg_upgrade. In binary-upgrade mode, pg_dump now dumps conflict log tables with their original OIDs, together with their privileges, comments and security labels. It also preserves conflict_log_destination, omitting the default value ('log'). A conflict log table is dumped only along with its subscription. The pg_conflict schema's own ACLs are dumped as for pg_catalog. During restore, CreateSubscription() links the subscription to the already restored conflict log table, raising an error if the table is missing rather than creating a new one. Outside binary-upgrade mode, conflict log tables are recreated when the subscription is restored, and nothing about them is dumped, unless they are named explicitly. --- doc/src/sgml/logical-replication.sgml | 6 +- doc/src/sgml/ref/pg_dump.sgml | 11 +++ src/backend/commands/subscriptioncmds.c | 26 +++++- src/backend/replication/logical/conflict.c | 2 +- src/bin/pg_dump/pg_dump.c | 101 +++++++++++++++++++-- src/bin/pg_dump/pg_dump.h | 1 + src/bin/pg_dump/t/002_pg_dump.pl | 35 ++++++- src/bin/pg_upgrade/pg_upgrade.c | 4 +- src/bin/pg_upgrade/t/004_subscription.pl | 57 +++++++++++- src/include/catalog/pg_subscription.h | 3 + 10 files changed, 225 insertions(+), 21 deletions(-) diff --git a/doc/src/sgml/logical-replication.sgml b/doc/src/sgml/logical-replication.sgml index 728348f3a72..5e665fad57c 100644 --- a/doc/src/sgml/logical-replication.sgml +++ b/doc/src/sgml/logical-replication.sgml @@ -3159,7 +3159,11 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER pg_upgrade also preserves the OID of every - subscription. + subscription. If + conflict_log_destination + is set to table or all, the subscription's + conflict log table + is preserved as well. diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml index 951de54e77f..9a87b5784b8 100644 --- a/doc/src/sgml/ref/pg_dump.sgml +++ b/doc/src/sgml/ref/pg_dump.sgml @@ -1736,6 +1736,17 @@ CREATE DATABASE foo WITH TEMPLATE template0; had been originally created with two_phase = true option. + + If a subscription has been created with the + conflict_log_destination + option set to table or all, + pg_dump does not dump its conflict log table + or its contents. Restoring the subscription creates a new, empty + conflict log table. Privileges, comments and security labels that had been + set on the old conflict log table are not restored, and must be set again on + the new table if needed. + + It is generally recommended to use the () option when restoring a database from a diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index e0ad5baefb2..abea2ecd276 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -923,12 +923,30 @@ CreateSubscription(ParseState *pstate, CreateSubscriptionStmt *stmt, CStringGetTextDatum(ConflictLogDestNames[opts.conflictlogdest]); /* - * We create the conflict log table here, if required, so that its - * relation OID can be stored when inserting the pg_subscription tuple - * below. + * Create the conflict log table here, if required, so that its relation + * OID can be stored when inserting the pg_subscription tuple below. In + * binary-upgrade mode, the table was already restored earlier in the dump + * with its original OID and filenode, so look it up and reuse it. */ if (CONFLICTS_LOGGED_TO_TABLE(opts.conflictlogdest)) - logrelid = create_conflict_log_table(subid, stmt->subname, owner); + { + if (IsBinaryUpgrade) + { + char relname[NAMEDATALEN]; + + snprintf(relname, NAMEDATALEN, CONFLICT_LOG_RELATION_NAME_FMT, + subid); + logrelid = get_relname_relid(relname, PG_CONFLICT_NAMESPACE); + if (!OidIsValid(logrelid)) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_TABLE), + errmsg("could not find conflict log table \"%s\" when in binary upgrade mode", + get_qualified_objname(PG_CONFLICT_NAMESPACE, + relname)))); + } + else + logrelid = create_conflict_log_table(subid, stmt->subname, owner); + } /* Store table OID in the catalog. */ values[Anum_pg_subscription_subconflictlogrelid - 1] = diff --git a/src/backend/replication/logical/conflict.c b/src/backend/replication/logical/conflict.c index 73c4077d05a..819f32cf7c0 100644 --- a/src/backend/replication/logical/conflict.c +++ b/src/backend/replication/logical/conflict.c @@ -207,7 +207,7 @@ create_conflict_log_table(Oid subid, char *subname, Oid subowner) Oid relid; char relname[NAMEDATALEN]; - snprintf(relname, NAMEDATALEN, "pg_conflict_log_%u", subid); + snprintf(relname, NAMEDATALEN, CONFLICT_LOG_RELATION_NAME_FMT, subid); /* Build the tuple descriptor for the new table. */ tupdesc = create_conflict_log_table_tupdesc(); diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 24517805b00..5deab580b13 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -1667,10 +1667,16 @@ expand_schema_name_patterns(Archive *fout, PQExpBufferData dbbuf; int dotcnt; + /* + * The pg_conflict schema, which holds the conflict log tables, is not + * selectable. The tables are dumped only by pg_upgrade, along with + * the subscriptions. + */ appendPQExpBufferStr(query, - "SELECT oid FROM pg_catalog.pg_namespace n\n"); + "SELECT oid FROM pg_catalog.pg_namespace n\n" + "WHERE n.nspname OPERATOR(pg_catalog.<>) 'pg_conflict'\n"); initPQExpBuffer(&dbbuf); - processSQLNamePattern(GetConnection(fout), query, cell->val, false, + processSQLNamePattern(GetConnection(fout), query, cell->val, true, false, NULL, "n.nspname", NULL, NULL, &dbbuf, &dotcnt); if (dotcnt > 1) @@ -1835,6 +1841,10 @@ expand_table_name_patterns(Archive *fout, * would be unnecessary given a pg_table_is_visible() variant taking a * search_path argument. * + * Conflict log tables, which are in the pg_conflict schema, are not + * selectable, as they are dumped only by pg_upgrade, along with the + * subscriptions. + * * For with_child_tables, we start with the basic query's results and * recursively search the inheritance tree to add child tables. */ @@ -1849,7 +1859,8 @@ expand_table_name_patterns(Archive *fout, "\n LEFT JOIN pg_catalog.pg_namespace n" "\n ON n.oid OPERATOR(pg_catalog.=) c.relnamespace" "\nWHERE c.relkind OPERATOR(pg_catalog.=) ANY" - "\n (array['%c', '%c', '%c', '%c', '%c', '%c'])\n", + "\n (array['%c', '%c', '%c', '%c', '%c', '%c'])" + "\n AND n.nspname OPERATOR(pg_catalog.<>) 'pg_conflict'\n", RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW, RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE, RELKIND_PARTITIONED_TABLE); @@ -1999,6 +2010,34 @@ selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout) */ nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL; } + else if (strcmp(nsinfo->dobj.name, "pg_conflict") == 0) + { + /* + * The pg_conflict schema is created by initdb and holds the + * subscriptions' conflict log tables. Dump the ACLs of the schema, + * as for pg_catalog. + */ + nsinfo->dobj.dump = DUMP_COMPONENT_ACL; + + /* + * Conflict log tables are dumped only in binary-upgrade mode, which + * only pg_upgrade uses. There, the tables are restored so that + * CREATE SUBSCRIPTION can reuse them. In other modes, CREATE + * SUBSCRIPTION creates them again, so nothing about them is dumped, + * not even their ACLs, as the restored subscription's table has a + * different name. + * + * In binary-upgrade mode, dump everything about the tables, + * including their privileges, comments, and security labels. For + * example, privileges may allow non-superusers to delete conflicts. + * The rows are not dumped, as pg_upgrade runs pg_dump with --no-data + * and carries them over with the table files. + */ + if (fout->dopt->binary_upgrade) + nsinfo->dobj.dump_contains = DUMP_COMPONENT_ALL; + else + nsinfo->dobj.dump_contains = DUMP_COMPONENT_NONE; + } else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 || strcmp(nsinfo->dobj.name, "information_schema") == 0) { @@ -2049,6 +2088,17 @@ selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout) (void) checkExtensionMembership(&nsinfo->dobj, fout); } +/* + * is_conflict_log_table: is this a subscription's conflict log table? + * + * Conflict log tables are the tables of the pg_conflict schema. + */ +static bool +is_conflict_log_table(const TableInfo *tbinfo) +{ + return strcmp(tbinfo->dobj.namespace->dobj.name, "pg_conflict") == 0; +} + /* * selectDumpableTable: policy-setting subroutine * Mark a table as to be dumped or not @@ -5142,6 +5192,7 @@ getSubscriptions(Archive *fout) int i_subfailover; int i_subretaindeadtuples; int i_submaxretention; + int i_subconflictlogdest; int i, ntups; @@ -5240,9 +5291,16 @@ getSubscriptions(Archive *fout) " '-1' AS subwalrcvtimeout,\n"); if (fout->remoteVersion >= 190000) - appendPQExpBufferStr(query, " fs.srvname AS subservername\n"); + appendPQExpBufferStr(query, " fs.srvname AS subservername,\n"); else - appendPQExpBufferStr(query, " NULL AS subservername\n"); + appendPQExpBufferStr(query, " NULL AS subservername,\n"); + + if (fout->remoteVersion >= 200000) + appendPQExpBufferStr(query, + " s.subconflictlogdest\n"); + else + appendPQExpBufferStr(query, + " 'log' AS subconflictlogdest\n"); appendPQExpBufferStr(query, "FROM pg_subscription s\n"); @@ -5291,6 +5349,7 @@ getSubscriptions(Archive *fout) i_subpublications = PQfnumber(res, "subpublications"); i_suborigin = PQfnumber(res, "suborigin"); i_suboriginremotelsn = PQfnumber(res, "suboriginremotelsn"); + i_subconflictlogdest = PQfnumber(res, "subconflictlogdest"); subinfo = pg_malloc_array(SubscriptionInfo, ntups); @@ -5349,6 +5408,9 @@ getSubscriptions(Archive *fout) subinfo[i].suboriginremotelsn = pg_strdup(PQgetvalue(res, i, i_suboriginremotelsn)); + subinfo[i].subconflictlogdest = + pg_strdup(PQgetvalue(res, i, i_subconflictlogdest)); + /* Decide whether we want to dump it */ selectDumpableObject(&(subinfo[i].dobj), fout); } @@ -5620,6 +5682,10 @@ dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo) if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0) appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin); + if (pg_strcasecmp(subinfo->subconflictlogdest, "log") != 0) + appendPQExpBuffer(query, ", conflict_log_destination = %s", + subinfo->subconflictlogdest); + appendPQExpBufferStr(query, ");\n"); /* @@ -17155,6 +17221,14 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) int j, k; + /* + * Do not emit "ALTER TABLE ... OWNER TO ..." for a conflict log table: + * changing its owner is not allowed. Its owner follows the owner of the + * subscription, and is set when the ALTER SUBSCRIPTION ... OWNER TO of + * the dump is restored. + */ + bool conflict_log_table = is_conflict_log_table(tbinfo); + /* We had better have loaded per-column details about this table */ Assert(tbinfo->interesting); @@ -17290,6 +17364,11 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) appendPQExpBuffer(delq, "DROP %s %s;\n", reltypename, qualrelname); + /* + * This also keeps the OIDs and filenodes of a conflict log table, so + * that pg_upgrade finds its files and pg_subscription's + * subconflictlogrelid stays the same. + */ if (dopt->binary_upgrade) binary_upgrade_set_pg_class_oids(fout, q, tbinfo->dobj.catId.oid); @@ -18061,7 +18140,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) .tablespace = tablespace, .tableam = tableam, .relkind = tbinfo->relkind, - .owner = tbinfo->rolname, + .owner = conflict_log_table ? NULL : tbinfo->rolname, .description = reltypename, .section = tbinfo->postponed_def ? SECTION_POST_DATA : SECTION_PRE_DATA, @@ -20409,12 +20488,16 @@ getDependencies(Archive *fout) * Ordinarily, table rowtypes have implicit dependencies on their * tables. However, for a composite type the implicit dependency goes * the other way in pg_depend; which is the right thing for DROP but - * it doesn't produce the dependency ordering we need. So in that one - * case, we reverse the direction of the dependency. + * it doesn't produce the dependency ordering we need. The same is + * true for a subscription's conflict log table, which has an internal + * dependency on its subscription for DROP, but must be dumped before + * the subscription in binary-upgrade mode. So in those cases, we + * reverse the direction of the dependency. */ if (deptype == 'i' && dobj->objType == DO_TABLE && - refdobj->objType == DO_TYPE) + (refdobj->objType == DO_TYPE || + refdobj->objType == DO_SUBSCRIPTION)) addObjectDependency(refdobj, dobj->dumpId); else /* normal case */ diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 41d932f003b..aa6e3e96a54 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -730,6 +730,7 @@ typedef struct _SubscriptionInfo char *subpublications; char *suborigin; char *suboriginremotelsn; + char *subconflictlogdest; } SubscriptionInfo; /* diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl index ec73c4d5be9..6fa6b13d522 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -3424,9 +3424,9 @@ my %tests = ( create_order => 50, create_sql => 'CREATE SUBSCRIPTION sub3 CONNECTION \'dbname=doesnotexist\' PUBLICATION pub1 - WITH (connect = false, origin = any, streaming = on);', + WITH (connect = false, origin = any, streaming = on, conflict_log_destination = table);', regexp => qr/^ - \QCREATE SUBSCRIPTION sub3 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub3', streaming = on);\E + \QCREATE SUBSCRIPTION sub3 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub3', streaming = on, conflict_log_destination = table);\E /xm, like => { %full_runs, section_post_data => 1, }, unlike => { @@ -3435,6 +3435,37 @@ my %tests = ( }, }, + # In binary-upgrade mode, the subscription OID is preserved and the + # conflict log table is dumped, to be reused by the CREATE SUBSCRIPTION + # above. The conflict log table is dumped before its subscription, and its + # name is derived from the subscription OID, so require that the table is + # the one named after the OID set for sub3, not the one of another + # subscription. + 'binary upgrade: pg_subscription OID and conflict log table of sub3' => { + regexp => qr/^ + \QCREATE TABLE pg_conflict.pg_conflict_log_\E(\d+)\Q (\E + .*? + ^\Q-- For binary upgrade, must preserve pg_subscription.oid\E\n + \QSELECT pg_catalog.binary_upgrade_set_next_pg_subscription_oid('\E\1\Q'::pg_catalog.oid);\E\n+ + \QCREATE SUBSCRIPTION sub3 \E + /xms, + like => { binary_upgrade => 1, }, + }, + + 'CREATE SUBSCRIPTION sub4' => { + create_order => 50, + create_sql => 'CREATE SUBSCRIPTION sub4 + CONNECTION \'dbname=doesnotexist\' PUBLICATION pub1 + WITH (connect = false, conflict_log_destination = all);', + regexp => qr/^ + \QCREATE SUBSCRIPTION sub4 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub4', streaming = parallel, conflict_log_destination = all);\E + /xm, + like => { %full_runs, section_post_data => 1, }, + unlike => { + no_subscriptions => 1, + no_subscriptions_restore => 1, + }, + }, # Regardless of whether the table or schema is excluded, publications must # still be dumped, as excluded objects do not apply to publications. We diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c index 0ba6965dd86..52b6fa4ba16 100644 --- a/src/bin/pg_upgrade/pg_upgrade.c +++ b/src/bin/pg_upgrade/pg_upgrade.c @@ -37,8 +37,8 @@ * names to match between the old and new cluster. * * We control assignment of pg_subscription.oid because we want the oid to - * match between the old and new cluster, as the names of the subscription's - * replication origin and slots contain it. + * match between the old and new cluster, so that the conflict log table, + * which is named using the subscription oid, can be preserved. */ diff --git a/src/bin/pg_upgrade/t/004_subscription.pl b/src/bin/pg_upgrade/t/004_subscription.pl index 5b148482518..d5340e4fb9f 100644 --- a/src/bin/pg_upgrade/t/004_subscription.pl +++ b/src/bin/pg_upgrade/t/004_subscription.pl @@ -290,7 +290,7 @@ $publisher->safe_psql( $old_sub->safe_psql( 'postgres', qq[ CREATE TABLE tab_upgraded2(id int); - CREATE SUBSCRIPTION regress_sub5 CONNECTION '$connstr' PUBLICATION regress_pub5; + CREATE SUBSCRIPTION regress_sub5 CONNECTION '$connstr' PUBLICATION regress_pub5 WITH (conflict_log_destination = 'table'); ]); # The table tab_upgraded2 will be in the init state as the subscriber's @@ -314,7 +314,33 @@ my $tab_upgraded2_oid = $old_sub->safe_psql('postgres', "SELECT oid FROM pg_class WHERE relname = 'tab_upgraded2'"); my $sub5_oid = $old_sub->safe_psql('postgres', "SELECT oid FROM pg_subscription where subname = 'regress_sub5'"); +my $sub_clt_relid = $old_sub->safe_psql('postgres', + "SELECT subconflictlogrelid FROM pg_subscription WHERE subname = 'regress_sub5'" +); +# A privilege granted on the conflict log table must survive the upgrade. +my $sub_clt_name = $old_sub->safe_psql('postgres', + "SELECT relname FROM pg_class WHERE oid = $sub_clt_relid"); +$old_sub->safe_psql( + 'postgres', qq[ + CREATE ROLE regress_clt_reader; + GRANT DELETE ON pg_conflict.$sub_clt_name TO regress_clt_reader; +]); + +# A comment on the table and on a column must survive the upgrade too. +my $sub_clt_col = $old_sub->safe_psql('postgres', + "SELECT attname FROM pg_attribute WHERE attrelid = $sub_clt_relid AND attnum = 1" +); +$old_sub->safe_psql( + 'postgres', qq[ + COMMENT ON TABLE pg_conflict.$sub_clt_name IS 'clt table comment'; + COMMENT ON COLUMN pg_conflict.$sub_clt_name.$sub_clt_col IS 'clt column comment'; +]); + +# A privilege revoked on the pg_conflict schema must stay revoked after the +# upgrade. +$old_sub->safe_psql('postgres', + 'REVOKE USAGE ON SCHEMA pg_conflict FROM PUBLIC'); $old_sub->stop; # Change configuration so that initial table sync does not get started @@ -396,11 +422,38 @@ $result = $new_sub->safe_psql('postgres', ); is($result, qq(t), "conflict detection slot exists"); -# The subscription oid should be preserved +# The subscription oid and the subscription conflict log table relid should be preserved $result = $new_sub->safe_psql('postgres', "SELECT oid FROM pg_subscription WHERE subname = 'regress_sub5'"); is($result, qq($sub5_oid), "subscription oid should have been preserved"); +$result = $new_sub->safe_psql('postgres', + "SELECT subconflictlogrelid FROM pg_subscription WHERE subname = 'regress_sub5'" +); +is($result, qq($sub_clt_relid), + "subscription conflict log table relid should have been preserved"); + +$result = $new_sub->safe_psql('postgres', + "SELECT has_table_privilege('regress_clt_reader', $sub_clt_relid, 'DELETE'), has_table_privilege('regress_clt_reader', $sub_clt_relid, 'INSERT')" +); +is($result, qq(t|f), + "privileges granted on the subscription conflict log table should have been preserved" +); + +$result = $new_sub->safe_psql('postgres', + "SELECT obj_description($sub_clt_relid, 'pg_class'), col_description($sub_clt_relid, 1)" +); +is( $result, + qq(clt table comment|clt column comment), + "comments on the subscription conflict log table should have been preserved" +); + +$result = $new_sub->safe_psql('postgres', + "SELECT has_schema_privilege('regress_clt_reader', 'pg_conflict', 'USAGE')" +); +is($result, qq(f), + "a privilege revoked on the pg_conflict schema should stay revoked"); + # Resume the initial sync and wait until all tables of subscription # 'regress_sub5' are synchronized $new_sub->append_conf('postgresql.conf', diff --git a/src/include/catalog/pg_subscription.h b/src/include/catalog/pg_subscription.h index b860f02d166..dd8bfd6eb5b 100644 --- a/src/include/catalog/pg_subscription.h +++ b/src/include/catalog/pg_subscription.h @@ -221,6 +221,9 @@ typedef struct Subscription #endif /* EXPOSE_TO_CLIENT_CODE */ +/* Conflict log table name format. %u is replaced with the subscription OID. */ +#define CONFLICT_LOG_RELATION_NAME_FMT "pg_conflict_log_%u" + extern Subscription *GetSubscription(Oid subid, bool missing_ok); extern char *SubscriptionConninfo(Subscription *sub); extern void DisableSubscription(Oid subid); -- 2.55.0