Skip to content

Commit 59b6b1f

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 2d1c7e6 commit 59b6b1f

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

src/bin/pg_dump/pg_backup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ typedef struct Archive
200200
int minRemoteVersion; /* allowable range */
201201
int maxRemoteVersion;
202202

203+
bool hasGenericLockTable; /* can LOCK TABLE do non-table rels */
204+
203205
int numWorkers; /* number of parallel processes */
204206
char *sync_snapshot_id; /* sync snapshot id for parallel operation */
205207

src/bin/pg_dump/pg_backup_db.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,71 @@ EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
537537
}
538538
}
539539

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

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, const 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: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,9 @@ setup_connection(Archive *AH, const char *dumpencoding,
11151115
ExecuteSqlStatement(AH, "SET row_security = off");
11161116
}
11171117

1118+
/* Detect whether LOCK TABLE can handle non-table relations */
1119+
AH->hasGenericLockTable = IsLockTableGeneric(AH);
1120+
11181121
/*
11191122
* Start transaction-snapshot mode transaction to dump consistent data.
11201123
*/
@@ -6662,16 +6665,16 @@ getTables(Archive *fout, int *numTables)
66626665
* assume our lock on the child is enough to prevent schema
66636666
* alterations to parent tables.
66646667
*
6665-
* NOTE: it'd be kinda nice to lock other relations too, not only
6666-
* plain or partitioned tables, but the backend doesn't presently
6667-
* allow that.
6668-
*
6669-
* We only need to lock the table for certain components; see
6668+
* We only need to lock the relation for certain components; see
66706669
* pg_dump.h
6670+
*
6671+
* On server versions that support it, we lock all relations not just
6672+
* plain tables.
66716673
*/
66726674
if (tblinfo[i].dobj.dump &&
6673-
(tblinfo[i].relkind == RELKIND_RELATION ||
6674-
tblinfo->relkind == RELKIND_PARTITIONED_TABLE) &&
6675+
(fout->hasGenericLockTable ||
6676+
tblinfo[i].relkind == RELKIND_PARTITIONED_TABLE ||
6677+
tblinfo[i].relkind == RELKIND_RELATION) &&
66756678
(tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK))
66766679
{
66776680
resetPQExpBuffer(query);

0 commit comments

Comments
 (0)