summaryrefslogtreecommitdiff
path: root/src/include/pgstat.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/pgstat.h')
-rw-r--r--src/include/pgstat.h266
1 files changed, 124 insertions, 142 deletions
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 0e4094bf3e9..83a45d7217f 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -5,7 +5,7 @@
*
* Copyright (c) 2001-2006, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/include/pgstat.h,v 1.46 2006/05/30 02:35:39 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/pgstat.h,v 1.47 2006/06/19 01:51:21 tgl Exp $
* ----------
*/
#ifndef PGSTAT_H
@@ -16,16 +16,14 @@
#include "utils/rel.h"
#include "utils/timestamp.h"
+
/* ----------
- * The types of backend/postmaster -> collector messages
+ * The types of backend -> collector messages
* ----------
*/
typedef enum StatMsgType
{
PGSTAT_MTYPE_DUMMY,
- PGSTAT_MTYPE_BESTART,
- PGSTAT_MTYPE_BETERM,
- PGSTAT_MTYPE_ACTIVITY,
PGSTAT_MTYPE_TABSTAT,
PGSTAT_MTYPE_TABPURGE,
PGSTAT_MTYPE_DROPDB,
@@ -56,8 +54,6 @@ typedef struct PgStat_MsgHdr
{
StatMsgType m_type;
int m_size;
- int m_backendid;
- int m_procpid;
} PgStat_MsgHdr;
/* ----------
@@ -68,6 +64,17 @@ typedef struct PgStat_MsgHdr
*/
#define PGSTAT_MSG_PAYLOAD (1000 - sizeof(PgStat_MsgHdr))
+
+/* ----------
+ * PgStat_MsgDummy A dummy message, ignored by the collector
+ * ----------
+ */
+typedef struct PgStat_MsgDummy
+{
+ PgStat_MsgHdr m_hdr;
+} PgStat_MsgDummy;
+
+
/* ----------
* PgStat_TableEntry Per-table info in a MsgTabstat
*
@@ -96,37 +103,67 @@ typedef struct PgStat_TableEntry
PgStat_Counter t_blocks_hit;
} PgStat_TableEntry;
+/* ----------
+ * PgStat_MsgTabstat Sent by the backend to report table
+ * and buffer access statistics.
+ * ----------
+ */
+#define PGSTAT_NUM_TABENTRIES \
+ ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int)) \
+ / sizeof(PgStat_TableEntry))
+
+typedef struct PgStat_MsgTabstat
+{
+ PgStat_MsgHdr m_hdr;
+ Oid m_databaseid;
+ int m_nentries;
+ int m_xact_commit;
+ int m_xact_rollback;
+ PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
+} PgStat_MsgTabstat;
+
/* ----------
- * PgStat_MsgDummy A dummy message, ignored by the collector
+ * PgStat_MsgTabpurge Sent by the backend to tell the collector
+ * about dead tables.
* ----------
*/
-typedef struct PgStat_MsgDummy
+#define PGSTAT_NUM_TABPURGE \
+ ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
+ / sizeof(Oid))
+
+typedef struct PgStat_MsgTabpurge
{
PgStat_MsgHdr m_hdr;
- char m_dummy[512];
-} PgStat_MsgDummy;
+ Oid m_databaseid;
+ int m_nentries;
+ Oid m_tableid[PGSTAT_NUM_TABPURGE];
+} PgStat_MsgTabpurge;
+
/* ----------
- * PgStat_MsgBestart Sent by the backend on startup
+ * PgStat_MsgDropdb Sent by the backend to tell the collector
+ * about a dropped database
* ----------
*/
-typedef struct PgStat_MsgBestart
+typedef struct PgStat_MsgDropdb
{
PgStat_MsgHdr m_hdr;
Oid m_databaseid;
- Oid m_userid;
- SockAddr m_clientaddr;
-} PgStat_MsgBestart;
+} PgStat_MsgDropdb;
+
/* ----------
- * PgStat_MsgBeterm Sent by the postmaster after backend exit
+ * PgStat_MsgResetcounter Sent by the backend to tell the collector
+ * to reset counters
* ----------
*/
-typedef struct PgStat_MsgBeterm
+typedef struct PgStat_MsgResetcounter
{
PgStat_MsgHdr m_hdr;
-} PgStat_MsgBeterm;
+ Oid m_databaseid;
+} PgStat_MsgResetcounter;
+
/* ----------
* PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
@@ -140,6 +177,7 @@ typedef struct PgStat_MsgAutovacStart
TimestampTz m_start_time;
} PgStat_MsgAutovacStart;
+
/* ----------
* PgStat_MsgVacuum Sent by the backend or autovacuum daemon
* after VACUUM or VACUUM ANALYZE
@@ -156,6 +194,7 @@ typedef struct PgStat_MsgVacuum
PgStat_Counter m_tuples;
} PgStat_MsgVacuum;
+
/* ----------
* PgStat_MsgAnalyze Sent by the backend or autovacuum daemon
* after ANALYZE
@@ -174,80 +213,6 @@ typedef struct PgStat_MsgAnalyze
/* ----------
- * PgStat_MsgActivity Sent by the backends when they start
- * to parse a query.
- * ----------
- */
-#define PGSTAT_ACTIVITY_SIZE PGSTAT_MSG_PAYLOAD
-
-typedef struct PgStat_MsgActivity
-{
- PgStat_MsgHdr m_hdr;
- char m_cmd_str[PGSTAT_ACTIVITY_SIZE];
-} PgStat_MsgActivity;
-
-/* ----------
- * PgStat_MsgTabstat Sent by the backend to report table
- * and buffer access statistics.
- * ----------
- */
-#define PGSTAT_NUM_TABENTRIES \
- ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int)) \
- / sizeof(PgStat_TableEntry))
-
-typedef struct PgStat_MsgTabstat
-{
- PgStat_MsgHdr m_hdr;
- Oid m_databaseid;
- int m_nentries;
- int m_xact_commit;
- int m_xact_rollback;
- PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
-} PgStat_MsgTabstat;
-
-/* ----------
- * PgStat_MsgTabpurge Sent by the backend to tell the collector
- * about dead tables.
- * ----------
- */
-#define PGSTAT_NUM_TABPURGE \
- ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
- / sizeof(Oid))
-
-typedef struct PgStat_MsgTabpurge
-{
- PgStat_MsgHdr m_hdr;
- Oid m_databaseid;
- int m_nentries;
- Oid m_tableid[PGSTAT_NUM_TABPURGE];
-} PgStat_MsgTabpurge;
-
-
-/* ----------
- * PgStat_MsgDropdb Sent by the backend to tell the collector
- * about a dropped database
- * ----------
- */
-typedef struct PgStat_MsgDropdb
-{
- PgStat_MsgHdr m_hdr;
- Oid m_databaseid;
-} PgStat_MsgDropdb;
-
-
-/* ----------
- * PgStat_MsgResetcounter Sent by the backend to tell the collector
- * to reset counters
- * ----------
- */
-typedef struct PgStat_MsgResetcounter
-{
- PgStat_MsgHdr m_hdr;
- Oid m_databaseid;
-} PgStat_MsgResetcounter;
-
-
-/* ----------
* PgStat_Msg Union over all possible messages.
* ----------
*/
@@ -255,8 +220,6 @@ typedef union PgStat_Msg
{
PgStat_MsgHdr msg_hdr;
PgStat_MsgDummy msg_dummy;
- PgStat_MsgBestart msg_bestart;
- PgStat_MsgActivity msg_activity;
PgStat_MsgTabstat msg_tabstat;
PgStat_MsgTabpurge msg_tabpurge;
PgStat_MsgDropdb msg_dropdb;
@@ -275,19 +238,15 @@ typedef union PgStat_Msg
* ------------------------------------------------------------
*/
-#define PGSTAT_FILE_FORMAT_ID 0x01A5BC95
+#define PGSTAT_FILE_FORMAT_ID 0x01A5BC96
/* ----------
* PgStat_StatDBEntry The collector's data per database
- *
- * Note: n_backends is not maintained within the collector. It's computed
- * when a backend reads the stats file for use.
* ----------
*/
typedef struct PgStat_StatDBEntry
{
Oid databaseid;
- int n_backends;
PgStat_Counter n_xact_commit;
PgStat_Counter n_xact_rollback;
PgStat_Counter n_blocks_fetched;
@@ -303,45 +262,12 @@ typedef struct PgStat_StatDBEntry
/* ----------
- * PgStat_StatBeEntry The collector's data per backend
- * ----------
- */
-typedef struct PgStat_StatBeEntry
-{
- /* An entry is non-empty iff procpid > 0 */
- int procpid;
- TimestampTz start_timestamp;
- TimestampTz activity_start_timestamp;
-
- /*
- * These fields are initialized by the BESTART message. If we have
- * received messages from a backend before we have received its BESTART,
- * these fields will be uninitialized: userid and databaseid will be
- * InvalidOid, and clientaddr will be undefined.
- */
- Oid userid;
- Oid databaseid;
- SockAddr clientaddr;
-
- /*
- * activity[] must be last in the struct, because we only write as much
- * of it as needed to the stats file.
- */
- char activity[PGSTAT_ACTIVITY_SIZE];
-} PgStat_StatBeEntry;
-
-
-/* ----------
* PgStat_StatTabEntry The collector's data per table (or index)
* ----------
*/
typedef struct PgStat_StatTabEntry
{
Oid tableid;
- TimestampTz vacuum_timestamp; /* user initiated vacuum */
- TimestampTz autovac_vacuum_timestamp; /* autovacuum initiated */
- TimestampTz analyze_timestamp; /* user initiated */
- TimestampTz autovac_analyze_timestamp; /* autovacuum initiated */
PgStat_Counter numscans;
@@ -358,27 +284,81 @@ typedef struct PgStat_StatTabEntry
PgStat_Counter blocks_fetched;
PgStat_Counter blocks_hit;
+
+ TimestampTz vacuum_timestamp; /* user initiated vacuum */
+ TimestampTz autovac_vacuum_timestamp; /* autovacuum initiated */
+ TimestampTz analyze_timestamp; /* user initiated */
+ TimestampTz autovac_analyze_timestamp; /* autovacuum initiated */
} PgStat_StatTabEntry;
/* ----------
+ * Shared-memory data structures
+ * ----------
+ */
+
+/* Max length of st_activity string ... perhaps replace with a GUC var? */
+#define PGBE_ACTIVITY_SIZE 1024
+
+/* ----------
+ * PgBackendStatus
+ *
+ * Each live backend maintains a PgBackendStatus struct in shared memory
+ * showing its current activity. (The structs are allocated according to
+ * BackendId, but that is not critical.) Note that the collector process
+ * has no involvement in, or even access to, these structs.
+ * ----------
+ */
+typedef struct PgBackendStatus
+{
+ /*
+ * To avoid locking overhead, we use the following protocol: a backend
+ * increments st_changecount before modifying its entry, and again after
+ * finishing a modification. A would-be reader should note the value
+ * of st_changecount, copy the entry into private memory, then check
+ * st_changecount again. If the value hasn't changed, and if it's even,
+ * the copy is valid; otherwise start over. This makes updates cheap
+ * while reads are potentially expensive, but that's the tradeoff we want.
+ */
+ int st_changecount;
+
+ /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
+ int st_procpid;
+
+ /* Times of backend process start and current activity start */
+ TimestampTz st_proc_start_timestamp;
+ TimestampTz st_activity_start_timestamp;
+
+ /* Database OID, owning user's OID, connection client address */
+ Oid st_databaseid;
+ Oid st_userid;
+ SockAddr st_clientaddr;
+
+ /* current command string; MUST be null-terminated */
+ char st_activity[PGBE_ACTIVITY_SIZE];
+} PgBackendStatus;
+
+
+/* ----------
* GUC parameters
* ----------
*/
extern bool pgstat_collect_startcollector;
extern bool pgstat_collect_resetonpmstart;
-extern bool pgstat_collect_querystring;
extern bool pgstat_collect_tuplelevel;
extern bool pgstat_collect_blocklevel;
+extern bool pgstat_collect_querystring;
/* ----------
* Functions called from postmaster
* ----------
*/
+extern Size BackendStatusShmemSize(void);
+extern void CreateSharedBackendStatus(void);
+
extern void pgstat_init(void);
extern int pgstat_start(void);
-extern void pgstat_beterm(int pid);
extern void pgstat_reset_all(void);
#ifdef EXEC_BACKEND
@@ -391,21 +371,23 @@ extern void PgstatCollectorMain(int argc, char *argv[]);
* Functions called from backends
* ----------
*/
-extern void pgstat_bestart(void);
-
extern void pgstat_ping(void);
-extern void pgstat_report_activity(const char *what);
+
extern void pgstat_report_tabstat(void);
+extern void pgstat_vacuum_tabstat(void);
+extern void pgstat_drop_relation(Oid relid);
+
+extern void pgstat_reset_counters(void);
+
extern void pgstat_report_autovac(Oid dboid);
extern void pgstat_report_vacuum(Oid tableoid, bool shared,
bool analyze, PgStat_Counter tuples);
extern void pgstat_report_analyze(Oid tableoid, bool shared,
PgStat_Counter livetuples,
PgStat_Counter deadtuples);
-extern void pgstat_vacuum_tabstat(void);
-extern void pgstat_drop_relation(Oid relid);
-extern void pgstat_reset_counters(void);
+extern void pgstat_bestart(void);
+extern void pgstat_report_activity(const char *what);
extern void pgstat_initstats(PgStat_Info *stats, Relation rel);
@@ -492,7 +474,7 @@ extern void pgstat_count_xact_rollback(void);
*/
extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
-extern PgStat_StatBeEntry *pgstat_fetch_stat_beentry(int beid);
+extern PgBackendStatus *pgstat_fetch_stat_beentry(int beid);
extern int pgstat_fetch_stat_numbackends(void);
#endif /* PGSTAT_H */