Skip to content

Commit d9ed510

Browse files
committed
pg_dump: Lock all relations, not just plain tables
Now that LOCK TABLE can take any relation type, acquire lock on all relations that are to be dumped. This prevents schema changes or deadlock errors that could cause a dump to fail after expending much effort. The server is tested to have the capability and the feature disabled if it doesn't, so that a patched pg_dump doesn't fail when connecting to an unpatched server. Backpatch to 9.5. Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reported-by: Wells Oliver <wells.oliver@gmail.com> Discussion: https://postgr.es/m/20201021200659.GA32358@alvherre.pgsql
1 parent 61ae9d6 commit d9ed510

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

src/bin/pg_dump/pg_backup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ typedef struct Archive
187187
int minRemoteVersion; /* allowable range */
188188
int maxRemoteVersion;
189189

190+
bool hasGenericLockTable; /* can LOCK TABLE do non-table rels */
191+
190192
int numWorkers; /* number of parallel processes */
191193
char *sync_snapshot_id; /* sync snapshot id for parallel
192194
* operation */

src/bin/pg_dump/pg_backup_db.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,71 @@ EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
551551
}
552552
}
553553

554+
/*
555+
* Does LOCK TABLE work on non-table relations on this server?
556+
*
557+
* Note: assumes it is called out of any transaction
558+
*/
559+
bool
560+
IsLockTableGeneric(Archive *AHX)
561+
{
562+
ArchiveHandle *AH = (ArchiveHandle *) AHX;
563+
PGresult *res;
564+
char *sqlstate;
565+
bool retval;
566+
567+
if (AHX->remoteVersion >= 140000)
568+
return true;
569+
else if (AHX->remoteVersion < 90500)
570+
return false;
571+
572+
StartTransaction(AHX);
573+
574+
/*
575+
* Try a LOCK TABLE on a well-known non-table catalog; WRONG_OBJECT_TYPE
576+
* tells us that this server doesn't support locking non-table rels, while
577+
* LOCK_NOT_AVAILABLE and INSUFFICIENT_PRIVILEGE tell us that it does.
578+
* Report anything else as a fatal problem.
579+
*/
580+
#define ERRCODE_INSUFFICIENT_PRIVILEGE "42501"
581+
#define ERRCODE_WRONG_OBJECT_TYPE "42809"
582+
#define ERRCODE_LOCK_NOT_AVAILABLE "55P03"
583+
res = PQexec(AH->connection,
584+
"LOCK TABLE pg_catalog.pg_class_tblspc_relfilenode_index IN ACCESS SHARE MODE NOWAIT");
585+
switch (PQresultStatus(res))
586+
{
587+
case PGRES_COMMAND_OK:
588+
retval = true;
589+
break;
590+
case PGRES_FATAL_ERROR:
591+
sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
592+
if (strcmp(sqlstate, ERRCODE_WRONG_OBJECT_TYPE) == 0)
593+
{
594+
retval = false;
595+
break;
596+
}
597+
else if (strcmp(sqlstate, ERRCODE_LOCK_NOT_AVAILABLE) == 0 ||
598+
strcmp(sqlstate, ERRCODE_INSUFFICIENT_PRIVILEGE) == 0)
599+
{
600+
retval = true;
601+
break;
602+
}
603+
/* else, falls through */
604+
default:
605+
warn_or_exit_horribly(AH, modulename,
606+
"LOCK TABLE failed for \"%s\": %s",
607+
"pg_catalog.pg_class_tblspc_relfilenode_index",
608+
PQerrorMessage(AH->connection));
609+
retval = false; /* not reached */
610+
break;
611+
}
612+
PQclear(res);
613+
614+
CommitTransaction(AHX);
615+
616+
return retval;
617+
}
618+
554619
void
555620
StartTransaction(Archive *AHX)
556621
{

src/bin/pg_dump/pg_backup_db.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ extern PGresult *ExecuteSqlQueryForSingleRow(Archive *fout, char *query);
2020

2121
extern void EndDBCopyMode(Archive *AHX, const char *tocEntryTag);
2222

23+
extern bool IsLockTableGeneric(Archive *AHX);
24+
2325
extern void StartTransaction(Archive *AHX);
2426
extern void CommitTransaction(Archive *AHX);
2527

src/bin/pg_dump/pg_dump.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,9 @@ setup_connection(Archive *AH, const char *dumpencoding,
10621062
ExecuteSqlStatement(AH, "SET row_security = off");
10631063
}
10641064

1065+
/* Detect whether LOCK TABLE can handle non-table relations */
1066+
AH->hasGenericLockTable = IsLockTableGeneric(AH);
1067+
10651068
/*
10661069
* Start transaction-snapshot mode transaction to dump consistent data.
10671070
*/
@@ -5398,10 +5401,12 @@ getTables(Archive *fout, int *numTables)
53985401
* assume our lock on the child is enough to prevent schema
53995402
* alterations to parent tables.
54005403
*
5401-
* NOTE: it'd be kinda nice to lock other relations too, not only
5402-
* plain tables, but the backend doesn't presently allow that.
5404+
* On server versions that support it, we lock all relations not just
5405+
* plain tables.
54035406
*/
5404-
if (tblinfo[i].dobj.dump && tblinfo[i].relkind == RELKIND_RELATION)
5407+
if (tblinfo[i].dobj.dump &&
5408+
(fout->hasGenericLockTable ||
5409+
tblinfo[i].relkind == RELKIND_RELATION))
54055410
{
54065411
resetPQExpBuffer(query);
54075412
appendPQExpBuffer(query,

0 commit comments

Comments
 (0)