Skip to content

Commit e7a9496

Browse files
committed
Add two attributes to pg_stat_database for parallel workers activity
Two attributes are added to pg_stat_database: * parallel_workers_to_launch, counting the total number of parallel workers that were planned to be launched. * parallel_workers_launched, counting the total number of parallel workers actually launched. The ratio of both fields can provide hints that there are not enough slots available when launching parallel workers, also useful when pg_stat_statements is not deployed on an instance (i.e. cf54a2c). This commit relies on de3a2ea, that has added two fields to EState, that get incremented when executing Gather or GatherMerge nodes. A test is added in select_parallel, where parallel workers are spawned. Bump catalog version. Author: Benoit Lobréau Discussion: https://postgr.es/m/783bc7f7-659a-42fa-99dd-ee0565644e25@dalibo.com
1 parent bf8835e commit e7a9496

File tree

11 files changed

+108
-1
lines changed

11 files changed

+108
-1
lines changed

doc/src/sgml/monitoring.sgml

+18
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,24 @@ description | Waiting for a newly initialized WAL file to reach durable storage
36113611
</para></entry>
36123612
</row>
36133613

3614+
<row>
3615+
<entry role="catalog_table_entry"><para role="column_definition">
3616+
<structfield>parallel_workers_to_launch</structfield> <type>bigint</type>
3617+
</para>
3618+
<para>
3619+
Number of parallel workers planned to be launched by queries on this database
3620+
</para></entry>
3621+
</row>
3622+
3623+
<row>
3624+
<entry role="catalog_table_entry"><para role="column_definition">
3625+
<structfield>parallel_workers_launched</structfield> <type>bigint</type>
3626+
</para>
3627+
<para>
3628+
Number of parallel workers launched by queries on this database
3629+
</para></entry>
3630+
</row>
3631+
36143632
<row>
36153633
<entry role="catalog_table_entry"><para role="column_definition">
36163634
<structfield>stats_reset</structfield> <type>timestamp with time zone</type>

src/backend/catalog/system_views.sql

+2
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,8 @@ CREATE VIEW pg_stat_database AS
10731073
pg_stat_get_db_sessions_abandoned(D.oid) AS sessions_abandoned,
10741074
pg_stat_get_db_sessions_fatal(D.oid) AS sessions_fatal,
10751075
pg_stat_get_db_sessions_killed(D.oid) AS sessions_killed,
1076+
pg_stat_get_db_parallel_workers_to_launch(D.oid) as parallel_workers_to_launch,
1077+
pg_stat_get_db_parallel_workers_launched(D.oid) as parallel_workers_launched,
10761078
pg_stat_get_db_stat_reset_time(D.oid) AS stats_reset
10771079
FROM (
10781080
SELECT 0 AS oid, NULL::name AS datname

src/backend/executor/execMain.c

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "miscadmin.h"
5353
#include "nodes/queryjumble.h"
5454
#include "parser/parse_relation.h"
55+
#include "pgstat.h"
5556
#include "rewrite/rewriteHandler.h"
5657
#include "tcop/utility.h"
5758
#include "utils/acl.h"
@@ -483,6 +484,10 @@ standard_ExecutorEnd(QueryDesc *queryDesc)
483484

484485
Assert(estate != NULL);
485486

487+
if (estate->es_parallel_workers_to_launch > 0)
488+
pgstat_update_parallel_workers_stats((PgStat_Counter) estate->es_parallel_workers_to_launch,
489+
(PgStat_Counter) estate->es_parallel_workers_launched);
490+
486491
/*
487492
* Check that ExecutorFinish was called, unless in EXPLAIN-only mode. This
488493
* Assert is needed because ExecutorFinish is new as of 9.1, and callers

src/backend/utils/activity/pgstat_database.c

+19
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,23 @@ AtEOXact_PgStat_Database(bool isCommit, bool parallel)
262262
}
263263
}
264264

265+
/*
266+
* Notify the stats system about parallel worker information.
267+
*/
268+
void
269+
pgstat_update_parallel_workers_stats(PgStat_Counter workers_to_launch,
270+
PgStat_Counter workers_launched)
271+
{
272+
PgStat_StatDBEntry *dbentry;
273+
274+
if (!OidIsValid(MyDatabaseId))
275+
return;
276+
277+
dbentry = pgstat_prep_database_pending(MyDatabaseId);
278+
dbentry->parallel_workers_to_launch += workers_to_launch;
279+
dbentry->parallel_workers_launched += workers_launched;
280+
}
281+
265282
/*
266283
* Subroutine for pgstat_report_stat(): Handle xact commit/rollback and I/O
267284
* timings.
@@ -425,6 +442,8 @@ pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
425442
PGSTAT_ACCUM_DBCOUNT(sessions_abandoned);
426443
PGSTAT_ACCUM_DBCOUNT(sessions_fatal);
427444
PGSTAT_ACCUM_DBCOUNT(sessions_killed);
445+
PGSTAT_ACCUM_DBCOUNT(parallel_workers_to_launch);
446+
PGSTAT_ACCUM_DBCOUNT(parallel_workers_launched);
428447
#undef PGSTAT_ACCUM_DBCOUNT
429448

430449
pgstat_unlock_entry(entry_ref);

src/backend/utils/adt/pgstatfuncs.c

+6
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,12 @@ PG_STAT_GET_DBENTRY_INT64(sessions_fatal)
10391039
/* pg_stat_get_db_sessions_killed */
10401040
PG_STAT_GET_DBENTRY_INT64(sessions_killed)
10411041

1042+
/* pg_stat_get_db_parallel_workers_to_launch */
1043+
PG_STAT_GET_DBENTRY_INT64(parallel_workers_to_launch)
1044+
1045+
/* pg_stat_get_db_parallel_workers_launched */
1046+
PG_STAT_GET_DBENTRY_INT64(parallel_workers_launched)
1047+
10421048
/* pg_stat_get_db_temp_bytes */
10431049
PG_STAT_GET_DBENTRY_INT64(temp_bytes)
10441050

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202411081
60+
#define CATALOG_VERSION_NO 202411111
6161

6262
#endif

src/include/catalog/pg_proc.dat

+10
Original file line numberDiff line numberDiff line change
@@ -5813,6 +5813,16 @@
58135813
proname => 'pg_stat_get_db_sessions_killed', provolatile => 's',
58145814
proparallel => 'r', prorettype => 'int8', proargtypes => 'oid',
58155815
prosrc => 'pg_stat_get_db_sessions_killed' },
5816+
{ oid => '8403',
5817+
descr => 'statistics: number of parallel workers planned to be launched by queries',
5818+
proname => 'pg_stat_get_db_parallel_workers_to_launch', provolatile => 's',
5819+
proparallel => 'r', prorettype => 'int8', proargtypes => 'oid',
5820+
prosrc => 'pg_stat_get_db_parallel_workers_to_launch' },
5821+
{ oid => '8404',
5822+
descr => 'statistics: number of parallel workers effectively launched by queries',
5823+
proname => 'pg_stat_get_db_parallel_workers_launched', provolatile => 's',
5824+
proparallel => 'r', prorettype => 'int8', proargtypes => 'oid',
5825+
prosrc => 'pg_stat_get_db_parallel_workers_launched' },
58165826
{ oid => '3195', descr => 'statistics: information about WAL archiver',
58175827
proname => 'pg_stat_get_archiver', proisstrict => 'f', provolatile => 's',
58185828
proparallel => 'r', prorettype => 'record', proargtypes => '',

src/include/pgstat.h

+4
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ typedef struct PgStat_StatDBEntry
386386
PgStat_Counter sessions_abandoned;
387387
PgStat_Counter sessions_fatal;
388388
PgStat_Counter sessions_killed;
389+
PgStat_Counter parallel_workers_to_launch;
390+
PgStat_Counter parallel_workers_launched;
389391

390392
TimestampTz stat_reset_timestamp;
391393
} PgStat_StatDBEntry;
@@ -583,6 +585,8 @@ extern void pgstat_report_deadlock(void);
583585
extern void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount);
584586
extern void pgstat_report_checksum_failure(void);
585587
extern void pgstat_report_connect(Oid dboid);
588+
extern void pgstat_update_parallel_workers_stats(PgStat_Counter workers_to_launch,
589+
PgStat_Counter workers_launched);
586590

587591
#define pgstat_count_buffer_read_time(n) \
588592
(pgStatBlockReadTime += (n))

src/test/regress/expected/rules.out

+2
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,8 @@ pg_stat_database| SELECT oid AS datid,
18631863
pg_stat_get_db_sessions_abandoned(oid) AS sessions_abandoned,
18641864
pg_stat_get_db_sessions_fatal(oid) AS sessions_fatal,
18651865
pg_stat_get_db_sessions_killed(oid) AS sessions_killed,
1866+
pg_stat_get_db_parallel_workers_to_launch(oid) AS parallel_workers_to_launch,
1867+
pg_stat_get_db_parallel_workers_launched(oid) AS parallel_workers_launched,
18661868
pg_stat_get_db_stat_reset_time(oid) AS stats_reset
18671869
FROM ( SELECT 0 AS oid,
18681870
NULL::name AS datname

src/test/regress/expected/select_parallel.out

+27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
--
22
-- PARALLEL
33
--
4+
-- Save parallel worker stats, used for comparison at the end
5+
select pg_stat_force_next_flush();
6+
pg_stat_force_next_flush
7+
--------------------------
8+
9+
(1 row)
10+
11+
select parallel_workers_to_launch as parallel_workers_to_launch_before,
12+
parallel_workers_launched as parallel_workers_launched_before
13+
from pg_stat_database
14+
where datname = current_database() \gset
415
create function sp_parallel_restricted(int) returns int as
516
$$begin return $1; end$$ language plpgsql parallel restricted;
617
begin;
@@ -1407,3 +1418,19 @@ CREATE UNIQUE INDEX parallel_hang_idx
14071418
SET debug_parallel_query = on;
14081419
DELETE FROM parallel_hang WHERE 380 <= i AND i <= 420;
14091420
ROLLBACK;
1421+
-- Check parallel worker stats
1422+
select pg_stat_force_next_flush();
1423+
pg_stat_force_next_flush
1424+
--------------------------
1425+
1426+
(1 row)
1427+
1428+
select parallel_workers_to_launch > :'parallel_workers_to_launch_before' AS wrk_to_launch,
1429+
parallel_workers_launched > :'parallel_workers_launched_before' AS wrk_launched
1430+
from pg_stat_database
1431+
where datname = current_database();
1432+
wrk_to_launch | wrk_launched
1433+
---------------+--------------
1434+
t | t
1435+
(1 row)
1436+

src/test/regress/sql/select_parallel.sql

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
-- PARALLEL
33
--
44

5+
-- Save parallel worker stats, used for comparison at the end
6+
select pg_stat_force_next_flush();
7+
select parallel_workers_to_launch as parallel_workers_to_launch_before,
8+
parallel_workers_launched as parallel_workers_launched_before
9+
from pg_stat_database
10+
where datname = current_database() \gset
11+
512
create function sp_parallel_restricted(int) returns int as
613
$$begin return $1; end$$ language plpgsql parallel restricted;
714

@@ -574,3 +581,10 @@ SET debug_parallel_query = on;
574581
DELETE FROM parallel_hang WHERE 380 <= i AND i <= 420;
575582

576583
ROLLBACK;
584+
585+
-- Check parallel worker stats
586+
select pg_stat_force_next_flush();
587+
select parallel_workers_to_launch > :'parallel_workers_to_launch_before' AS wrk_to_launch,
588+
parallel_workers_launched > :'parallel_workers_launched_before' AS wrk_launched
589+
from pg_stat_database
590+
where datname = current_database();

0 commit comments

Comments
 (0)