From 013e54c9df54b7a324a30beba22138a86417f34f Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 23 Oct 2017 18:55:12 +0200 Subject: [PATCH v2] Fix autovacuum work items --- src/backend/postmaster/autovacuum.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index 776b1c0a9d..dc76728d71 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -2521,9 +2521,10 @@ deleted: { AutoVacuumWorkItem *workitem = &AutoVacuumShmem->av_workItems[i]; - if (!workitem->avw_used) - continue; - if (workitem->avw_active) + /* Item empty, already being processed, not this database? skip it */ + if (!workitem->avw_used || + workitem->avw_active || + workitem->avw_database != MyDatabaseId) continue; /* claim this one, and release lock while performing it */ @@ -2531,6 +2532,7 @@ deleted: LWLockRelease(AutovacuumLock); perform_work_item(workitem); + /* we intentially do not set did_vacuum here */ /* * Check for config changes before acquiring lock for further @@ -2601,11 +2603,9 @@ perform_work_item(AutoVacuumWorkItem *workitem) /* * Save the relation name for a possible error message, to avoid a catalog * lookup in case of an error. If any of these return NULL, then the - * relation has been dropped since last we checked; skip it. Note: they - * must live in a long-lived memory context because we call vacuum and - * analyze in different transactions. + * relation has been dropped since last we checked; skip it. */ - + MemoryContextSwitchTo(AutovacMemCxt); cur_relname = get_rel_name(workitem->avw_relation); cur_nspname = get_namespace_name(get_rel_namespace(workitem->avw_relation)); cur_datname = get_database_name(MyDatabaseId); @@ -2615,6 +2615,8 @@ perform_work_item(AutoVacuumWorkItem *workitem) autovac_report_workitem(workitem, cur_nspname, cur_datname); /* + * Have at it. + * * We will abort the current work item if something errors out, and * continue with the next one; in particular, this happens if we are * interrupted with SIGINT. Note that this means that the work item list @@ -2622,9 +2624,6 @@ perform_work_item(AutoVacuumWorkItem *workitem) */ PG_TRY(); { - /* have at it */ - MemoryContextSwitchTo(TopTransactionContext); - switch (workitem->avw_type) { case AVW_BRINSummarizeRange: @@ -2668,10 +2667,8 @@ perform_work_item(AutoVacuumWorkItem *workitem) } PG_END_TRY(); - /* We intentionally do not set did_vacuum here */ - - /* be tidy */ deleted2: + /* be tidy */ if (cur_datname) pfree(cur_datname); if (cur_nspname) -- 2.11.0