Skip to content

Commit 2b1e36c

Browse files
committed
Tweak stats collector start logic so that we will not try to spawn a
new stats collector oftener than once a minute. Per gripe from Erik Walthinsen 4/25/03.
1 parent 9fdb1de commit 2b1e36c

File tree

3 files changed

+89
-54
lines changed

3 files changed

+89
-54
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
*
1111
* - Add a pgstat config column to pg_database, so this
1212
* entire thing can be enabled/disabled on a per db base.
13-
* Not to be done before 7.2 - requires catalog change and
14-
* thus an initdb and we might want to provide this as a
15-
* patch for 7.1.
1613
*
17-
* Copyright (c) 2001, PostgreSQL Global Development Group
14+
* Copyright (c) 2001-2003, PostgreSQL Global Development Group
1815
*
19-
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.33 2003/04/25 01:24:00 momjian Exp $
16+
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.34 2003/04/26 02:57:14 tgl Exp $
2017
* ----------
2118
*/
2219
#include "postgres.h"
@@ -31,6 +28,8 @@
3128
#include <errno.h>
3229
#include <signal.h>
3330

31+
#include "pgstat.h"
32+
3433
#include "access/xact.h"
3534
#include "access/heapam.h"
3635
#include "catalog/catname.h"
@@ -47,8 +46,6 @@
4746
#include "utils/ps_status.h"
4847
#include "utils/syscache.h"
4948

50-
#include "pgstat.h"
51-
5249

5350
/* ----------
5451
* GUC parameters
@@ -60,6 +57,12 @@ bool pgstat_collect_querystring = false;
6057
bool pgstat_collect_tuplelevel = false;
6158
bool pgstat_collect_blocklevel = false;
6259

60+
/* ----------
61+
* Other global variables
62+
* ----------
63+
*/
64+
bool pgstat_is_running = false;
65+
6366
/* ----------
6467
* Local data
6568
* ----------
@@ -69,8 +72,8 @@ static int pgStatPipe[2];
6972
static struct sockaddr_in pgStatAddr;
7073
static int pgStatPmPipe[2] = {-1, -1};
7174

72-
static int pgStatRunning = 0;
7375
static int pgStatPid;
76+
static time_t last_pgstat_start_time;
7477

7578
static long pgStatNumMessages = 0;
7679

@@ -130,14 +133,12 @@ static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
130133
* pgstat_init() -
131134
*
132135
* Called from postmaster at startup. Create the resources required
133-
* by the statistics collector process.
134-
*
135-
* NOTE: failure exit from this routine causes the postmaster to abort.
136-
* This is unfriendly and should not be done except in dire straits.
137-
* Better to let the postmaster start with stats collection disabled.
136+
* by the statistics collector process. If unable to do so, do not
137+
* fail --- better to let the postmaster start with stats collection
138+
* disabled.
138139
* ----------
139140
*/
140-
int
141+
void
141142
pgstat_init(void)
142143
{
143144
int alen;
@@ -168,7 +169,7 @@ pgstat_init(void)
168169
* Nothing else required if collector will not get started
169170
*/
170171
if (!pgstat_collect_startcollector)
171-
return 0;
172+
return;
172173

173174
/*
174175
* Create the UDP socket for sending and receiving statistic messages
@@ -231,51 +232,70 @@ pgstat_init(void)
231232
goto startup_failed;
232233
}
233234

234-
return 0;
235+
return;
235236

236237
startup_failed:
237238
if (pgStatSock >= 0)
238239
closesocket(pgStatSock);
239240
pgStatSock = -1;
240241

241242
/* Adjust GUC variables to suppress useless activity */
243+
pgstat_collect_startcollector = false;
242244
pgstat_collect_querystring = false;
243245
pgstat_collect_tuplelevel = false;
244246
pgstat_collect_blocklevel = false;
245-
246-
return 0;
247247
}
248248

249249

250250
/* ----------
251251
* pgstat_start() -
252252
*
253253
* Called from postmaster at startup or after an existing collector
254-
* died. Fire up a fresh statistics collector.
254+
* died. Attempt to fire up a fresh statistics collector.
255255
*
256-
* NOTE: failure exit from this routine causes the postmaster to abort.
256+
* Note: if fail, we will be called again from the postmaster main loop.
257257
* ----------
258258
*/
259-
int
259+
void
260260
pgstat_start(void)
261261
{
262+
time_t curtime;
263+
262264
/*
263265
* Do nothing if no collector needed
264266
*/
265-
if (!pgstat_collect_startcollector)
266-
return 0;
267+
if (pgstat_is_running || !pgstat_collect_startcollector)
268+
return;
269+
270+
/*
271+
* Do nothing if too soon since last collector start. This is a
272+
* safety valve to protect against continuous respawn attempts if
273+
* the collector is dying immediately at launch. Note that since
274+
* we will be re-called from the postmaster main loop, we will get
275+
* another chance later.
276+
*/
277+
curtime = time(NULL);
278+
if ((unsigned int) (curtime - last_pgstat_start_time) <
279+
(unsigned int) PGSTAT_RESTART_INTERVAL)
280+
return;
281+
last_pgstat_start_time = curtime;
267282

268283
/*
269-
* Check that the socket is there, else pgstat_init failed
284+
* Check that the socket is there, else pgstat_init failed.
270285
*/
271286
if (pgStatSock < 0)
272287
{
273288
elog(LOG, "PGSTAT: statistics collector startup skipped");
274-
return 0;
289+
/*
290+
* We can only get here if someone tries to manually turn
291+
* pgstat_collect_startcollector on after it had been off.
292+
*/
293+
pgstat_collect_startcollector = false;
294+
return;
275295
}
276296

277297
/*
278-
* Then fork off the collector. Remember its PID for pgstat_ispgstat.
298+
* Okay, fork off the collector. Remember its PID for pgstat_ispgstat.
279299
*/
280300

281301
fflush(stdout);
@@ -294,15 +314,14 @@ pgstat_start(void)
294314
beos_backend_startup_failed();
295315
#endif
296316
elog(LOG, "PGSTAT: fork() failed: %m");
297-
pgStatRunning = 0;
298-
return 0;
317+
return;
299318

300319
case 0:
301320
break;
302321

303322
default:
304-
pgStatRunning = 1;
305-
return 0;
323+
pgstat_is_running = true;
324+
return;
306325
}
307326

308327
/* in postmaster child ... */
@@ -335,16 +354,19 @@ pgstat_start(void)
335354
* was the statistics collector.
336355
* ----------
337356
*/
338-
int
357+
bool
339358
pgstat_ispgstat(int pid)
340359
{
341-
if (pgStatRunning == 0)
342-
return 0;
360+
if (!pgstat_is_running)
361+
return false;
343362

344363
if (pgStatPid != pid)
345-
return 0;
364+
return false;
365+
366+
/* Oh dear ... */
367+
pgstat_is_running = false;
346368

347-
return 1;
369+
return true;
348370
}
349371

350372

src/backend/postmaster/postmaster.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.314 2003/04/22 00:08:06 tgl Exp $
40+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.315 2003/04/26 02:57:14 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -790,12 +790,10 @@ PostmasterMain(int argc, char *argv[])
790790
}
791791

792792
/*
793-
* Initialize and startup the statistics collector process
793+
* Initialize and try to startup the statistics collector process
794794
*/
795-
if (pgstat_init() < 0)
796-
ExitPostmaster(1);
797-
if (pgstat_start() < 0)
798-
ExitPostmaster(1);
795+
pgstat_init();
796+
pgstat_start();
799797

800798
/*
801799
* Load cached files for client authentication.
@@ -1058,6 +1056,10 @@ ServerLoop(void)
10581056
ConnFree(port);
10591057
}
10601058
}
1059+
1060+
/* If we have lost the stats collector, try to start a new one */
1061+
if (!pgstat_is_running)
1062+
pgstat_start();
10611063
}
10621064
}
10631065

@@ -1720,8 +1722,9 @@ reaper(SIGNAL_ARGS)
17201722
#endif
17211723

17221724
/*
1723-
* Check if this child was the statistics collector. If so, start
1724-
* a new one.
1725+
* Check if this child was the statistics collector. If so,
1726+
* try to start a new one. (If fail, we'll try again in
1727+
* future cycles of the main loop.)
17251728
*/
17261729
if (pgstat_ispgstat(pid))
17271730
{

src/include/pgstat.h

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
*
44
* Definitions for the PostgreSQL statistics collector daemon.
55
*
6-
* Copyright (c) 2001, PostgreSQL Global Development Group
6+
* Copyright (c) 2001-2003, PostgreSQL Global Development Group
77
*
8-
* $Id: pgstat.h,v 1.13 2003/03/20 03:34:56 momjian Exp $
8+
* $Id: pgstat.h,v 1.14 2003/04/26 02:57:14 tgl Exp $
99
* ----------
1010
*/
1111
#ifndef PGSTAT_H
1212
#define PGSTAT_H
1313

14+
#include "utils/hsearch.h"
1415
#include "utils/nabstime.h"
16+
#include "utils/rel.h"
17+
1518

1619
/* ----------
1720
* Paths for the statistics files. The %s is replaced with the
@@ -26,16 +29,17 @@
2629
* ----------
2730
*/
2831
#define PGSTAT_STAT_INTERVAL 500 /* How often to write the status */
29-
/* file, in milliseconds. */
32+
/* file; in milliseconds. */
3033

3134
#define PGSTAT_DESTROY_DELAY 10000 /* How long to keep destroyed */
32-
/* objects known to give delayed */
33-
/* UDP packets time to arrive, */
35+
/* objects known, to give delayed */
36+
/* UDP packets time to arrive; */
3437
/* in milliseconds. */
3538

36-
#define PGSTAT_DESTROY_COUNT (PGSTAT_DESTROY_DELAY \
37-
/ PGSTAT_STAT_INTERVAL)
39+
#define PGSTAT_DESTROY_COUNT (PGSTAT_DESTROY_DELAY / PGSTAT_STAT_INTERVAL)
3840

41+
#define PGSTAT_RESTART_INTERVAL 60 /* How often to attempt to restart */
42+
/* a failed statistics collector; in seconds. */
3943

4044
/* ----------
4145
* How much of the actual query string to send to the collector.
@@ -323,7 +327,7 @@ typedef union PgStat_Msg
323327

324328

325329
/* ----------
326-
* Global variables
330+
* GUC parameters
327331
* ----------
328332
*/
329333
extern bool pgstat_collect_startcollector;
@@ -332,13 +336,19 @@ extern bool pgstat_collect_querystring;
332336
extern bool pgstat_collect_tuplelevel;
333337
extern bool pgstat_collect_blocklevel;
334338

339+
/* ----------
340+
* Other global variables
341+
* ----------
342+
*/
343+
extern bool pgstat_is_running;
344+
335345
/* ----------
336346
* Functions called from postmaster
337347
* ----------
338348
*/
339-
extern int pgstat_init(void);
340-
extern int pgstat_start(void);
341-
extern int pgstat_ispgstat(int pid);
349+
extern void pgstat_init(void);
350+
extern void pgstat_start(void);
351+
extern bool pgstat_ispgstat(int pid);
342352
extern void pgstat_close_sockets(void);
343353
extern void pgstat_beterm(int pid);
344354

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