5555 *
5656 *
5757 * IDENTIFICATION
58- * $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.61 2007/09 /2404:12:01 alvherre Exp $
58+ * $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.62 2007/10 /2419:08:25 alvherre Exp $
5959 *
6060 *-------------------------------------------------------------------------
6161 */
@@ -182,7 +182,7 @@ typedef struct autovac_table
182182 * wi_linksentry into free list or running list
183183 * wi_dboidOID of the database this worker is supposed to work on
184184 * wi_tableoidOID of the table currently being vacuumed
185- *wi_workerpidPID of the running worker,0 if not yet started
185+ *wi_procpointer to PGPROC of the running worker,NULL if not started
186186 * wi_launchtime Time at which this worker was launched
187187 * wi_cost_*Vacuum cost-based delay parameters current in this worker
188188 *
@@ -196,7 +196,7 @@ typedef struct WorkerInfoData
196196SHM_QUEUE wi_links ;
197197Oid wi_dboid ;
198198Oid wi_tableoid ;
199- int wi_workerpid ;
199+ PGPROC * wi_proc ;
200200TimestampTz wi_launchtime ;
201201int wi_cost_delay ;
202202int wi_cost_limit ;
@@ -395,6 +395,9 @@ AutoVacLauncherMain(int argc, char *argv[])
395395/* Identify myself via ps */
396396init_ps_display ("autovacuum launcher process" ,"" ,"" ,"" );
397397
398+ if (PostAuthDelay )
399+ pg_usleep (PostAuthDelay * 1000000L );
400+
398401SetProcessingMode (InitProcessing );
399402
400403/*
@@ -694,7 +697,7 @@ AutoVacLauncherMain(int argc, char *argv[])
694697worker = (WorkerInfo )MAKE_PTR (AutoVacuumShmem -> av_startingWorker );
695698worker -> wi_dboid = InvalidOid ;
696699worker -> wi_tableoid = InvalidOid ;
697- worker -> wi_workerpid = 0 ;
700+ worker -> wi_proc = NULL ;
698701worker -> wi_launchtime = 0 ;
699702worker -> wi_links .next = AutoVacuumShmem -> av_freeWorkers ;
700703AutoVacuumShmem -> av_freeWorkers = MAKE_OFFSET (worker );
@@ -1198,7 +1201,7 @@ do_start_worker(void)
11981201AutoVacuumShmem -> av_freeWorkers = worker -> wi_links .next ;
11991202
12001203worker -> wi_dboid = avdb -> adw_datid ;
1201- worker -> wi_workerpid = 0 ;
1204+ worker -> wi_proc = NULL ;
12021205worker -> wi_launchtime = GetCurrentTimestamp ();
12031206
12041207AutoVacuumShmem -> av_startingWorker = sworker ;
@@ -1437,6 +1440,9 @@ AutoVacWorkerMain(int argc, char *argv[])
14371440/* Identify myself via ps */
14381441init_ps_display ("autovacuum worker process" ,"" ,"" ,"" );
14391442
1443+ if (PostAuthDelay )
1444+ pg_usleep (PostAuthDelay * 1000000L );
1445+
14401446SetProcessingMode (InitProcessing );
14411447
14421448/*
@@ -1542,7 +1548,7 @@ AutoVacWorkerMain(int argc, char *argv[])
15421548{
15431549MyWorkerInfo = (WorkerInfo )MAKE_PTR (AutoVacuumShmem -> av_startingWorker );
15441550dbid = MyWorkerInfo -> wi_dboid ;
1545- MyWorkerInfo -> wi_workerpid = MyProcPid ;
1551+ MyWorkerInfo -> wi_proc = MyProc ;
15461552
15471553/* insert into the running list */
15481554SHMQueueInsertBefore (& AutoVacuumShmem -> av_runningWorkers ,
@@ -1637,7 +1643,7 @@ FreeWorkerInfo(int code, Datum arg)
16371643MyWorkerInfo -> wi_links .next = AutoVacuumShmem -> av_freeWorkers ;
16381644MyWorkerInfo -> wi_dboid = InvalidOid ;
16391645MyWorkerInfo -> wi_tableoid = InvalidOid ;
1640- MyWorkerInfo -> wi_workerpid = 0 ;
1646+ MyWorkerInfo -> wi_proc = NULL ;
16411647MyWorkerInfo -> wi_launchtime = 0 ;
16421648MyWorkerInfo -> wi_cost_delay = 0 ;
16431649MyWorkerInfo -> wi_cost_limit = 0 ;
@@ -1701,7 +1707,7 @@ autovac_balance_cost(void)
17011707 offsetof(WorkerInfoData ,wi_links ));
17021708while (worker )
17031709{
1704- if (worker -> wi_workerpid != 0 &&
1710+ if (worker -> wi_proc != NULL &&
17051711worker -> wi_cost_limit_base > 0 && worker -> wi_cost_delay > 0 )
17061712cost_total +=
17071713(double )worker -> wi_cost_limit_base /worker -> wi_cost_delay ;
@@ -1724,7 +1730,7 @@ autovac_balance_cost(void)
17241730 offsetof(WorkerInfoData ,wi_links ));
17251731while (worker )
17261732{
1727- if (worker -> wi_workerpid != 0 &&
1733+ if (worker -> wi_proc != NULL &&
17281734worker -> wi_cost_limit_base > 0 && worker -> wi_cost_delay > 0 )
17291735{
17301736int limit = (int )
@@ -1737,7 +1743,7 @@ autovac_balance_cost(void)
17371743worker -> wi_cost_limit = Max (Min (limit ,worker -> wi_cost_limit_base ),1 );
17381744
17391745elog (DEBUG2 ,"autovac_balance_cost(pid=%u db=%u, rel=%u, cost_limit=%d, cost_delay=%d)" ,
1740- worker -> wi_workerpid ,worker -> wi_dboid ,
1746+ worker -> wi_proc -> pid ,worker -> wi_dboid ,
17411747worker -> wi_tableoid ,worker -> wi_cost_limit ,worker -> wi_cost_delay );
17421748}
17431749
@@ -2062,25 +2068,27 @@ do_autovacuum(void)
20622068VacuumCostDelay = tab -> at_vacuum_cost_delay ;
20632069VacuumCostLimit = tab -> at_vacuum_cost_limit ;
20642070
2065- /*
2066- * Advertise my cost delay parameters for the balancing algorithm, and
2067- * do a balance
2068- */
2071+ /* Last fixups before actually starting to work */
20692072LWLockAcquire (AutovacuumLock ,LW_EXCLUSIVE );
2073+
2074+ /* advertise my cost delay parameters for the balancing algorithm */
20702075MyWorkerInfo -> wi_cost_delay = tab -> at_vacuum_cost_delay ;
20712076MyWorkerInfo -> wi_cost_limit = tab -> at_vacuum_cost_limit ;
20722077MyWorkerInfo -> wi_cost_limit_base = tab -> at_vacuum_cost_limit ;
2078+
2079+ /* do a balance */
20732080autovac_balance_cost ();
2081+
2082+ /* done */
20742083LWLockRelease (AutovacuumLock );
20752084
20762085/* clean up memory before each iteration */
20772086MemoryContextResetAndDeleteChildren (PortalContext );
20782087
20792088/*
2080- * We will abort vacuuming the current table if we are interrupted, and
2081- * continue with the next one in schedule; but if anything else
2082- * happens, we will do our usual error handling which is to cause the
2083- * worker process to exit.
2089+ * We will abort vacuuming the current table if something errors out,
2090+ * and continue with the next one in schedule; in particular, this
2091+ * happens if we are interrupted with SIGINT.
20842092 */
20852093PG_TRY ();
20862094{
@@ -2094,39 +2102,40 @@ do_autovacuum(void)
20942102}
20952103PG_CATCH ();
20962104{
2097- ErrorData * errdata ;
2098-
2099- MemoryContextSwitchTo (TopTransactionContext );
2100- errdata = CopyErrorData ();
2101-
21022105/*
2103- * If we errored out due to a cancel request, abort and restart the
2104- * transaction and go to the next table. Otherwise rethrow the
2105- * error so that the outermost handler deals with it.
2106+ * Abort the transaction, start a new one, and proceed with the
2107+ * next table in our list.
21062108 */
2107- if (errdata -> sqlerrcode == ERRCODE_QUERY_CANCELED )
2108- {
2109- HOLD_INTERRUPTS ();
2110- elog (LOG ,"cancelling autovacuum of table \"%s.%s.%s\"" ,
2111- get_database_name (MyDatabaseId ),
2112- get_namespace_name (get_rel_namespace (tab -> at_relid )),
2113- get_rel_name (tab -> at_relid ));
2114-
2115- AbortOutOfAnyTransaction ();
2116- FlushErrorState ();
2117- MemoryContextResetAndDeleteChildren (PortalContext );
2118-
2119- /* restart our transaction for the following operations */
2120- StartTransactionCommand ();
2121- RESUME_INTERRUPTS ();
2122- }
2109+ HOLD_INTERRUPTS ();
2110+ if (tab -> at_dovacuum )
2111+ errcontext ("automatic vacuum of table \"%s.%s.%s\"" ,
2112+ get_database_name (MyDatabaseId ),
2113+ get_namespace_name (get_rel_namespace (tab -> at_relid )),
2114+ get_rel_name (tab -> at_relid ));
21232115else
2124- PG_RE_THROW ();
2116+ errcontext ("automatic analyze of table \"%s.%s.%s\"" ,
2117+ get_database_name (MyDatabaseId ),
2118+ get_namespace_name (get_rel_namespace (tab -> at_relid )),
2119+ get_rel_name (tab -> at_relid ));
2120+ EmitErrorReport ();
2121+
2122+ AbortOutOfAnyTransaction ();
2123+ FlushErrorState ();
2124+ MemoryContextResetAndDeleteChildren (PortalContext );
2125+
2126+ /* restart our transaction for the following operations */
2127+ StartTransactionCommand ();
2128+ RESUME_INTERRUPTS ();
21252129}
21262130PG_END_TRY ();
21272131
21282132/* be tidy */
21292133pfree (tab );
2134+
2135+ /* remove my info from shared memory */
2136+ LWLockAcquire (AutovacuumLock ,LW_EXCLUSIVE );
2137+ MyWorkerInfo -> wi_tableoid = InvalidOid ;
2138+ LWLockRelease (AutovacuumLock );
21302139}
21312140
21322141/*