Skip to content

Commit 8d675c8

Browse files
committed
pgstat's on-proc-exit hook has to execute after the last transaction commit
or abort within a backend; rearrange InitPostgres processing to make it so. Revealed by just-added Asserts along with ECPG regression tests (hm, I wonder why the core regression tests didn't expose it?). This possibly is another reason for missing stats updates ...
1 parent 77947c5 commit 8d675c8

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright (c) 2001-2007, PostgreSQL Global Development Group
1515
*
16-
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.156 2007/05/27 03:50:39 tgl Exp $
16+
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.157 2007/05/27 05:37:49 tgl Exp $
1717
* ----------
1818
*/
1919
#include "postgres.h"
@@ -1771,28 +1771,45 @@ CreateSharedBackendStatus(void)
17711771
}
17721772

17731773

1774+
/* ----------
1775+
* pgstat_initialize() -
1776+
*
1777+
* Initialize pgstats state, and set up our on-proc-exit hook.
1778+
* Called from InitPostgres. MyBackendId must be set,
1779+
* but we must not have started any transaction yet (since the
1780+
* exit hook must run after the last transaction exit).
1781+
* ----------
1782+
*/
1783+
void
1784+
pgstat_initialize(void)
1785+
{
1786+
/* Initialize MyBEEntry */
1787+
Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
1788+
MyBEEntry = &BackendStatusArray[MyBackendId - 1];
1789+
1790+
/* Set up a process-exit hook to clean up */
1791+
on_shmem_exit(pgstat_beshutdown_hook, 0);
1792+
}
1793+
17741794
/* ----------
17751795
* pgstat_bestart() -
17761796
*
1777-
* Initialize this backend's entry in the PgBackendStatus array,
1778-
* and set up an on-proc-exit hook that will clear it again.
1779-
* Called from InitPostgres. MyBackendId and MyDatabaseId must be set.
1797+
* Initialize this backend's entry in the PgBackendStatus array.
1798+
* Called from InitPostgres. MyDatabaseId and session userid must be set
1799+
* (hence, this cannot be combined with pgstat_initialize).
17801800
* ----------
17811801
*/
17821802
void
17831803
pgstat_bestart(void)
17841804
{
1785-
volatile PgBackendStatus *beentry;
17861805
TimestampTz proc_start_timestamp;
17871806
Oid userid;
17881807
SockAddr clientaddr;
1789-
1790-
Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
1791-
MyBEEntry = &BackendStatusArray[MyBackendId - 1];
1808+
volatile PgBackendStatus *beentry;
17921809

17931810
/*
1794-
* To minimize the time spent modifying the entry, fetch all the needed
1795-
* data first.
1811+
* To minimize the time spent modifying the PgBackendStatus entry,
1812+
* fetch all the needed data first.
17961813
*
17971814
* If we have a MyProcPort, use its session start time (for consistency,
17981815
* and to save a kernel call).
@@ -1839,11 +1856,6 @@ pgstat_bestart(void)
18391856

18401857
beentry->st_changecount++;
18411858
Assert((beentry->st_changecount & 1) == 0);
1842-
1843-
/*
1844-
* Set up a process-exit hook to clean up.
1845-
*/
1846-
on_shmem_exit(pgstat_beshutdown_hook, 0);
18471859
}
18481860

18491861
/*

src/backend/utils/init/postinit.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.175 2007/03/13 00:33:42 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.176 2007/05/27 05:37:49 tgl Exp $
1212
*
1313
*
1414
*-------------------------------------------------------------------------
@@ -435,6 +435,10 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
435435
/* Initialize portal manager */
436436
EnablePortalManager();
437437

438+
/* Initialize stats collection --- must happen before first xact */
439+
if (!bootstrap)
440+
pgstat_initialize();
441+
438442
/*
439443
* Set up process-exit callback to do pre-shutdown cleanup. This has to
440444
* be after we've initialized all the low-level modules like the buffer
@@ -587,7 +591,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
587591
/* initialize client encoding */
588592
InitializeClientEncoding();
589593

590-
/* initialize statistics collection for this backend */
594+
/* report this backend in the PgBackendStatus array */
591595
if (!bootstrap)
592596
pgstat_bestart();
593597

src/include/pgstat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 2001-2007, PostgreSQL Global Development Group
77
*
8-
* $PostgreSQL: pgsql/src/include/pgstat.h,v 1.59 2007/05/27 03:50:39 tgl Exp $
8+
* $PostgreSQL: pgsql/src/include/pgstat.h,v 1.60 2007/05/27 05:37:50 tgl Exp $
99
* ----------
1010
*/
1111
#ifndef PGSTAT_H
@@ -501,7 +501,9 @@ extern void pgstat_report_analyze(Oid tableoid, bool shared,
501501
PgStat_Counter livetuples,
502502
PgStat_Counter deadtuples);
503503

504+
extern void pgstat_initialize(void);
504505
extern void pgstat_bestart(void);
506+
505507
extern void pgstat_report_activity(const char *what);
506508
extern void pgstat_report_txn_timestamp(TimestampTz tstamp);
507509
extern void pgstat_report_waiting(bool waiting);

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