55
55
*
56
56
*
57
57
* IDENTIFICATION
58
- * $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.80 2008/07/01 02:09:34 tgl Exp $
58
+ * $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.81 2008/07/17 21: 02:31 alvherre Exp $
59
59
*
60
60
*-------------------------------------------------------------------------
61
61
*/
@@ -176,6 +176,9 @@ typedef struct autovac_table
176
176
int at_vacuum_cost_delay ;
177
177
int at_vacuum_cost_limit ;
178
178
bool at_wraparound ;
179
+ char * at_relname ;
180
+ char * at_nspname ;
181
+ char * at_datname ;
179
182
} autovac_table ;
180
183
181
184
/*-------------
@@ -282,15 +285,13 @@ static void relation_needs_vacanalyze(Oid relid, Form_pg_autovacuum avForm,
282
285
PgStat_StatTabEntry * tabentry , bool * dovacuum ,
283
286
bool * doanalyze , bool * wraparound );
284
287
285
- static void autovacuum_do_vac_analyze (Oid relid , bool dovacuum ,
286
- bool doanalyze , int freeze_min_age ,
287
- bool for_wraparound ,
288
+ static void autovacuum_do_vac_analyze (autovac_table * tab ,
288
289
BufferAccessStrategy bstrategy );
289
290
static HeapTuple get_pg_autovacuum_tuple_relid (Relation avRel , Oid relid );
290
291
static PgStat_StatTabEntry * get_pgstat_tabentry_relid (Oid relid , bool isshared ,
291
292
PgStat_StatDBEntry * shared ,
292
293
PgStat_StatDBEntry * dbentry );
293
- static void autovac_report_activity (VacuumStmt * vacstmt , Oid relid );
294
+ static void autovac_report_activity (autovac_table * tab );
294
295
static void avl_sighup_handler (SIGNAL_ARGS );
295
296
static void avl_sigusr1_handler (SIGNAL_ARGS );
296
297
static void avl_sigterm_handler (SIGNAL_ARGS );
@@ -2061,9 +2062,6 @@ do_autovacuum(void)
2061
2062
autovac_table * tab ;
2062
2063
WorkerInfo worker ;
2063
2064
bool skipit ;
2064
- char * datname ,
2065
- * nspname ,
2066
- * relname ;
2067
2065
2068
2066
CHECK_FOR_INTERRUPTS ();
2069
2067
@@ -2158,13 +2156,17 @@ do_autovacuum(void)
2158
2156
2159
2157
/*
2160
2158
* Save the relation name for a possible error message, to avoid a
2161
- * catalog lookup in case of an error. Note: they must live in a
2162
- * long-lived memory context because we call vacuum and analyze in
2163
- * different transactions.
2159
+ * catalog lookup in case of an error. If any of these return NULL,
2160
+ * then the relation has been dropped since last we checked; skip it.
2161
+ * Note: they must live in a long-lived memory context because we call
2162
+ * vacuum and analyze in different transactions.
2164
2163
*/
2165
- datname = get_database_name (MyDatabaseId );
2166
- nspname = get_namespace_name (get_rel_namespace (tab -> at_relid ));
2167
- relname = get_rel_name (tab -> at_relid );
2164
+
2165
+ tab -> at_relname = get_rel_name (tab -> at_relid );
2166
+ tab -> at_nspname = get_namespace_name (get_rel_namespace (tab -> at_relid ));
2167
+ tab -> at_datname = get_database_name (MyDatabaseId );
2168
+ if (!tab -> at_relname || !tab -> at_nspname || !tab -> at_datname )
2169
+ goto deleted ;
2168
2170
2169
2171
/*
2170
2172
* We will abort vacuuming the current table if something errors out,
@@ -2175,12 +2177,7 @@ do_autovacuum(void)
2175
2177
{
2176
2178
/* have at it */
2177
2179
MemoryContextSwitchTo (TopTransactionContext );
2178
- autovacuum_do_vac_analyze (tab -> at_relid ,
2179
- tab -> at_dovacuum ,
2180
- tab -> at_doanalyze ,
2181
- tab -> at_freeze_min_age ,
2182
- tab -> at_wraparound ,
2183
- bstrategy );
2180
+ autovacuum_do_vac_analyze (tab , bstrategy );
2184
2181
2185
2182
/*
2186
2183
* Clear a possible query-cancel signal, to avoid a late reaction
@@ -2199,10 +2196,10 @@ do_autovacuum(void)
2199
2196
HOLD_INTERRUPTS ();
2200
2197
if (tab -> at_dovacuum )
2201
2198
errcontext ("automatic vacuum of table \"%s.%s.%s\"" ,
2202
- datname , nspname , relname );
2199
+ tab -> at_datname , tab -> at_nspname , tab -> at_relname );
2203
2200
else
2204
2201
errcontext ("automatic analyze of table \"%s.%s.%s\"" ,
2205
- datname , nspname , relname );
2202
+ tab -> at_datname , tab -> at_nspname , tab -> at_relname );
2206
2203
EmitErrorReport ();
2207
2204
2208
2205
/* this resets the PGPROC flags too */
@@ -2219,10 +2216,14 @@ do_autovacuum(void)
2219
2216
/* the PGPROC flags are reset at the next end of transaction */
2220
2217
2221
2218
/* be tidy */
2219
+ deleted :
2220
+ if (tab -> at_datname != NULL )
2221
+ pfree (tab -> at_datname );
2222
+ if (tab -> at_nspname != NULL )
2223
+ pfree (tab -> at_nspname );
2224
+ if (tab -> at_relname != NULL )
2225
+ pfree (tab -> at_relname );
2222
2226
pfree (tab );
2223
- pfree (datname );
2224
- pfree (nspname );
2225
- pfree (relname );
2226
2227
2227
2228
/* remove my info from shared memory */
2228
2229
LWLockAcquire (AutovacuumLock , LW_EXCLUSIVE );
@@ -2299,6 +2300,8 @@ get_pgstat_tabentry_relid(Oid relid, bool isshared, PgStat_StatDBEntry *shared,
2299
2300
* Recheck whether a plain table still needs vacuum or analyze; be it because
2300
2301
* it does directly, or because its TOAST table does. Return value is a valid
2301
2302
* autovac_table pointer if it does, NULL otherwise.
2303
+ *
2304
+ * Note that the returned autovac_table does not have the name fields set.
2302
2305
*/
2303
2306
static autovac_table *
2304
2307
table_recheck_autovac (Oid relid )
@@ -2437,6 +2440,9 @@ table_recheck_autovac(Oid relid)
2437
2440
tab -> at_vacuum_cost_limit = vac_cost_limit ;
2438
2441
tab -> at_vacuum_cost_delay = vac_cost_delay ;
2439
2442
tab -> at_wraparound = wraparound || toast_wraparound ;
2443
+ tab -> at_relname = NULL ;
2444
+ tab -> at_nspname = NULL ;
2445
+ tab -> at_datname = NULL ;
2440
2446
}
2441
2447
2442
2448
heap_close (avRel , AccessShareLock );
@@ -2607,8 +2613,7 @@ relation_needs_vacanalyze(Oid relid,
2607
2613
* Vacuum and/or analyze the specified table
2608
2614
*/
2609
2615
static void
2610
- autovacuum_do_vac_analyze (Oid relid , bool dovacuum , bool doanalyze ,
2611
- int freeze_min_age , bool for_wraparound ,
2616
+ autovacuum_do_vac_analyze (autovac_table * tab ,
2612
2617
BufferAccessStrategy bstrategy )
2613
2618
{
2614
2619
VacuumStmt vacstmt ;
@@ -2617,18 +2622,18 @@ autovacuum_do_vac_analyze(Oid relid, bool dovacuum, bool doanalyze,
2617
2622
MemSet (& vacstmt , 0 , sizeof (vacstmt ));
2618
2623
2619
2624
vacstmt .type = T_VacuumStmt ;
2620
- vacstmt .vacuum = dovacuum ;
2625
+ vacstmt .vacuum = tab -> at_dovacuum ;
2621
2626
vacstmt .full = false;
2622
- vacstmt .analyze = doanalyze ;
2623
- vacstmt .freeze_min_age = freeze_min_age ;
2627
+ vacstmt .analyze = tab -> at_doanalyze ;
2628
+ vacstmt .freeze_min_age = tab -> at_freeze_min_age ;
2624
2629
vacstmt .verbose = false;
2625
2630
vacstmt .relation = NULL ; /* not used since we pass a relid */
2626
2631
vacstmt .va_cols = NIL ;
2627
2632
2628
2633
/* Let pgstat know what we're doing */
2629
- autovac_report_activity (& vacstmt , relid );
2634
+ autovac_report_activity (tab );
2630
2635
2631
- vacuum (& vacstmt , relid , bstrategy , for_wraparound , true);
2636
+ vacuum (& vacstmt , tab -> at_relid , bstrategy , tab -> at_wraparound , true);
2632
2637
}
2633
2638
2634
2639
/*
@@ -2643,37 +2648,28 @@ autovacuum_do_vac_analyze(Oid relid, bool dovacuum, bool doanalyze,
2643
2648
* bother to report "<IDLE>" or some such.
2644
2649
*/
2645
2650
static void
2646
- autovac_report_activity (VacuumStmt * vacstmt , Oid relid )
2651
+ autovac_report_activity (autovac_table * tab )
2647
2652
{
2648
- char * relname = get_rel_name (relid );
2649
- char * nspname = get_namespace_name (get_rel_namespace (relid ));
2650
-
2651
2653
#define MAX_AUTOVAC_ACTIV_LEN (NAMEDATALEN * 2 + 32)
2652
- char activity [MAX_AUTOVAC_ACTIV_LEN ];
2654
+ char activity [MAX_AUTOVAC_ACTIV_LEN ];
2655
+ int len ;
2653
2656
2654
2657
/* Report the command and possible options */
2655
- if (vacstmt -> vacuum )
2658
+ if (tab -> at_dovacuum )
2656
2659
snprintf (activity , MAX_AUTOVAC_ACTIV_LEN ,
2657
2660
"autovacuum: VACUUM%s" ,
2658
- vacstmt -> analyze ? " ANALYZE" : "" );
2661
+ tab -> at_doanalyze ? " ANALYZE" : "" );
2659
2662
else
2660
2663
snprintf (activity , MAX_AUTOVAC_ACTIV_LEN ,
2661
2664
"autovacuum: ANALYZE" );
2662
2665
2663
2666
/*
2664
2667
* Report the qualified name of the relation.
2665
- *
2666
- * Paranoia is appropriate here in case relation was recently dropped ---
2667
- * the lsyscache routines we just invoked will return NULL rather than
2668
- * failing.
2669
2668
*/
2670
- if (relname && nspname )
2671
- {
2672
- int len = strlen (activity );
2669
+ len = strlen (activity );
2673
2670
2674
- snprintf (activity + len , MAX_AUTOVAC_ACTIV_LEN - len ,
2675
- " %s.%s" , nspname , relname );
2676
- }
2671
+ snprintf (activity + len , MAX_AUTOVAC_ACTIV_LEN - len ,
2672
+ " %s.%s" , tab -> at_nspname , tab -> at_relname );
2677
2673
2678
2674
/* Set statement_timestamp() to current time for pg_stat_activity */
2679
2675
SetCurrentStatementStartTimestamp ();
0 commit comments