Skip to content

Commit 8fe3d4c

Browse files
committed
PG-1907 Add columns to track parallel worker activity
These are the same counters as were intorduced in pg_stat_statements in commit cf54a2c.
1 parent fddd856 commit 8fe3d4c

10 files changed

Lines changed: 157 additions & 7 deletions

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ REGRESS = basic \
3232
tags \
3333
user \
3434
level_tracking \
35-
decode_error_level
35+
decode_error_level \
36+
parallel
3637

3738
# Disabled because these tests require "shared_preload_libraries=pg_stat_statements",
3839
# which typical installcheck users do not have (e.g. buildfarm clients).

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ tests += {
4141
'guc',
4242
'histogram',
4343
'level_tracking'
44+
'parallel',
4445
'pgsqm_query_id',
4546
'relations',
4647
'rows',

pg_stat_monitor--2.2--2.3.sql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ CREATE FUNCTION pg_stat_monitor_internal(
8787
OUT jit_deform_count int8,
8888
OUT jit_deform_time float8,
8989

90-
OUT stats_since timestamp with time zone, -- 67
90+
OUT parallel_workers_to_launch int, -- 67
91+
OUT parallel_workers_launched int,
92+
93+
OUT stats_since timestamp with time zone, -- 69
9194
OUT minmax_stats_since timestamp with time zone,
9295

93-
OUT toplevel BOOLEAN, -- 69
96+
OUT toplevel BOOLEAN, -- 71
9497
OUT bucket_done BOOLEAN
9598
)
9699
RETURNS SETOF record
@@ -175,6 +178,9 @@ CREATE VIEW pg_stat_monitor AS SELECT
175178
jit_deform_count,
176179
jit_deform_time,
177180

181+
parallel_workers_to_launch,
182+
parallel_workers_launched,
183+
178184
stats_since,
179185
minmax_stats_since
180186

pg_stat_monitor.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ PG_MODULE_MAGIC;
4949
#define PG_STAT_MONITOR_COLS_V1_0 52
5050
#define PG_STAT_MONITOR_COLS_V2_0 64
5151
#define PG_STAT_MONITOR_COLS_V2_1 70
52-
#define PG_STAT_MONITOR_COLS_V2_3 71
52+
#define PG_STAT_MONITOR_COLS_V2_3 73
5353
#define PG_STAT_MONITOR_COLS PG_STAT_MONITOR_COLS_V2_3 /* maximum of above */
5454

5555
#define PGSM_TEXT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat_monitor_query"
@@ -230,6 +230,8 @@ static void pgsm_update_entry(pgsmEntry *entry,
230230
BufferUsage *bufusage,
231231
WalUsage *walusage,
232232
const struct JitInstrumentation *jitusage,
233+
int parallel_workers_to_launch,
234+
int parallel_workers_launched,
233235
bool reset,
234236
pgsmStoreKind kind);
235237
static void pgsm_store(pgsmEntry *entry);
@@ -783,6 +785,13 @@ pgsm_ExecutorEnd(QueryDesc *queryDesc)
783785
queryDesc->estate->es_jit ? &queryDesc->estate->es_jit->instr : NULL, /* jitusage */
784786
#else
785787
NULL,
788+
#endif
789+
#if PG_VERSION_NUM >= 180000
790+
queryDesc->estate->es_parallel_workers_to_launch, /* parallel_workers_to_launch */
791+
queryDesc->estate->es_parallel_workers_launched, /* parallel_workers_launched */
792+
#else
793+
0, /* parallel_workers_to_launch */
794+
0, /* parallel_workers_launched */
786795
#endif
787796
false, /* reset */
788797
PGSM_EXEC); /* kind */
@@ -964,6 +973,8 @@ pgsm_planner_hook(Query *parse, const char *query_string, int cursorOptions, Par
964973
&bufusage, /* bufusage */
965974
&walusage, /* walusage */
966975
NULL, /* jitusage */
976+
0, /* parallel_workers_to_launch */
977+
0, /* parallel_workers_launched */
967978
false, /* reset */
968979
PGSM_PLAN); /* kind */
969980
}
@@ -1189,6 +1200,8 @@ pgsm_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11891200
&bufusage, /* bufusage */
11901201
&walusage, /* walusage */
11911202
NULL, /* jitusage */
1203+
0, /* parallel_workers_to_launch */
1204+
0, /* parallel_workers_launched */
11921205
false, /* reset */
11931206
PGSM_EXEC); /* kind */
11941207

@@ -1373,6 +1386,8 @@ pgsm_update_entry(pgsmEntry *entry,
13731386
BufferUsage *bufusage,
13741387
WalUsage *walusage,
13751388
const struct JitInstrumentation *jitusage,
1389+
int parallel_workers_to_launch,
1390+
int parallel_workers_launched,
13761391
bool reset,
13771392
pgsmStoreKind kind)
13781393
{
@@ -1628,6 +1643,10 @@ pgsm_update_entry(pgsmEntry *entry,
16281643
}
16291644
}
16301645

1646+
/* parallel worker counters */
1647+
entry->counters.parallel_workers_to_launch += parallel_workers_to_launch;
1648+
entry->counters.parallel_workers_launched += parallel_workers_launched;
1649+
16311650
if (kind == PGSM_STORE)
16321651
SpinLockRelease(&entry->mutex);
16331652
}
@@ -2018,6 +2037,8 @@ pgsm_store(pgsmEntry *entry)
20182037
&bufusage, /* bufusage */
20192038
&walusage, /* walusage */
20202039
&jitusage, /* jitusage */
2040+
entry->counters.parallel_workers_to_launch, /* parallel_workers_to_launch */
2041+
entry->counters.parallel_workers_launched, /* parallel_workers_launched */
20212042
reset, /* reset */
20222043
PGSM_STORE);
20232044

@@ -2548,17 +2569,24 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
25482569
}
25492570
}
25502571

2551-
if (api_version >= PGSM_V2_1)
2572+
if (api_version >= PGSM_V2_3)
25522573
{
25532574
/* at column number 67 */
2575+
values[i++] = Int64GetDatumFast(tmp.parallel_workers_to_launch);
2576+
values[i++] = Int64GetDatumFast(tmp.parallel_workers_launched);
2577+
}
2578+
2579+
if (api_version >= PGSM_V2_1)
2580+
{
2581+
/* at column number 69 */
25542582
values[i++] = TimestampTzGetDatum(entry->stats_since);
25552583
values[i++] = TimestampTzGetDatum(entry->minmax_stats_since);
25562584
}
25572585

2558-
/* toplevel at column number 69 */
2586+
/* toplevel at column number 71 */
25592587
values[i++] = BoolGetDatum(toplevel);
25602588

2561-
/* bucket_done at column number 70 */
2589+
/* bucket_done at column number 72 */
25622590
values[i++] = BoolGetDatum(pg_atomic_read_u64(&pgsm->current_wbucket) != bucketid);
25632591

25642592
/* clean up and return the tuplestore */

pg_stat_monitor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ typedef struct Counters
328328
Wal_Usage walusage;
329329
int resp_calls[MAX_RESPONSE_BUCKET]; /* execution time's in
330330
* msec */
331+
int64 parallel_workers_to_launch; /* # of parallel workers planned
332+
* to be launched */
333+
int64 parallel_workers_launched; /* # of parallel workers actually
334+
* launched */
331335
} Counters;
332336

333337
/* Some global structure to get the cpu usage, really don't like the idea of global variable */

regression/expected/parallel.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--
2+
-- Tests for parallel statistics
3+
--
4+
SELECT setting::integer < 180000 AS skip_test FROM pg_settings where name = 'server_version_num' \gset
5+
\if :skip_test
6+
\quit

regression/expected/parallel_1.out

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--
2+
-- Tests for parallel statistics
3+
--
4+
SELECT setting::integer < 180000 AS skip_test FROM pg_settings where name = 'server_version_num' \gset
5+
\if :skip_test
6+
\quit
7+
\endif
8+
CREATE EXTENSION pg_stat_monitor;
9+
SET pgsm.track_utility = FALSE;
10+
-- encourage use of parallel plans
11+
SET parallel_setup_cost = 0;
12+
SET parallel_tuple_cost = 0;
13+
SET min_parallel_table_scan_size = 0;
14+
SET max_parallel_workers_per_gather = 2;
15+
CREATE TABLE pgsm_parallel_tab (a int);
16+
SELECT pg_stat_monitor_reset() IS NOT NULL AS t;
17+
t
18+
---
19+
t
20+
(1 row)
21+
22+
SELECT count(*) FROM pgsm_parallel_tab;
23+
count
24+
-------
25+
0
26+
(1 row)
27+
28+
SELECT query,
29+
parallel_workers_to_launch > 0 AS has_workers_to_launch,
30+
parallel_workers_launched > 0 AS has_workers_launched
31+
FROM pg_stat_monitor
32+
WHERE query ~ 'SELECT count'
33+
ORDER BY query COLLATE "C";
34+
query | has_workers_to_launch | has_workers_launched
35+
----------------------------------------+-----------------------+----------------------
36+
SELECT count(*) FROM pgsm_parallel_tab | t | t
37+
(1 row)
38+
39+
DROP TABLE pgsm_parallel_tab;

regression/sql/parallel.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--
2+
-- Tests for parallel statistics
3+
--
4+
5+
SELECT setting::integer < 180000 AS skip_test FROM pg_settings where name = 'server_version_num' \gset
6+
\if :skip_test
7+
\quit
8+
\endif
9+
10+
CREATE EXTENSION pg_stat_monitor;
11+
SET pgsm.track_utility = FALSE;
12+
13+
-- encourage use of parallel plans
14+
SET parallel_setup_cost = 0;
15+
SET parallel_tuple_cost = 0;
16+
SET min_parallel_table_scan_size = 0;
17+
SET max_parallel_workers_per_gather = 2;
18+
19+
CREATE TABLE pgsm_parallel_tab (a int);
20+
21+
SELECT pg_stat_monitor_reset() IS NOT NULL AS t;
22+
23+
SELECT count(*) FROM pgsm_parallel_tab;
24+
25+
SELECT query,
26+
parallel_workers_to_launch > 0 AS has_workers_to_launch,
27+
parallel_workers_launched > 0 AS has_workers_launched
28+
FROM pg_stat_monitor
29+
WHERE query ~ 'SELECT count'
30+
ORDER BY query COLLATE "C";
31+
32+
DROP TABLE pgsm_parallel_tab;

t/018_column_names.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"local_blk_read_time,local_blk_write_time,local_blks_dirtied,local_blks_hit,".
3232
"local_blks_read,local_blks_written,max_exec_time,max_plan_time,mean_exec_time," .
3333
"mean_plan_time,message,min_exec_time,min_plan_time,minmax_stats_since," .
34+
"parallel_workers_launched,parallel_workers_to_launch," .
3435
"pgsm_query_id,planid,plans,query,query_plan,queryid,relations,resp_calls,rows," .
3536
"shared_blk_read_time,shared_blk_write_time,shared_blks_dirtied," .
3637
"shared_blks_hit,shared_blks_read,shared_blks_written,sqlcode,stats_since," .

t/025_compare_pgss.pl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@
145145
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.wal_buffers_full) = SUM(PGSS.wal_buffers_full) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%DELETE FROM pgbench_accounts%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
146146
trim($stdout);
147147
is($stdout,'t',"Compare: wal_buffers_full are equal.");
148+
149+
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.parallel_workers_to_launch) = SUM(PGSS.parallel_workers_to_launch) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%DELETE FROM pgbench_accounts%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
150+
trim($stdout);
151+
is($stdout,'t',"Compare: parallel_workers_to_launch are equal.");
152+
153+
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.parallel_workers_launched) = SUM(PGSS.parallel_workers_launched) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%DELETE FROM pgbench_accounts%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
154+
trim($stdout);
155+
is($stdout,'t',"Compare: parallel_workers_launched are equal.");
148156
}
149157

150158
# Compare values for query 'INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES ($1, $2, $3, $4, CURRENT_TIMESTAMP)'
@@ -201,6 +209,14 @@
201209
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.wal_buffers_full) = SUM(PGSS.wal_buffers_full) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%INSERT INTO pgbench_history%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
202210
trim($stdout);
203211
is($stdout,'t',"Compare: wal_buffers_full are equal.");
212+
213+
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.parallel_workers_to_launch) = SUM(PGSS.parallel_workers_to_launch) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%INSERT INTO pgbench_history%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
214+
trim($stdout);
215+
is($stdout,'t',"Compare: parallel_workers_to_launch are equal.");
216+
217+
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.parallel_workers_launched) = SUM(PGSS.parallel_workers_launched) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%INSERT INTO pgbench_history%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
218+
trim($stdout);
219+
is($stdout,'t',"Compare: parallel_workers_launched are equal.");
204220
}
205221

206222
# Compare values for query 'SELECT abalance FROM pgbench_accounts WHERE aid = $1'
@@ -257,6 +273,14 @@
257273
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.wal_buffers_full) = SUM(PGSS.wal_buffers_full) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%SELECT abalance FROM pgbench_accounts%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
258274
trim($stdout);
259275
is($stdout,'t',"Compare: wal_buffers_full are equal.");
276+
277+
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.parallel_workers_to_launch) = SUM(PGSS.parallel_workers_to_launch) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%SELECT abalance FROM pgbench_accounts%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
278+
trim($stdout);
279+
is($stdout,'t',"Compare: parallel_workers_to_launch are equal.");
280+
281+
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.parallel_workers_launched) = SUM(PGSS.parallel_workers_launched) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%SELECT abalance FROM pgbench_accounts%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
282+
trim($stdout);
283+
is($stdout,'t',"Compare: parallel_workers_launched are equal.");
260284
}
261285

262286
# Compare values for query 'UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2'
@@ -309,6 +333,14 @@
309333
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.wal_buffers_full) = SUM(PGSS.wal_buffers_full) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%UPDATE pgbench_accounts%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
310334
trim($stdout);
311335
is($stdout,'t',"Compare: wal_buffers_full are equal.");
336+
337+
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.parallel_workers_to_launch) = SUM(PGSS.parallel_workers_to_launch) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%UPDATE pgbench_accounts%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
338+
trim($stdout);
339+
is($stdout,'t',"Compare: parallel_workers_to_launch are equal.");
340+
341+
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT SUM(PGSM.parallel_workers_launched) = SUM(PGSS.parallel_workers_launched) FROM pg_stat_monitor AS PGSM INNER JOIN pg_stat_statements AS PGSS ON PGSS.query = PGSM.query WHERE PGSM.query LIKE \'%UPDATE pgbench_accounts%\' GROUP BY PGSM.query;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
342+
trim($stdout);
343+
is($stdout,'t',"Compare: parallel_workers_launched are equal.");
312344
}
313345

314346
# DROP EXTENSION

0 commit comments

Comments
 (0)