Skip to content
Snippets Groups Projects
Commit 5199abac authored by Alvaro Herrera's avatar Alvaro Herrera
Browse files

Make autovacuum more selective about temp tables to keep

When temp tables are in danger of XID wraparound, autovacuum drops them;
however, it preserves those that are owned by a working session.  This
is desirable, except when the session is connected to a different
database (because the temp tables cannot be from that session), so make
it only keep the temp tables only if the backend is in the same database
as the temp tables.

This is not bulletproof: it fails to detect temp tables left by a
session whose backend ID is reused in the same database but the new
session does not use temp tables.  Commit 943576bd fixes that case
too, for branches 11 and up (which is why we don't apply this fix to
those branches), but back-patching that one is not universally agreed
on.

Discussion: https://postgr.es/m/20181214162843.37g6h3txto43akrb@alvherre.pgsql
Reviewed-by: Takayuki Tsunakawa, Michaël Paquier
parent 1d700767
Branches
Tags
No related merge requests found
...@@ -2108,11 +2108,18 @@ do_autovacuum(void) ...@@ -2108,11 +2108,18 @@ do_autovacuum(void)
if (classForm->relpersistence == RELPERSISTENCE_TEMP) if (classForm->relpersistence == RELPERSISTENCE_TEMP)
{ {
int backendID; int backendID;
PGPROC *proc;
backendID = GetTempNamespaceBackendId(classForm->relnamespace); backendID = GetTempNamespaceBackendId(classForm->relnamespace);
/* We just ignore it if the owning backend is still active */ /*
if (backendID == MyBackendId || BackendIdGetProc(backendID) == NULL) * We just ignore it if the owning backend is still active in the
* same database.
*/
if (backendID != InvalidBackendId &&
(backendID == MyBackendId ||
(proc = BackendIdGetProc(backendID)) == NULL ||
proc->databaseId != MyDatabaseId))
{ {
/* /*
* We found an orphan temp table (which was probably left * We found an orphan temp table (which was probably left
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment