1313 *
1414 *Copyright (c) 2001-2010, PostgreSQL Global Development Group
1515 *
16- *$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.201 2010/02/26 02:00:55 momjian Exp $
16+ *$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.202 2010/03/12 22:19:19 tgl Exp $
1717 * ----------
1818 */
1919#include "postgres.h"
@@ -1177,12 +1177,10 @@ pgstat_reset_shared_counters(const char *target)
11771177if (strcmp (target ,"bgwriter" )== 0 )
11781178msg .m_resettarget = RESET_BGWRITER ;
11791179else
1180- {
11811180ereport (ERROR ,
1182- (errcode (ERRCODE_SYNTAX_ERROR ),
1183- errmsg ("unrecognized reset target: '%s'" ,target ),
1184- errhint ("allowed targets are 'bgwriter'." )));
1185- }
1181+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1182+ errmsg ("unrecognized reset target: \"%s\"" ,target ),
1183+ errhint ("Target must be \"bgwriter\"." )));
11861184
11871185pgstat_setheader (& msg .m_hdr ,PGSTAT_MTYPE_RESETSHAREDCOUNTER );
11881186pgstat_send (& msg ,sizeof (msg ));
@@ -3292,11 +3290,15 @@ pgstat_write_statsfile(bool permanent)
32923290/*
32933291 * It's not entirely clear whether there could be clock skew between
32943292 * backends and the collector; but just in case someone manages to
3295- * send us a stats request time that'sfar in the future, reset it.
3293+ * send us a stats request time that's in the future, reset it.
32963294 * This ensures that no inquiry message can cause more than one stats
32973295 * file write to occur.
32983296 */
3299- last_statrequest = last_statwrite ;
3297+ if (last_statrequest > last_statwrite )
3298+ {
3299+ elog (LOG ,"last_statrequest is in the future, resetting" );
3300+ last_statrequest = last_statwrite ;
3301+ }
33003302}
33013303
33023304if (permanent )
@@ -3355,9 +3357,20 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
33553357 * Try to open the status file. If it doesn't exist, the backends simply
33563358 * return zero for anything and the collector simply starts from scratch
33573359 * with empty counters.
3360+ *
3361+ * ENOENT is a possibility if the stats collector is not running or has
3362+ * not yet written the stats file the first time. Any other failure
3363+ * condition is suspicious.
33583364 */
33593365if ((fpin = AllocateFile (statfile ,PG_BINARY_R ))== NULL )
3366+ {
3367+ if (errno != ENOENT )
3368+ ereport (pgStatRunningInCollector ?LOG :WARNING ,
3369+ (errcode_for_file_access (),
3370+ errmsg ("could not open statistics file \"%s\": %m" ,
3371+ statfile )));
33603372return dbhash ;
3373+ }
33613374
33623375/*
33633376 * Verify it's of the expected format.
@@ -3366,7 +3379,7 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
33663379|| format_id != PGSTAT_FILE_FORMAT_ID )
33673380{
33683381ereport (pgStatRunningInCollector ?LOG :WARNING ,
3369- (errmsg ("corruptedpgstat.stat file" )));
3382+ (errmsg ("corruptedstatistics file \"%s\"" , statfile )));
33703383gotodone ;
33713384}
33723385
@@ -3376,7 +3389,7 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
33763389if (fread (& globalStats ,1 ,sizeof (globalStats ),fpin )!= sizeof (globalStats ))
33773390{
33783391ereport (pgStatRunningInCollector ?LOG :WARNING ,
3379- (errmsg ("corruptedpgstat.stat file" )));
3392+ (errmsg ("corruptedstatistics file \"%s\"" , statfile )));
33803393gotodone ;
33813394}
33823395
@@ -3398,7 +3411,8 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
33983411fpin )!= offsetof(PgStat_StatDBEntry ,tables ))
33993412{
34003413ereport (pgStatRunningInCollector ?LOG :WARNING ,
3401- (errmsg ("corrupted pgstat.stat file" )));
3414+ (errmsg ("corrupted statistics file \"%s\"" ,
3415+ statfile )));
34023416gotodone ;
34033417}
34043418
@@ -3412,7 +3426,8 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
34123426if (found )
34133427{
34143428ereport (pgStatRunningInCollector ?LOG :WARNING ,
3415- (errmsg ("corrupted pgstat.stat file" )));
3429+ (errmsg ("corrupted statistics file \"%s\"" ,
3430+ statfile )));
34163431gotodone ;
34173432}
34183433
@@ -3474,7 +3489,8 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
34743489fpin )!= sizeof (PgStat_StatTabEntry ))
34753490{
34763491ereport (pgStatRunningInCollector ?LOG :WARNING ,
3477- (errmsg ("corrupted pgstat.stat file" )));
3492+ (errmsg ("corrupted statistics file \"%s\"" ,
3493+ statfile )));
34783494gotodone ;
34793495}
34803496
@@ -3491,7 +3507,8 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
34913507if (found )
34923508{
34933509ereport (pgStatRunningInCollector ?LOG :WARNING ,
3494- (errmsg ("corrupted pgstat.stat file" )));
3510+ (errmsg ("corrupted statistics file \"%s\"" ,
3511+ statfile )));
34953512gotodone ;
34963513}
34973514
@@ -3506,7 +3523,8 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
35063523fpin )!= sizeof (PgStat_StatFuncEntry ))
35073524{
35083525ereport (pgStatRunningInCollector ?LOG :WARNING ,
3509- (errmsg ("corrupted pgstat.stat file" )));
3526+ (errmsg ("corrupted statistics file \"%s\"" ,
3527+ statfile )));
35103528gotodone ;
35113529}
35123530
@@ -3523,7 +3541,8 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
35233541if (found )
35243542{
35253543ereport (pgStatRunningInCollector ?LOG :WARNING ,
3526- (errmsg ("corrupted pgstat.stat file" )));
3544+ (errmsg ("corrupted statistics file \"%s\"" ,
3545+ statfile )));
35273546gotodone ;
35283547}
35293548
@@ -3538,7 +3557,8 @@ pgstat_read_statsfile(Oid onlydb, bool permanent)
35383557
35393558default :
35403559ereport (pgStatRunningInCollector ?LOG :WARNING ,
3541- (errmsg ("corrupted pgstat.stat file" )));
3560+ (errmsg ("corrupted statistics file \"%s\"" ,
3561+ statfile )));
35423562gotodone ;
35433563}
35443564}
@@ -3568,17 +3588,27 @@ pgstat_read_statsfile_timestamp(bool permanent, TimestampTz *ts)
35683588const char * statfile = permanent ?PGSTAT_STAT_PERMANENT_FILENAME :pgstat_stat_filename ;
35693589
35703590/*
3571- * Try to open the status file.
3591+ * Try to open the status file. As above, anything but ENOENT is worthy
3592+ * of complaining about.
35723593 */
35733594if ((fpin = AllocateFile (statfile ,PG_BINARY_R ))== NULL )
3595+ {
3596+ if (errno != ENOENT )
3597+ ereport (pgStatRunningInCollector ?LOG :WARNING ,
3598+ (errcode_for_file_access (),
3599+ errmsg ("could not open statistics file \"%s\": %m" ,
3600+ statfile )));
35743601return false;
3602+ }
35753603
35763604/*
35773605 * Verify it's of the expected format.
35783606 */
35793607if (fread (& format_id ,1 ,sizeof (format_id ),fpin )!= sizeof (format_id )
35803608|| format_id != PGSTAT_FILE_FORMAT_ID )
35813609{
3610+ ereport (pgStatRunningInCollector ?LOG :WARNING ,
3611+ (errmsg ("corrupted statistics file \"%s\"" ,statfile )));
35823612FreeFile (fpin );
35833613return false;
35843614}
@@ -3588,6 +3618,8 @@ pgstat_read_statsfile_timestamp(bool permanent, TimestampTz *ts)
35883618 */
35893619if (fread (& myGlobalStats ,1 ,sizeof (myGlobalStats ),fpin )!= sizeof (myGlobalStats ))
35903620{
3621+ ereport (pgStatRunningInCollector ?LOG :WARNING ,
3622+ (errmsg ("corrupted statistics file \"%s\"" ,statfile )));
35913623FreeFile (fpin );
35923624return false;
35933625}