]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
pg_restore: Use dependency-based matching for STATISTICS DATA master github/master
authorMichael Paquier <michael@paquier.xyz>
Tue, 16 Jun 2026 06:58:12 +0000 (15:58 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 16 Jun 2026 06:58:12 +0000 (15:58 +0900)
The previous approach introduced by 0dd93de69e80 was weak in terms of
name matching, as an --index=foo could match with a table with the same
name but from a different schema, pulling in more data than necessary.

For example, imagine the following case:
CREATE SCHEMA s1;
CREATE SCHEMA s2;
CREATE TABLE s1.foo (id int);
INSERT INTO s1.foo SELECT generate_series(1,100);
ANALYZE s1.foo;
CREATE TABLE s2.bar (id int);
CREATE INDEX foo ON s2.bar(id);
INSERT INTO s2.bar SELECT generate_series(1,100);
ANALYZE s2.bar;

A targetted pg_restore --index=foo would grab the relation and attribute
stats of s1.foo on top of the index s2.foo, which is incorrect.  This
commit fixes this scenario by relying on a lookup of the dependencies of
a STATISTICS DATA TOC entry, checking if a TOC entry depends on an index
or another relkind before matching with the names of the objects wanted
for the restore.

Discussion: https://postgr.es/m/ajDBwpxs-otl585H@paquier.xyz
Backpatch-through: 18

src/bin/pg_dump/pg_backup_archiver.c

index 4fa6cc1c566af2b3ceadd4459ccd257f5112eecd..4ec43f29622e7cce907f6914c03985e787f35746 100644 (file)
@@ -3250,17 +3250,36 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
                                bool            dumpthis = false;
 
                                /*
                                bool            dumpthis = false;
 
                                /*
-                                * Statistics data can be assigned for tables or indexes, so
-                                * check both.
+                                * Statistics data entries can be for tables or indexes. Check
+                                * the parent dependency to determine which type this entry
+                                * belongs to, then apply the appropriate name filter.
                                 */
                                 */
-                               if (ropt->selTable &&
-                                       (ropt->tableNames.head == NULL ||
-                                        simple_string_list_member(&ropt->tableNames, te->tag)))
-                                       dumpthis = true;
-                               if (ropt->selIndex &&
-                                       (ropt->indexNames.head == NULL ||
-                                        simple_string_list_member(&ropt->indexNames, te->tag)))
-                                       dumpthis = true;
+                               for (int i = 0; i < te->nDeps; i++)
+                               {
+                                       TocEntry   *pte = getTocEntryByDumpId(AH, te->dependencies[i]);
+
+                                       if (!pte)
+                                               continue;
+
+                                       if (ropt->selTable &&
+                                               (strcmp(pte->desc, "TABLE") == 0 ||
+                                                strcmp(pte->desc, "VIEW") == 0 ||
+                                                strcmp(pte->desc, "FOREIGN TABLE") == 0 ||
+                                                strcmp(pte->desc, "MATERIALIZED VIEW") == 0))
+                                       {
+                                               if (ropt->tableNames.head == NULL ||
+                                                       simple_string_list_member(&ropt->tableNames, pte->tag))
+                                                       dumpthis = true;
+                                       }
+
+                                       if (ropt->selIndex &&
+                                               strcmp(pte->desc, "INDEX") == 0)
+                                       {
+                                               if (ropt->indexNames.head == NULL ||
+                                                       simple_string_list_member(&ropt->indexNames, pte->tag))
+                                                       dumpthis = true;
+                                       }
+                               }
                                if (!dumpthis)
                                        return 0;
                        }
                                if (!dumpthis)
                                        return 0;
                        }