mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	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).
This commit is contained in:
		@@ -104,6 +104,7 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
 | 
				
			|||||||
	volatile bool in_outer_xact,
 | 
						volatile bool in_outer_xact,
 | 
				
			||||||
				use_own_xacts;
 | 
									use_own_xacts;
 | 
				
			||||||
	List	   *relations;
 | 
						List	   *relations;
 | 
				
			||||||
 | 
						static bool in_vacuum = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* sanity checks on options */
 | 
						/* sanity checks on options */
 | 
				
			||||||
	Assert(vacstmt->options & (VACOPT_VACUUM | VACOPT_ANALYZE));
 | 
						Assert(vacstmt->options & (VACOPT_VACUUM | VACOPT_ANALYZE));
 | 
				
			||||||
@@ -129,6 +130,14 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
 | 
				
			|||||||
	else
 | 
						else
 | 
				
			||||||
		in_outer_xact = IsInTransactionChain(isTopLevel);
 | 
							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
 | 
						 * Send info about dead objects to the statistics collector, unless we are
 | 
				
			||||||
	 * in autovacuum --- autovacuum.c does this for itself.
 | 
						 * in autovacuum --- autovacuum.c does this for itself.
 | 
				
			||||||
@@ -221,6 +230,7 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
 | 
				
			|||||||
	{
 | 
						{
 | 
				
			||||||
		ListCell   *cur;
 | 
							ListCell   *cur;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							in_vacuum = true;
 | 
				
			||||||
		VacuumCostActive = (VacuumCostDelay > 0);
 | 
							VacuumCostActive = (VacuumCostDelay > 0);
 | 
				
			||||||
		VacuumCostBalance = 0;
 | 
							VacuumCostBalance = 0;
 | 
				
			||||||
		VacuumPageHit = 0;
 | 
							VacuumPageHit = 0;
 | 
				
			||||||
@@ -265,13 +275,13 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	PG_CATCH();
 | 
						PG_CATCH();
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		/* Make sure cost accounting is turned off after error */
 | 
							in_vacuum = false;
 | 
				
			||||||
		VacuumCostActive = false;
 | 
							VacuumCostActive = false;
 | 
				
			||||||
		PG_RE_THROW();
 | 
							PG_RE_THROW();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	PG_END_TRY();
 | 
						PG_END_TRY();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Turn off vacuum cost accounting */
 | 
						in_vacuum = false;
 | 
				
			||||||
	VacuumCostActive = false;
 | 
						VacuumCostActive = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/*
 | 
						/*
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -60,12 +60,24 @@ VACUUM (FULL, FREEZE) vactst;
 | 
				
			|||||||
VACUUM (ANALYZE, FULL) vactst;
 | 
					VACUUM (ANALYZE, FULL) vactst;
 | 
				
			||||||
CREATE TABLE vaccluster (i INT PRIMARY KEY);
 | 
					CREATE TABLE vaccluster (i INT PRIMARY KEY);
 | 
				
			||||||
ALTER TABLE vaccluster CLUSTER ON vaccluster_pkey;
 | 
					ALTER TABLE vaccluster CLUSTER ON vaccluster_pkey;
 | 
				
			||||||
INSERT INTO vaccluster SELECT * FROM vactst;
 | 
					 | 
				
			||||||
CLUSTER vaccluster;
 | 
					CLUSTER vaccluster;
 | 
				
			||||||
 | 
					CREATE FUNCTION do_analyze() RETURNS VOID VOLATILE LANGUAGE SQL
 | 
				
			||||||
 | 
						AS 'ANALYZE pg_am';
 | 
				
			||||||
 | 
					CREATE FUNCTION wrap_do_analyze(c INT) RETURNS INT IMMUTABLE LANGUAGE SQL
 | 
				
			||||||
 | 
						AS 'SELECT $1 FROM do_analyze()';
 | 
				
			||||||
 | 
					CREATE INDEX ON vactst(wrap_do_analyze(i));
 | 
				
			||||||
 | 
					INSERT INTO vactst VALUES (1), (2);
 | 
				
			||||||
 | 
					ANALYZE vactst;
 | 
				
			||||||
 | 
					ERROR:  ANALYZE cannot be executed from VACUUM or ANALYZE
 | 
				
			||||||
 | 
					CONTEXT:  SQL function "do_analyze" statement 1
 | 
				
			||||||
 | 
					SQL function "wrap_do_analyze" statement 1
 | 
				
			||||||
VACUUM FULL pg_am;
 | 
					VACUUM FULL pg_am;
 | 
				
			||||||
VACUUM FULL pg_class;
 | 
					VACUUM FULL pg_class;
 | 
				
			||||||
VACUUM FULL pg_database;
 | 
					VACUUM FULL pg_database;
 | 
				
			||||||
VACUUM FULL vaccluster;
 | 
					VACUUM FULL vaccluster;
 | 
				
			||||||
VACUUM FULL vactst;
 | 
					VACUUM FULL vactst;
 | 
				
			||||||
 | 
					ERROR:  ANALYZE cannot be executed from VACUUM or ANALYZE
 | 
				
			||||||
 | 
					CONTEXT:  SQL function "do_analyze" statement 1
 | 
				
			||||||
 | 
					SQL function "wrap_do_analyze" statement 1
 | 
				
			||||||
DROP TABLE vaccluster;
 | 
					DROP TABLE vaccluster;
 | 
				
			||||||
DROP TABLE vactst;
 | 
					DROP TABLE vactst;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -44,9 +44,16 @@ VACUUM (ANALYZE, FULL) vactst;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
CREATE TABLE vaccluster (i INT PRIMARY KEY);
 | 
					CREATE TABLE vaccluster (i INT PRIMARY KEY);
 | 
				
			||||||
ALTER TABLE vaccluster CLUSTER ON vaccluster_pkey;
 | 
					ALTER TABLE vaccluster CLUSTER ON vaccluster_pkey;
 | 
				
			||||||
INSERT INTO vaccluster SELECT * FROM vactst;
 | 
					 | 
				
			||||||
CLUSTER vaccluster;
 | 
					CLUSTER vaccluster;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CREATE FUNCTION do_analyze() RETURNS VOID VOLATILE LANGUAGE SQL
 | 
				
			||||||
 | 
						AS 'ANALYZE pg_am';
 | 
				
			||||||
 | 
					CREATE FUNCTION wrap_do_analyze(c INT) RETURNS INT IMMUTABLE LANGUAGE SQL
 | 
				
			||||||
 | 
						AS 'SELECT $1 FROM do_analyze()';
 | 
				
			||||||
 | 
					CREATE INDEX ON vactst(wrap_do_analyze(i));
 | 
				
			||||||
 | 
					INSERT INTO vactst VALUES (1), (2);
 | 
				
			||||||
 | 
					ANALYZE vactst;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
VACUUM FULL pg_am;
 | 
					VACUUM FULL pg_am;
 | 
				
			||||||
VACUUM FULL pg_class;
 | 
					VACUUM FULL pg_class;
 | 
				
			||||||
VACUUM FULL pg_database;
 | 
					VACUUM FULL pg_database;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user