1313 *
1414 *Copyright (c) 2001-2006, PostgreSQL Global Development Group
1515 *
16- *$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.126 2006/05/19 19:08:26 alvherre Exp $
16+ *$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.127 2006/05/30 02:35:39 momjian Exp $
1717 * ----------
1818 */
1919#include "postgres.h"
2828#include <arpa/inet.h>
2929#include <signal.h>
3030#include <time.h>
31- #include <sys/stat.h>
3231
3332#include "pgstat.h"
3433
6766 * Timer definitions.
6867 * ----------
6968 */
69+ #define PGSTAT_STAT_INTERVAL 500/* How often to write the status file;
70+ * in milliseconds. */
7071
71- /* How often to write the status file, in milliseconds. */
72- #define PGSTAT_STAT_INTERVAL (5*60*1000)
73-
74- /*
75- *How often to attempt to restart a failed statistics collector; in ms.
76- *Must be at least PGSTAT_STAT_INTERVAL.
77- */
78- #define PGSTAT_RESTART_INTERVAL (5*60*1000)
72+ #define PGSTAT_RESTART_INTERVAL 60/* How often to attempt to restart a
73+ * failed statistics collector; in
74+ * seconds. */
7975
8076/* ----------
8177 * Amount of space reserved in pgstat_recvbuffer().
@@ -176,12 +172,11 @@ static void pgstat_drop_database(Oid databaseid);
176172static void pgstat_write_statsfile (void );
177173static void pgstat_read_statsfile (HTAB * * dbhash ,Oid onlydb ,
178174PgStat_StatBeEntry * * betab ,
179- int * numbackends , bool rewrite );
175+ int * numbackends );
180176static void backend_read_statsfile (void );
181177
182178static void pgstat_setheader (PgStat_MsgHdr * hdr ,StatMsgType mtype );
183179static void pgstat_send (void * msg ,int len );
184- static void pgstat_send_rewrite (void );
185180
186181static void pgstat_recv_bestart (PgStat_MsgBestart * msg ,int len );
187182static void pgstat_recv_beterm (PgStat_MsgBeterm * msg ,int len );
@@ -1458,24 +1453,6 @@ pgstat_send(void *msg, int len)
14581453#endif
14591454}
14601455
1461- /*
1462- * pgstat_send_rewrite() -
1463- *
1464- *Send a command to the collector to rewrite the stats file.
1465- * ----------
1466- */
1467- static void
1468- pgstat_send_rewrite (void )
1469- {
1470- PgStat_MsgRewrite msg ;
1471-
1472- if (pgStatSock < 0 )
1473- return ;
1474-
1475- pgstat_setheader (& msg .m_hdr ,PGSTAT_MTYPE_REWRITE );
1476- pgstat_send (& msg ,sizeof (msg ));
1477- }
1478-
14791456
14801457/* ----------
14811458 * PgstatBufferMain() -
@@ -1576,7 +1553,7 @@ PgstatCollectorMain(int argc, char *argv[])
15761553fd_set rfds ;
15771554int readPipe ;
15781555int len = 0 ;
1579- struct itimerval timeout , canceltimeout ;
1556+ struct itimerval timeout ;
15801557bool need_timer = false;
15811558
15821559MyProcPid = getpid ();/* reset MyProcPid */
@@ -1631,15 +1608,12 @@ PgstatCollectorMain(int argc, char *argv[])
16311608timeout .it_value .tv_sec = PGSTAT_STAT_INTERVAL /1000 ;
16321609timeout .it_value .tv_usec = PGSTAT_STAT_INTERVAL %1000 ;
16331610
1634- /* Values set to zero will cancel the active timer */
1635- MemSet (& canceltimeout ,0 ,sizeof (struct itimerval ));
1636-
16371611/*
16381612 * Read in an existing statistics stats file or initialize the stats to
16391613 * zero.
16401614 */
16411615pgStatRunningInCollector = true;
1642- pgstat_read_statsfile (& pgStatDBHash ,InvalidOid ,NULL ,NULL , false );
1616+ pgstat_read_statsfile (& pgStatDBHash ,InvalidOid ,NULL ,NULL );
16431617
16441618/*
16451619 * Create the known backends table
@@ -1794,12 +1768,6 @@ PgstatCollectorMain(int argc, char *argv[])
17941768pgstat_recv_analyze ((PgStat_MsgAnalyze * )& msg ,nread );
17951769break ;
17961770
1797- case PGSTAT_MTYPE_REWRITE :
1798- need_statwrite = true;
1799- /* Disable the timer - it will be restarted on next data update */
1800- setitimer (ITIMER_REAL ,& canceltimeout ,NULL );
1801- break ;
1802-
18031771default :
18041772break ;
18051773}
@@ -2380,7 +2348,7 @@ comparePids(const void *v1, const void *v2)
23802348 */
23812349static void
23822350pgstat_read_statsfile (HTAB * * dbhash ,Oid onlydb ,
2383- PgStat_StatBeEntry * * betab ,int * numbackends , bool rewrite )
2351+ PgStat_StatBeEntry * * betab ,int * numbackends )
23842352{
23852353PgStat_StatDBEntry * dbentry ;
23862354PgStat_StatDBEntry dbbuf ;
@@ -2399,71 +2367,6 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
23992367MemoryContext use_mcxt ;
24002368int mcxt_flags ;
24012369
2402-
2403- if (rewrite )
2404- {
2405- /*
2406- * To force a rewrite of the stats file from the collector, send
2407- * a REWRITE message to the stats collector. Then wait for the file
2408- * to change. On Unix, we wait for the inode to change (as the file
2409- * is renamed into place from a different file). Win32 has no concept
2410- * of inodes, so we wait for the date on the file to change instead.
2411- * We can do this on win32 because we have high-res timing on the
2412- * file dates, but we can't on unix, because it has 1sec resolution
2413- * on the fields in struct stat.
2414- */
2415- int i ;
2416- #ifndef WIN32
2417- struct stat st1 ,st2 ;
2418-
2419- if (stat (PGSTAT_STAT_FILENAME ,& st1 ))
2420- {
2421- /* Assume no file there yet */
2422- st1 .st_ino = 0 ;
2423- }
2424- st2 .st_ino = 0 ;
2425- #else
2426- WIN32_FILE_ATTRIBUTE_DATA fd1 ,fd2 ;
2427-
2428- if (!GetFileAttributesEx (PGSTAT_STAT_FILENAME ,GetFileExInfoStandard ,& fd1 ))
2429- {
2430- fd1 .ftLastWriteTime .dwLowDateTime = 0 ;
2431- fd1 .ftLastWriteTime .dwHighDateTime = 0 ;
2432- }
2433- fd2 .ftLastWriteTime .dwLowDateTime = 0 ;
2434- fd2 .ftLastWriteTime .dwHighDateTime = 0 ;
2435- #endif
2436-
2437-
2438- /* Send rewrite message */
2439- pgstat_send_rewrite ();
2440-
2441- /* Now wait for the file to change */
2442- for (i = 0 ;i < 50 ;i ++ )
2443- {
2444- #ifndef WIN32
2445- if (!stat (PGSTAT_STAT_FILENAME ,& st2 ))
2446- {
2447- if (st2 .st_ino != st1 .st_ino )
2448- break ;
2449- }
2450- #else
2451- if (GetFileAttributesEx (PGSTAT_STAT_FILENAME ,GetFileExInfoStandard ,& fd2 ))
2452- {
2453- if (fd1 .ftLastWriteTime .dwLowDateTime != fd2 .ftLastWriteTime .dwLowDateTime ||
2454- fd1 .ftLastWriteTime .dwHighDateTime != fd2 .ftLastWriteTime .dwHighDateTime )
2455- break ;
2456- }
2457- #endif
2458-
2459- pg_usleep (50000 );
2460- }
2461- if (i >=50 )
2462- ereport (WARNING ,
2463- (errmsg ("pgstat update timeout" )));
2464- /* Fallthrough and read the old file anyway - old data better than no data */
2465- }
2466-
24672370/*
24682371 * If running in the collector or the autovacuum process, we use the
24692372 * DynaHashCxt memory context.If running in a backend, we use the
@@ -2782,7 +2685,7 @@ backend_read_statsfile(void)
27822685return ;
27832686Assert (!pgStatRunningInCollector );
27842687pgstat_read_statsfile (& pgStatDBHash ,InvalidOid ,
2785- & pgStatBeTable ,& pgStatNumBackends , true );
2688+ & pgStatBeTable ,& pgStatNumBackends );
27862689}
27872690else
27882691{
@@ -2792,7 +2695,7 @@ backend_read_statsfile(void)
27922695{
27932696Assert (!pgStatRunningInCollector );
27942697pgstat_read_statsfile (& pgStatDBHash ,MyDatabaseId ,
2795- & pgStatBeTable ,& pgStatNumBackends , true );
2698+ & pgStatBeTable ,& pgStatNumBackends );
27962699pgStatDBHashXact = topXid ;
27972700}
27982701}