2004-10-01 23:03:42 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pgstatfuncs.c
|
|
|
|
* Functions for accessing the statistics collector data
|
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2004-10-01 23:03:42 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2006-05-19 17:15:37 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.28 2006/05/19 15:15:37 alvherre Exp $
|
2004-10-01 23:03:42 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2001-06-22 21:18:36 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/xact.h"
|
2004-10-01 23:03:42 +02:00
|
|
|
#include "fmgr.h"
|
|
|
|
#include "funcapi.h"
|
|
|
|
#include "miscadmin.h"
|
2001-06-22 21:18:36 +02:00
|
|
|
#include "nodes/execnodes.h"
|
|
|
|
#include "pgstat.h"
|
2004-10-01 23:03:42 +02:00
|
|
|
#include "utils/hsearch.h"
|
2005-05-09 13:31:34 +02:00
|
|
|
#include "utils/inet.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "libpq/ip.h"
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2004-10-01 23:03:42 +02:00
|
|
|
/* bogus ... these externs should be in a header file */
|
2001-10-25 07:50:21 +02:00
|
|
|
extern Datum pg_stat_get_numscans(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_tuples_returned(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_tuples_updated(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_blocks_hit(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
extern Datum pg_stat_get_backend_idset(PG_FUNCTION_ARGS);
|
2002-08-04 21:51:30 +02:00
|
|
|
extern Datum pg_backend_pid(PG_FUNCTION_ARGS);
|
2002-08-20 06:47:52 +02:00
|
|
|
extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
|
2001-10-25 07:50:21 +02:00
|
|
|
extern Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS);
|
2003-03-20 04:34:57 +01:00
|
|
|
extern Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS);
|
2005-05-09 13:31:34 +02:00
|
|
|
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);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
|
|
|
extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_numscans(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
Oid relid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
relid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (tabentry->numscans);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
Oid relid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
relid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (tabentry->tuples_returned);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
Oid relid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
relid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (tabentry->tuples_fetched);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
Oid relid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
relid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (tabentry->tuples_inserted);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
Oid relid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
relid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (tabentry->tuples_updated);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
Oid relid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
relid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (tabentry->tuples_deleted);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
Oid relid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
relid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (tabentry->blocks_fetched);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
Oid relid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
relid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (tabentry->blocks_hit);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-10-01 23:03:42 +02:00
|
|
|
FuncCallContext *funcctx;
|
|
|
|
int *fctx;
|
2001-10-25 07:50:21 +02:00
|
|
|
int32 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2004-10-01 23:03:42 +02:00
|
|
|
/* stuff done only on the first call of the function */
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
2001-06-22 21:18:36 +02:00
|
|
|
{
|
2004-10-01 23:03:42 +02:00
|
|
|
/* create a function context for cross-call persistence */
|
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
|
|
|
|
|
|
|
fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx,
|
|
|
|
2 * sizeof(int));
|
|
|
|
funcctx->user_fctx = fctx;
|
2003-07-27 06:53:12 +02:00
|
|
|
|
2004-10-01 23:03:42 +02:00
|
|
|
fctx[0] = 0;
|
|
|
|
fctx[1] = pgstat_fetch_stat_numbackends();
|
2001-06-22 21:18:36 +02:00
|
|
|
}
|
|
|
|
|
2004-10-01 23:03:42 +02:00
|
|
|
/* stuff done on every call of the function */
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
|
|
|
fctx = funcctx->user_fctx;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2004-10-01 23:03:42 +02:00
|
|
|
fctx[0] += 1;
|
|
|
|
result = fctx[0];
|
|
|
|
|
|
|
|
if (result <= fctx[1])
|
2001-06-22 21:18:36 +02:00
|
|
|
{
|
2004-10-01 23:03:42 +02:00
|
|
|
/* do when there is more left to send */
|
|
|
|
SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* do when there is no more left */
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
2001-06-22 21:18:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-31 02:40:40 +02:00
|
|
|
Datum
|
2002-08-04 21:51:30 +02:00
|
|
|
pg_backend_pid(PG_FUNCTION_ARGS)
|
2002-07-31 02:40:40 +02:00
|
|
|
{
|
|
|
|
PG_RETURN_INT32(MyProcPid);
|
|
|
|
}
|
|
|
|
|
2002-08-20 06:47:52 +02:00
|
|
|
/*
|
|
|
|
* Built-in function for resetting the counters
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_stat_reset(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
pgstat_reset_counters();
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
}
|
2002-07-31 02:40:40 +02:00
|
|
|
|
2001-06-22 21:18:36 +02:00
|
|
|
Datum
|
|
|
|
pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatBeEntry *beentry;
|
|
|
|
int32 beid;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
beid = PG_GETARG_INT32(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
PG_RETURN_INT32(beentry->procpid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatBeEntry *beentry;
|
|
|
|
int32 beid;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
beid = PG_GETARG_INT32(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-05-11 03:41:41 +02:00
|
|
|
/* Not initialized yet? */
|
|
|
|
if (!OidIsValid(beentry->databaseid))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2001-06-22 21:18:36 +02:00
|
|
|
PG_RETURN_OID(beentry->databaseid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatBeEntry *beentry;
|
|
|
|
int32 beid;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
beid = PG_GETARG_INT32(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-05-11 03:41:41 +02:00
|
|
|
/* Not initialized yet? */
|
|
|
|
if (!OidIsValid(beentry->userid))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
PG_RETURN_OID(beentry->userid);
|
2001-06-22 21:18:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatBeEntry *beentry;
|
|
|
|
int32 beid;
|
|
|
|
int len;
|
2004-02-12 02:44:22 +01:00
|
|
|
char *activity;
|
2001-10-25 07:50:21 +02:00
|
|
|
text *result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
beid = PG_GETARG_INT32(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
2004-02-12 02:44:22 +01:00
|
|
|
activity = "<backend information not available>";
|
|
|
|
else if (!superuser() && beentry->userid != GetUserId())
|
|
|
|
activity = "<insufficient privilege>";
|
|
|
|
else if (*(beentry->activity) == '\0')
|
|
|
|
activity = "<command string not enabled>";
|
|
|
|
else
|
|
|
|
activity = beentry->activity;
|
2003-03-20 19:51:16 +01:00
|
|
|
|
2004-02-12 02:44:22 +01:00
|
|
|
len = strlen(activity);
|
2001-06-22 21:18:36 +02:00
|
|
|
result = palloc(VARHDRSZ + len);
|
|
|
|
VARATT_SIZEP(result) = VARHDRSZ + len;
|
2004-02-12 02:44:22 +01:00
|
|
|
memcpy(VARDATA(result), activity, len);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-20 04:34:57 +01:00
|
|
|
Datum
|
|
|
|
pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-06-30 00:51:57 +02:00
|
|
|
int32 beid = PG_GETARG_INT32(0);
|
2003-08-04 02:43:34 +02:00
|
|
|
TimestampTz result;
|
2005-06-30 00:51:57 +02:00
|
|
|
PgStat_StatBeEntry *beentry;
|
2003-03-20 04:34:57 +01:00
|
|
|
|
2003-04-04 05:03:54 +02:00
|
|
|
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
2003-03-20 04:34:57 +01:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2003-04-04 05:03:54 +02:00
|
|
|
if (!superuser() && beentry->userid != GetUserId())
|
2003-03-20 04:34:57 +01:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-06-30 00:51:57 +02:00
|
|
|
result = beentry->activity_start_timestamp;
|
2003-03-20 04:34:57 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* No time recorded for start of current query -- this is the case if the
|
|
|
|
* user hasn't enabled query-level stats collection.
|
2003-03-20 04:34:57 +01:00
|
|
|
*/
|
2005-06-30 00:51:57 +02:00
|
|
|
if (result == 0)
|
2003-03-20 04:34:57 +01:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2003-04-04 05:03:54 +02:00
|
|
|
PG_RETURN_TIMESTAMPTZ(result);
|
2003-03-20 04:34:57 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 13:31:34 +02:00
|
|
|
Datum
|
|
|
|
pg_stat_get_backend_start(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
int32 beid = PG_GETARG_INT32(0);
|
2005-05-09 13:31:34 +02:00
|
|
|
TimestampTz result;
|
2005-06-30 00:51:57 +02:00
|
|
|
PgStat_StatBeEntry *beentry;
|
2005-05-09 13:31:34 +02:00
|
|
|
|
|
|
|
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
if (!superuser() && beentry->userid != GetUserId())
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-06-30 00:51:57 +02:00
|
|
|
result = beentry->start_timestamp;
|
2005-05-09 13:31:34 +02:00
|
|
|
|
2005-06-30 00:51:57 +02:00
|
|
|
if (result == 0) /* probably can't happen? */
|
2005-05-09 13:31:34 +02:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
PG_RETURN_TIMESTAMPTZ(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PgStat_StatBeEntry *beentry;
|
2006-05-19 17:15:37 +02:00
|
|
|
SockAddr zero_clientaddr;
|
2005-10-15 04:49:52 +02:00
|
|
|
int32 beid;
|
2005-05-09 13:31:34 +02:00
|
|
|
char remote_host[NI_MAXHOST];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
beid = PG_GETARG_INT32(0);
|
|
|
|
|
|
|
|
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-05-11 03:41:41 +02:00
|
|
|
/* Not initialized yet? */
|
|
|
|
if (!OidIsValid(beentry->userid))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-05-09 13:31:34 +02:00
|
|
|
if (!superuser() && beentry->userid != GetUserId())
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2006-05-19 17:15:37 +02:00
|
|
|
/* A zeroed client addr means we don't know */
|
|
|
|
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
|
|
|
|
if (memcmp(&(beentry->clientaddr), &zero_clientaddr,
|
|
|
|
sizeof(zero_clientaddr) == 0))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-05-09 13:31:34 +02:00
|
|
|
switch (beentry->clientaddr.addr.ss_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
|
|
|
remote_host[0] = '\0';
|
2005-10-17 18:24:20 +02:00
|
|
|
ret = pg_getnameinfo_all(&beentry->clientaddr.addr, beentry->clientaddr.salen,
|
|
|
|
remote_host, sizeof(remote_host),
|
|
|
|
NULL, 0,
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
2005-05-09 13:31:34 +02:00
|
|
|
if (ret)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
|
|
|
|
CStringGetDatum(remote_host)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PgStat_StatBeEntry *beentry;
|
2006-05-19 17:15:37 +02:00
|
|
|
SockAddr zero_clientaddr;
|
2005-10-15 04:49:52 +02:00
|
|
|
int32 beid;
|
2005-05-09 13:31:34 +02:00
|
|
|
char remote_port[NI_MAXSERV];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
beid = PG_GETARG_INT32(0);
|
|
|
|
|
|
|
|
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-05-11 03:41:41 +02:00
|
|
|
/* Not initialized yet? */
|
|
|
|
if (!OidIsValid(beentry->userid))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-05-09 13:31:34 +02:00
|
|
|
if (!superuser() && beentry->userid != GetUserId())
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2006-05-19 17:15:37 +02:00
|
|
|
/* A zeroed client addr means we don't know */
|
|
|
|
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
|
|
|
|
if (memcmp(&(beentry->clientaddr), &zero_clientaddr,
|
|
|
|
sizeof(zero_clientaddr) == 0))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2005-05-09 13:31:34 +02:00
|
|
|
switch (beentry->clientaddr.addr.ss_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case AF_UNIX:
|
|
|
|
PG_RETURN_INT32(-1);
|
|
|
|
default:
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
|
|
|
remote_port[0] = '\0';
|
2005-10-17 18:24:20 +02:00
|
|
|
ret = pg_getnameinfo_all(&beentry->clientaddr.addr,
|
|
|
|
beentry->clientaddr.salen,
|
|
|
|
NULL, 0,
|
|
|
|
remote_port, sizeof(remote_port),
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
2005-05-09 13:31:34 +02:00
|
|
|
if (ret)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall1(int4in, CStringGetDatum(remote_port)));
|
|
|
|
}
|
|
|
|
|
2003-03-20 04:34:57 +01:00
|
|
|
|
2001-06-22 21:18:36 +02:00
|
|
|
Datum
|
|
|
|
pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatDBEntry *dbentry;
|
|
|
|
Oid dbid;
|
|
|
|
int32 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
dbid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int32) (dbentry->n_backends);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT32(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatDBEntry *dbentry;
|
|
|
|
Oid dbid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
dbid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (dbentry->n_xact_commit);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatDBEntry *dbentry;
|
|
|
|
Oid dbid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
dbid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (dbentry->n_xact_rollback);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatDBEntry *dbentry;
|
|
|
|
Oid dbid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
dbid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (dbentry->n_blocks_fetched);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
PgStat_StatDBEntry *dbentry;
|
|
|
|
Oid dbid;
|
|
|
|
int64 result;
|
2001-06-22 21:18:36 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
dbid = PG_GETARG_OID(0);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
|
|
|
|
result = 0;
|
|
|
|
else
|
2001-10-25 07:50:21 +02:00
|
|
|
result = (int64) (dbentry->n_blocks_hit);
|
2001-06-22 21:18:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_INT64(result);
|
|
|
|
}
|