Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add more columns/dimensions to pg_wait_sampling #97

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
Medvecrab wants to merge 13 commits into master
base: master
Choose a base branch
Loading
from new_dimensions
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
6cd4efa
Add profile_extended and history_extended views with additional dimen...
Mar 18, 2025
9208eb2
Update README to include information about new *_extended views
Mar 26, 2025
f0ee939
Fixes after review
Medvecrab Jun 2, 2025
292aaa9
Fixes after review, part 2
Medvecrab Jun 10, 2025
da75db0
Add serialization
Medvecrab Jun 11, 2025
46af7da
Fixing serialization
Medvecrab Jun 23, 2025
64efec7
Add history reset
Medvecrab Jun 24, 2025
f6a2203
remove old functions for good
Medvecrab Jun 24, 2025
09fc662
Do as pg_stat_statements does with different extension versions
Medvecrab Jun 25, 2025
42b4828
Fixes after self-review
Medvecrab Jun 25, 2025
01c45a4
Fix and add info about sampling dimensions to README
Medvecrab Jun 26, 2025
885496f
Fixes after review
Medvecrab Sep 22, 2025
5bc04b5
More fixes after review
Medvecrab Sep 24, 2025
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
Prev Previous commit
Next Next commit
Fixes after self-review
  • Loading branch information
Medvecrab committed Jun 26, 2025
commit 42b482814509c6cf63067a5537d04f0d1f6cf10d
11 changes: 2 additions & 9 deletions collector.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ realloc_history(History *observations, int count)
j;
int serialized_size;

//saved_history_dimensions = pgws_history_dimensions; // TODO вроде как
serialized_size = get_serialized_size(saved_history_dimensions, true);

/* Allocate new array for history */
Expand Down Expand Up @@ -578,9 +577,6 @@ probe_waits(History *observations, HTAB *profile_hash,
LWLockAcquire(ProcArrayLock, LW_SHARED);
for (i = 0; i < ProcGlobal->allProcCount; i++)
{
//HistoryItem item_history,
// *observation;
//ProfileItem item_profile;
PGPROC *proc = &ProcGlobal->allProcs[i];
int pid;
uint32 wait_event_info;
Expand Down Expand Up @@ -611,9 +607,6 @@ probe_waits(History *observations, HTAB *profile_hash,
&common_dimensions,
saved_profile_dimensions);

//item_history.ts = ts;
//item_history.dimensions = history_dimensions;

/* Write to the history if needed */
if (write_history)
{
Expand Down Expand Up @@ -646,7 +639,7 @@ probe_waits(History *observations, HTAB *profile_hash,
&serialized_item, &serialized_key, &serialized_size,
(TimestampTz) 0, count, false);

stored_item = (char *) hash_search(profile_hash, serialized_key,
stored_item = (char *) hash_search(profile_hash, serialized_key,
HASH_ENTER, &found);

if (found)
Expand All @@ -673,7 +666,7 @@ probe_waits(History *observations, HTAB *profile_hash,
* Send waits history to shared memory queue.
*/
static void
send_history(History *observations, shm_mq_handle *mqh) //TODO TODO TODO
send_history(History *observations, shm_mq_handle *mqh)
{
int serialized_size = get_serialized_size(saved_history_dimensions, true);
Size count,
Expand Down
76 changes: 53 additions & 23 deletions expected/load.out
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
CREATE EXTENSION pg_wait_sampling;
\d pg_wait_sampling_current
View "public.pg_wait_sampling_current"
Column | Type | Modifiers
------------+---------+-----------
pid | integer |
event_type | text |
event | text |
queryid | bigint |
View "public.pg_wait_sampling_current"
Column | Type | Collation | Nullable | Default
---------------------+--------------------------+-----------+----------+---------
pid | integer | | |
event_type | text | | |
event | text | | |
queryid | bigint | | |
role_id | bigint | | |
database_id | bigint | | |
parallel_leader_pid | integer | | |
is_regular_backend | boolean | | |
backend_type | text | | |
backend_state | text | | |
proc_start | timestamp with time zone | | |
client_addr | text | | |
client_hostname | text | | |
appname | text | | |

\d pg_wait_sampling_history
View "public.pg_wait_sampling_history"
Column | Type | Modifiers
------------+--------------------------+-----------
pid | integer |
ts | timestamp with time zone |
event_type | text |
event | text |
queryid | bigint |
View "public.pg_wait_sampling_history"
Column | Type | Collation | Nullable | Default
---------------------+--------------------------+-----------+----------+---------
pid | integer | | |
event_type | text | | |
event | text | | |
queryid | bigint | | |
role_id | bigint | | |
database_id | bigint | | |
parallel_leader_pid | integer | | |
is_regular_backend | boolean | | |
backend_type | text | | |
backend_state | text | | |
proc_start | timestamp with time zone | | |
client_addr | text | | |
client_hostname | text | | |
appname | text | | |
ts | timestamp with time zone | | |

\d pg_wait_sampling_profile
View "public.pg_wait_sampling_profile"
Column | Type | Modifiers
------------+---------+-----------
pid | integer |
event_type | text |
event | text |
queryid | bigint |
count | bigint |
View "public.pg_wait_sampling_profile"
Column | Type | Collation | Nullable | Default
---------------------+--------------------------+-----------+----------+---------
pid | integer | | |
event_type | text | | |
event | text | | |
queryid | bigint | | |
role_id | bigint | | |
database_id | bigint | | |
parallel_leader_pid | integer | | |
is_regular_backend | boolean | | |
backend_type | text | | |
backend_state | text | | |
proc_start | timestamp with time zone | | |
client_addr | text | | |
client_hostname | text | | |
appname | text | | |
count | bigint | | |

DROP EXTENSION pg_wait_sampling;
31 changes: 0 additions & 31 deletions expected/load_1.out
View file Open in desktop

This file was deleted.

45 changes: 0 additions & 45 deletions expected/queries.out
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,6 @@ WITH t as (SELECT sum(0) FROM pg_wait_sampling_profile)
0
(1 row)

WITH t as (SELECT sum(0) FROM pg_wait_sampling_current_extended)
SELECT sum(0) FROM generate_series(1, 2), t;
sum
-----
0
(1 row)

WITH t as (SELECT sum(0) FROM pg_wait_sampling_history_extended)
SELECT sum(0) FROM generate_series(1, 2), t;
sum
-----
0
(1 row)

WITH t as (SELECT sum(0) FROM pg_wait_sampling_profile_extended)
SELECT sum(0) FROM generate_series(1, 2), t;
sum
-----
0
(1 row)

-- Some dummy checks just to be sure that all our functions work and return something.
SELECT count(*) = 1 as test FROM pg_wait_sampling_get_current(pg_backend_pid());
test
Expand All @@ -66,28 +45,4 @@ SELECT pg_wait_sampling_reset_profile();

(1 row)

SELECT count(*) = 1 as test FROM pg_wait_sampling_get_current_extended(pg_backend_pid());
test
------
t
(1 row)

SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_profile_extended();
test
------
t
(1 row)

SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_history_extended();
test
------
t
(1 row)

SELECT pg_wait_sampling_reset_profile();
pg_wait_sampling_reset_profile
--------------------------------

(1 row)

DROP EXTENSION pg_wait_sampling;
31 changes: 19 additions & 12 deletions pg_wait_sampling.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,9 @@ pgws_general_dimensions_check_hook (char **newvalue, void **extra, GucSource sou
int extrachecks = 0;
int *myextra;

/* Check special cases when we turn all or none dimensions */
/* Check special case when we turn all dimensions */
if (pg_strcasecmp(*newvalue, "all") == 0)
extrachecks = PGWS_DIMENSIONS_ALL;
else if (pg_strcasecmp(*newvalue, "none") == 0)
extrachecks = PGWS_DIMENSIONS_NONE;
else
{
/* Need a modifiable copy of string */
Expand Down Expand Up @@ -388,7 +386,7 @@ pgws_general_dimensions_check_hook (char **newvalue, void **extra, GucSource sou
extrachecks |= PGWS_DIMENSIONS_CLIENT_HOSTNAME;
else if (pg_strcasecmp(tok, "appname") == 0)
extrachecks |= PGWS_DIMENSIONS_APPNAME;
else if (pg_strcasecmp(tok, "all") == 0 || pg_strcasecmp(tok, "none") == 0)
else if (pg_strcasecmp(tok, "all") == 0)
{
GUC_check_errdetail("Key word \"%s\" cannot be combined with other key words.", tok);
pfree(rawstring);
Expand Down Expand Up @@ -747,8 +745,17 @@ get_beentry_by_procpid(int pid)
/* Here beid is just index in localBackendStatusTable */
local_beentry = pgstat_fetch_stat_local_beentry(cur_be_idx);
#endif
#if defined(PGPRO_EE) || defined(PGPRO_STD) && PG_VERSION_NUM >= 160000
if (local_beentry->backendStatus->st_procpid == pid)
return local_beentry->backendStatus;
#else
if (local_beentry->backendStatus.st_procpid == pid)
{
PgBackendStatus *result = palloc0(sizeof(PgBackendStatus));
*result = local_beentry->backendStatus;
return result;
}
#endif
}
return NULL;
}
Expand Down Expand Up @@ -831,12 +838,12 @@ fill_values_and_nulls(Datum *values, bool *nulls, SamplingDimensions dimensions,
values[3] = UInt64GetDatum(dimensions.queryId);
else
values[3] = (Datum) 0;
if (dimensions_mask & PGWS_DIMENSIONS_ROLE_ID)
values[4] = ObjectIdGetDatum(dimensions.role_id);
else
nulls[4] = true;
if (api_version >= PGWS_V1_2)
{
if (dimensions_mask & PGWS_DIMENSIONS_ROLE_ID)
values[4] = ObjectIdGetDatum(dimensions.role_id);
else
nulls[4] = true;
if (dimensions_mask & PGWS_DIMENSIONS_DB_ID)
values[5] = ObjectIdGetDatum(dimensions.database_id);
else
Expand Down Expand Up @@ -887,7 +894,7 @@ PG_FUNCTION_INFO_V1(pg_wait_sampling_get_current_1_2);
Datum
pg_wait_sampling_get_current_1_2(PG_FUNCTION_ARGS)
{
return pg_wait_sampling_get_current_internal(fcinfo, PGWS_V1_2);
return pg_wait_sampling_get_current_internal(fcinfo, PGWS_V1_2);
}

Datum
Expand Down Expand Up @@ -1187,7 +1194,7 @@ PG_FUNCTION_INFO_V1(pg_wait_sampling_get_profile_1_2);
Datum
pg_wait_sampling_get_profile_1_2(PG_FUNCTION_ARGS)
{
return pg_wait_sampling_get_profile_internal(fcinfo, PGWS_V1_2);
return pg_wait_sampling_get_profile_internal(fcinfo, PGWS_V1_2);
}

Datum
Expand Down Expand Up @@ -1328,7 +1335,7 @@ pg_wait_sampling_reset_history(PG_FUNCTION_ARGS)
LockAcquire(&collectorTag, ExclusiveLock, false, false);
pgws_collector_hdr->request = HISTORY_RESET;
LockRelease(&collectorTag, ExclusiveLock, false);

if (!pgws_collector_hdr->latch)
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
errmsg("pg_wait_sampling collector wasn't started")));
Expand All @@ -1351,7 +1358,7 @@ PG_FUNCTION_INFO_V1(pg_wait_sampling_get_history_1_2);
Datum
pg_wait_sampling_get_history_1_2(PG_FUNCTION_ARGS)
{
return pg_wait_sampling_get_history_internal(fcinfo, PGWS_V1_2);
return pg_wait_sampling_get_history_internal(fcinfo, PGWS_V1_2);
}

Datum
Expand Down
2 changes: 0 additions & 2 deletions pg_wait_sampling.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#define PGWS_COLLECTOR_LOCK 1

/* Values for sampling dimensions */
#define PGWS_DIMENSIONS_NONE 0

#define PGWS_DIMENSIONS_PID (1 << 0)
#define PGWS_DIMENSIONS_WAIT_EVENT_TYPE (1 << 1)
#define PGWS_DIMENSIONS_WAIT_EVENT (1 << 2)
Expand Down
14 changes: 0 additions & 14 deletions sql/queries.sql
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,10 @@ WITH t as (SELECT sum(0) FROM pg_wait_sampling_history)
WITH t as (SELECT sum(0) FROM pg_wait_sampling_profile)
SELECT sum(0) FROM generate_series(1, 2), t;

WITH t as (SELECT sum(0) FROM pg_wait_sampling_current_extended)
SELECT sum(0) FROM generate_series(1, 2), t;

WITH t as (SELECT sum(0) FROM pg_wait_sampling_history_extended)
SELECT sum(0) FROM generate_series(1, 2), t;

WITH t as (SELECT sum(0) FROM pg_wait_sampling_profile_extended)
SELECT sum(0) FROM generate_series(1, 2), t;

-- Some dummy checks just to be sure that all our functions work and return something.
SELECT count(*) = 1 as test FROM pg_wait_sampling_get_current(pg_backend_pid());
SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_profile();
SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_history();
SELECT pg_wait_sampling_reset_profile();

SELECT count(*) = 1 as test FROM pg_wait_sampling_get_current_extended(pg_backend_pid());
SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_profile_extended();
SELECT count(*) >= 0 as test FROM pg_wait_sampling_get_history_extended();
SELECT pg_wait_sampling_reset_profile();

DROP EXTENSION pg_wait_sampling;

AltStyle によって変換されたページ (->オリジナル) /