From 616830052ba3ed8abc52ba215f53d55f8413eae9 Mon Sep 17 00:00:00 2001 From: Noah Misch Date: Wed, 7 Jan 2015 22:33:58 -0500 Subject: Reject ANALYZE commands during VACUUM FULL or another ANALYZE. vacuum()'s static variable handling makes it non-reentrant; an ensuing null pointer deference crashed the backend. Back-patch to 9.0 (all supported versions). --- src/backend/commands/vacuum.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/backend/commands') diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 41ca15c6627..c5170efea50 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -97,6 +97,7 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast, volatile bool in_outer_xact, use_own_xacts; List *relations; + static bool in_vacuum = false; /* sanity checks on options */ Assert(vacstmt->options & (VACOPT_VACUUM | VACOPT_ANALYZE)); @@ -122,6 +123,14 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast, else in_outer_xact = IsInTransactionChain(isTopLevel); + /* + * Due to static variables vac_context, anl_context and vac_strategy, + * vacuum() is not reentrant. This matters when VACUUM FULL or ANALYZE + * calls a hostile index expression that itself calls ANALYZE. + */ + if (in_vacuum) + elog(ERROR, "%s cannot be executed from VACUUM or ANALYZE", stmttype); + /* * Send info about dead objects to the statistics collector, unless we are * in autovacuum --- autovacuum.c does this for itself. @@ -214,6 +223,7 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast, { ListCell *cur; + in_vacuum = true; VacuumCostActive = (VacuumCostDelay > 0); VacuumCostBalance = 0; @@ -255,13 +265,13 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast, } PG_CATCH(); { - /* Make sure cost accounting is turned off after error */ + in_vacuum = false; VacuumCostActive = false; PG_RE_THROW(); } PG_END_TRY(); - /* Turn off vacuum cost accounting */ + in_vacuum = false; VacuumCostActive = false; /* -- cgit v1.2.3