Commit 70d75697 authored by Magnus Hagander's avatar Magnus Hagander

Move pgstat.tmp into a temporary directory under $PGDATA named pg_stat_tmp.

This allows the use of a ramdrive (either through mount or symlink) for
the temporary file that's written every half second, which should
reduce I/O.

On server shutdown/startup, the file is written to the old location in
the global directory, to preserve data across restarts.

Bump catversion since the $PGDATA directory layout changed.
parent 7e61edf2
<!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.60 2008/06/18 17:44:12 neilc Exp $ --> <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.61 2008/08/05 12:09:30 mha Exp $ -->
<chapter id="monitoring"> <chapter id="monitoring">
<title>Monitoring Database Activity</title> <title>Monitoring Database Activity</title>
...@@ -164,6 +164,17 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re ...@@ -164,6 +164,17 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
only superusers are allowed to change these parameters with only superusers are allowed to change these parameters with
<command>SET</>.) <command>SET</>.)
</para> </para>
<para>
The statistics collector communicates with the backends needing
information (including autovacuum) through temporary files.
These files are stored in the <filename>pg_stat_tmp</filename> subdirectory.
When the postmaster shuts down, a permanent copy of the statistics
data is stored in the <filename>global</filename> subdirectory. For increased
performance, it is possible to mount or symlink a RAM based
filesystem to the <filename>pg_stat_tmp</filename> directory.
</para>
</sect2> </sect2>
<sect2 id="monitoring-stats-views"> <sect2 id="monitoring-stats-views">
......
<!-- $PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.23 2008/05/02 01:08:26 tgl Exp $ --> <!-- $PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.24 2008/08/05 12:09:30 mha Exp $ -->
<chapter id="storage"> <chapter id="storage">
...@@ -77,6 +77,12 @@ Item ...@@ -77,6 +77,12 @@ Item
(used for shared row locks)</entry> (used for shared row locks)</entry>
</row> </row>
<row>
<entry><filename>pg_stat_tmp</></entry>
<entry>Subdirectory containing temporary files for the statistics
subsystem</entry>
</row>
<row> <row>
<entry><filename>pg_subtrans</></entry> <entry><filename>pg_subtrans</></entry>
<entry>Subdirectory containing subtransaction status data</entry> <entry>Subdirectory containing subtransaction status data</entry>
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* *
* Copyright (c) 2001-2008, PostgreSQL Global Development Group * 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" #include "postgres.h"
...@@ -68,8 +68,10 @@ ...@@ -68,8 +68,10 @@
* Paths for the statistics files (relative to installation's $PGDATA). * Paths for the statistics files (relative to installation's $PGDATA).
* ---------- * ----------
*/ */
#define PGSTAT_STAT_FILENAME "global/pgstat.stat" #define PGSTAT_STAT_PERMANENT_FILENAME "global/pgstat.stat"
#define PGSTAT_STAT_TMPFILE "global/pgstat.tmp" #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. * Timer definitions.
...@@ -219,8 +221,8 @@ static void force_statwrite(SIGNAL_ARGS); ...@@ -219,8 +221,8 @@ static void force_statwrite(SIGNAL_ARGS);
static void pgstat_beshutdown_hook(int code, Datum arg); static void pgstat_beshutdown_hook(int code, Datum arg);
static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create); static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
static void pgstat_write_statsfile(void); static void pgstat_write_statsfile(bool permanent);
static HTAB *pgstat_read_statsfile(Oid onlydb); static HTAB *pgstat_read_statsfile(Oid onlydb, bool permanent);
static void backend_read_statsfile(void); static void backend_read_statsfile(void);
static void pgstat_read_current_status(void); static void pgstat_read_current_status(void);
...@@ -510,6 +512,7 @@ void ...@@ -510,6 +512,7 @@ void
pgstat_reset_all(void) pgstat_reset_all(void)
{ {
unlink(PGSTAT_STAT_FILENAME); unlink(PGSTAT_STAT_FILENAME);
unlink(PGSTAT_STAT_PERMANENT_FILENAME);
} }
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
...@@ -2598,7 +2601,7 @@ PgstatCollectorMain(int argc, char *argv[]) ...@@ -2598,7 +2601,7 @@ PgstatCollectorMain(int argc, char *argv[])
* zero. * zero.
*/ */
pgStatRunningInCollector = true; 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 * Setup the descriptor set for select(2). Since only one bit in the set
...@@ -2638,7 +2641,7 @@ PgstatCollectorMain(int argc, char *argv[]) ...@@ -2638,7 +2641,7 @@ PgstatCollectorMain(int argc, char *argv[])
if (!PostmasterIsAlive(true)) if (!PostmasterIsAlive(true))
break; break;
pgstat_write_statsfile(); pgstat_write_statsfile(false);
need_statwrite = false; need_statwrite = false;
need_timer = true; need_timer = true;
} }
...@@ -2806,7 +2809,7 @@ PgstatCollectorMain(int argc, char *argv[]) ...@@ -2806,7 +2809,7 @@ PgstatCollectorMain(int argc, char *argv[])
/* /*
* Save the final stats to reuse at next startup. * Save the final stats to reuse at next startup.
*/ */
pgstat_write_statsfile(); pgstat_write_statsfile(true);
exit(0); exit(0);
} }
...@@ -2891,10 +2894,14 @@ pgstat_get_db_entry(Oid databaseid, bool create) ...@@ -2891,10 +2894,14 @@ pgstat_get_db_entry(Oid databaseid, bool create)
* pgstat_write_statsfile() - * pgstat_write_statsfile() -
* *
* Tell the news. * 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 static void
pgstat_write_statsfile(void) pgstat_write_statsfile(bool permanent)
{ {
HASH_SEQ_STATUS hstat; HASH_SEQ_STATUS hstat;
HASH_SEQ_STATUS tstat; HASH_SEQ_STATUS tstat;
...@@ -2904,17 +2911,19 @@ pgstat_write_statsfile(void) ...@@ -2904,17 +2911,19 @@ pgstat_write_statsfile(void)
PgStat_StatFuncEntry *funcentry; PgStat_StatFuncEntry *funcentry;
FILE *fpout; FILE *fpout;
int32 format_id; 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. * 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) if (fpout == NULL)
{ {
ereport(LOG, ereport(LOG,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not open temporary statistics file \"%s\": %m", errmsg("could not open temporary statistics file \"%s\": %m",
PGSTAT_STAT_TMPFILE))); tmpfile)));
return; return;
} }
...@@ -2981,26 +2990,29 @@ pgstat_write_statsfile(void) ...@@ -2981,26 +2990,29 @@ pgstat_write_statsfile(void)
ereport(LOG, ereport(LOG,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not write temporary statistics file \"%s\": %m", errmsg("could not write temporary statistics file \"%s\": %m",
PGSTAT_STAT_TMPFILE))); tmpfile)));
fclose(fpout); fclose(fpout);
unlink(PGSTAT_STAT_TMPFILE); unlink(tmpfile);
} }
else if (fclose(fpout) < 0) else if (fclose(fpout) < 0)
{ {
ereport(LOG, ereport(LOG,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not close temporary statistics file \"%s\": %m", errmsg("could not close temporary statistics file \"%s\": %m",
PGSTAT_STAT_TMPFILE))); tmpfile)));
unlink(PGSTAT_STAT_TMPFILE); unlink(tmpfile);
} }
else if (rename(PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME) < 0) else if (rename(tmpfile, statfile) < 0)
{ {
ereport(LOG, ereport(LOG,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m", errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME))); tmpfile, statfile)));
unlink(PGSTAT_STAT_TMPFILE); unlink(tmpfile);
} }
if (permanent)
unlink(PGSTAT_STAT_FILENAME);
} }
...@@ -3012,7 +3024,7 @@ pgstat_write_statsfile(void) ...@@ -3012,7 +3024,7 @@ pgstat_write_statsfile(void)
* ---------- * ----------
*/ */
static HTAB * static HTAB *
pgstat_read_statsfile(Oid onlydb) pgstat_read_statsfile(Oid onlydb, bool permanent)
{ {
PgStat_StatDBEntry *dbentry; PgStat_StatDBEntry *dbentry;
PgStat_StatDBEntry dbbuf; PgStat_StatDBEntry dbbuf;
...@@ -3027,6 +3039,7 @@ pgstat_read_statsfile(Oid onlydb) ...@@ -3027,6 +3039,7 @@ pgstat_read_statsfile(Oid onlydb)
FILE *fpin; FILE *fpin;
int32 format_id; int32 format_id;
bool found; bool found;
const char *statfile = permanent?PGSTAT_STAT_PERMANENT_FILENAME:PGSTAT_STAT_FILENAME;
/* /*
* The tables will live in pgStatLocalContext. * The tables will live in pgStatLocalContext.
...@@ -3055,7 +3068,7 @@ pgstat_read_statsfile(Oid onlydb) ...@@ -3055,7 +3068,7 @@ pgstat_read_statsfile(Oid onlydb)
* return zero for anything and the collector simply starts from scratch * return zero for anything and the collector simply starts from scratch
* with empty counters. * with empty counters.
*/ */
if ((fpin = AllocateFile(PGSTAT_STAT_FILENAME, PG_BINARY_R)) == NULL) if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
return dbhash; return dbhash;
/* /*
...@@ -3244,6 +3257,9 @@ pgstat_read_statsfile(Oid onlydb) ...@@ -3244,6 +3257,9 @@ pgstat_read_statsfile(Oid onlydb)
done: done:
FreeFile(fpin); FreeFile(fpin);
if (permanent)
unlink(PGSTAT_STAT_PERMANENT_FILENAME);
return dbhash; return dbhash;
} }
...@@ -3262,9 +3278,9 @@ backend_read_statsfile(void) ...@@ -3262,9 +3278,9 @@ backend_read_statsfile(void)
/* Autovacuum launcher wants stats about all databases */ /* Autovacuum launcher wants stats about all databases */
if (IsAutoVacuumLauncherProcess()) if (IsAutoVacuumLauncherProcess())
pgStatDBHash = pgstat_read_statsfile(InvalidOid); pgStatDBHash = pgstat_read_statsfile(InvalidOid, false);
else else
pgStatDBHash = pgstat_read_statsfile(MyDatabaseId); pgStatDBHash = pgstat_read_statsfile(MyDatabaseId, false);
} }
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* Portions taken from FreeBSD. * Portions taken from FreeBSD.
* *
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.158 2008/07/19 04:01:29 tgl Exp $ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.159 2008/08/05 12:09:30 mha Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -2461,7 +2461,8 @@ main(int argc, char *argv[]) ...@@ -2461,7 +2461,8 @@ main(int argc, char *argv[])
"pg_multixact/offsets", "pg_multixact/offsets",
"base", "base",
"base/1", "base/1",
"pg_tblspc" "pg_tblspc",
"pg_stat_tmp"
}; };
progname = get_progname(argv[0]); progname = get_progname(argv[0]);
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.474 2008/08/02 21:32:00 tgl Exp $ * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.475 2008/08/05 12:09:30 mha Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -53,6 +53,6 @@ ...@@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 200808011 #define CATALOG_VERSION_NO 200808051
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment