diff options
author | Neil Conway <neilc@samurai.com> | 2006-12-06 18:06:48 +0000 |
---|---|---|
committer | Neil Conway <neilc@samurai.com> | 2006-12-06 18:06:48 +0000 |
commit | 886a02d1cb19fe25859bd3c2d1f6a64b04cdc710 (patch) | |
tree | a0684de747e49ba23b5c7fead1052d378ecbb4dd /src/backend | |
parent | dd740e1fd066a5df628ba28dfdaee02d58dee0c5 (diff) | |
download | postgresql-886a02d1cb19fe25859bd3c2d1f6a64b04cdc710.tar.gz postgresql-886a02d1cb19fe25859bd3c2d1f6a64b04cdc710.zip |
Add a txn_start column to pg_stat_activity. This makes it easier to
identify long-running transactions. Since we already need to record
the transaction-start time (e.g. for now()), we don't need any
additional system calls to report this information.
Catversion bumped, initdb required.
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/access/transam/xact.c | 5 | ||||
-rw-r--r-- | src/backend/catalog/system_views.sql | 3 | ||||
-rw-r--r-- | src/backend/postmaster/pgstat.c | 26 | ||||
-rw-r--r-- | src/backend/utils/adt/pgstatfuncs.c | 26 |
4 files changed, 56 insertions, 4 deletions
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index 673a34ad034..b0b8970380e 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.229 2006/11/23 01:14:59 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.230 2006/12/06 18:06:47 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -1416,6 +1416,7 @@ StartTransaction(void) * GetCurrentTimestamp() call (which'd be expensive anyway). */ xactStartTimestamp = stmtStartTimestamp; + pgstat_report_txn_timestamp(xactStartTimestamp); /* * initialize current transaction state fields @@ -1628,6 +1629,7 @@ CommitTransaction(void) /* smgrcommit already done */ AtEOXact_Files(); pgstat_count_xact_commit(); + pgstat_report_txn_timestamp(0); CurrentResourceOwner = NULL; ResourceOwnerDelete(TopTransactionResourceOwner); @@ -1994,6 +1996,7 @@ AbortTransaction(void) smgrabort(); AtEOXact_Files(); pgstat_count_xact_rollback(); + pgstat_report_txn_timestamp(0); /* * State remains TRANS_ABORT until CleanupTransaction(). diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index 9dd19e8c5d2..e79b0f00fb7 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -3,7 +3,7 @@ * * Copyright (c) 1996-2006, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/backend/catalog/system_views.sql,v 1.32 2006/11/24 21:18:42 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/catalog/system_views.sql,v 1.33 2006/12/06 18:06:47 neilc Exp $ */ CREATE VIEW pg_roles AS @@ -335,6 +335,7 @@ CREATE VIEW pg_stat_activity AS U.rolname AS usename, pg_stat_get_backend_activity(S.backendid) AS current_query, pg_stat_get_backend_waiting(S.backendid) AS waiting, + pg_stat_get_backend_txn_start(S.backendid) AS txn_start, pg_stat_get_backend_activity_start(S.backendid) AS query_start, pg_stat_get_backend_start(S.backendid) AS backend_start, pg_stat_get_backend_client_addr(S.backendid) AS client_addr, diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 19a694caf30..ef9487ade03 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -13,7 +13,7 @@ * * Copyright (c) 2001-2006, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.140 2006/11/21 20:59:52 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.141 2006/12/06 18:06:47 neilc Exp $ * ---------- */ #include "postgres.h" @@ -1355,6 +1355,7 @@ pgstat_bestart(void) beentry->st_procpid = MyProcPid; beentry->st_proc_start_timestamp = proc_start_timestamp; beentry->st_activity_start_timestamp = 0; + beentry->st_txn_start_timestamp = 0; beentry->st_databaseid = MyDatabaseId; beentry->st_userid = userid; beentry->st_clientaddr = clientaddr; @@ -1443,6 +1444,29 @@ pgstat_report_activity(const char *cmd_str) Assert((beentry->st_changecount & 1) == 0); } +/* + * Set the current transaction start timestamp to the specified + * value. If there is no current active transaction, this is signified + * by 0. + */ +void +pgstat_report_txn_timestamp(TimestampTz tstamp) +{ + volatile PgBackendStatus *beentry = MyBEEntry; + + if (!pgstat_collect_querystring || !beentry) + return; + + /* + * Update my status entry, following the protocol of bumping + * st_changecount before and after. We use a volatile pointer + * here to ensure the compiler doesn't try to get cute. + */ + beentry->st_changecount++; + beentry->st_txn_start_timestamp = tstamp; + beentry->st_changecount++; + Assert((beentry->st_changecount & 1) == 0); +} /* ---------- * pgstat_report_waiting() - diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index e2d302b5a64..18709a981d4 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.34 2006/10/04 00:29:59 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.35 2006/12/06 18:06:47 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -44,6 +44,7 @@ extern Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS); extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS); extern Datum pg_stat_get_backend_waiting(PG_FUNCTION_ARGS); extern Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS); +extern Datum pg_stat_get_backend_txn_start(PG_FUNCTION_ARGS); extern Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS); extern Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS); extern Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS); @@ -422,6 +423,29 @@ pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS) PG_RETURN_TIMESTAMPTZ(result); } + +Datum +pg_stat_get_backend_txn_start(PG_FUNCTION_ARGS) +{ + int32 beid = PG_GETARG_INT32(0); + TimestampTz result; + PgBackendStatus *beentry; + + if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL) + PG_RETURN_NULL(); + + if (!superuser() && beentry->st_userid != GetUserId()) + PG_RETURN_NULL(); + + result = beentry->st_txn_start_timestamp; + + if (result == 0) /* not in a transaction */ + PG_RETURN_NULL(); + + PG_RETURN_TIMESTAMPTZ(result); +} + + Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS) { |