v10-0003-Add-relallfrozen-to-pg_dump-statistics.patch
text/x-patch
Filename: v10-0003-Add-relallfrozen-to-pg_dump-statistics.patch
Type: text/x-patch
Part: 3
Message:
Re: Statistics Import and Export
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v10-0003
Subject: Add relallfrozen to pg_dump statistics.
| File | + | − |
|---|---|---|
| src/bin/pg_dump/pg_dump.c | 36 | 16 |
| src/bin/pg_dump/pg_dump.h | 1 | 0 |
| src/bin/pg_dump/t/002_pg_dump.pl | 2 | 1 |
From 87da7d4c517c3b2e63666892e64b9de2a8dbbe44 Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Sat, 15 Mar 2025 17:34:30 -0400
Subject: [PATCH v10 3/4] Add relallfrozen to pg_dump statistics.
The column relallfrozen was recently added to pg_class and it also
represent statistics, so we should add it to the dump/restore/upgrade
operations.
Dumps of databases prior to v18 will not attempt to restore any value to
relallfrozen, allowing pg_restore_relation_stats() to set the default it
deems appropriate.
---
src/bin/pg_dump/pg_dump.c | 52 ++++++++++++++++++++++----------
src/bin/pg_dump/pg_dump.h | 1 +
src/bin/pg_dump/t/002_pg_dump.pl | 3 +-
3 files changed, 39 insertions(+), 17 deletions(-)
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index e3f2dac33ec..6c366fd55d3 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -6897,7 +6897,8 @@ getFuncs(Archive *fout)
*/
static RelStatsInfo *
getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
- char *reltuples, int32 relallvisible, char relkind,
+ char *reltuples, int32 relallvisible,
+ int32 relallfrozen, char relkind,
char **indAttNames, int nindAttNames)
{
if (!fout->dopt->dumpStatistics)
@@ -6926,6 +6927,7 @@ getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
info->relpages = relpages;
info->reltuples = pstrdup(reltuples);
info->relallvisible = relallvisible;
+ info->relallfrozen = relallfrozen;
info->relkind = relkind;
info->indAttNames = indAttNames;
info->nindAttNames = nindAttNames;
@@ -6965,6 +6967,7 @@ getTables(Archive *fout, int *numTables)
int i_relpages;
int i_reltuples;
int i_relallvisible;
+ int i_relallfrozen;
int i_toastpages;
int i_owning_tab;
int i_owning_col;
@@ -7015,8 +7018,13 @@ getTables(Archive *fout, int *numTables)
"c.relowner, "
"c.relchecks, "
"c.relhasindex, c.relhasrules, c.relpages, "
- "c.reltuples, c.relallvisible, c.relhastriggers, "
- "c.relpersistence, "
+ "c.reltuples, c.relallvisible, ");
+
+ if (fout->remoteVersion >= 180000)
+ appendPQExpBufferStr(query, "c.relallfrozen, ");
+
+ appendPQExpBufferStr(query,
+ "c.relhastriggers, c.relpersistence, "
"c.reloftype, "
"c.relacl, "
"acldefault(CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE)
@@ -7181,6 +7189,7 @@ getTables(Archive *fout, int *numTables)
i_relpages = PQfnumber(res, "relpages");
i_reltuples = PQfnumber(res, "reltuples");
i_relallvisible = PQfnumber(res, "relallvisible");
+ i_relallfrozen = PQfnumber(res, "relallfrozen");
i_toastpages = PQfnumber(res, "toastpages");
i_owning_tab = PQfnumber(res, "owning_tab");
i_owning_col = PQfnumber(res, "owning_col");
@@ -7228,6 +7237,7 @@ getTables(Archive *fout, int *numTables)
for (i = 0; i < ntups; i++)
{
int32 relallvisible = atoi(PQgetvalue(res, i, i_relallvisible));
+ int32 relallfrozen = atoi(PQgetvalue(res, i, i_relallfrozen));
tblinfo[i].dobj.objType = DO_TABLE;
tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid));
@@ -7330,7 +7340,7 @@ getTables(Archive *fout, int *numTables)
if (tblinfo[i].interesting)
getRelationStatistics(fout, &tblinfo[i].dobj, tblinfo[i].relpages,
PQgetvalue(res, i, i_reltuples),
- relallvisible, tblinfo[i].relkind, NULL, 0);
+ relallvisible, relallfrozen, tblinfo[i].relkind, NULL, 0);
/*
* Read-lock target tables to make sure they aren't DROPPED or altered
@@ -7599,6 +7609,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
i_relpages,
i_reltuples,
i_relallvisible,
+ i_relallfrozen,
i_parentidx,
i_indexdef,
i_indnkeyatts,
@@ -7653,7 +7664,12 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
appendPQExpBufferStr(query,
"SELECT t.tableoid, t.oid, i.indrelid, "
"t.relname AS indexname, "
- "t.relpages, t.reltuples, t.relallvisible, "
+ "t.relpages, t.reltuples, t.relallvisible, ");
+
+ if (fout->remoteVersion >= 180000)
+ appendPQExpBufferStr(query, "t.relallfrozen, ");
+
+ appendPQExpBufferStr(query,
"pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
"i.indkey, i.indisclustered, "
"c.contype, c.conname, "
@@ -7769,6 +7785,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
i_relpages = PQfnumber(res, "relpages");
i_reltuples = PQfnumber(res, "reltuples");
i_relallvisible = PQfnumber(res, "relallvisible");
+ i_relallfrozen = PQfnumber(res, "relallfrozen");
i_parentidx = PQfnumber(res, "parentidx");
i_indexdef = PQfnumber(res, "indexdef");
i_indnkeyatts = PQfnumber(res, "indnkeyatts");
@@ -7840,6 +7857,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
RelStatsInfo *relstats;
int32 relpages = atoi(PQgetvalue(res, j, i_relpages));
int32 relallvisible = atoi(PQgetvalue(res, j, i_relallvisible));
+ int32 relallfrozen = atoi(PQgetvalue(res, j, i_relallfrozen));
indxinfo[j].dobj.objType = DO_INDEX;
indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
@@ -7882,7 +7900,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
relstats = getRelationStatistics(fout, &indxinfo[j].dobj, relpages,
PQgetvalue(res, j, i_reltuples),
- relallvisible, indexkind,
+ relallvisible, relallfrozen, indexkind,
indAttNames, nindAttNames);
contype = *(PQgetvalue(res, j, i_contype));
@@ -10862,19 +10880,21 @@ printRelationStats(Archive *fout, const void *userArg)
initPQExpBuffer(&out);
/* restore relation stats */
- appendPQExpBufferStr(&out, "SELECT * FROM pg_catalog.pg_restore_relation_stats(\n");
- appendPQExpBuffer(&out, "\t'version', '%u'::integer,\n",
+ appendPQExpBufferStr(&out, "SELECT * FROM pg_catalog.pg_restore_relation_stats(");
+ appendPQExpBuffer(&out, "\n\t'version', '%u'::integer",
fout->remoteVersion);
- appendPQExpBufferStr(&out, "\t'schemaname', ");
+ appendPQExpBufferStr(&out, ",\n\t'schemaname', ");
appendStringLiteralAH(&out, rsinfo->dobj.namespace->dobj.name, fout);
- appendPQExpBufferStr(&out, ",\n");
- appendPQExpBufferStr(&out, "\t'relname', ");
+ appendPQExpBufferStr(&out, ",\n\t'relname', ");
appendStringLiteralAH(&out, rsinfo->dobj.name, fout);
- appendPQExpBufferStr(&out, ",\n");
- appendPQExpBuffer(&out, "\t'relpages', '%d'::integer,\n", rsinfo->relpages);
- appendPQExpBuffer(&out, "\t'reltuples', '%s'::real,\n", rsinfo->reltuples);
- appendPQExpBuffer(&out, "\t'relallvisible', '%d'::integer\n);\n",
- rsinfo->relallvisible);
+ appendPQExpBuffer(&out, ",\n\t'relpages', '%d'::integer", rsinfo->relpages);
+ appendPQExpBuffer(&out, ",\n\t'reltuples', '%s'::real", rsinfo->reltuples);
+ appendPQExpBuffer(&out, ",\n\t'relallvisible', '%d'::integer", rsinfo->relallvisible);
+
+ if (fout->remoteVersion >= 180000)
+ appendPQExpBuffer(&out, ",\n\t'relallfrozen', '%d'::integer", rsinfo->relallfrozen);
+
+ appendPQExpBufferStr(&out, "\n);\n");
AH->txnCount++;
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index bbdb30b5f54..82f1eb3c4b7 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -441,6 +441,7 @@ typedef struct _relStatsInfo
int32 relpages;
char *reltuples;
int32 relallvisible;
+ int32 relallfrozen;
char relkind; /* 'r', 'm', 'i', etc */
/*
diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index 51ebf8ad13c..576326daec7 100644
--- a/src/bin/pg_dump/t/002_pg_dump.pl
+++ b/src/bin/pg_dump/t/002_pg_dump.pl
@@ -4771,7 +4771,8 @@ my %tests = (
'relname',\s'dup_test_post_data_ix',\s+
'relpages',\s'\d+'::integer,\s+
'reltuples',\s'\d+'::real,\s+
- 'relallvisible',\s'\d+'::integer\s+
+ 'relallvisible',\s'\d+'::integer,\s+
+ 'relallfrozen',\s'\d+'::integer\s+
\);\s+
\QSELECT * FROM pg_catalog.pg_restore_attribute_stats(\E\s+
'version',\s'\d+'::integer,\s+
--
2.49.0