Skip to content

Commit 573e446

Browse files
committed
For pg_upgrade, update template0's datfrozenxid and its relfrozenxids to
match the behavior of autovacuum, which does this as the xid advances even if autovacuum is turned off.
1 parent 3ef9574 commit 573e446

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

contrib/pg_upgrade/pg_upgrade.c

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ prepare_new_cluster(migratorContext *ctx)
164164
check_ok(ctx);
165165

166166
/*
167-
* We do freeze after analyze so pg_statistic is also frozen
167+
* We do freeze after analyze so pg_statistic is also frozen.
168+
* template0 is not frozen here, but data rows were frozen by initdb,
169+
* and we set its datfrozenxid and relfrozenxids later to match the
170+
* new xid counter later.
168171
*/
169172
prep_status(ctx, "Freezing all rows on the new cluster");
170173
exec_prog(ctx, true,
@@ -292,48 +295,72 @@ void
292295
set_frozenxids(migratorContext *ctx)
293296
{
294297
int dbnum;
295-
PGconn *conn;
298+
PGconn *conn, *conn_template1;
296299
PGresult *dbres;
297300
int ntups;
301+
int i_datname;
302+
int i_datallowconn;
298303

299304
prep_status(ctx, "Setting frozenxid counters in new cluster");
300305

301-
conn = connectToServer(ctx, "template1", CLUSTER_NEW);
306+
conn_template1 = connectToServer(ctx, "template1", CLUSTER_NEW);
302307

303308
/* set pg_database.datfrozenxid */
304-
PQclear(executeQueryOrDie(ctx, conn,
309+
PQclear(executeQueryOrDie(ctx, conn_template1,
305310
"UPDATE pg_catalog.pg_database "
306-
"SET datfrozenxid = '%u' "
307-
"WHERE datallowconn = true",
311+
"SET datfrozenxid = '%u'",
308312
ctx->old.controldata.chkpnt_nxtxid));
309313

310314
/* get database names */
311-
dbres = executeQueryOrDie(ctx, conn,
312-
"SELECT datname "
313-
"FROM pg_catalog.pg_database "
314-
"WHERE datallowconn = true");
315+
dbres = executeQueryOrDie(ctx, conn_template1,
316+
"SELECT datname, datallowconn "
317+
"FROM pg_catalog.pg_database");
315318

316-
/* free dbres below */
317-
PQfinish(conn);
319+
i_datname = PQfnumber(dbres, "datname");
320+
i_datallowconn = PQfnumber(dbres, "datallowconn");
318321

319322
ntups = PQntuples(dbres);
320323
for (dbnum = 0; dbnum < ntups; dbnum++)
321324
{
322-
conn = connectToServer(ctx, PQgetvalue(dbres, dbnum, 0), CLUSTER_NEW);
325+
char *datname = PQgetvalue(dbres, dbnum, i_datname);
326+
char *datallowconn= PQgetvalue(dbres, dbnum, i_datallowconn);
327+
328+
/*
329+
* We must update databases where datallowconn = false, e.g.
330+
* template0, because autovacuum increments their datfrozenxids and
331+
* relfrozenxids even if autovacuum is turned off, and even though
332+
* all the data rows are already frozen To enable this, we
333+
* temporarily change datallowconn.
334+
*/
335+
if (strcmp(datallowconn, "f") == 0)
336+
PQclear(executeQueryOrDie(ctx, conn_template1,
337+
"UPDATE pg_catalog.pg_database "
338+
"SET datallowconn = true "
339+
"WHERE datname = '%s'", datname));
340+
341+
conn = connectToServer(ctx, datname, CLUSTER_NEW);
323342

324343
/* set pg_class.relfrozenxid */
325344
PQclear(executeQueryOrDie(ctx, conn,
326345
"UPDATE pg_catalog.pg_class "
327346
"SET relfrozenxid = '%u' "
328347
/* only heap and TOAST are vacuumed */
329-
"WHERE relkind = 'r' OR "
330-
" relkind = 't'",
348+
"WHERE relkind IN ('r', 't')",
331349
ctx->old.controldata.chkpnt_nxtxid));
332350
PQfinish(conn);
351+
352+
/* Reset datallowconn flag */
353+
if (strcmp(datallowconn, "f") == 0)
354+
PQclear(executeQueryOrDie(ctx, conn_template1,
355+
"UPDATE pg_catalog.pg_database "
356+
"SET datallowconn = false "
357+
"WHERE datname = '%s'", datname));
333358
}
334359

335360
PQclear(dbres);
336361

362+
PQfinish(conn_template1);
363+
337364
check_ok(ctx);
338365
}
339366

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy