Skip to content

add isBackgroundWorker, databaseId and roleId to pg_wait_sampling_current view #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions collector.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ probe_waits(History *observations, HTAB *profile_hash,
item.queryId = 0;

item.ts = ts;
item.isRegularBackend = !(proc->isBackgroundWorker);
item.databaseId = proc->databaseId;
item.roleId = proc->roleId;

/* Write to the history if needed */
if (write_history)
Expand Down
73 changes: 73 additions & 0 deletions pg_wait_sampling--1.1--1.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* contrib/pg_wait_sampling/pg_wait_sampling--1.0--1.1.sql */

DROP FUNCTION pg_wait_sampling_get_current (
pid int4,
OUT pid int4,
OUT event_type text,
OUT event text
) CASCADE;

DROP FUNCTION pg_wait_sampling_get_history (
OUT pid int4,
OUT ts timestamptz,
OUT event_type text,
OUT event text
) CASCADE;

DROP FUNCTION pg_wait_sampling_get_profile (
OUT pid int4,
OUT event_type text,
OUT event text,
OUT count bigint
) CASCADE;

CREATE FUNCTION pg_wait_sampling_get_current (
pid int4,
OUT pid int4,
OUT event_type text,
OUT event text,
OUT queryid int8,
OUT isregularbackend boolean,
OUT databaseid oid,
OUT roleid oid
)
RETURNS SETOF record
AS 'MODULE_PATHNAME'
LANGUAGE C VOLATILE CALLED ON NULL INPUT;

CREATE VIEW pg_wait_sampling_current AS
SELECT * FROM pg_wait_sampling_get_current(NULL::integer);

GRANT SELECT ON pg_wait_sampling_current TO PUBLIC;

CREATE FUNCTION pg_wait_sampling_get_history (
OUT pid int4,
OUT ts timestamptz,
OUT event_type text,
OUT event text,
OUT queryid int8
)
RETURNS SETOF record
AS 'MODULE_PATHNAME'
LANGUAGE C VOLATILE STRICT;

CREATE VIEW pg_wait_sampling_history AS
SELECT * FROM pg_wait_sampling_get_history();

GRANT SELECT ON pg_wait_sampling_history TO PUBLIC;

CREATE FUNCTION pg_wait_sampling_get_profile (
OUT pid int4,
OUT event_type text,
OUT event text,
OUT queryid int8,
OUT count int8
)
RETURNS SETOF record
AS 'MODULE_PATHNAME'
LANGUAGE C VOLATILE STRICT;

CREATE VIEW pg_wait_sampling_profile AS
SELECT * FROM pg_wait_sampling_get_profile();

GRANT SELECT ON pg_wait_sampling_profile TO PUBLIC;
66 changes: 66 additions & 0 deletions pg_wait_sampling--1.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* contrib/pg_wait_sampling/setup.sql */

-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pg_wait_sampling" to load this file. \quit

CREATE FUNCTION pg_wait_sampling_get_current (
pid int4,
OUT pid int4,
OUT event_type text,
OUT event text,
OUT queryid int8,
OUT isregularbackend boolean,
OUT databaseid oid,
OUT roleid oid
)
RETURNS SETOF record
AS 'MODULE_PATHNAME'
LANGUAGE C VOLATILE CALLED ON NULL INPUT;

CREATE VIEW pg_wait_sampling_current AS
SELECT * FROM pg_wait_sampling_get_current(NULL::integer);

GRANT SELECT ON pg_wait_sampling_current TO PUBLIC;

CREATE FUNCTION pg_wait_sampling_get_history (
OUT pid int4,
OUT ts timestamptz,
OUT event_type text,
OUT event text,
OUT queryid int8,
OUT isregularbackend boolean,
OUT databaseid oid,
OUT roleid oid
)
RETURNS SETOF record
AS 'MODULE_PATHNAME'
LANGUAGE C VOLATILE STRICT;

CREATE VIEW pg_wait_sampling_history AS
SELECT * FROM pg_wait_sampling_get_history();

GRANT SELECT ON pg_wait_sampling_history TO PUBLIC;

CREATE FUNCTION pg_wait_sampling_get_profile (
OUT pid int4,
OUT event_type text,
OUT event text,
OUT queryid int8,
OUT count int8
)
RETURNS SETOF record
AS 'MODULE_PATHNAME'
LANGUAGE C VOLATILE STRICT;

CREATE VIEW pg_wait_sampling_profile AS
SELECT * FROM pg_wait_sampling_get_profile();

GRANT SELECT ON pg_wait_sampling_profile TO PUBLIC;

CREATE FUNCTION pg_wait_sampling_reset_profile()
RETURNS void
AS 'MODULE_PATHNAME'
LANGUAGE C VOLATILE STRICT;

-- Don't want this to be available to non-superusers.
REVOKE ALL ON FUNCTION pg_wait_sampling_reset_profile() FROM PUBLIC;
37 changes: 31 additions & 6 deletions pg_wait_sampling.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS)
params->ts = GetCurrentTimestamp();

funcctx->user_fctx = params;
tupdesc = CreateTemplateTupleDesc(4);
tupdesc = CreateTemplateTupleDesc(7);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pid",
INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "type",
Expand All @@ -523,6 +523,12 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS)
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "queryid",
INT8OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "isregularbackend",
BOOLOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 6, "databaseid",
OIDOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 7, "roleid",
OIDOID, -1, 0);

funcctx->tuple_desc = BlessTupleDesc(tupdesc);

Expand All @@ -540,6 +546,9 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS)
item->pid = proc->pid;
item->wait_event_info = proc->wait_event_info;
item->queryId = pgws_proc_queryids[proc - ProcGlobal->allProcs];
item->isRegularBackend = !(proc->isBackgroundWorker);
item->databaseId = proc->databaseId;
item->roleId = proc->roleId;
funcctx->max_calls = 1;
}
else
Expand All @@ -562,6 +571,9 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS)
params->items[j].pid = proc->pid;
params->items[j].wait_event_info = proc->wait_event_info;
params->items[j].queryId = pgws_proc_queryids[i];
params->items[j].isRegularBackend = !(proc->isBackgroundWorker);
params->items[j].databaseId = proc->databaseId;
params->items[j].roleId = proc->roleId;
j++;
}
funcctx->max_calls = j;
Expand All @@ -579,8 +591,8 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS)
if (funcctx->call_cntr < funcctx->max_calls)
{
HeapTuple tuple;
Datum values[4];
bool nulls[4];
Datum values[7];
bool nulls[7];
const char *event_type,
*event;
HistoryItem *item;
Expand All @@ -604,6 +616,9 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS)
nulls[2] = true;

values[3] = UInt64GetDatum(item->queryId);
values[4] = BoolGetDatum(item->isRegularBackend);
values[5] = ObjectIdGetDatum(item->databaseId);
values[6] = ObjectIdGetDatum(item->roleId);
tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);

SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
Expand Down Expand Up @@ -858,7 +873,7 @@ pg_wait_sampling_get_history(PG_FUNCTION_ARGS)
funcctx->max_calls = history->count;

/* Make tuple descriptor */
tupdesc = CreateTemplateTupleDesc(5);
tupdesc = CreateTemplateTupleDesc(8);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pid",
INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "sample_ts",
Expand All @@ -869,6 +884,13 @@ pg_wait_sampling_get_history(PG_FUNCTION_ARGS)
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "queryid",
INT8OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 6, "isregularbackend",
BOOLOID, -1, 0),
TupleDescInitEntry(tupdesc, (AttrNumber) 7, "databaseid",
OIDOID, -1, 0),
TupleDescInitEntry(tupdesc, (AttrNumber) 8, "roleid",
OIDOID, -1, 0);

funcctx->tuple_desc = BlessTupleDesc(tupdesc);

MemoryContextSwitchTo(oldcontext);
Expand All @@ -883,8 +905,8 @@ pg_wait_sampling_get_history(PG_FUNCTION_ARGS)
{
HeapTuple tuple;
HistoryItem *item;
Datum values[5];
bool nulls[5];
Datum values[8];
bool nulls[8];
const char *event_type,
*event;

Expand All @@ -908,6 +930,9 @@ pg_wait_sampling_get_history(PG_FUNCTION_ARGS)
nulls[3] = true;

values[4] = UInt64GetDatum(item->queryId);
values[5] = BoolGetDatum(item->isRegularBackend);
values[6] = ObjectIdGetDatum(item->databaseId);
values[7] = ObjectIdGetDatum(item->roleId);
tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);

history->index++;
Expand Down
2 changes: 1 addition & 1 deletion pg_wait_sampling.control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pg_wait_sampling extension
comment = 'sampling based statistics of wait events'
default_version = '1.1'
default_version = '1.2'
module_pathname = '$libdir/pg_wait_sampling'
relocatable = true
3 changes: 3 additions & 0 deletions pg_wait_sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ typedef struct
int pid;
uint32 wait_event_info;
uint64 queryId;
bool isRegularBackend;
Oid databaseId;
Oid roleId;
TimestampTz ts;
} HistoryItem;

Expand Down