*
* Copyright (c) 2001-2008, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.177 2008/08/01 13:16:08 alvherre Exp $
+ * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.178 2008/08/05 12:09:30 mha Exp $
* ----------
*/
#include "postgres.h"
* Paths for the statistics files (relative to installation's $PGDATA).
* ----------
*/
-#define PGSTAT_STAT_FILENAME "global/pgstat.stat"
-#define PGSTAT_STAT_TMPFILE "global/pgstat.tmp"
+#define PGSTAT_STAT_PERMANENT_FILENAME "global/pgstat.stat"
+#define PGSTAT_STAT_PERMANENT_TMPFILE "global/pgstat.tmp"
+#define PGSTAT_STAT_FILENAME "pg_stat_tmp/pgstat.stat"
+#define PGSTAT_STAT_TMPFILE "pg_stat_tmp/pgstat.tmp"
/* ----------
* Timer definitions.
static void pgstat_beshutdown_hook(int code, Datum arg);
static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
-static void pgstat_write_statsfile(void);
-static HTAB *pgstat_read_statsfile(Oid onlydb);
+static void pgstat_write_statsfile(bool permanent);
+static HTAB *pgstat_read_statsfile(Oid onlydb, bool permanent);
static void backend_read_statsfile(void);
static void pgstat_read_current_status(void);
pgstat_reset_all(void)
{
unlink(PGSTAT_STAT_FILENAME);
+ unlink(PGSTAT_STAT_PERMANENT_FILENAME);
}
#ifdef EXEC_BACKEND
* zero.
*/
pgStatRunningInCollector = true;
- pgStatDBHash = pgstat_read_statsfile(InvalidOid);
+ pgStatDBHash = pgstat_read_statsfile(InvalidOid, true);
/*
* Setup the descriptor set for select(2). Since only one bit in the set
if (!PostmasterIsAlive(true))
break;
- pgstat_write_statsfile();
+ pgstat_write_statsfile(false);
need_statwrite = false;
need_timer = true;
}
/*
* Save the final stats to reuse at next startup.
*/
- pgstat_write_statsfile();
+ pgstat_write_statsfile(true);
exit(0);
}
* pgstat_write_statsfile() -
*
* Tell the news.
+ * If writing to the permanent file (happens when the collector is
+ * shutting down only), remove the temporary file so that backends
+ * starting up under a new postmaster can't read the old data before
+ * the new collector is ready.
* ----------
*/
static void
-pgstat_write_statsfile(void)
+pgstat_write_statsfile(bool permanent)
{
HASH_SEQ_STATUS hstat;
HASH_SEQ_STATUS tstat;
PgStat_StatFuncEntry *funcentry;
FILE *fpout;
int32 format_id;
+ const char *tmpfile = permanent?PGSTAT_STAT_PERMANENT_TMPFILE:PGSTAT_STAT_TMPFILE;
+ const char *statfile = permanent?PGSTAT_STAT_PERMANENT_FILENAME:PGSTAT_STAT_FILENAME;
/*
* Open the statistics temp file to write out the current values.
*/
- fpout = fopen(PGSTAT_STAT_TMPFILE, PG_BINARY_W);
+ fpout = fopen(tmpfile, PG_BINARY_W);
if (fpout == NULL)
{
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not open temporary statistics file \"%s\": %m",
- PGSTAT_STAT_TMPFILE)));
+ tmpfile)));
return;
}
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not write temporary statistics file \"%s\": %m",
- PGSTAT_STAT_TMPFILE)));
+ tmpfile)));
fclose(fpout);
- unlink(PGSTAT_STAT_TMPFILE);
+ unlink(tmpfile);
}
else if (fclose(fpout) < 0)
{
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not close temporary statistics file \"%s\": %m",
- PGSTAT_STAT_TMPFILE)));
- unlink(PGSTAT_STAT_TMPFILE);
+ tmpfile)));
+ unlink(tmpfile);
}
- else if (rename(PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME) < 0)
+ else if (rename(tmpfile, statfile) < 0)
{
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
- PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME)));
- unlink(PGSTAT_STAT_TMPFILE);
+ tmpfile, statfile)));
+ unlink(tmpfile);
}
+
+ if (permanent)
+ unlink(PGSTAT_STAT_FILENAME);
}
* ----------
*/
static HTAB *
-pgstat_read_statsfile(Oid onlydb)
+pgstat_read_statsfile(Oid onlydb, bool permanent)
{
PgStat_StatDBEntry *dbentry;
PgStat_StatDBEntry dbbuf;
FILE *fpin;
int32 format_id;
bool found;
+ const char *statfile = permanent?PGSTAT_STAT_PERMANENT_FILENAME:PGSTAT_STAT_FILENAME;
/*
* The tables will live in pgStatLocalContext.
* return zero for anything and the collector simply starts from scratch
* with empty counters.
*/
- if ((fpin = AllocateFile(PGSTAT_STAT_FILENAME, PG_BINARY_R)) == NULL)
+ if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
return dbhash;
/*
done:
FreeFile(fpin);
+ if (permanent)
+ unlink(PGSTAT_STAT_PERMANENT_FILENAME);
+
return dbhash;
}
/* Autovacuum launcher wants stats about all databases */
if (IsAutoVacuumLauncherProcess())
- pgStatDBHash = pgstat_read_statsfile(InvalidOid);
+ pgStatDBHash = pgstat_read_statsfile(InvalidOid, false);
else
- pgStatDBHash = pgstat_read_statsfile(MyDatabaseId);
+ pgStatDBHash = pgstat_read_statsfile(MyDatabaseId, false);
}