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.
From 235f05dc6a1fcb99e4cfb6f9d6a3ca8bc5c392db Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Fri, 27 Jun 2025 11:59:31 +0800
Subject: [PATCH v1 1/1] fix pg_restore not restore comments

in pg_restore, if --no-policies is specified, then there's no need to restore
comments on policies either.
---
 src/bin/pg_dump/pg_backup_archiver.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 197c1295d93..d81e392a832 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -3020,6 +3020,14 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
 		 strcmp(te->desc, "ROW SECURITY") == 0))
 		return 0;
 
+	/*
+	 * If --no-policies is specified, there's no need to restore comments on
+	 * policies either.
+	*/
+	if (ropt->no_policies && (strcmp(te->desc, "COMMENT") == 0) &&
+		(strncmp(te->tag, "POLICY", strlen("POLICY")) == 0))
+		return 0;
+
 	/*
 	 * If it's a publication or a table part of a publication, maybe ignore
 	 * it.
-- 
2.34.1

Reply via email to