Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2910cce

Browse files
committed
Avoid crash in interrupted autovacuum worker, caused by leaving the current
memory context pointing at a context not long lived enough.Also, create a fake PortalContext where to store the vac_context, if onlyto avoid having it be a top-level memory context.
1 parent10af02b commit2910cce

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

‎src/backend/postmaster/autovacuum.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
*
5656
*
5757
* IDENTIFICATION
58-
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.52 2007/06/29 17:07:39 alvherre Exp $
58+
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.53 2007/06/30 04:08:05 alvherre Exp $
5959
*
6060
*-------------------------------------------------------------------------
6161
*/
@@ -1427,8 +1427,8 @@ AutoVacWorkerMain(int argc, char *argv[])
14271427
pqsignal(SIGHUP,SIG_IGN);
14281428

14291429
/*
1430-
*Presently,SIGINTwill lead toautovacuum shutdown, because that'show
1431-
*we handle ereport(ERROR). It could be improved however.
1430+
* SIGINTis used tosignal cancelling the current table'svacuum;
1431+
*SIGTERM means abort and exit cleanly, and SIGQUIT means abandon ship.
14321432
*/
14331433
pqsignal(SIGINT,StatementCancelHandler);
14341434
pqsignal(SIGTERM,die);
@@ -1513,6 +1513,7 @@ AutoVacWorkerMain(int argc, char *argv[])
15131513
/* insert into the running list */
15141514
SHMQueueInsertBefore(&AutoVacuumShmem->av_runningWorkers,
15151515
&MyWorkerInfo->wi_links);
1516+
15161517
/*
15171518
* remove from the "starting" pointer, so that the launcher can start
15181519
* a new worker if required
@@ -1560,13 +1561,6 @@ AutoVacWorkerMain(int argc, char *argv[])
15601561
ereport(DEBUG1,
15611562
(errmsg("autovacuum: processing database \"%s\"",dbname)));
15621563

1563-
/* Create the memory context where cross-transaction state is stored */
1564-
AutovacMemCxt=AllocSetContextCreate(TopMemoryContext,
1565-
"AV worker",
1566-
ALLOCSET_DEFAULT_MINSIZE,
1567-
ALLOCSET_DEFAULT_INITSIZE,
1568-
ALLOCSET_DEFAULT_MAXSIZE);
1569-
15701564
/* And do an appropriate amount of work */
15711565
recentXid=ReadNewTransactionId();
15721566
do_autovacuum();
@@ -1787,6 +1781,18 @@ do_autovacuum(void)
17871781
PgStat_StatDBEntry*dbentry;
17881782
BufferAccessStrategybstrategy;
17891783

1784+
/*
1785+
* StartTransactionCommand and CommitTransactionCommand will automatically
1786+
* switch to other contexts. We need this one to keep the list of
1787+
* relations to vacuum/analyze across transactions.
1788+
*/
1789+
AutovacMemCxt=AllocSetContextCreate(TopMemoryContext,
1790+
"AV worker",
1791+
ALLOCSET_DEFAULT_MINSIZE,
1792+
ALLOCSET_DEFAULT_INITSIZE,
1793+
ALLOCSET_DEFAULT_MAXSIZE);
1794+
MemoryContextSwitchTo(AutovacMemCxt);
1795+
17901796
/*
17911797
* may be NULL if we couldn't find an entry (only happens if we
17921798
* are forcing a vacuum for anti-wrap purposes).
@@ -1825,11 +1831,7 @@ do_autovacuum(void)
18251831

18261832
ReleaseSysCache(tuple);
18271833

1828-
/*
1829-
* StartTransactionCommand and CommitTransactionCommand will automatically
1830-
* switch to other contexts. We need this one to keep the list of
1831-
* relations to vacuum/analyze across transactions.
1832-
*/
1834+
/* StartTransactionCommand changed elsewhere */
18331835
MemoryContextSwitchTo(AutovacMemCxt);
18341836

18351837
/* The database hash where pgstat keeps shared relations */
@@ -1932,6 +1934,16 @@ do_autovacuum(void)
19321934
*/
19331935
bstrategy=GetAccessStrategy(BAS_VACUUM);
19341936

1937+
/*
1938+
* create a memory context to act as fake PortalContext, so that the
1939+
* contexts created in the vacuum code are cleaned up for each table.
1940+
*/
1941+
PortalContext=AllocSetContextCreate(AutovacMemCxt,
1942+
"Autovacuum Portal",
1943+
ALLOCSET_DEFAULT_INITSIZE,
1944+
ALLOCSET_DEFAULT_MINSIZE,
1945+
ALLOCSET_DEFAULT_MAXSIZE);
1946+
19351947
/*
19361948
* Perform operations on collected tables.
19371949
*/
@@ -1996,6 +2008,7 @@ do_autovacuum(void)
19962008
* FIXME we ignore the possibility that the table was finished being
19972009
* vacuumed in the last 500ms (PGSTAT_STAT_INTERVAL). This is a bug.
19982010
*/
2011+
MemoryContextSwitchTo(AutovacMemCxt);
19992012
tab=table_recheck_autovac(relid);
20002013
if (tab==NULL)
20012014
{
@@ -2026,6 +2039,9 @@ do_autovacuum(void)
20262039
autovac_balance_cost();
20272040
LWLockRelease(AutovacuumLock);
20282041

2042+
/* clean up memory before each iteration */
2043+
MemoryContextResetAndDeleteChildren(PortalContext);
2044+
20292045
/*
20302046
* We will abort vacuuming the current table if we are interrupted, and
20312047
* continue with the next one in schedule; but if anything else
@@ -2035,6 +2051,7 @@ do_autovacuum(void)
20352051
PG_TRY();
20362052
{
20372053
/* have at it */
2054+
MemoryContextSwitchTo(TopTransactionContext);
20382055
autovacuum_do_vac_analyze(tab->at_relid,
20392056
tab->at_dovacuum,
20402057
tab->at_doanalyze,
@@ -2063,6 +2080,7 @@ do_autovacuum(void)
20632080

20642081
AbortOutOfAnyTransaction();
20652082
FlushErrorState();
2083+
MemoryContextResetAndDeleteChildren(PortalContext);
20662084

20672085
/* restart our transaction for the following operations */
20682086
StartTransactionCommand();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp