Skip to content

Commit 70d7569

Browse files
committed
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.
1 parent 7e61edf commit 70d7569

File tree

5 files changed

+62
-28
lines changed

5 files changed

+62
-28
lines changed

doc/src/sgml/monitoring.sgml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.60 2008/06/18 17:44:12 neilc Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.61 2008/08/05 12:09:30 mha Exp $ -->
22

33
<chapter id="monitoring">
44
<title>Monitoring Database Activity</title>
@@ -164,6 +164,17 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
164164
only superusers are allowed to change these parameters with
165165
<command>SET</>.)
166166
</para>
167+
168+
<para>
169+
The statistics collector communicates with the backends needing
170+
information (including autovacuum) through temporary files.
171+
These files are stored in the <filename>pg_stat_tmp</filename> subdirectory.
172+
When the postmaster shuts down, a permanent copy of the statistics
173+
data is stored in the <filename>global</filename> subdirectory. For increased
174+
performance, it is possible to mount or symlink a RAM based
175+
filesystem to the <filename>pg_stat_tmp</filename> directory.
176+
</para>
177+
167178
</sect2>
168179

169180
<sect2 id="monitoring-stats-views">

doc/src/sgml/storage.sgml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.23 2008/05/02 01:08:26 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.24 2008/08/05 12:09:30 mha Exp $ -->
22

33
<chapter id="storage">
44

@@ -77,6 +77,12 @@ Item
7777
(used for shared row locks)</entry>
7878
</row>
7979

80+
<row>
81+
<entry><filename>pg_stat_tmp</></entry>
82+
<entry>Subdirectory containing temporary files for the statistics
83+
subsystem</entry>
84+
</row>
85+
8086
<row>
8187
<entry><filename>pg_subtrans</></entry>
8288
<entry>Subdirectory containing subtransaction status data</entry>

src/backend/postmaster/pgstat.c

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright (c) 2001-2008, PostgreSQL Global Development Group
1515
*
16-
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.177 2008/08/01 13:16:08 alvherre Exp $
16+
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.178 2008/08/05 12:09:30 mha Exp $
1717
* ----------
1818
*/
1919
#include "postgres.h"
@@ -68,8 +68,10 @@
6868
* Paths for the statistics files (relative to installation's $PGDATA).
6969
* ----------
7070
*/
71-
#define PGSTAT_STAT_FILENAME "global/pgstat.stat"
72-
#define PGSTAT_STAT_TMPFILE "global/pgstat.tmp"
71+
#define PGSTAT_STAT_PERMANENT_FILENAME "global/pgstat.stat"
72+
#define PGSTAT_STAT_PERMANENT_TMPFILE "global/pgstat.tmp"
73+
#define PGSTAT_STAT_FILENAME "pg_stat_tmp/pgstat.stat"
74+
#define PGSTAT_STAT_TMPFILE "pg_stat_tmp/pgstat.tmp"
7375

7476
/* ----------
7577
* Timer definitions.
@@ -219,8 +221,8 @@ static void force_statwrite(SIGNAL_ARGS);
219221
static void pgstat_beshutdown_hook(int code, Datum arg);
220222

221223
static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
222-
static void pgstat_write_statsfile(void);
223-
static HTAB *pgstat_read_statsfile(Oid onlydb);
224+
static void pgstat_write_statsfile(bool permanent);
225+
static HTAB *pgstat_read_statsfile(Oid onlydb, bool permanent);
224226
static void backend_read_statsfile(void);
225227
static void pgstat_read_current_status(void);
226228

@@ -510,6 +512,7 @@ void
510512
pgstat_reset_all(void)
511513
{
512514
unlink(PGSTAT_STAT_FILENAME);
515+
unlink(PGSTAT_STAT_PERMANENT_FILENAME);
513516
}
514517

515518
#ifdef EXEC_BACKEND
@@ -2598,7 +2601,7 @@ PgstatCollectorMain(int argc, char *argv[])
25982601
* zero.
25992602
*/
26002603
pgStatRunningInCollector = true;
2601-
pgStatDBHash = pgstat_read_statsfile(InvalidOid);
2604+
pgStatDBHash = pgstat_read_statsfile(InvalidOid, true);
26022605

26032606
/*
26042607
* Setup the descriptor set for select(2). Since only one bit in the set
@@ -2638,7 +2641,7 @@ PgstatCollectorMain(int argc, char *argv[])
26382641
if (!PostmasterIsAlive(true))
26392642
break;
26402643

2641-
pgstat_write_statsfile();
2644+
pgstat_write_statsfile(false);
26422645
need_statwrite = false;
26432646
need_timer = true;
26442647
}
@@ -2806,7 +2809,7 @@ PgstatCollectorMain(int argc, char *argv[])
28062809
/*
28072810
* Save the final stats to reuse at next startup.
28082811
*/
2809-
pgstat_write_statsfile();
2812+
pgstat_write_statsfile(true);
28102813

28112814
exit(0);
28122815
}
@@ -2891,10 +2894,14 @@ pgstat_get_db_entry(Oid databaseid, bool create)
28912894
* pgstat_write_statsfile() -
28922895
*
28932896
* Tell the news.
2897+
* If writing to the permanent file (happens when the collector is
2898+
* shutting down only), remove the temporary file so that backends
2899+
* starting up under a new postmaster can't read the old data before
2900+
* the new collector is ready.
28942901
* ----------
28952902
*/
28962903
static void
2897-
pgstat_write_statsfile(void)
2904+
pgstat_write_statsfile(bool permanent)
28982905
{
28992906
HASH_SEQ_STATUS hstat;
29002907
HASH_SEQ_STATUS tstat;
@@ -2904,17 +2911,19 @@ pgstat_write_statsfile(void)
29042911
PgStat_StatFuncEntry *funcentry;
29052912
FILE *fpout;
29062913
int32 format_id;
2914+
const char *tmpfile = permanent?PGSTAT_STAT_PERMANENT_TMPFILE:PGSTAT_STAT_TMPFILE;
2915+
const char *statfile = permanent?PGSTAT_STAT_PERMANENT_FILENAME:PGSTAT_STAT_FILENAME;
29072916

29082917
/*
29092918
* Open the statistics temp file to write out the current values.
29102919
*/
2911-
fpout = fopen(PGSTAT_STAT_TMPFILE, PG_BINARY_W);
2920+
fpout = fopen(tmpfile, PG_BINARY_W);
29122921
if (fpout == NULL)
29132922
{
29142923
ereport(LOG,
29152924
(errcode_for_file_access(),
29162925
errmsg("could not open temporary statistics file \"%s\": %m",
2917-
PGSTAT_STAT_TMPFILE)));
2926+
tmpfile)));
29182927
return;
29192928
}
29202929

@@ -2981,26 +2990,29 @@ pgstat_write_statsfile(void)
29812990
ereport(LOG,
29822991
(errcode_for_file_access(),
29832992
errmsg("could not write temporary statistics file \"%s\": %m",
2984-
PGSTAT_STAT_TMPFILE)));
2993+
tmpfile)));
29852994
fclose(fpout);
2986-
unlink(PGSTAT_STAT_TMPFILE);
2995+
unlink(tmpfile);
29872996
}
29882997
else if (fclose(fpout) < 0)
29892998
{
29902999
ereport(LOG,
29913000
(errcode_for_file_access(),
29923001
errmsg("could not close temporary statistics file \"%s\": %m",
2993-
PGSTAT_STAT_TMPFILE)));
2994-
unlink(PGSTAT_STAT_TMPFILE);
3002+
tmpfile)));
3003+
unlink(tmpfile);
29953004
}
2996-
else if (rename(PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME) < 0)
3005+
else if (rename(tmpfile, statfile) < 0)
29973006
{
29983007
ereport(LOG,
29993008
(errcode_for_file_access(),
30003009
errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
3001-
PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME)));
3002-
unlink(PGSTAT_STAT_TMPFILE);
3010+
tmpfile, statfile)));
3011+
unlink(tmpfile);
30033012
}
3013+
3014+
if (permanent)
3015+
unlink(PGSTAT_STAT_FILENAME);
30043016
}
30053017

30063018

@@ -3012,7 +3024,7 @@ pgstat_write_statsfile(void)
30123024
* ----------
30133025
*/
30143026
static HTAB *
3015-
pgstat_read_statsfile(Oid onlydb)
3027+
pgstat_read_statsfile(Oid onlydb, bool permanent)
30163028
{
30173029
PgStat_StatDBEntry *dbentry;
30183030
PgStat_StatDBEntry dbbuf;
@@ -3027,6 +3039,7 @@ pgstat_read_statsfile(Oid onlydb)
30273039
FILE *fpin;
30283040
int32 format_id;
30293041
bool found;
3042+
const char *statfile = permanent?PGSTAT_STAT_PERMANENT_FILENAME:PGSTAT_STAT_FILENAME;
30303043

30313044
/*
30323045
* The tables will live in pgStatLocalContext.
@@ -3055,7 +3068,7 @@ pgstat_read_statsfile(Oid onlydb)
30553068
* return zero for anything and the collector simply starts from scratch
30563069
* with empty counters.
30573070
*/
3058-
if ((fpin = AllocateFile(PGSTAT_STAT_FILENAME, PG_BINARY_R)) == NULL)
3071+
if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
30593072
return dbhash;
30603073

30613074
/*
@@ -3244,6 +3257,9 @@ pgstat_read_statsfile(Oid onlydb)
32443257
done:
32453258
FreeFile(fpin);
32463259

3260+
if (permanent)
3261+
unlink(PGSTAT_STAT_PERMANENT_FILENAME);
3262+
32473263
return dbhash;
32483264
}
32493265

@@ -3262,9 +3278,9 @@ backend_read_statsfile(void)
32623278

32633279
/* Autovacuum launcher wants stats about all databases */
32643280
if (IsAutoVacuumLauncherProcess())
3265-
pgStatDBHash = pgstat_read_statsfile(InvalidOid);
3281+
pgStatDBHash = pgstat_read_statsfile(InvalidOid, false);
32663282
else
3267-
pgStatDBHash = pgstat_read_statsfile(MyDatabaseId);
3283+
pgStatDBHash = pgstat_read_statsfile(MyDatabaseId, false);
32683284
}
32693285

32703286

src/bin/initdb/initdb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* Portions Copyright (c) 1994, Regents of the University of California
4343
* Portions taken from FreeBSD.
4444
*
45-
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.158 2008/07/19 04:01:29 tgl Exp $
45+
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.159 2008/08/05 12:09:30 mha Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -2461,7 +2461,8 @@ main(int argc, char *argv[])
24612461
"pg_multixact/offsets",
24622462
"base",
24632463
"base/1",
2464-
"pg_tblspc"
2464+
"pg_tblspc",
2465+
"pg_stat_tmp"
24652466
};
24662467

24672468
progname = get_progname(argv[0]);

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.474 2008/08/02 21:32:00 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.475 2008/08/05 12:09:30 mha Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200808011
56+
#define CATALOG_VERSION_NO 200808051
5757

5858
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy