Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5f53b42

Browse files
committed
During pg_dump startup, acquire table locks in batches.
Combine multiple LOCK TABLE commands to reduce the number of round trips to the server. This is particularly helpful when dumping from a remote server, but it seems useful even without that. In particular, shortening the time from seeing a table in pg_class to acquiring lock on it reduces the window for trouble from concurrent DDL. Aleksander Alekseev, reviewed by Fabrízio de Royes Mello, Gilles Darold, and Andres Freund Discussion: https://postgr.es/m/CAJ7c6TO4z1+OBa-R+fC8FnaUgbEWJUf2Kq=nRngTW5EXtKru2g@mail.gmail.com
1 parent b23837d commit 5f53b42

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/bin/pg_dump/pg_dump.c

+31-5
Original file line numberDiff line numberDiff line change
@@ -6470,6 +6470,8 @@ getTables(Archive *fout, int *numTables)
64706470
ExecuteSqlStatement(fout, query->data);
64716471
}
64726472

6473+
resetPQExpBuffer(query);
6474+
64736475
for (i = 0; i < ntups; i++)
64746476
{
64756477
tblinfo[i].dobj.objType = DO_TABLE;
@@ -6587,14 +6589,38 @@ getTables(Archive *fout, int *numTables)
65876589
(tblinfo[i].relkind == RELKIND_RELATION ||
65886590
tblinfo[i].relkind == RELKIND_PARTITIONED_TABLE))
65896591
{
6590-
resetPQExpBuffer(query);
6591-
appendPQExpBuffer(query,
6592-
"LOCK TABLE %s IN ACCESS SHARE MODE",
6593-
fmtQualifiedDumpable(&tblinfo[i]));
6594-
ExecuteSqlStatement(fout, query->data);
6592+
/*
6593+
* Tables are locked in batches. When dumping from a remote
6594+
* server this can save a significant amount of time by reducing
6595+
* the number of round trips.
6596+
*/
6597+
if (query->len == 0)
6598+
appendPQExpBuffer(query, "LOCK TABLE %s",
6599+
fmtQualifiedDumpable(&tblinfo[i]));
6600+
else
6601+
{
6602+
appendPQExpBuffer(query, ", %s",
6603+
fmtQualifiedDumpable(&tblinfo[i]));
6604+
6605+
/* Arbitrarily end a batch when query length reaches 100K. */
6606+
if (query->len >= 100000)
6607+
{
6608+
/* Lock another batch of tables. */
6609+
appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
6610+
ExecuteSqlStatement(fout, query->data);
6611+
resetPQExpBuffer(query);
6612+
}
6613+
}
65956614
}
65966615
}
65976616

6617+
if (query->len != 0)
6618+
{
6619+
/* Lock the tables in the last batch. */
6620+
appendPQExpBufferStr(query, " IN ACCESS SHARE MODE");
6621+
ExecuteSqlStatement(fout, query->data);
6622+
}
6623+
65986624
if (dopt->lockWaitTimeout)
65996625
{
66006626
ExecuteSqlStatement(fout, "SET statement_timeout = 0");

0 commit comments

Comments
 (0)