pg_restore --no-policies should not restore policies' comment
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2025-06-27T04:08:35Z
Lists: pgsql-hackers
Attachments
- v1-0001-fix-pg_restore-not-restore-comments.patch (application/x-patch) patch v1-0001
hi.
first looking at function dumpPolicy (pg_dump.c):
appendPQExpBuffer(polprefix, "POLICY %s ON",
fmtId(polinfo->polname));
tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name);
....
if (polinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
dumpComment(fout, polprefix->data, qtabname,
tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
then looking at function dumpCommentExtended:
appendPQExpBuffer(query, "COMMENT ON %s ", type);
if (namespace && *namespace)
appendPQExpBuffer(query, "%s.", fmtId(namespace));
appendPQExpBuffer(query, "%s IS ", name);
appendStringLiteralAH(query, comments->descr, fout);
appendPQExpBufferStr(query, ";\n");
appendPQExpBuffer(tag, "%s %s", type, name);
/*
* We mark comments as SECTION_NONE because they really belong in the
* same section as their parent, whether that is pre-data or
* post-data.
*/
ArchiveEntry(fout, nilCatalogId, createDumpId(),
ARCHIVE_OPTS(.tag = tag->data,
.namespace = namespace,
.owner = owner,
.description = "COMMENT",
.section = SECTION_NONE,
.createStmt = query->data,
.deps = &dumpId,
.nDeps = 1));
also looking at function ArchiveEntry in pg_backup_archiver.c
newToc->tag = pg_strdup(opts->tag);
if pg_restore --no-policies is specified then we generally don't want
to restore policies' comments too.
To do that, we need
1. we checked that COMMENTS on policies, the TocEntry->tag begins with
"POLICY". which is true, see above code walk through.
2. We also need to make sure that no other dumpComment call results in a
COMMENT command whose TocEntry->tag also starts with "POLICY".
which is also true, per https://www.postgresql.org/docs/current/sql-comment.html
after "COMMENT ON", the next word is fixed, and "POLICY" only occurs once.
If this is what we want, we can do the same for
"--no-publications", "--no-subscriptions" too.
Commits
-
pg_dump: Remove unnecessary code for security labels on extensions.
- 762faf702c6f 19 (unreleased) landed
-
pg_restore: Fix security label handling with --no-publications/subscriptions.
- a4dbb11bb9ac 13.23 landed
- bc476f8b8b64 14.20 landed
- 5f42008f9030 15.15 landed
- 0870397ccfbd 16.11 landed
- dc8aa2f586d0 17.7 landed
- 7aecc00b3d66 18.0 landed
- 45f50c995fb6 19 (unreleased) landed
-
pg_dump: Fix dumping of security labels on subscriptions and event triggers.
- dff7591a7041 13.23 landed
- 295c0a644af5 14.20 landed
- 165b07efe4e2 15.15 landed
- 20b23784fc31 16.11 landed
- 968141898777 17.7 landed
- 176002c5bfa6 18.0 landed
- 8e5b92928d28 19 (unreleased) landed
-
pg_restore: Fix comment handling with --no-policies.
- 2ddbfede0c61 18.0 landed
- 66dabc06b1b0 19 (unreleased) landed
-
pg_restore: Fix comment handling with --no-publications / --no-subscriptions.
- 8fbd1f8ea2e1 13.23 landed
- db900ec358bd 14.20 landed
- c8ed16050564 15.15 landed
- 97527a5e68d5 16.11 landed
- f7f9c5d65816 17.7 landed
- 77d2b155caf7 18.0 landed
- b54e8dbfe3b1 19 (unreleased) landed
-
Core support for "extensions", which are packages of SQL objects.
- d9572c4e3b47 9.1.0 cited