Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9f829ae

Browse files
committed
pg_dump: Lock all relations, not just plain tables
Now that LOCK TABLE can take any relation type, acquire lock on allrelations that are to be dumped. This prevents schema changes ordeadlock errors that could cause a dump to fail after expending mucheffort. The server is tested to have the capability and the featuredisabled if it doesn't, so that a patched pg_dump doesn't fail whenconnecting 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 parent7ffead2 commit9f829ae

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

‎src/bin/pg_dump/pg_backup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ typedef struct Archive
199199
intminRemoteVersion;/* allowable range */
200200
intmaxRemoteVersion;
201201

202+
boolhasGenericLockTable;/* can LOCK TABLE do non-table rels */
203+
202204
intnumWorkers;/* number of parallel processes */
203205
char*sync_snapshot_id;/* sync snapshot id for parallel operation */
204206

‎src/bin/pg_dump/pg_backup_db.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,70 @@ EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
534534
}
535535
}
536536

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

‎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
externvoidEndDBCopyMode(Archive*AHX,constchar*tocEntryTag);
2222

23+
externboolIsLockTableGeneric(Archive*AHX);
24+
2325
externvoidStartTransaction(Archive*AHX);
2426
externvoidCommitTransaction(Archive*AHX);
2527

‎src/bin/pg_dump/pg_dump.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,9 @@ setup_connection(Archive *AH, const char *dumpencoding,
11681168
ExecuteSqlStatement(AH, "SET row_security = off");
11691169
}
11701170

1171+
/* Detect whether LOCK TABLE can handle non-table relations */
1172+
AH->hasGenericLockTable = IsLockTableGeneric(AH);
1173+
11711174
/*
11721175
* Start transaction-snapshot mode transaction to dump consistent data.
11731176
*/
@@ -6727,16 +6730,16 @@ getTables(Archive *fout, int *numTables)
67276730
* assume our lock on the child is enough to prevent schema
67286731
* alterations to parent tables.
67296732
*
6730-
* NOTE: it'd be kinda nice to lock other relations too, not only
6731-
* plain or partitioned tables, but the backend doesn't presently
6732-
* allow that.
6733-
*
6734-
* We only need to lock the table for certain components; see
6733+
* We only need to lock the relation for certain components; see
67356734
* pg_dump.h
6735+
*
6736+
* On server versions that support it, we lock all relations not just
6737+
* plain tables.
67366738
*/
67376739
if (tblinfo[i].dobj.dump &&
6738-
(tblinfo[i].relkind == RELKIND_RELATION ||
6739-
tblinfo->relkind == RELKIND_PARTITIONED_TABLE) &&
6740+
(fout->hasGenericLockTable ||
6741+
tblinfo[i].relkind == RELKIND_PARTITIONED_TABLE ||
6742+
tblinfo[i].relkind == RELKIND_RELATION) &&
67406743
(tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK))
67416744
{
67426745
resetPQExpBuffer(query);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp