From eb63cc3da82cec03c54a9534a6c91d287078abd1 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Tue, 16 Jan 2007 13:28:57 +0000 Subject: [PATCH] Arrange for autovacuum to be killed when another operation wants to be alone accessing it, like DROP DATABASE. This allows the regression tests to pass with autovacuum enabled, which open the gates for finally enabling autovacuum by default. --- src/backend/access/transam/twophase.c | 3 ++- src/backend/commands/dbcommands.c | 12 +++++------ src/backend/postmaster/autovacuum.c | 15 ++++++++++--- src/backend/postmaster/postmaster.c | 6 +++++- src/backend/storage/ipc/procarray.c | 31 +++++++++++++++++++++------ src/backend/storage/lmgr/proc.c | 5 ++++- src/include/postmaster/autovacuum.h | 3 ++- src/include/storage/proc.h | 3 ++- src/include/storage/procarray.h | 4 ++-- 9 files changed, 59 insertions(+), 23 deletions(-) diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c index 4026daa54a..45220d1196 100644 --- a/src/backend/access/transam/twophase.c +++ b/src/backend/access/transam/twophase.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.26 2007/01/05 22:19:23 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.27 2007/01/16 13:28:56 alvherre Exp $ * * NOTES * Each global transaction is associated with a global transaction @@ -280,6 +280,7 @@ MarkAsPreparing(TransactionId xid, const char *gid, gxact->proc.databaseId = databaseid; gxact->proc.roleId = owner; gxact->proc.inVacuum = false; + gxact->proc.isAutovacuum = false; gxact->proc.lwWaiting = false; gxact->proc.lwExclusive = false; gxact->proc.lwWaitLink = NULL; diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index ef0aff99ac..18340481d2 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -13,7 +13,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.188 2007/01/05 22:19:25 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.189 2007/01/16 13:28:56 alvherre Exp $ * *------------------------------------------------------------------------- */ @@ -250,11 +250,11 @@ createdb(const CreatedbStmt *stmt) * (exception is to allow CREATE DB while connected to template1). * Otherwise we might copy inconsistent data. */ - if (DatabaseHasActiveBackends(src_dboid, true)) + if (DatabaseCancelAutovacuumActivity(src_dboid, true)) ereport(ERROR, (errcode(ERRCODE_OBJECT_IN_USE), - errmsg("source database \"%s\" is being accessed by other users", - dbtemplate))); + errmsg("source database \"%s\" is being accessed by other users", + dbtemplate))); /* If encoding is defaulted, use source's encoding */ if (encoding < 0) @@ -602,7 +602,7 @@ dropdb(const char *dbname, bool missing_ok) * Check for active backends in the target database. (Because we hold the * database lock, no new ones can start after this.) */ - if (DatabaseHasActiveBackends(db_id, false)) + if (DatabaseCancelAutovacuumActivity(db_id, false)) ereport(ERROR, (errcode(ERRCODE_OBJECT_IN_USE), errmsg("database \"%s\" is being accessed by other users", @@ -706,7 +706,7 @@ RenameDatabase(const char *oldname, const char *newname) * Make sure the database does not have active sessions. This is the same * concern as above, but applied to other sessions. */ - if (DatabaseHasActiveBackends(db_id, false)) + if (DatabaseCancelAutovacuumActivity(db_id, false)) ereport(ERROR, (errcode(ERRCODE_OBJECT_IN_USE), errmsg("database \"%s\" is being accessed by other users", diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index 8caf7ce919..130ad45fd8 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.30 2007/01/05 22:19:36 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.31 2007/01/16 13:28:56 alvherre Exp $ * *------------------------------------------------------------------------- */ @@ -216,6 +216,15 @@ autovac_forkexec(void) return postmaster_forkexec(ac, av); } + +/* + * We need this set from the outside, before InitProcess is called + */ +void +AutovacuumIAm(void) +{ + am_autovacuum = true; +} #endif /* EXEC_BACKEND */ /* @@ -307,8 +316,8 @@ AutoVacMain(int argc, char *argv[]) EmitErrorReport(); /* - * We can now go away. Note that because we'll call InitProcess, a - * callback will be registered to do ProcKill, which will clean up + * We can now go away. Note that because we called InitProcess, a + * callback was registered to do ProcKill, which will clean up * necessary state. */ proc_exit(0); diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 14ccf5dc1c..f4c424c968 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -37,7 +37,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.507 2007/01/05 22:19:36 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.508 2007/01/16 13:28:56 alvherre Exp $ * * NOTES * @@ -3298,6 +3298,10 @@ SubPostmasterMain(int argc, char *argv[]) strcmp(argv[1], "--forkboot") == 0) PGSharedMemoryReAttach(); + /* autovacuum needs this set before calling InitProcess */ + if (strcmp(argv[1], "--forkautovac") == 0) + AutovacuumIAm(); + /* * Start our win32 signal implementation. This has to be done after we * read the backend variables, because we need to pick up the signal pipe diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index b31bdf9b7d..19e5efa683 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -23,12 +23,14 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.20 2007/01/05 22:19:38 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.21 2007/01/16 13:28:56 alvherre Exp $ * *------------------------------------------------------------------------- */ #include "postgres.h" +#include + #include "access/subtrans.h" #include "access/transam.h" #include "access/xact.h" @@ -678,7 +680,9 @@ GetSnapshotData(Snapshot snapshot, bool serializable) } /* - * DatabaseHasActiveBackends -- are there any backends running in the given DB + * DatabaseCancelAutovacuumActivity -- are there any backends running in the + * given DB, apart from autovacuum? If an autovacuum process is running on the + * database, kill it and restart the counting. * * If 'ignoreMyself' is TRUE, ignore this particular backend while checking * for backends in the target database. @@ -691,11 +695,16 @@ GetSnapshotData(Snapshot snapshot, bool serializable) * backend startup. */ bool -DatabaseHasActiveBackends(Oid databaseId, bool ignoreMyself) +DatabaseCancelAutovacuumActivity(Oid databaseId, bool ignoreMyself) { - bool result = false; ProcArrayStruct *arrayP = procArray; int index; + int num; + +restart: + num = 0; + + CHECK_FOR_INTERRUPTS(); LWLockAcquire(ProcArrayLock, LW_SHARED); @@ -708,14 +717,22 @@ DatabaseHasActiveBackends(Oid databaseId, bool ignoreMyself) if (ignoreMyself && proc == MyProc) continue; - result = true; - break; + num++; + + if (proc->isAutovacuum) + { + /* an autovacuum -- kill it and restart */ + LWLockRelease(ProcArrayLock); + kill(proc->pid, SIGINT); + pg_usleep(100 * 1000); /* 100ms */ + goto restart; + } } } LWLockRelease(ProcArrayLock); - return result; + return (num != 0); } /* diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index d4500b5c35..6e2a0ce81c 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.182 2007/01/05 22:19:38 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.183 2007/01/16 13:28:56 alvherre Exp $ * *------------------------------------------------------------------------- */ @@ -38,6 +38,7 @@ #include "access/transam.h" #include "access/xact.h" #include "miscadmin.h" +#include "postmaster/autovacuum.h" #include "storage/ipc.h" #include "storage/proc.h" #include "storage/procarray.h" @@ -258,6 +259,7 @@ InitProcess(void) MyProc->databaseId = InvalidOid; MyProc->roleId = InvalidOid; MyProc->inVacuum = false; + MyProc->isAutovacuum = IsAutoVacuumProcess(); MyProc->lwWaiting = false; MyProc->lwExclusive = false; MyProc->lwWaitLink = NULL; @@ -390,6 +392,7 @@ InitDummyProcess(void) MyProc->databaseId = InvalidOid; MyProc->roleId = InvalidOid; MyProc->inVacuum = false; + MyProc->isAutovacuum = false; MyProc->lwWaiting = false; MyProc->lwExclusive = false; MyProc->lwWaitLink = NULL; diff --git a/src/include/postmaster/autovacuum.h b/src/include/postmaster/autovacuum.h index c232ae3b48..7b3ad7e550 100644 --- a/src/include/postmaster/autovacuum.h +++ b/src/include/postmaster/autovacuum.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/postmaster/autovacuum.h,v 1.6 2007/01/05 22:19:57 momjian Exp $ + * $PostgreSQL: pgsql/src/include/postmaster/autovacuum.h,v 1.7 2007/01/16 13:28:57 alvherre Exp $ * *------------------------------------------------------------------------- */ @@ -36,6 +36,7 @@ extern void autovac_stopped(void); #ifdef EXEC_BACKEND extern void AutoVacMain(int argc, char *argv[]); +extern void AutovacuumIAm(void); #endif #endif /* AUTOVACUUM_H */ diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index a52850fb21..b86f210fbe 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/storage/proc.h,v 1.92 2007/01/05 22:19:58 momjian Exp $ + * $PostgreSQL: pgsql/src/include/storage/proc.h,v 1.93 2007/01/16 13:28:57 alvherre Exp $ * *------------------------------------------------------------------------- */ @@ -75,6 +75,7 @@ struct PGPROC Oid roleId; /* OID of role using this backend */ bool inVacuum; /* true if current xact is a LAZY VACUUM */ + bool isAutovacuum; /* true if it's autovacuum */ /* Info about LWLock the process is currently waiting for, if any. */ bool lwWaiting; /* true if waiting for an LW lock */ diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h index 265f4eb0b7..93e82e4368 100644 --- a/src/include/storage/procarray.h +++ b/src/include/storage/procarray.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.11 2007/01/05 22:19:58 momjian Exp $ + * $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.12 2007/01/16 13:28:57 alvherre Exp $ * *------------------------------------------------------------------------- */ @@ -29,7 +29,7 @@ extern TransactionId GetOldestXmin(bool allDbs, bool ignoreVacuum); extern PGPROC *BackendPidGetProc(int pid); extern int BackendXidGetPid(TransactionId xid); extern bool IsBackendPid(int pid); -extern bool DatabaseHasActiveBackends(Oid databaseId, bool ignoreMyself); +extern bool DatabaseCancelAutovacuumActivity(Oid databaseId, bool ignoreMyself); extern int CountActiveBackends(void); extern int CountDBBackends(Oid databaseid); -- 2.40.0