1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-07 19:06:32 +03:00

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.
This commit is contained in:
Neil Conway
2006-12-06 18:06:48 +00:00
parent dd740e1fd0
commit 886a02d1cb
9 changed files with 77 additions and 21 deletions

View File

@@ -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().

View File

@@ -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,

View File

@@ -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() -

View File

@@ -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)
{