pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
WIP: new system catalog pg_wait_event
34+ messages / 6 participants
[nested] [flat]

Commits from this thread:
  2023-10-23 befe9451fb  doc: Improve example query related to pg_wait_events
  2023-08-20 1e68e43d3f  Add system view pg_wait_events
* WIP: new system catalog pg_wait_event
@ 2023-08-04 14:22 Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-04 14:22 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi hackers,

Now that fa88928470 generates automatically code and documentation
related to wait events, why not exposing the wait events description
through a system catalog relation? (the idea has been proposed on twitter
by Yves Colin [1]).

I think that could be useful to:

- join this new relation with pg_stat_activity and have a quick
understanding of what the sessions are waiting for (if any).
- quickly compare the wait events across versions (what are the new
ones if any,..)

Please find attached a POC patch creating this new system catalog
pg_wait_event.

The patch:

- updates the documentation
- adds a new option to generate-wait_event_types.pl to generate the pg_wait_event.dat
- creates the pg_wait_event.h
- works with autoconf

It currently does not:

- works with meson (has to be done)
- add tests (not sure it's needed)
- create an index on the new system catalog (not sure it's needed as the data fits
in 4 pages (8kB default size)).

Outcome example:

postgres=# select a.pid, a.application_name, a.wait_event,d.description from pg_stat_activity a, pg_wait_event d where a.wait_event = d.wait_event_name and state='active';
    pid   | application_name | wait_event  |                            description
---------+------------------+-------------+-------------------------------------------------------------------
  2937546 | pgbench          | WALInitSync | Waiting for a newly initialized WAL file to reach durable storage
(1 row)

There is still some work to be done to generate the pg_wait_event.dat file, specially when the
same wait event name can be found in multiple places (like for example "WALWrite" in IO and LWLock),
leading to:

postgres=# select * from pg_wait_event where wait_event_name = 'WALWrite';
  wait_event_name |                                   description
-----------------+----------------------------------------------------------------------------------
  WALWrite        | Waiting for a write to a WAL file. Waiting for WAL buffers to be written to disk
  WALWrite        | Waiting for WAL buffers to be written to disk
(2 rows)

which is obviously not right (we'll probably have to add the wait class name to the game).

I'm sharing it now (even if it's still WIP) so that you can start sharing your thoughts
about it.

[1]: https://twitter.com/Ycolin/status/1676598065048743948

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v1-0001-pg_wait_event.patch (text/plain, 10.6K) download From c56ce13af7193bf0d679ec0a7533ab686464f34e Mon Sep 17 00:00:00 2001 From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Date: Tue, 1 Aug 2023 03:55:51 +0000 Subject: [PATCH v1] pg_wait_event Adding a new shared catalog relation, namely pg_wait_event, that describes the wait events. The content is auto-generated with generate-wait_event_types.pl. --- doc/src/sgml/catalogs.sgml | 62 ++++++++++++++++++ src/backend/catalog/.gitignore | 1 + src/backend/catalog/Makefile | 13 ++-- src/backend/catalog/catalog.c | 4 +- .../activity/generate-wait_event_types.pl | 65 ++++++++++++++++++- src/include/catalog/catversion.h | 2 +- src/include/catalog/pg_wait_event.h | 45 +++++++++++++ 7 files changed, 184 insertions(+), 8 deletions(-) 26.7% doc/src/sgml/ 16.5% src/backend/catalog/ 33.6% src/backend/utils/activity/ 23.0% src/include/catalog/ diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index 307ad88b50..f181a5cedb 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -369,6 +369,11 @@ <entry><link linkend="catalog-pg-user-mapping"><structname>pg_user_mapping</structname></link></entry> <entry>mappings of users to foreign servers</entry> </row> + + <row> + <entry><link linkend="catalog-pg-wait-event"><structname>pg_wait_event</structname></link></entry> + <entry>wait events description</entry> + </row> </tbody> </tgroup> </table> @@ -9668,4 +9673,61 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l </table> </sect1> + + <sect1 id="catalog-pg-wait-event"> + <title><structname>pg_wait_event</structname></title> + + <indexterm zone="catalog-pg-wait-event"> + <primary>pg_wait_event</primary> + </indexterm> + + <para> + The catalog <structname>pg_wait_event</structname> stores description about + the wait events. + </para> + + <para> + Unlike most system catalogs, <structname>pg_wait_event</structname> + is shared across all databases of a cluster: there is only one + copy of <structname>pg_wait_event</structname> per cluster, not + one per database. + </para> + + <table> + <title><structname>pg_wait_event</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>wait_event_name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>text</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/backend/catalog/.gitignore b/src/backend/catalog/.gitignore index 237ff54165..7f2f3045dc 100644 --- a/src/backend/catalog/.gitignore +++ b/src/backend/catalog/.gitignore @@ -4,3 +4,4 @@ /system_constraints.sql /pg_*_d.h /bki-stamp +/pg_wait_event.dat diff --git a/src/backend/catalog/Makefile b/src/backend/catalog/Makefile index a60107bf94..68a97fbdcd 100644 --- a/src/backend/catalog/Makefile +++ b/src/backend/catalog/Makefile @@ -72,7 +72,8 @@ CATALOG_HEADERS := \ pg_collation.h pg_parameter_acl.h pg_partitioned_table.h \ pg_range.h pg_transform.h \ pg_sequence.h pg_publication.h pg_publication_namespace.h \ - pg_publication_rel.h pg_subscription.h pg_subscription_rel.h + pg_publication_rel.h pg_subscription.h pg_subscription_rel.h \ + pg_wait_event.h GENERATED_HEADERS := $(CATALOG_HEADERS:%.h=%_d.h) schemapg.h system_fk_info.h @@ -89,9 +90,13 @@ POSTGRES_BKI_DATA = $(addprefix $(top_srcdir)/src/include/catalog/,\ pg_ts_template.dat pg_type.dat \ ) -all: distprep generated-header-symlinks +all: distprep generated-header-symlinks pg_wait_event.dat -distprep: bki-stamp +pg_wait_event.dat: + $(PERL) $(top_srcdir)/src/backend/utils/activity/generate-wait_event_types.pl --data $(top_srcdir)/src/backend/utils/activity/wait_event_names.txt + $(LN_S) "$(top_srcdir)/src/backend/catalog/pg_wait_event.dat" '$(top_srcdir)/src/include/catalog/pg_wait_event.dat' + +distprep: pg_wait_event.dat bki-stamp .PHONY: generated-header-symlinks @@ -143,4 +148,4 @@ uninstall-data: clean: maintainer-clean: clean - rm -f bki-stamp postgres.bki system_constraints.sql $(GENERATED_HEADERS) + rm -f pg_wait_event.dat bki-stamp postgres.bki system_constraints.sql $(GENERATED_HEADERS) $(top_srcdir)/src/include/catalog/pg_wait_event.dat diff --git a/src/backend/catalog/catalog.c b/src/backend/catalog/catalog.c index 1bf6c5633c..da12247551 100644 --- a/src/backend/catalog/catalog.c +++ b/src/backend/catalog/catalog.c @@ -41,6 +41,7 @@ #include "catalog/pg_subscription.h" #include "catalog/pg_tablespace.h" #include "catalog/pg_type.h" +#include "catalog/pg_wait_event.h" #include "miscadmin.h" #include "storage/fd.h" #include "utils/fmgroids.h" @@ -255,7 +256,8 @@ IsSharedRelation(Oid relationId) relationId == SharedDescriptionRelationId || relationId == SharedSecLabelRelationId || relationId == SubscriptionRelationId || - relationId == TableSpaceRelationId) + relationId == TableSpaceRelationId || + relationId == WaitEventRelationId) return true; /* These are their indexes */ if (relationId == AuthIdOidIndexId || diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..81f21eaf44 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -5,6 +5,7 @@ # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) +# - pg_wait_event.dat (if --data is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group # Portions Copyright (c) 1994, Regents of the University of California @@ -20,6 +21,7 @@ use Getopt::Long; my $output_path = '.'; my $gen_docs = 0; my $gen_code = 0; +my $gen_data = 0; my $continue = "\n"; my %hashwe; @@ -27,14 +29,21 @@ my %hashwe; GetOptions( 'outdir:s' => \$output_path, 'docs' => \$gen_docs, + 'data' => \$gen_data, 'code' => \$gen_code) || usage(); -die "Needs to specify --docs or --code" - if (!$gen_docs && !$gen_code); +die "Needs to specify --docs, --code or --data" + if (!$gen_docs && !$gen_code && !$gen_data); die "Not possible to specify --docs and --code simultaneously" if ($gen_docs && $gen_code); +die "Not possible to specify --docs and --data simultaneously" + if ($gen_docs && $gen_data); + +die "Not possible to specify --code and --data simultaneously" + if ($gen_code && $gen_data); + open my $wait_event_names, '<', $ARGV[0] or die; my @lines; @@ -239,7 +248,59 @@ elsif ($gen_docs) rename($stmp, "$output_path/wait_event_types.sgml") || die "rename: $stmp to $output_path/wait_event_types.sgml: $!"; } +# Generate the .dat file. +elsif ($gen_data) +{ + # Include PID in suffix in case parallel make runs this script + # multiple times. + my $dtmp = "$output_path/pg_wait_event.dat.tmp$$"; + open my $d, '>', $dtmp or die "Could not open $dtmp: $!"; + + my $data_header_comment = +'#---------------------------------------------------------------------- +# +# %s +# Initial contents of the pg_wait_event system catalog. +# +# Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group +# Portions Copyright (c) 1994, Regents of the University of California +# +# NOTES +# ****************************** +# *** DO NOT EDIT THIS FILE! *** +# ****************************** +# +# It has been GENERATED by src/backend/utils/activity/generate-wait_event_types.pl +# +#---------------------------------------------------------------------- +'; + + printf $d $data_header_comment, 'pg_wait_event.dat'; + printf $d "[\n\n"; + + # uc() is being used to force the comparison to be case-insensitive. + foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) + { + foreach my $wev (@{ $hashwe{$waitclass} }) + { + my $new_desc = substr $wev->[2], 1, -2; + $new_desc =~ s/'/\\'/g; + $new_desc =~ s/<.*>(.*?)<.*>/$1/g; + $new_desc =~ s/<xref linkend="guc-(.*?)"\/>/$1/g; + $new_desc =~ s/; see.*$//; + + printf $d "{ wait_event_name => '$wev->[1]', description => '%s' },\n", + $new_desc; + } + } + + printf $d "\n]"; + close $d; + + rename($dtmp, "$output_path/pg_wait_event.dat") + || die "rename: $dtmp to $output_path/pg_wait_event.dat: $!"; +} close $wait_event_names; sub usage diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index f507b49bb2..5a534771ed 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -57,6 +57,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 202307261 +#define CATALOG_VERSION_NO 202308011 #endif diff --git a/src/include/catalog/pg_wait_event.h b/src/include/catalog/pg_wait_event.h new file mode 100644 index 0000000000..9647d45bd1 --- /dev/null +++ b/src/include/catalog/pg_wait_event.h @@ -0,0 +1,45 @@ +/*------------------------------------------------------------------------- + * + * pg_wait_event.h + * definition of the "wait event" system catalog (pg_wait_event) + * + * + * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/catalog/pg_wait_event.h + * + * NOTES + * The Catalog.pm module reads this file and derives schema + * information. + * + *------------------------------------------------------------------------- + */ +#ifndef PG_SE_H +#define PG_WAIT_EVENT_H + +#include "catalog/genbki.h" +#include "catalog/pg_wait_event_d.h" + +/* ---------------- + * pg_wait_event definition. cpp turns this into + * typedef struct FormData_pg_wait_event + * ---------------- + */ +CATALOG(pg_wait_event,8403,WaitEventRelationId) BKI_SHARED_RELATION +{ +#ifdef CATALOG_VARLEN /* variable-length fields start here */ + text wait_event_name BKI_FORCE_NOT_NULL; + + text description BKI_FORCE_NOT_NULL; +#endif +} FormData_pg_wait_event; + +/* ---------------- + * Form_pg_wait_event corresponds to a pointer to a tuple with + * the format of pg_wait_event relation. + * ---------------- + */ +typedef FormData_pg_wait_event * Form_pg_wait_event; + +#endif /* PG_WAIT_EVENT_H */ -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-04 15:08 ` Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Tom Lane @ 2023-08-04 15:08 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

"Drouvot, Bertrand" <bertranddrouvot.pg@gmail.com> writes:
> Now that fa88928470 generates automatically code and documentation
> related to wait events, why not exposing the wait events description
> through a system catalog relation?

I think you'd be better off making this a view over a set-returning
function.  The nearby work to allow run-time extensibility of the
set of wait events is not going to be happy with a static catalog.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
@ 2023-08-07 08:23   ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-07 08:23 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi,

On 8/4/23 5:08 PM, Tom Lane wrote:
> "Drouvot, Bertrand" <bertranddrouvot.pg@gmail.com> writes:
>> Now that fa88928470 generates automatically code and documentation
>> related to wait events, why not exposing the wait events description
>> through a system catalog relation?
> 
> I think you'd be better off making this a view over a set-returning
> function.  The nearby work to allow run-time extensibility of the
> set of wait events is not going to be happy with a static catalog.

Oh right, good point, thanks!: I'll come back with a new patch version to make
use of SRF instead.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-07 15:11     ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-07 15:11 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi,

On 8/7/23 10:23 AM, Drouvot, Bertrand wrote:
> Hi,
> 
> On 8/4/23 5:08 PM, Tom Lane wrote:
>> "Drouvot, Bertrand" <bertranddrouvot.pg@gmail.com> writes:
>>> Now that fa88928470 generates automatically code and documentation
>>> related to wait events, why not exposing the wait events description
>>> through a system catalog relation?
>>
>> I think you'd be better off making this a view over a set-returning
>> function.  The nearby work to allow run-time extensibility of the
>> set of wait events is not going to be happy with a static catalog.
> 
> Oh right, good point, thanks!: I'll come back with a new patch version to make
> use of SRF instead.

Please find attached v2 making use of SRF.

v2:

- adds a new pg_wait_event.c that acts as the "template" for the SRF
- generates pg_wait_event_insert.c (through generate-wait_event_types.pl) that
is included into pg_wait_event.c

That way I think it's flexible enough to add more code if needed in the SRF.

The patch also:

- updates the doc
- works with autoconf and meson
- adds a simple test

I'm adding a new CF entry for it.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v2-0001-pg_wait_event.patch (text/plain, 16.1K) download From 63f1316b2c160b72bdc1bcff6272e2888fce0db4 Mon Sep 17 00:00:00 2001 From: bdrouvotAWS <bdrouvot@amazon.com> Date: Sat, 5 Aug 2023 12:39:42 +0000 Subject: [PATCH v2] pg_wait_event Adding a new system view, namely pg_wait_event, that describes the wait events. --- doc/src/sgml/system-views.sgml | 55 ++++++++++++++ src/backend/catalog/system_views.sql | 3 + src/backend/utils/activity/.gitignore | 1 + src/backend/utils/activity/Makefile | 6 +- .../activity/generate-wait_event_types.pl | 76 +++++++++++++------ src/backend/utils/activity/meson.build | 1 + src/backend/utils/activity/pg_wait_event.c | 41 ++++++++++ src/include/catalog/pg_proc.dat | 6 ++ src/include/utils/meson.build | 4 +- src/test/regress/expected/rules.out | 3 + src/test/regress/expected/sysviews.out | 8 ++ src/test/regress/sql/sysviews.sql | 4 + src/tools/msvc/clean.bat | 1 + 13 files changed, 183 insertions(+), 26 deletions(-) 19.8% doc/src/sgml/ 60.6% src/backend/utils/activity/ 5.3% src/include/catalog/ 5.7% src/test/regress/expected/ 3.0% src/test/regress/sql/ 5.3% src/ diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 57b228076e..e9cbeff682 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -221,6 +221,11 @@ <entry>views</entry> </row> + <row> + <entry><link linkend="view-pg-wait-event"><structname>pg_wait_event</structname></link></entry> + <entry>wait events</entry> + </row> + </tbody> </tgroup> </table> @@ -4825,4 +4830,54 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx </table> </sect1> + + <sect1 id="view-pg-wait-event"> + <title><structname>pg_wait_event</structname></title> + + <indexterm zone="view-pg-wait-event"> + <primary>pg_wait_event</primary> + </indexterm> + + <para> + The view <structname>pg_wait_event</structname> provides description about the + wait events. + </para> + + <table> + <title><structname>pg_wait_event</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>wait_event_name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>texte</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index af65af6bdd..f86a4dd770 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1342,3 +1342,6 @@ CREATE VIEW pg_stat_subscription_stats AS ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; + +CREATE VIEW pg_wait_event AS + SELECT * FROM pg_get_wait_events() AS we; diff --git a/src/backend/utils/activity/.gitignore b/src/backend/utils/activity/.gitignore index d77079285b..ad089a0b63 100644 --- a/src/backend/utils/activity/.gitignore +++ b/src/backend/utils/activity/.gitignore @@ -1,2 +1,3 @@ /pgstat_wait_event.c /wait_event_types.h +/pg_wait_event_insert.c diff --git a/src/backend/utils/activity/Makefile b/src/backend/utils/activity/Makefile index f1117745d4..8595e6ea77 100644 --- a/src/backend/utils/activity/Makefile +++ b/src/backend/utils/activity/Makefile @@ -32,10 +32,14 @@ OBJS = \ pgstat_subscription.o \ pgstat_wal.o \ pgstat_xact.o \ + pg_wait_event.o \ wait_event.o include $(top_srcdir)/src/backend/common.mk +pg_wait_event.o: pg_wait_event_insert.c +pg_wait_event_insert.c: wait_event_types.h + wait_event.o: pgstat_wait_event.c pgstat_wait_event.c: wait_event_types.h touch $@ @@ -44,4 +48,4 @@ wait_event_types.h: $(top_srcdir)/src/backend/utils/activity/wait_event_names.tx $(PERL) $(srcdir)/generate-wait_event_types.pl --code $< maintainer-clean: clean - rm -f wait_event_types.h pgstat_wait_event.c + rm -f wait_event_types.h pgstat_wait_event.c pg_wait_event_insert.c diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..6f2b823b6b 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -4,6 +4,7 @@ # Generate wait events support files from wait_event_names.txt: # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) +# - pg_wait_event_insert.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -98,8 +99,10 @@ if ($gen_code) # multiple times. my $htmp = "$output_path/wait_event_types.h.tmp$$"; my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$"; + my $ictmp = "$output_path/pg_wait_event_insert.c.tmp$$"; open my $h, '>', $htmp or die "Could not open $htmp: $!"; open my $c, '>', $ctmp or die "Could not open $ctmp: $!"; + open my $ic, '>', $ictmp or die "Could not open $ictmp: $!"; my $header_comment = '/*------------------------------------------------------------------------- @@ -129,16 +132,17 @@ if ($gen_code) printf $c $header_comment, 'pgstat_wait_event.c'; + printf $ic $header_comment, 'pg_wait_event_insert.c'; + # uc() is being used to force the comparison to be case-insensitive. foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) { - # Don't generate .c and .h files for Extension, LWLock and - # Lock, these are handled independently. - next - if ( $waitclass eq 'WaitEventExtension' - || $waitclass eq 'WaitEventLWLock' - || $waitclass eq 'WaitEventLock'); + # Don't generate .c (except pg_wait_event_insert.c) and .h files for + # Extension, LWLock and Lock, these are handled independently. + my $is_exception = $waitclass eq 'WaitEventExtension' || + $waitclass eq 'WaitEventLWLock' || + $waitclass eq 'WaitEventLock'; my $last = $waitclass; $last =~ s/^WaitEvent//; @@ -147,50 +151,76 @@ if ($gen_code) my $firstpass = 1; my $pg_wait_class; - printf $c - "static const char *\npgstat_get_wait_$lastlc($waitclass w)\n{\n"; - printf $c "\tconst char *event_name = \"unknown wait event\";\n\n"; - printf $c "\tswitch (w)\n\t{\n"; + if (!$is_exception) + { + printf $c + "static const char *\npgstat_get_wait_$lastlc($waitclass w)\n{\n"; + printf $c "\tconst char *event_name = \"unknown wait event\";\n\n"; + printf $c "\tswitch (w)\n\t{\n"; + } foreach my $wev (@{ $hashwe{$waitclass} }) { - if ($firstpass) + if ($firstpass && !$is_exception) { printf $h "typedef enum\n{\n"; $pg_wait_class = "PG_WAIT_" . $lastuc; printf $h "\t%s = %s", $wev->[0], $pg_wait_class; $continue = ",\n"; } - else + elsif (!$is_exception) { printf $h "%s\t%s", $continue, $wev->[0]; $continue = ",\n"; } - $firstpass = 0; - printf $c "\t\t case %s:\n", $wev->[0]; - # Apply quotes to the wait event name string. - printf $c "\t\t\t event_name = \"%s\";\n\t\t\t break;\n", - $wev->[1]; + if (!$is_exception) + { + $firstpass = 0; + + printf $c "\t\t case %s:\n", $wev->[0]; + # Apply quotes to the wait event name string. + printf $c "\t\t\t event_name = \"%s\";\n\t\t\t break;\n", + $wev->[1]; + } + + my $new_desc = substr $wev->[2], 1, -2; + $new_desc =~ s/'/\\'/g; + $new_desc =~ s/<.*>(.*?)<.*>/$1/g; + $new_desc =~ s/<xref linkend="guc-(.*?)"\/>/$1/g; + $new_desc =~ s/; see.*$//; + + printf $ic "\tmemset(values, 0, sizeof(values));\n"; + printf $ic "\tmemset(nulls, 0, sizeof(nulls));\n\n"; + printf $ic "\tvalues[0] = CStringGetTextDatum(\"%s\");\n", $wev->[1]; + printf $ic "\tvalues[1] = CStringGetTextDatum(\"%s\");\n\n", $new_desc; + + printf $ic "\ttuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);\n\n"; } - printf $h "\n} $waitclass;\n\n"; + if (!$is_exception) + { + printf $h "\n} $waitclass;\n\n"; - printf $c - "\t\t\t /* no default case, so that compiler will warn */\n"; - printf $c "\t}\n\n"; - printf $c "\treturn event_name;\n"; - printf $c "}\n\n"; + printf $c + "\t\t\t /* no default case, so that compiler will warn */\n"; + printf $c "\t}\n\n"; + printf $c "\treturn event_name;\n"; + printf $c "}\n\n"; + } } printf $h "#endif /* WAIT_EVENT_TYPES_H */\n"; close $h; close $c; + close $ic; rename($htmp, "$output_path/wait_event_types.h") || die "rename: $htmp to $output_path/wait_event_types.h: $!"; rename($ctmp, "$output_path/pgstat_wait_event.c") || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!"; + rename($ictmp, "$output_path/pg_wait_event_insert.c") + || die "rename: $ictmp to $output_path/pg_wait_event_insert.c: $!"; } # Generate the .sgml file. elsif ($gen_docs) diff --git a/src/backend/utils/activity/meson.build b/src/backend/utils/activity/meson.build index 9633f3623c..774e0bd348 100644 --- a/src/backend/utils/activity/meson.build +++ b/src/backend/utils/activity/meson.build @@ -23,6 +23,7 @@ backend_sources += files( # seems nicer to not add that as an include path for the whole backend. waitevent_sources = files( 'wait_event.c', + 'pg_wait_event.c', ) wait_event = static_library('wait_event_names', diff --git a/src/backend/utils/activity/pg_wait_event.c b/src/backend/utils/activity/pg_wait_event.c new file mode 100644 index 0000000000..9407d18254 --- /dev/null +++ b/src/backend/utils/activity/pg_wait_event.c @@ -0,0 +1,41 @@ +/* ---------- + * pg_wait_event.c + * Wait event reporting infrastructure. + * + * Copyright (c) 2001-2023, PostgreSQL Global Development Group + * + * + * IDENTIFICATION + * src/backend/utils/activity/pg_wait_event.c + * + * NOTES + * + * The "pg_wait_event_insert.c" included in this file is generated by + * src/backend/utils/activity/generate-wait_event_types.pl + * + */ +#include "postgres.h" + +#include "funcapi.h" +#include "utils/builtins.h" + +/* + * This function lists the wait events and their descriptions. + * + * The system view pg_wait_event provides a user interface to this + * SRF. + */ +Datum +pg_get_wait_events(PG_FUNCTION_ARGS) +{ +#define NUM_WAIT_EVENT_TABLES_ELEM 2 + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + Datum values[NUM_WAIT_EVENT_TABLES_ELEM]; + bool nulls[NUM_WAIT_EVENT_TABLES_ELEM]; + + /* Build tuplestore to hold the result rows */ + InitMaterializedSRF(fcinfo, 0); + + #include "pg_wait_event_insert.c" + return (Datum) 0; +} diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..57d8875dee 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5417,6 +5417,12 @@ proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}', proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}', prosrc => 'pg_stat_get_activity' }, +{ oid => '8403', descr => 'describe wait events', + proname => 'pg_get_wait_events', procost => '10', prorows => '100', + proretset => 't', provolatile => 's', prorettype => 'record', + proargtypes => '', proallargtypes => '{text,text}', + proargmodes => '{o,o}', proargnames => '{wait_event_name,description}', + prosrc => 'pg_get_wait_events' }, { oid => '3318', descr => 'statistics: information about progress of backends running maintenance command', proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't', diff --git a/src/include/utils/meson.build b/src/include/utils/meson.build index 6de5d93799..4f14f770ef 100644 --- a/src/include/utils/meson.build +++ b/src/include/utils/meson.build @@ -1,6 +1,6 @@ # Copyright (c) 2022-2023, PostgreSQL Global Development Group -wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c'] +wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c', 'pg_wait_event_insert.c'] wait_event_target = custom_target('wait_event_names', input: files('../../backend/utils/activity/wait_event_names.txt'), output: wait_event_output, @@ -11,7 +11,7 @@ wait_event_target = custom_target('wait_event_names', ], build_by_default: true, install: true, - install_dir: [dir_include_server / 'utils', false], + install_dir: [dir_include_server / 'utils', false, false], ) wait_event_types_h = wait_event_target[0] diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e07afcd4aa..23b5f7dbca 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2631,6 +2631,9 @@ pg_views| SELECT n.nspname AS schemaname, FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char"); +pg_wait_event| SELECT wait_event_name, + description + FROM pg_get_wait_events() we(wait_event_name, description); SELECT tablename, rulename, definition FROM pg_rules WHERE schemaname = 'pg_catalog' ORDER BY tablename, rulename; diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 001c6e7eb9..12309be267 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -134,6 +134,14 @@ select name, setting from pg_settings where name like 'enable%'; enable_tidscan | on (21 rows) +-- There will surely be at least 240 wait events and at least 27 related to WAL +select count(*) > 239 as ok, count(*) FILTER (WHERE description like '%WAL%') > 26 AS ok_wal_desc + from pg_wait_event; + ok | ok_wal_desc +----+------------- + t | t +(1 row) + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 351e469c77..6bc6bd1322 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -55,6 +55,10 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- a regression test run. select name, setting from pg_settings where name like 'enable%'; +-- There will surely be at least 240 wait events and at least 27 related to WAL +select count(*) > 239 as ok, count(*) FILTER (WHERE description like '%WAL%') > 26 AS ok_wal_desc + from pg_wait_event; + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index 7cb23ea894..028d174d87 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -55,6 +55,7 @@ if exist src\include\catalog\header-stamp del /q src\include\catalog\header-stam if exist doc\src\sgml\version.sgml del /q doc\src\sgml\version.sgml if %DIST%==1 if exist src\backend\utils\activity\pgstat_wait_event.c del /q src\backend\utils\activity\pgstat_wait_event.c +if %DIST%==1 if exist src\backend\utils\activity\pg_wait_event_insert.c del /q src\backend\utils\activity\pg_wait_event_insert.c if %DIST%==1 if exist src\backend\utils\activity\wait_event_types.h del /q src\backend\utils\activity\wait_event_types.h if %DIST%==1 if exist src\backend\utils\fmgroids.h del /q src\backend\utils\fmgroids.h if %DIST%==1 if exist src\backend\utils\fmgrprotos.h del /q src\backend\utils\fmgrprotos.h -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-08 02:53       ` Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 34+ messages in thread

From: Kyotaro Horiguchi @ 2023-08-08 02:53 UTC (permalink / raw)
  To: bertranddrouvot.pg@gmail.com; +Cc: tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

At Mon, 7 Aug 2023 17:11:50 +0200, "Drouvot, Bertrand" <bertranddrouvot.pg@gmail.com> wrote in 
> That way I think it's flexible enough to add more code if needed in
> the SRF.
> 
> The patch also:
> 
> - updates the doc
> - works with autoconf and meson
> - adds a simple test
> 
> I'm adding a new CF entry for it.

As I mentioned in another thread, I'm uncertain about our stance on
the class id of the wait event. If a class acts as a namespace, we
should include it in the view. Otherwise, if the class id is just an
attribute of the wait event, we should make the event name unique.

regards.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
@ 2023-08-08 03:05         ` Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Michael Paquier @ 2023-08-08 03:05 UTC (permalink / raw)
  To: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; +Cc: bertranddrouvot.pg@gmail.com; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Tue, Aug 08, 2023 at 11:53:32AM +0900, Kyotaro Horiguchi wrote:
> As I mentioned in another thread, I'm uncertain about our stance on
> the class id of the wait event. If a class acts as a namespace, we
> should include it in the view. Otherwise, if the class id is just an
> attribute of the wait event, we should make the event name unique.

Including the class name in the view makes the most sense to me, FWIW,
as it could be also possible that one reuses an event name in the
existing in-core list, but for extensions.  That's of course not
something I would recommend.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-08 05:37           ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-08 05:37 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; +Cc: tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/8/23 5:05 AM, Michael Paquier wrote:
> On Tue, Aug 08, 2023 at 11:53:32AM +0900, Kyotaro Horiguchi wrote:
>> As I mentioned in another thread, I'm uncertain about our stance on
>> the class id of the wait event. If a class acts as a namespace, we
>> should include it in the view. Otherwise, if the class id is just an
>> attribute of the wait event, we should make the event name unique.
> 
> Including the class name in the view makes the most sense to me, FWIW,
> as it could be also possible that one reuses an event name in the
> existing in-core list, but for extensions.  That's of course not
> something I would recommend.

Thanks Kyotaro-san and Michael for the feedback. I do agree and will
add the class name.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-08 08:16             ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-08 08:16 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; +Cc: tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/8/23 7:37 AM, Drouvot, Bertrand wrote:
> Hi,
> 
> On 8/8/23 5:05 AM, Michael Paquier wrote:
>> On Tue, Aug 08, 2023 at 11:53:32AM +0900, Kyotaro Horiguchi wrote:
>>> As I mentioned in another thread, I'm uncertain about our stance on
>>> the class id of the wait event. If a class acts as a namespace, we
>>> should include it in the view. Otherwise, if the class id is just an
>>> attribute of the wait event, we should make the event name unique.
>>
>> Including the class name in the view makes the most sense to me, FWIW,
>> as it could be also possible that one reuses an event name in the
>> existing in-core list, but for extensions.  That's of course not
>> something I would recommend.
> 
> Thanks Kyotaro-san and Michael for the feedback. I do agree and will
> add the class name.
> 

Please find attached v3 adding the wait event types.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v3-0001-pg_wait_event.patch (text/plain, 16.7K) download From b54aa2dd33835a83bfe08a99338874200512fdf0 Mon Sep 17 00:00:00 2001 From: bdrouvotAWS <bdrouvot@amazon.com> Date: Sat, 5 Aug 2023 12:39:42 +0000 Subject: [PATCH v3] pg_wait_event Adding a new system view, namely pg_wait_event, that describes the wait events. --- doc/src/sgml/system-views.sgml | 64 +++++++++++++++ src/backend/catalog/system_views.sql | 3 + src/backend/utils/activity/.gitignore | 1 + src/backend/utils/activity/Makefile | 6 +- .../activity/generate-wait_event_types.pl | 77 +++++++++++++------ src/backend/utils/activity/meson.build | 1 + src/backend/utils/activity/pg_wait_event.c | 41 ++++++++++ src/include/catalog/pg_proc.dat | 6 ++ src/include/utils/meson.build | 4 +- src/test/regress/expected/rules.out | 4 + src/test/regress/expected/sysviews.out | 11 +++ src/test/regress/sql/sysviews.sql | 7 ++ src/tools/msvc/clean.bat | 1 + 13 files changed, 200 insertions(+), 26 deletions(-) 21.6% doc/src/sgml/ 56.7% src/backend/utils/activity/ 5.2% src/include/catalog/ 7.4% src/test/regress/expected/ 4.0% src/test/regress/sql/ 4.9% src/ diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 57b228076e..ed26c8326f 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -221,6 +221,11 @@ <entry>views</entry> </row> + <row> + <entry><link linkend="view-pg-wait-event"><structname>pg_wait_event</structname></link></entry> + <entry>wait events</entry> + </row> + </tbody> </tgroup> </table> @@ -4825,4 +4830,63 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx </table> </sect1> + + <sect1 id="view-pg-wait-event"> + <title><structname>pg_wait_event</structname></title> + + <indexterm zone="view-pg-wait-event"> + <primary>pg_wait_event</primary> + </indexterm> + + <para> + The view <structname>pg_wait_event</structname> provides description about the + wait events. + </para> + + <table> + <title><structname>pg_wait_event</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>wait_event_type</structfield> <type>text</type> + </para> + <para> + Wait event type + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>wait_event_name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>texte</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index af65af6bdd..f86a4dd770 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1342,3 +1342,6 @@ CREATE VIEW pg_stat_subscription_stats AS ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; + +CREATE VIEW pg_wait_event AS + SELECT * FROM pg_get_wait_events() AS we; diff --git a/src/backend/utils/activity/.gitignore b/src/backend/utils/activity/.gitignore index d77079285b..ad089a0b63 100644 --- a/src/backend/utils/activity/.gitignore +++ b/src/backend/utils/activity/.gitignore @@ -1,2 +1,3 @@ /pgstat_wait_event.c /wait_event_types.h +/pg_wait_event_insert.c diff --git a/src/backend/utils/activity/Makefile b/src/backend/utils/activity/Makefile index f1117745d4..8595e6ea77 100644 --- a/src/backend/utils/activity/Makefile +++ b/src/backend/utils/activity/Makefile @@ -32,10 +32,14 @@ OBJS = \ pgstat_subscription.o \ pgstat_wal.o \ pgstat_xact.o \ + pg_wait_event.o \ wait_event.o include $(top_srcdir)/src/backend/common.mk +pg_wait_event.o: pg_wait_event_insert.c +pg_wait_event_insert.c: wait_event_types.h + wait_event.o: pgstat_wait_event.c pgstat_wait_event.c: wait_event_types.h touch $@ @@ -44,4 +48,4 @@ wait_event_types.h: $(top_srcdir)/src/backend/utils/activity/wait_event_names.tx $(PERL) $(srcdir)/generate-wait_event_types.pl --code $< maintainer-clean: clean - rm -f wait_event_types.h pgstat_wait_event.c + rm -f wait_event_types.h pgstat_wait_event.c pg_wait_event_insert.c diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..c3f58acb4b 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -4,6 +4,7 @@ # Generate wait events support files from wait_event_names.txt: # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) +# - pg_wait_event_insert.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -98,8 +99,10 @@ if ($gen_code) # multiple times. my $htmp = "$output_path/wait_event_types.h.tmp$$"; my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$"; + my $ictmp = "$output_path/pg_wait_event_insert.c.tmp$$"; open my $h, '>', $htmp or die "Could not open $htmp: $!"; open my $c, '>', $ctmp or die "Could not open $ctmp: $!"; + open my $ic, '>', $ictmp or die "Could not open $ictmp: $!"; my $header_comment = '/*------------------------------------------------------------------------- @@ -129,16 +132,17 @@ if ($gen_code) printf $c $header_comment, 'pgstat_wait_event.c'; + printf $ic $header_comment, 'pg_wait_event_insert.c'; + # uc() is being used to force the comparison to be case-insensitive. foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) { - # Don't generate .c and .h files for Extension, LWLock and - # Lock, these are handled independently. - next - if ( $waitclass eq 'WaitEventExtension' - || $waitclass eq 'WaitEventLWLock' - || $waitclass eq 'WaitEventLock'); + # Don't generate .c (except pg_wait_event_insert.c) and .h files for + # Extension, LWLock and Lock, these are handled independently. + my $is_exception = $waitclass eq 'WaitEventExtension' || + $waitclass eq 'WaitEventLWLock' || + $waitclass eq 'WaitEventLock'; my $last = $waitclass; $last =~ s/^WaitEvent//; @@ -147,50 +151,77 @@ if ($gen_code) my $firstpass = 1; my $pg_wait_class; - printf $c - "static const char *\npgstat_get_wait_$lastlc($waitclass w)\n{\n"; - printf $c "\tconst char *event_name = \"unknown wait event\";\n\n"; - printf $c "\tswitch (w)\n\t{\n"; + if (!$is_exception) + { + printf $c + "static const char *\npgstat_get_wait_$lastlc($waitclass w)\n{\n"; + printf $c "\tconst char *event_name = \"unknown wait event\";\n\n"; + printf $c "\tswitch (w)\n\t{\n"; + } foreach my $wev (@{ $hashwe{$waitclass} }) { - if ($firstpass) + if ($firstpass && !$is_exception) { printf $h "typedef enum\n{\n"; $pg_wait_class = "PG_WAIT_" . $lastuc; printf $h "\t%s = %s", $wev->[0], $pg_wait_class; $continue = ",\n"; } - else + elsif (!$is_exception) { printf $h "%s\t%s", $continue, $wev->[0]; $continue = ",\n"; } - $firstpass = 0; - printf $c "\t\t case %s:\n", $wev->[0]; - # Apply quotes to the wait event name string. - printf $c "\t\t\t event_name = \"%s\";\n\t\t\t break;\n", - $wev->[1]; + if (!$is_exception) + { + $firstpass = 0; + + printf $c "\t\t case %s:\n", $wev->[0]; + # Apply quotes to the wait event name string. + printf $c "\t\t\t event_name = \"%s\";\n\t\t\t break;\n", + $wev->[1]; + } + + my $new_desc = substr $wev->[2], 1, -2; + $new_desc =~ s/'/\\'/g; + $new_desc =~ s/<.*>(.*?)<.*>/$1/g; + $new_desc =~ s/<xref linkend="guc-(.*?)"\/>/$1/g; + $new_desc =~ s/; see.*$//; + + printf $ic "\tmemset(values, 0, sizeof(values));\n"; + printf $ic "\tmemset(nulls, 0, sizeof(nulls));\n\n"; + printf $ic "\tvalues[0] = CStringGetTextDatum(\"%s\");\n", $last; + printf $ic "\tvalues[1] = CStringGetTextDatum(\"%s\");\n", $wev->[1]; + printf $ic "\tvalues[2] = CStringGetTextDatum(\"%s\");\n\n", $new_desc; + + printf $ic "\ttuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);\n\n"; } - printf $h "\n} $waitclass;\n\n"; + if (!$is_exception) + { + printf $h "\n} $waitclass;\n\n"; - printf $c - "\t\t\t /* no default case, so that compiler will warn */\n"; - printf $c "\t}\n\n"; - printf $c "\treturn event_name;\n"; - printf $c "}\n\n"; + printf $c + "\t\t\t /* no default case, so that compiler will warn */\n"; + printf $c "\t}\n\n"; + printf $c "\treturn event_name;\n"; + printf $c "}\n\n"; + } } printf $h "#endif /* WAIT_EVENT_TYPES_H */\n"; close $h; close $c; + close $ic; rename($htmp, "$output_path/wait_event_types.h") || die "rename: $htmp to $output_path/wait_event_types.h: $!"; rename($ctmp, "$output_path/pgstat_wait_event.c") || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!"; + rename($ictmp, "$output_path/pg_wait_event_insert.c") + || die "rename: $ictmp to $output_path/pg_wait_event_insert.c: $!"; } # Generate the .sgml file. elsif ($gen_docs) diff --git a/src/backend/utils/activity/meson.build b/src/backend/utils/activity/meson.build index 9633f3623c..774e0bd348 100644 --- a/src/backend/utils/activity/meson.build +++ b/src/backend/utils/activity/meson.build @@ -23,6 +23,7 @@ backend_sources += files( # seems nicer to not add that as an include path for the whole backend. waitevent_sources = files( 'wait_event.c', + 'pg_wait_event.c', ) wait_event = static_library('wait_event_names', diff --git a/src/backend/utils/activity/pg_wait_event.c b/src/backend/utils/activity/pg_wait_event.c new file mode 100644 index 0000000000..e6bfe36d4d --- /dev/null +++ b/src/backend/utils/activity/pg_wait_event.c @@ -0,0 +1,41 @@ +/* ---------- + * pg_wait_event.c + * Wait event reporting infrastructure. + * + * Copyright (c) 2001-2023, PostgreSQL Global Development Group + * + * + * IDENTIFICATION + * src/backend/utils/activity/pg_wait_event.c + * + * NOTES + * + * The "pg_wait_event_insert.c" included in this file is generated by + * src/backend/utils/activity/generate-wait_event_types.pl + * + */ +#include "postgres.h" + +#include "funcapi.h" +#include "utils/builtins.h" + +/* + * This function lists the wait events and their descriptions. + * + * The system view pg_wait_event provides a user interface to this + * SRF. + */ +Datum +pg_get_wait_events(PG_FUNCTION_ARGS) +{ +#define NUM_WAIT_EVENT_TABLES_ELEM 3 + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + Datum values[NUM_WAIT_EVENT_TABLES_ELEM]; + bool nulls[NUM_WAIT_EVENT_TABLES_ELEM]; + + /* Build tuplestore to hold the result rows */ + InitMaterializedSRF(fcinfo, 0); + + #include "pg_wait_event_insert.c" + return (Datum) 0; +} diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..6d10d2e5be 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5417,6 +5417,12 @@ proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}', proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}', prosrc => 'pg_stat_get_activity' }, +{ oid => '8403', descr => 'describe wait events', + proname => 'pg_get_wait_events', procost => '10', prorows => '100', + proretset => 't', provolatile => 's', prorettype => 'record', + proargtypes => '', proallargtypes => '{text,text,text}', + proargmodes => '{o,o,o}', proargnames => '{wait_event_type,wait_event_name,description}', + prosrc => 'pg_get_wait_events' }, { oid => '3318', descr => 'statistics: information about progress of backends running maintenance command', proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't', diff --git a/src/include/utils/meson.build b/src/include/utils/meson.build index 6de5d93799..4f14f770ef 100644 --- a/src/include/utils/meson.build +++ b/src/include/utils/meson.build @@ -1,6 +1,6 @@ # Copyright (c) 2022-2023, PostgreSQL Global Development Group -wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c'] +wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c', 'pg_wait_event_insert.c'] wait_event_target = custom_target('wait_event_names', input: files('../../backend/utils/activity/wait_event_names.txt'), output: wait_event_output, @@ -11,7 +11,7 @@ wait_event_target = custom_target('wait_event_names', ], build_by_default: true, install: true, - install_dir: [dir_include_server / 'utils', false], + install_dir: [dir_include_server / 'utils', false, false], ) wait_event_types_h = wait_event_target[0] diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e07afcd4aa..f160cb5642 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2631,6 +2631,10 @@ pg_views| SELECT n.nspname AS schemaname, FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char"); +pg_wait_event| SELECT wait_event_type, + wait_event_name, + description + FROM pg_get_wait_events() we(wait_event_type, wait_event_name, description); SELECT tablename, rulename, definition FROM pg_rules WHERE schemaname = 'pg_catalog' ORDER BY tablename, rulename; diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 001c6e7eb9..bcf251c0c8 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -134,6 +134,17 @@ select name, setting from pg_settings where name like 'enable%'; enable_tidscan | on (21 rows) +-- There will surely be at least 9 wait event types, 240 wait events and at +-- least 27 related to WAL +select count(distinct(wait_event_type)) > 8 as ok_type, + count(*) > 239 as ok, + count(*) FILTER (WHERE description like '%WAL%') > 26 AS ok_wal_desc +from pg_wait_event; + ok_type | ok | ok_wal_desc +---------+----+------------- + t | t | t +(1 row) + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 351e469c77..7a9dc6021d 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -55,6 +55,13 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- a regression test run. select name, setting from pg_settings where name like 'enable%'; +-- There will surely be at least 9 wait event types, 240 wait events and at +-- least 27 related to WAL +select count(distinct(wait_event_type)) > 8 as ok_type, + count(*) > 239 as ok, + count(*) FILTER (WHERE description like '%WAL%') > 26 AS ok_wal_desc +from pg_wait_event; + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index 7cb23ea894..028d174d87 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -55,6 +55,7 @@ if exist src\include\catalog\header-stamp del /q src\include\catalog\header-stam if exist doc\src\sgml\version.sgml del /q doc\src\sgml\version.sgml if %DIST%==1 if exist src\backend\utils\activity\pgstat_wait_event.c del /q src\backend\utils\activity\pgstat_wait_event.c +if %DIST%==1 if exist src\backend\utils\activity\pg_wait_event_insert.c del /q src\backend\utils\activity\pg_wait_event_insert.c if %DIST%==1 if exist src\backend\utils\activity\wait_event_types.h del /q src\backend\utils\activity\wait_event_types.h if %DIST%==1 if exist src\backend\utils\fmgroids.h del /q src\backend\utils\fmgroids.h if %DIST%==1 if exist src\backend\utils\fmgrprotos.h del /q src\backend\utils\fmgrprotos.h -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-09 07:56               ` Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Michael Paquier @ 2023-08-09 07:56 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Tue, Aug 08, 2023 at 10:16:37AM +0200, Drouvot, Bertrand wrote:
> Please find attached v3 adding the wait event types.

+-- There will surely be at least 9 wait event types, 240 wait events and at
+-- least 27 related to WAL
+select count(distinct(wait_event_type)) > 8 as ok_type,
+       count(*) > 239 as ok,
+       count(*) FILTER (WHERE description like '%WAL%') > 26 AS ok_wal_desc
+from pg_wait_event;
The point is to check the execution of this function, so this could be
simpler, like that or a GROUP BY clause with the event type:
SELECT count(*) > 0 FROM pg_wait_event;
SELECT wait_event_type, count(*) > 0 AS has_data FROM pg_wait_event
  GROUP BY wait_event_type ORDER BY wait_event_type;

+           printf $ic "\tmemset(values, 0, sizeof(values));\n";
+           printf $ic "\tmemset(nulls, 0, sizeof(nulls));\n\n";
+           printf $ic "\tvalues[0] = CStringGetTextDatum(\"%s\");\n", $last;
+           printf $ic "\tvalues[1] = CStringGetTextDatum(\"%s\");\n", $wev->[1];
+           printf $ic "\tvalues[2] = CStringGetTextDatum(\"%s\");\n\n", $new_desc;

That's overcomplicated for some code generated.  Wouldn't it be
simpler to generate a list of elements, with the code inserting the
tuples materialized looping over it?

+           my $new_desc = substr $wev->[2], 1, -2;
+           $new_desc =~ s/'/\\'/g;
+           $new_desc =~ s/<.*>(.*?)<.*>/$1/g;
+           $new_desc =~ s/<xref linkend="guc-(.*?)"\/>/$1/g;
+           $new_desc =~ s/; see.*$//;
Better to document what this does, the contents produced look good.

+   rename($ictmp, "$output_path/pg_wait_event_insert.c")
+     || die "rename: $ictmp to $output_path/pg_wait_event_insert.c: $!";

 # seems nicer to not add that as an include path for the whole backend.
 waitevent_sources = files(
   'wait_event.c',
+  'pg_wait_event.c',
 )

This could use a name referring to SQL functions, say
wait_event_funcs.c, with a wait_event_data.c or a
wait_event_funcs_data.c?

+       # Don't generate .c (except pg_wait_event_insert.c) and .h files for
+       # Extension, LWLock and Lock, these are handled independently.
+       my $is_exception = $waitclass eq 'WaitEventExtension' ||
+                          $waitclass eq 'WaitEventLWLock' ||
+                          $waitclass eq 'WaitEventLock';
Perhaps it would be cleaner to use a separate loop?
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-10 18:09                 ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-10 18:09 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/9/23 9:56 AM, Michael Paquier wrote:
> On Tue, Aug 08, 2023 at 10:16:37AM +0200, Drouvot, Bertrand wrote:
>> Please find attached v3 adding the wait event types.
> 
> +-- There will surely be at least 9 wait event types, 240 wait events and at
> +-- least 27 related to WAL
> +select count(distinct(wait_event_type)) > 8 as ok_type,
> +       count(*) > 239 as ok,
> +       count(*) FILTER (WHERE description like '%WAL%') > 26 AS ok_wal_desc
> +from pg_wait_event;
> The point is to check the execution of this function, so this could be
> simpler, like that or a GROUP BY clause with the event type:
> SELECT count(*) > 0 FROM pg_wait_event;
> SELECT wait_event_type, count(*) > 0 AS has_data FROM pg_wait_event
>    GROUP BY wait_event_type ORDER BY wait_event_type;
> 

Thanks for looking at it!
Right, so v4 attached is just testing "SELECT count(*) > 0 FROM pg_wait_event;",
that does look enough to test.

> +           printf $ic "\tmemset(values, 0, sizeof(values));\n";
> +           printf $ic "\tmemset(nulls, 0, sizeof(nulls));\n\n";
> +           printf $ic "\tvalues[0] = CStringGetTextDatum(\"%s\");\n", $last;
> +           printf $ic "\tvalues[1] = CStringGetTextDatum(\"%s\");\n", $wev->[1];
> +           printf $ic "\tvalues[2] = CStringGetTextDatum(\"%s\");\n\n", $new_desc;
> 
> That's overcomplicated for some code generated.  Wouldn't it be
> simpler to generate a list of elements, with the code inserting the
> tuples materialized looping over it?
> 

Yeah, agree thanks!
In v4, the perl script now appends the wait events in a List that way:

"
             printf $ic "\telement = (wait_event_element *) palloc(sizeof(wait_event_element));\n";

             printf $ic "\telement->wait_event_type = \"%s\";\n", $last;
             printf $ic "\telement->wait_event_name = \"%s\";\n", $wev->[1];
             printf $ic "\telement->wait_event_description = \"%s\";\n\n", $new_desc;

             printf $ic "\twait_event = lappend(wait_event, element);\n\n";
"

And the C function pg_get_wait_events() now iterates over this List.

> +           my $new_desc = substr $wev->[2], 1, -2;
> +           $new_desc =~ s/'/\\'/g;
> +           $new_desc =~ s/<.*>(.*?)<.*>/$1/g;
> +           $new_desc =~ s/<xref linkend="guc-(.*?)"\/>/$1/g;
> +           $new_desc =~ s/; see.*$//;
> Better to document what this does,

good idea...

I had to turn them "on" one by one to recall why they are there...;-)
Done in v4.

> the contents produced look good.

yeap

> 
> +   rename($ictmp, "$output_path/pg_wait_event_insert.c")
> +     || die "rename: $ictmp to $output_path/pg_wait_event_insert.c: $!";
> 
>   # seems nicer to not add that as an include path for the whole backend.
>   waitevent_sources = files(
>     'wait_event.c',
> +  'pg_wait_event.c',
>   )
> 
> This could use a name referring to SQL functions, say
> wait_event_funcs.c, with a wait_event_data.c or a
> wait_event_funcs_data.c?

That sounds better indeed, thanks! v4 is using wait_event_funcs.c and
wait_event_funcs_data.c.

> 
> +       # Don't generate .c (except pg_wait_event_insert.c) and .h files for
> +       # Extension, LWLock and Lock, these are handled independently.
> +       my $is_exception = $waitclass eq 'WaitEventExtension' ||
> +                          $waitclass eq 'WaitEventLWLock' ||
> +                          $waitclass eq 'WaitEventLock';
> Perhaps it would be cleaner to use a separate loop?

Agree that's worth it given the fact that iterating one more time is not that
costly here.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v4-0001-pg_wait_event.patch (text/plain, 15.5K) download From aa3e81810daaba7b1b2e05219ef662f2fd0607f8 Mon Sep 17 00:00:00 2001 From: bdrouvotAWS <bdrouvot@amazon.com> Date: Sat, 5 Aug 2023 12:39:42 +0000 Subject: [PATCH v4] pg_wait_event Adding a new system view, namely pg_wait_event, that describes the wait events. --- doc/src/sgml/system-views.sgml | 64 +++++++++++++++++ src/backend/catalog/system_views.sql | 3 + src/backend/utils/activity/.gitignore | 1 + src/backend/utils/activity/Makefile | 6 +- .../activity/generate-wait_event_types.pl | 46 ++++++++++++- src/backend/utils/activity/meson.build | 1 + src/backend/utils/activity/wait_event_funcs.c | 69 +++++++++++++++++++ src/include/catalog/pg_proc.dat | 6 ++ src/include/utils/meson.build | 4 +- src/test/regress/expected/rules.out | 4 ++ src/test/regress/expected/sysviews.out | 7 ++ src/test/regress/sql/sysviews.sql | 3 + src/tools/msvc/clean.bat | 1 + 13 files changed, 209 insertions(+), 6 deletions(-) 24.4% doc/src/sgml/ 58.5% src/backend/utils/activity/ 5.9% src/include/catalog/ 4.1% src/test/regress/expected/ 6.9% src/ diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 57b228076e..ed26c8326f 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -221,6 +221,11 @@ <entry>views</entry> </row> + <row> + <entry><link linkend="view-pg-wait-event"><structname>pg_wait_event</structname></link></entry> + <entry>wait events</entry> + </row> + </tbody> </tgroup> </table> @@ -4825,4 +4830,63 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx </table> </sect1> + + <sect1 id="view-pg-wait-event"> + <title><structname>pg_wait_event</structname></title> + + <indexterm zone="view-pg-wait-event"> + <primary>pg_wait_event</primary> + </indexterm> + + <para> + The view <structname>pg_wait_event</structname> provides description about the + wait events. + </para> + + <table> + <title><structname>pg_wait_event</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>wait_event_type</structfield> <type>text</type> + </para> + <para> + Wait event type + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>wait_event_name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>texte</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index af65af6bdd..f86a4dd770 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1342,3 +1342,6 @@ CREATE VIEW pg_stat_subscription_stats AS ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; + +CREATE VIEW pg_wait_event AS + SELECT * FROM pg_get_wait_events() AS we; diff --git a/src/backend/utils/activity/.gitignore b/src/backend/utils/activity/.gitignore index d77079285b..bd0c0c7772 100644 --- a/src/backend/utils/activity/.gitignore +++ b/src/backend/utils/activity/.gitignore @@ -1,2 +1,3 @@ /pgstat_wait_event.c /wait_event_types.h +/wait_event_funcs_data.c diff --git a/src/backend/utils/activity/Makefile b/src/backend/utils/activity/Makefile index f1117745d4..289afe7c2d 100644 --- a/src/backend/utils/activity/Makefile +++ b/src/backend/utils/activity/Makefile @@ -32,10 +32,14 @@ OBJS = \ pgstat_subscription.o \ pgstat_wal.o \ pgstat_xact.o \ + wait_event_funcs.o \ wait_event.o include $(top_srcdir)/src/backend/common.mk +wait_event_funcs.o: wait_event_funcs_data.c +wait_event_funcs_data.c: wait_event_types.h + wait_event.o: pgstat_wait_event.c pgstat_wait_event.c: wait_event_types.h touch $@ @@ -44,4 +48,4 @@ wait_event_types.h: $(top_srcdir)/src/backend/utils/activity/wait_event_names.tx $(PERL) $(srcdir)/generate-wait_event_types.pl --code $< maintainer-clean: clean - rm -f wait_event_types.h pgstat_wait_event.c + rm -f wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..6f4fcd22d2 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -4,6 +4,7 @@ # Generate wait events support files from wait_event_names.txt: # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) +# - wait_event_funcs_data.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -98,8 +99,10 @@ if ($gen_code) # multiple times. my $htmp = "$output_path/wait_event_types.h.tmp$$"; my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$"; + my $wctmp = "$output_path/wait_event_funcs_data.c.tmp$$"; open my $h, '>', $htmp or die "Could not open $htmp: $!"; open my $c, '>', $ctmp or die "Could not open $ctmp: $!"; + open my $wc, '>', $wctmp or die "Could not open $wctmp: $!"; my $header_comment = '/*------------------------------------------------------------------------- @@ -129,12 +132,14 @@ if ($gen_code) printf $c $header_comment, 'pgstat_wait_event.c'; + printf $wc $header_comment, 'wait_event_funcs_data.c'; + + # Generate the pgstat_wait_event.c and wait_event_types.h files # uc() is being used to force the comparison to be case-insensitive. foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) { - - # Don't generate .c and .h files for Extension, LWLock and - # Lock, these are handled independently. + # Don't generate the pgstat_wait_event.c and wait_event_types.h files + # for Extension, LWLock and Lock, these are handled independently. next if ( $waitclass eq 'WaitEventExtension' || $waitclass eq 'WaitEventLWLock' @@ -183,14 +188,49 @@ if ($gen_code) printf $c "}\n\n"; } + # Generate the wait_event_funcs_data.c file + # uc() is being used to force the comparison to be case-insensitive. + foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) + { + my $last = $waitclass; + $last =~ s/^WaitEvent//; + + foreach my $wev (@{ $hashwe{$waitclass} }) + { + my $new_desc = substr $wev->[2], 1, -2; + # put an escape \ in front of ' + $new_desc =~ s/'/\\'/g; + + # just keep text from <whatever>text</wahtever> + $new_desc =~ s/<.*>(.*?)<.*>/$1/g; + + # just keep text from <xref linkend="text"/> + $new_desc =~ s/<xref linkend="guc-(.*?)"\/>/$1/g; + + # remove any "see <xref linkend="text"/>" + $new_desc =~ s/; see.*$//; + + printf $wc "\telement = (wait_event_element *) palloc(sizeof(wait_event_element));\n"; + + printf $wc "\telement->wait_event_type = \"%s\";\n", $last; + printf $wc "\telement->wait_event_name = \"%s\";\n", $wev->[1]; + printf $wc "\telement->wait_event_description = \"%s\";\n\n", $new_desc; + + printf $wc "\twait_event = lappend(wait_event, element);\n\n"; + } + } + printf $h "#endif /* WAIT_EVENT_TYPES_H */\n"; close $h; close $c; + close $wc; rename($htmp, "$output_path/wait_event_types.h") || die "rename: $htmp to $output_path/wait_event_types.h: $!"; rename($ctmp, "$output_path/pgstat_wait_event.c") || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!"; + rename($wctmp, "$output_path/wait_event_funcs_data.c") + || die "rename: $ctmp to $output_path/wait_event_funcs_data.c: $!"; } # Generate the .sgml file. elsif ($gen_docs) diff --git a/src/backend/utils/activity/meson.build b/src/backend/utils/activity/meson.build index 9633f3623c..46a27e7548 100644 --- a/src/backend/utils/activity/meson.build +++ b/src/backend/utils/activity/meson.build @@ -23,6 +23,7 @@ backend_sources += files( # seems nicer to not add that as an include path for the whole backend. waitevent_sources = files( 'wait_event.c', + 'wait_event_funcs.c', ) wait_event = static_library('wait_event_names', diff --git a/src/backend/utils/activity/wait_event_funcs.c b/src/backend/utils/activity/wait_event_funcs.c new file mode 100644 index 0000000000..3e27a637e9 --- /dev/null +++ b/src/backend/utils/activity/wait_event_funcs.c @@ -0,0 +1,69 @@ +/* ---------- + * wait_event_funcs.c + * Wait event reporting infrastructure. + * + * Copyright (c) 2001-2023, PostgreSQL Global Development Group + * + * + * IDENTIFICATION + * src/backend/utils/activity/wait_event_funcs.c + * + * NOTES + * + * The "wait_event_funcs_data.c" included in this file is generated by + * src/backend/utils/activity/generate-wait_event_types.pl + * + */ +#include "postgres.h" + +#include "funcapi.h" +#include "utils/builtins.h" + +/* + * This function lists the wait events and their descriptions. + * + * The system view pg_wait_event provides a user interface to this + * SRF. + */ +Datum +pg_get_wait_events(PG_FUNCTION_ARGS) +{ +#define NUM_WAIT_EVENT_TABLES_ELEM 3 + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + Datum values[NUM_WAIT_EVENT_TABLES_ELEM]; + bool nulls[NUM_WAIT_EVENT_TABLES_ELEM]; + List *wait_event = NIL; + ListCell *cell; + + typedef struct + { + char *wait_event_type; + char *wait_event_name; + char *wait_event_description; + } wait_event_element; + + wait_event_element *element = NULL; + /* Build tuplestore to hold the result rows */ + InitMaterializedSRF(fcinfo, 0); + + /* Populate the wait_event List */ + #include "wait_event_funcs_data.c" + + /* Iterate over the list of wait events */ + foreach(cell, wait_event) + { + + wait_event_element *elem = lfirst(cell); + + memset(values, 0, sizeof(values)); + memset(nulls, 0, sizeof(nulls)); + + values[0] = CStringGetTextDatum(elem->wait_event_type); + values[1] = CStringGetTextDatum(elem->wait_event_name); + values[2] = CStringGetTextDatum(elem->wait_event_description); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + return (Datum) 0; +} diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..6d10d2e5be 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5417,6 +5417,12 @@ proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}', proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}', prosrc => 'pg_stat_get_activity' }, +{ oid => '8403', descr => 'describe wait events', + proname => 'pg_get_wait_events', procost => '10', prorows => '100', + proretset => 't', provolatile => 's', prorettype => 'record', + proargtypes => '', proallargtypes => '{text,text,text}', + proargmodes => '{o,o,o}', proargnames => '{wait_event_type,wait_event_name,description}', + prosrc => 'pg_get_wait_events' }, { oid => '3318', descr => 'statistics: information about progress of backends running maintenance command', proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't', diff --git a/src/include/utils/meson.build b/src/include/utils/meson.build index 6de5d93799..c179478611 100644 --- a/src/include/utils/meson.build +++ b/src/include/utils/meson.build @@ -1,6 +1,6 @@ # Copyright (c) 2022-2023, PostgreSQL Global Development Group -wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c'] +wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c', 'wait_event_funcs_data.c'] wait_event_target = custom_target('wait_event_names', input: files('../../backend/utils/activity/wait_event_names.txt'), output: wait_event_output, @@ -11,7 +11,7 @@ wait_event_target = custom_target('wait_event_names', ], build_by_default: true, install: true, - install_dir: [dir_include_server / 'utils', false], + install_dir: [dir_include_server / 'utils', false, false], ) wait_event_types_h = wait_event_target[0] diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e07afcd4aa..f160cb5642 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2631,6 +2631,10 @@ pg_views| SELECT n.nspname AS schemaname, FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char"); +pg_wait_event| SELECT wait_event_type, + wait_event_name, + description + FROM pg_get_wait_events() we(wait_event_type, wait_event_name, description); SELECT tablename, rulename, definition FROM pg_rules WHERE schemaname = 'pg_catalog' ORDER BY tablename, rulename; diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 001c6e7eb9..7ace77d714 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -134,6 +134,13 @@ select name, setting from pg_settings where name like 'enable%'; enable_tidscan | on (21 rows) +-- There is wait event descriptions +select count(*) > 0 as ok FROM pg_wait_event; + ok +---- + t +(1 row) + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 351e469c77..643dfcc1d3 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -55,6 +55,9 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- a regression test run. select name, setting from pg_settings where name like 'enable%'; +-- There is wait event descriptions +select count(*) > 0 as ok FROM pg_wait_event; + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index 7cb23ea894..ac8da581e4 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -55,6 +55,7 @@ if exist src\include\catalog\header-stamp del /q src\include\catalog\header-stam if exist doc\src\sgml\version.sgml del /q doc\src\sgml\version.sgml if %DIST%==1 if exist src\backend\utils\activity\pgstat_wait_event.c del /q src\backend\utils\activity\pgstat_wait_event.c +if %DIST%==1 if exist src\backend\utils\activity\wait_event_funcs_data.c del /q src\backend\utils\activity\wait_event_funcs_data.c if %DIST%==1 if exist src\backend\utils\activity\wait_event_types.h del /q src\backend\utils\activity\wait_event_types.h if %DIST%==1 if exist src\backend\utils\fmgroids.h del /q src\backend\utils\fmgroids.h if %DIST%==1 if exist src\backend\utils\fmgrprotos.h del /q src\backend\utils\fmgrprotos.h -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-14 04:37                   ` Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Michael Paquier @ 2023-08-14 04:37 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Thu, Aug 10, 2023 at 08:09:34PM +0200, Drouvot, Bertrand wrote:
> Agree that's worth it given the fact that iterating one more time is not that
> costly here.

I have reviewed v4, and finished by putting my hands on it to see what
I could do.

+            printf $wc "\telement = (wait_event_element *) palloc(sizeof(wait_event_element));\n";
+
+            printf $wc "\telement->wait_event_type = \"%s\";\n", $last;
+            printf $wc "\telement->wait_event_name = \"%s\";\n", $wev->[1];
+            printf $wc "\telement->wait_event_description = \"%s\";\n\n", $new_desc;
+
+            printf $wc "\twait_event = lappend(wait_event, element);\n\n";
+        }

This is simpler than the previous versions, still I am not much a fan
of implying the use of a list and these pallocs.  There are two things
that we could do:
- Hide that behind a macro defined in wait_event_funcs.c.
- Feed the data generated here into a static structure, like:
+static const struct
+{
+   const char *type;
+   const char *name;
+   const char *description;
+}

After experimenting with both, I've found the latter a tad cleaner, so
the attached version does that.

+       <structfield>description</structfield> <type>texte</type>
This one was difficult to see..

I am not sure that "pg_wait_event" is a good idea for the name if the
new view.  How about "pg_wait_events" instead, in plural form?  There
is more than one wait event listed.

One log entry in Solution.pm has missed the addition of a reference to
wait_event_funcs_data.c.

And..  src/backend/Makefile missed two updates for maintainer-clean & co,
no?

One thing that the patch is still missing is the handling of custom
wait events for extensions.  So this still requires more code.  I was
thinking about listed these events as:
- Type: "Extension"
- Name: What a module has registered.
- Description: "Custom wait event \"%Name%\" defined by extension".

For now I am attaching a v5.
--
Michael

Attachments:

v5-0001-pg_wait_event.patch (text/x-diff, 17.8K) download From 4f0172e216bfa7b929b9ca3465d66088b2ac1566 Mon Sep 17 00:00:00 2001 From: bdrouvotAWS <bdrouvot@amazon.com> Date: Sat, 5 Aug 2023 12:39:42 +0000 Subject: [PATCH v5] Add catalog pg_wait_events Adding a new system view, namely pg_wait_event, that describes the wait events. --- src/include/catalog/pg_proc.dat | 6 ++ src/include/utils/meson.build | 4 +- src/backend/Makefile | 3 +- src/backend/catalog/system_views.sql | 3 + src/backend/utils/activity/.gitignore | 1 + src/backend/utils/activity/Makefile | 8 ++- .../activity/generate-wait_event_types.pl | 46 +++++++++++-- src/backend/utils/activity/meson.build | 1 + src/backend/utils/activity/wait_event_funcs.c | 69 +++++++++++++++++++ src/test/regress/expected/rules.out | 4 ++ src/test/regress/expected/sysviews.out | 16 +++++ src/test/regress/sql/sysviews.sql | 4 ++ doc/src/sgml/system-views.sgml | 64 +++++++++++++++++ src/tools/msvc/Solution.pm | 3 +- src/tools/msvc/clean.bat | 1 + 15 files changed, 223 insertions(+), 10 deletions(-) create mode 100644 src/backend/utils/activity/wait_event_funcs.c diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..1a942c678c 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5417,6 +5417,12 @@ proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}', proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}', prosrc => 'pg_stat_get_activity' }, +{ oid => '8403', descr => 'describe wait events', + proname => 'pg_get_wait_events', procost => '10', prorows => '100', + proretset => 't', provolatile => 's', prorettype => 'record', + proargtypes => '', proallargtypes => '{text,text,text}', + proargmodes => '{o,o,o}', proargnames => '{type,name,description}', + prosrc => 'pg_get_wait_events' }, { oid => '3318', descr => 'statistics: information about progress of backends running maintenance command', proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't', diff --git a/src/include/utils/meson.build b/src/include/utils/meson.build index 6de5d93799..c179478611 100644 --- a/src/include/utils/meson.build +++ b/src/include/utils/meson.build @@ -1,6 +1,6 @@ # Copyright (c) 2022-2023, PostgreSQL Global Development Group -wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c'] +wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c', 'wait_event_funcs_data.c'] wait_event_target = custom_target('wait_event_names', input: files('../../backend/utils/activity/wait_event_names.txt'), output: wait_event_output, @@ -11,7 +11,7 @@ wait_event_target = custom_target('wait_event_names', ], build_by_default: true, install: true, - install_dir: [dir_include_server / 'utils', false], + install_dir: [dir_include_server / 'utils', false, false], ) wait_event_types_h = wait_event_target[0] diff --git a/src/backend/Makefile b/src/backend/Makefile index 1c929383c4..3e275ac759 100644 --- a/src/backend/Makefile +++ b/src/backend/Makefile @@ -134,7 +134,7 @@ storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lw $(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c utils/activity/wait_event_types.h: utils/activity/generate-wait_event_types.pl utils/activity/wait_event_names.txt - $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c + $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c # run this unconditionally to avoid needing to know its dependencies here: submake-catalog-headers: @@ -311,6 +311,7 @@ maintainer-clean: distclean storage/lmgr/lwlocknames.c \ storage/lmgr/lwlocknames.h \ utils/activity/pgstat_wait_event.c \ + utils/activity/wait_event_funcs_data.c \ utils/activity/wait_event_types.h \ utils/adt/jsonpath_gram.c \ utils/adt/jsonpath_gram.h \ diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index af65af6bdd..362b1ea8d5 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1342,3 +1342,6 @@ CREATE VIEW pg_stat_subscription_stats AS ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; + +CREATE VIEW pg_wait_events AS + SELECT * FROM pg_get_wait_events() AS we; diff --git a/src/backend/utils/activity/.gitignore b/src/backend/utils/activity/.gitignore index d77079285b..bd0c0c7772 100644 --- a/src/backend/utils/activity/.gitignore +++ b/src/backend/utils/activity/.gitignore @@ -1,2 +1,3 @@ /pgstat_wait_event.c /wait_event_types.h +/wait_event_funcs_data.c diff --git a/src/backend/utils/activity/Makefile b/src/backend/utils/activity/Makefile index f1117745d4..f57cf3958c 100644 --- a/src/backend/utils/activity/Makefile +++ b/src/backend/utils/activity/Makefile @@ -32,10 +32,14 @@ OBJS = \ pgstat_subscription.o \ pgstat_wal.o \ pgstat_xact.o \ - wait_event.o + wait_event.o \ + wait_event_funcs.o include $(top_srcdir)/src/backend/common.mk +wait_event_funcs.o: wait_event_funcs_data.c +wait_event_funcs_data.c: wait_event_types.h + wait_event.o: pgstat_wait_event.c pgstat_wait_event.c: wait_event_types.h touch $@ @@ -44,4 +48,4 @@ wait_event_types.h: $(top_srcdir)/src/backend/utils/activity/wait_event_names.tx $(PERL) $(srcdir)/generate-wait_event_types.pl --code $< maintainer-clean: clean - rm -f wait_event_types.h pgstat_wait_event.c + rm -f wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..6483966c6a 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -4,6 +4,7 @@ # Generate wait events support files from wait_event_names.txt: # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) +# - wait_event_funcs_data.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -98,8 +99,10 @@ if ($gen_code) # multiple times. my $htmp = "$output_path/wait_event_types.h.tmp$$"; my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$"; + my $wctmp = "$output_path/wait_event_funcs_data.c.tmp$$"; open my $h, '>', $htmp or die "Could not open $htmp: $!"; open my $c, '>', $ctmp or die "Could not open $ctmp: $!"; + open my $wc, '>', $wctmp or die "Could not open $wctmp: $!"; my $header_comment = '/*------------------------------------------------------------------------- @@ -129,12 +132,14 @@ if ($gen_code) printf $c $header_comment, 'pgstat_wait_event.c'; + printf $wc $header_comment, 'wait_event_funcs_data.c'; + + # Generate the pgstat_wait_event.c and wait_event_types.h files # uc() is being used to force the comparison to be case-insensitive. foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) { - - # Don't generate .c and .h files for Extension, LWLock and - # Lock, these are handled independently. + # Don't generate the pgstat_wait_event.c and wait_event_types.h files + # for Extension, LWLock and Lock, these are handled independently. next if ( $waitclass eq 'WaitEventExtension' || $waitclass eq 'WaitEventLWLock' @@ -183,14 +188,47 @@ if ($gen_code) printf $c "}\n\n"; } + # Generate wait_event_funcs_data.c, for the contents of static C + # structure holding all the information about the wait events. + # uc() is being used to force the comparison to be case-insensitive, + # even though it is not strictly mandatory here. + foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) + { + my $last = $waitclass; + $last =~ s/^WaitEvent//; + + foreach my $wev (@{ $hashwe{$waitclass} }) + { + my $new_desc = substr $wev->[2], 1, -2; + # Escape single quotes. + $new_desc =~ s/'/\\'/g; + + # Remove SGML markups + $new_desc =~ s/<.*>(.*?)<.*>/$1/g; + + # Tweak contents about links <xref linkend="text"/> on GUCs, + # then remove any reference to "see <xref linkend="text"/>". + $new_desc =~ s/<xref linkend="guc-(.*?)"\/>/$1/g; + $new_desc =~ s/; see.*$//; + + # One element to the C structure holding the wait event + # info, as of (type, name, description). + printf $wc "\t{\"%s\", \"%s\", \"%s\"},\n", $last, $wev->[1], + $new_desc; + } + } + printf $h "#endif /* WAIT_EVENT_TYPES_H */\n"; close $h; close $c; + close $wc; rename($htmp, "$output_path/wait_event_types.h") || die "rename: $htmp to $output_path/wait_event_types.h: $!"; rename($ctmp, "$output_path/pgstat_wait_event.c") || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!"; + rename($wctmp, "$output_path/wait_event_funcs_data.c") + || die "rename: $ctmp to $output_path/wait_event_funcs_data.c: $!"; } # Generate the .sgml file. elsif ($gen_docs) @@ -249,7 +287,7 @@ Usage: perl [--output <path>] [--code ] [ --sgml ] input_file Options: --outdir Output directory (default '.') - --code Generate wait_event_types.h and pgstat_wait_event.c. + --code Generate C and header files. --sgml Generate wait_event_types.sgml. generate-wait_event_types.pl generates the SGML documentation and code diff --git a/src/backend/utils/activity/meson.build b/src/backend/utils/activity/meson.build index 9633f3623c..46a27e7548 100644 --- a/src/backend/utils/activity/meson.build +++ b/src/backend/utils/activity/meson.build @@ -23,6 +23,7 @@ backend_sources += files( # seems nicer to not add that as an include path for the whole backend. waitevent_sources = files( 'wait_event.c', + 'wait_event_funcs.c', ) wait_event = static_library('wait_event_names', diff --git a/src/backend/utils/activity/wait_event_funcs.c b/src/backend/utils/activity/wait_event_funcs.c new file mode 100644 index 0000000000..19d13c0dcd --- /dev/null +++ b/src/backend/utils/activity/wait_event_funcs.c @@ -0,0 +1,69 @@ +/*------------------------------------------------------------------------ + * + * wait_event_funcs.c + * Functions for accessing wait event data. + * + * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/utils/activity/wait_event_funcs.c + * + *------------------------------------------------------------------------ + */ +#include "postgres.h" + +#include "funcapi.h" +#include "utils/builtins.h" + +/* + * Each wait event has one corresponding entry in this structure, fed to + * the SQL function of this file. + */ +static const struct +{ + const char *type; + const char *name; + const char *description; +} + + waitEventData[] = +{ +#include "wait_event_funcs_data.c" + /* end of list */ + {NULL, NULL, NULL} +}; + + +/* + * pg_get_wait_events + * + * List all the wait events (type, name) and their descriptions. + */ +Datum +pg_get_wait_events(PG_FUNCTION_ARGS) +{ +#define PG_GET_WAIT_EVENTS_COLS 3 + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + + /* Build tuplestore to hold the result rows */ + InitMaterializedSRF(fcinfo, 0); + + /* Iterate over the list of wait events */ + for (int idx = 0; waitEventData[idx].type != NULL; idx++) + { + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + values[0] = CStringGetTextDatum(waitEventData[idx].type); + values[1] = CStringGetTextDatum(waitEventData[idx].name); + values[2] = CStringGetTextDatum(waitEventData[idx].description); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + /* XXX: this needs to handle custom wait events, not yet in the tree */ + + return (Datum) 0; +} diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e07afcd4aa..55447d8090 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2631,6 +2631,10 @@ pg_views| SELECT n.nspname AS schemaname, FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char"); +pg_wait_events| SELECT type, + name, + description + FROM pg_get_wait_events() we(type, name, description); SELECT tablename, rulename, definition FROM pg_rules WHERE schemaname = 'pg_catalog' ORDER BY tablename, rulename; diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 001c6e7eb9..f3ac8dfe94 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -134,6 +134,22 @@ select name, setting from pg_settings where name like 'enable%'; enable_tidscan | on (21 rows) +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_events + group by type order by type; + type | ok +-----------+---- + Activity | t + BufferPin | t + Client | t + Extension | t + IO | t + IPC | t + Lock | t + LWLock | t + Timeout | t +(9 rows) + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 351e469c77..853daa85eb 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -55,6 +55,10 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- a regression test run. select name, setting from pg_settings where name like 'enable%'; +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_events + group by type order by type; + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 57b228076e..2b35c2f91b 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -221,6 +221,11 @@ <entry>views</entry> </row> + <row> + <entry><link linkend="view-pg-wait-events"><structname>pg_wait_events</structname></link></entry> + <entry>wait events</entry> + </row> + </tbody> </tgroup> </table> @@ -4825,4 +4830,63 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx </table> </sect1> + + <sect1 id="view-pg-wait-events"> + <title><structname>pg_wait_events</structname></title> + + <indexterm zone="view-pg-wait-events"> + <primary>pg_wait_events</primary> + </indexterm> + + <para> + The view <structname>pg_wait_events</structname> provides description about the + wait events. + </para> + + <table> + <title><structname>pg_wait_events</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>type</structfield> <type>text</type> + </para> + <para> + Wait event type + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>text</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index c98a1e9f9a..a50f730260 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -588,7 +588,8 @@ sub GenerateFiles 'src/include/utils/wait_event_types.h', 'src/backend/utils/activity/wait_event_names.txt')) { - print "Generating pgstat_wait_event.c and wait_event_types.h...\n"; + print + "Generating pgstat_wait_event.c, wait_event_types.h and wait_event_funcs_data.c...\n"; my $activ = 'src/backend/utils/activity'; system( "perl $activ/generate-wait_event_types.pl --outdir $activ --code $activ/wait_event_names.txt" diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index 7cb23ea894..ac8da581e4 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -55,6 +55,7 @@ if exist src\include\catalog\header-stamp del /q src\include\catalog\header-stam if exist doc\src\sgml\version.sgml del /q doc\src\sgml\version.sgml if %DIST%==1 if exist src\backend\utils\activity\pgstat_wait_event.c del /q src\backend\utils\activity\pgstat_wait_event.c +if %DIST%==1 if exist src\backend\utils\activity\wait_event_funcs_data.c del /q src\backend\utils\activity\wait_event_funcs_data.c if %DIST%==1 if exist src\backend\utils\activity\wait_event_types.h del /q src\backend\utils\activity\wait_event_types.h if %DIST%==1 if exist src\backend\utils\fmgroids.h del /q src\backend\utils\fmgroids.h if %DIST%==1 if exist src\backend\utils\fmgrprotos.h del /q src\backend\utils\fmgrprotos.h -- 2.40.1
signature.asc (application/pgp-signature, 832B) download ^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-16 05:04                     ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-16 05:04 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/14/23 6:37 AM, Michael Paquier wrote:
> On Thu, Aug 10, 2023 at 08:09:34PM +0200, Drouvot, Bertrand wrote:
>> Agree that's worth it given the fact that iterating one more time is not that
>> costly here.
> 
> I have reviewed v4, and finished by putting my hands on it to see what
> I could do.

Thanks!


> There are two things
> that we could do:
> - Hide that behind a macro defined in wait_event_funcs.c.
> - Feed the data generated here into a static structure, like:
> +static const struct
> +{
> +   const char *type;
> +   const char *name;
> +   const char *description;
> +}
> 
> After experimenting with both, I've found the latter a tad cleaner, so
> the attached version does that.

Yeah, looking at what you've done in v5, I also agree that's better
that what has been done in v4 (I also think it will be easier to maintain).

> I am not sure that "pg_wait_event" is a good idea for the name if the
> new view.  How about "pg_wait_events" instead, in plural form?  There
> is more than one wait event listed.
> 

I'd prefer the singular form. There is a lot of places where it's already used
(pg_database, pg_user, pg_namespace...to name a few) and it looks like that using
the plural form are exceptions.

> One log entry in Solution.pm has missed the addition of a reference to
> wait_event_funcs_data.c.
> 

Oh right, thanks for fixing it in v5.

> And..  src/backend/Makefile missed two updates for maintainer-clean & co,
> no?
>  
Oh right, thanks for fixing it in v5.

> One thing that the patch is still missing is the handling of custom
> wait events for extensions. 

Yeah, now that af720b4c50 is done, I'll add the custom wait events handling
in v6.

> So this still requires more code.  I was
> thinking about listed these events as:
> - Type: "Extension"
> - Name: What a module has registered.
> - Description: "Custom wait event \"%Name%\" defined by extension".

That sounds good to me, I'll do that.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-16 06:22                       ` Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Michael Paquier @ 2023-08-16 06:22 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Wed, Aug 16, 2023 at 07:04:53AM +0200, Drouvot, Bertrand wrote:
> I'd prefer the singular form. There is a lot of places where it's already used
> (pg_database, pg_user, pg_namespace...to name a few) and it looks like that using
> the plural form are exceptions.

Okay, fine by me.  Also, I would remove the "wait_event_" prefixes to
the field names for the attribute names.

> Yeah, now that af720b4c50 is done, I'll add the custom wait events handling
> in v6.

Thanks.  I guess that the hash tables had better remain local to
wait_event.c.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-16 11:43                         ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 12:08                           ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  0 siblings, 2 replies; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-16 11:43 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/16/23 8:22 AM, Michael Paquier wrote:
> On Wed, Aug 16, 2023 at 07:04:53AM +0200, Drouvot, Bertrand wrote:
>> I'd prefer the singular form. There is a lot of places where it's already used
>> (pg_database, pg_user, pg_namespace...to name a few) and it looks like that using
>> the plural form are exceptions.
> 
> Okay, fine by me.  

Great, singular form is being used in v6 attached.

> Also, I would remove the "wait_event_" prefixes to
> the field names for the attribute names.
> 

It looks like it has already been done in v5.

>> Yeah, now that af720b4c50 is done, I'll add the custom wait events handling
>> in v6.
> 
> Thanks.  I guess that the hash tables had better remain local to
> wait_event.c.

Yeah, agree, done that way in v6 (also added a test in 001_worker_spi.pl
to ensure that "worker_spi_main" is reported in pg_wait_event).

I added a "COLLATE "C"" in the "order by" clause's test that we added in
src/test/regress/sql/sysviews.sql (the check was failing on my environment).

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v6-0001-Add-catalog-pg_wait_event.patch (text/plain, 22.6K) download From f55bc01ca03b80116664b03b0174bc97dc01b6b9 Mon Sep 17 00:00:00 2001 From: bdrouvotAWS <bdrouvot@amazon.com> Date: Sat, 5 Aug 2023 12:39:42 +0000 Subject: [PATCH v6] Add catalog pg_wait_event Adding a new system view, namely pg_wait_event, that describes the wait events. --- doc/src/sgml/system-views.sgml | 64 +++++++++++++ src/backend/Makefile | 3 +- src/backend/catalog/system_views.sql | 3 + src/backend/utils/activity/.gitignore | 1 + src/backend/utils/activity/Makefile | 8 +- .../activity/generate-wait_event_types.pl | 46 ++++++++- src/backend/utils/activity/meson.build | 1 + src/backend/utils/activity/wait_event.c | 50 ++++++++-- src/backend/utils/activity/wait_event_funcs.c | 94 +++++++++++++++++++ src/include/catalog/pg_proc.dat | 6 ++ src/include/utils/meson.build | 4 +- src/include/utils/wait_event.h | 7 ++ .../modules/worker_spi/t/001_worker_spi.pl | 6 ++ src/test/regress/expected/rules.out | 4 + src/test/regress/expected/sysviews.out | 16 ++++ src/test/regress/sql/sysviews.sql | 4 + src/tools/msvc/Solution.pm | 3 +- src/tools/msvc/clean.bat | 1 + 18 files changed, 304 insertions(+), 17 deletions(-) 15.7% doc/src/sgml/ 62.3% src/backend/utils/activity/ 3.6% src/include/catalog/ 4.2% src/include/utils/ 4.6% src/test/regress/expected/ 4.6% src/test/ 3.1% src/tools/msvc/ diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 57b228076e..b2ed309fe2 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -221,6 +221,11 @@ <entry>views</entry> </row> + <row> + <entry><link linkend="view-pg-wait-event"><structname>pg_wait_event</structname></link></entry> + <entry>wait events</entry> + </row> + </tbody> </tgroup> </table> @@ -4825,4 +4830,63 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx </table> </sect1> + + <sect1 id="view-pg-wait-event"> + <title><structname>pg_wait_event</structname></title> + + <indexterm zone="view-pg-wait-event"> + <primary>pg_wait_event</primary> + </indexterm> + + <para> + The view <structname>pg_wait_event</structname> provides description about the + wait events. + </para> + + <table> + <title><structname>pg_wait_event</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>type</structfield> <type>text</type> + </para> + <para> + Wait event type + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>text</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/backend/Makefile b/src/backend/Makefile index 1c929383c4..3e275ac759 100644 --- a/src/backend/Makefile +++ b/src/backend/Makefile @@ -134,7 +134,7 @@ storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lw $(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c utils/activity/wait_event_types.h: utils/activity/generate-wait_event_types.pl utils/activity/wait_event_names.txt - $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c + $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c # run this unconditionally to avoid needing to know its dependencies here: submake-catalog-headers: @@ -311,6 +311,7 @@ maintainer-clean: distclean storage/lmgr/lwlocknames.c \ storage/lmgr/lwlocknames.h \ utils/activity/pgstat_wait_event.c \ + utils/activity/wait_event_funcs_data.c \ utils/activity/wait_event_types.h \ utils/adt/jsonpath_gram.c \ utils/adt/jsonpath_gram.h \ diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index af65af6bdd..f86a4dd770 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1342,3 +1342,6 @@ CREATE VIEW pg_stat_subscription_stats AS ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; + +CREATE VIEW pg_wait_event AS + SELECT * FROM pg_get_wait_events() AS we; diff --git a/src/backend/utils/activity/.gitignore b/src/backend/utils/activity/.gitignore index d77079285b..bd0c0c7772 100644 --- a/src/backend/utils/activity/.gitignore +++ b/src/backend/utils/activity/.gitignore @@ -1,2 +1,3 @@ /pgstat_wait_event.c /wait_event_types.h +/wait_event_funcs_data.c diff --git a/src/backend/utils/activity/Makefile b/src/backend/utils/activity/Makefile index f1117745d4..f57cf3958c 100644 --- a/src/backend/utils/activity/Makefile +++ b/src/backend/utils/activity/Makefile @@ -32,10 +32,14 @@ OBJS = \ pgstat_subscription.o \ pgstat_wal.o \ pgstat_xact.o \ - wait_event.o + wait_event.o \ + wait_event_funcs.o include $(top_srcdir)/src/backend/common.mk +wait_event_funcs.o: wait_event_funcs_data.c +wait_event_funcs_data.c: wait_event_types.h + wait_event.o: pgstat_wait_event.c pgstat_wait_event.c: wait_event_types.h touch $@ @@ -44,4 +48,4 @@ wait_event_types.h: $(top_srcdir)/src/backend/utils/activity/wait_event_names.tx $(PERL) $(srcdir)/generate-wait_event_types.pl --code $< maintainer-clean: clean - rm -f wait_event_types.h pgstat_wait_event.c + rm -f wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..6483966c6a 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -4,6 +4,7 @@ # Generate wait events support files from wait_event_names.txt: # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) +# - wait_event_funcs_data.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -98,8 +99,10 @@ if ($gen_code) # multiple times. my $htmp = "$output_path/wait_event_types.h.tmp$$"; my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$"; + my $wctmp = "$output_path/wait_event_funcs_data.c.tmp$$"; open my $h, '>', $htmp or die "Could not open $htmp: $!"; open my $c, '>', $ctmp or die "Could not open $ctmp: $!"; + open my $wc, '>', $wctmp or die "Could not open $wctmp: $!"; my $header_comment = '/*------------------------------------------------------------------------- @@ -129,12 +132,14 @@ if ($gen_code) printf $c $header_comment, 'pgstat_wait_event.c'; + printf $wc $header_comment, 'wait_event_funcs_data.c'; + + # Generate the pgstat_wait_event.c and wait_event_types.h files # uc() is being used to force the comparison to be case-insensitive. foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) { - - # Don't generate .c and .h files for Extension, LWLock and - # Lock, these are handled independently. + # Don't generate the pgstat_wait_event.c and wait_event_types.h files + # for Extension, LWLock and Lock, these are handled independently. next if ( $waitclass eq 'WaitEventExtension' || $waitclass eq 'WaitEventLWLock' @@ -183,14 +188,47 @@ if ($gen_code) printf $c "}\n\n"; } + # Generate wait_event_funcs_data.c, for the contents of static C + # structure holding all the information about the wait events. + # uc() is being used to force the comparison to be case-insensitive, + # even though it is not strictly mandatory here. + foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) + { + my $last = $waitclass; + $last =~ s/^WaitEvent//; + + foreach my $wev (@{ $hashwe{$waitclass} }) + { + my $new_desc = substr $wev->[2], 1, -2; + # Escape single quotes. + $new_desc =~ s/'/\\'/g; + + # Remove SGML markups + $new_desc =~ s/<.*>(.*?)<.*>/$1/g; + + # Tweak contents about links <xref linkend="text"/> on GUCs, + # then remove any reference to "see <xref linkend="text"/>". + $new_desc =~ s/<xref linkend="guc-(.*?)"\/>/$1/g; + $new_desc =~ s/; see.*$//; + + # One element to the C structure holding the wait event + # info, as of (type, name, description). + printf $wc "\t{\"%s\", \"%s\", \"%s\"},\n", $last, $wev->[1], + $new_desc; + } + } + printf $h "#endif /* WAIT_EVENT_TYPES_H */\n"; close $h; close $c; + close $wc; rename($htmp, "$output_path/wait_event_types.h") || die "rename: $htmp to $output_path/wait_event_types.h: $!"; rename($ctmp, "$output_path/pgstat_wait_event.c") || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!"; + rename($wctmp, "$output_path/wait_event_funcs_data.c") + || die "rename: $ctmp to $output_path/wait_event_funcs_data.c: $!"; } # Generate the .sgml file. elsif ($gen_docs) @@ -249,7 +287,7 @@ Usage: perl [--output <path>] [--code ] [ --sgml ] input_file Options: --outdir Output directory (default '.') - --code Generate wait_event_types.h and pgstat_wait_event.c. + --code Generate C and header files. --sgml Generate wait_event_types.sgml. generate-wait_event_types.pl generates the SGML documentation and code diff --git a/src/backend/utils/activity/meson.build b/src/backend/utils/activity/meson.build index 9633f3623c..46a27e7548 100644 --- a/src/backend/utils/activity/meson.build +++ b/src/backend/utils/activity/meson.build @@ -23,6 +23,7 @@ backend_sources += files( # seems nicer to not add that as an include path for the whole backend. waitevent_sources = files( 'wait_event.c', + 'wait_event_funcs.c', ) wait_event = static_library('wait_event_names', diff --git a/src/backend/utils/activity/wait_event.c b/src/backend/utils/activity/wait_event.c index 4b9b5c01cb..4fa91bb227 100644 --- a/src/backend/utils/activity/wait_event.c +++ b/src/backend/utils/activity/wait_event.c @@ -73,13 +73,6 @@ typedef struct WaitEventExtensionEntryById char wait_event_name[NAMEDATALEN]; /* custom wait event name */ } WaitEventExtensionEntryById; -typedef struct WaitEventExtensionEntryByName -{ - char wait_event_name[NAMEDATALEN]; /* hash key */ - uint16 event_id; /* wait event ID */ -} WaitEventExtensionEntryByName; - - /* dynamic allocation counter for custom wait events in extensions */ typedef struct WaitEventExtensionCounterData { @@ -264,6 +257,49 @@ GetWaitEventExtensionIdentifier(uint16 eventId) } +/* + * Returns a list of currently defined custom wait events for extensions. + * The result is a palloc'd array, with the number of elements returned into + * *nwaitevents. + */ +WaitEventExtensionEntryByName * +GetWaitEventExtensionEntries(int *nwaitevents) +{ + WaitEventExtensionEntryByName *waitevententries; + WaitEventExtensionEntryByName *hentry; + HASH_SEQ_STATUS hash_seq; + int index; + int els; + + LWLockAcquire(WaitEventExtensionLock, LW_SHARED); + + /* Now we can safely count the number of entries */ + els = hash_get_num_entries(WaitEventExtensionHashByName); + + /* Allocate enough space for alll entries */ + waitevententries = palloc(els * sizeof(WaitEventExtensionEntryByName)); + + /* Now scan the hash table to copy the data */ + hash_seq_init(&hash_seq, WaitEventExtensionHashByName); + + index = 0; + while ((hentry = (WaitEventExtensionEntryByName *) hash_seq_search(&hash_seq)) != NULL) + { + strlcpy(waitevententries[index].wait_event_name, hentry->wait_event_name, + sizeof(waitevententries[index].wait_event_name)); + waitevententries[index].event_id = hentry->event_id; + + index++; + } + + LWLockRelease(WaitEventExtensionLock); + + Assert(index <= els); + + *nwaitevents = index; + return waitevententries; +} + /* * Configure wait event reporting to report wait events to *wait_event_info. * *wait_event_info needs to be valid until pgstat_reset_wait_event_storage() diff --git a/src/backend/utils/activity/wait_event_funcs.c b/src/backend/utils/activity/wait_event_funcs.c new file mode 100644 index 0000000000..b74250ca35 --- /dev/null +++ b/src/backend/utils/activity/wait_event_funcs.c @@ -0,0 +1,94 @@ +/*------------------------------------------------------------------------ + * + * wait_event_funcs.c + * Functions for accessing wait event data. + * + * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/utils/activity/wait_event_funcs.c + * + *------------------------------------------------------------------------ + */ +#include "postgres.h" + +#include "funcapi.h" +#include "utils/builtins.h" +#include "utils/wait_event.h" + +/* + * Each wait event has one corresponding entry in this structure, fed to + * the SQL function of this file. + */ +static const struct +{ + const char *type; + const char *name; + const char *description; +} + +waitEventData[] = +{ +#include "wait_event_funcs_data.c" + /* end of list */ + {NULL, NULL, NULL} +}; + + +/* + * pg_get_wait_events + * + * List all the wait events (type, name) and their descriptions. + */ +Datum +pg_get_wait_events(PG_FUNCTION_ARGS) +{ +#define PG_GET_WAIT_EVENTS_COLS 3 + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + WaitEventExtensionEntryByName *waitevententries; + int nbextwaitevents; + + /* Build tuplestore to hold the result rows */ + InitMaterializedSRF(fcinfo, 0); + + /* Iterate over the list of wait events */ + for (int idx = 0; waitEventData[idx].type != NULL; idx++) + { + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + values[0] = CStringGetTextDatum(waitEventData[idx].type); + values[1] = CStringGetTextDatum(waitEventData[idx].name); + values[2] = CStringGetTextDatum(waitEventData[idx].description); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + /* Handling extension custom wait events */ + waitevententries = GetWaitEventExtensionEntries(&nbextwaitevents); + for (int idx = 0; idx < nbextwaitevents; idx++) + { + char buf[NAMEDATALEN + 44]; //"Custom wait event \"%Name%\" defined by extension" + + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + values[0] = CStringGetTextDatum("Extension"); + values[1] = CStringGetTextDatum(waitevententries[idx].wait_event_name); + + buf[0] = '\0'; + strcat(buf, "Custom wait event \""); + strcat(buf, waitevententries[idx].wait_event_name); + strcat(buf, "\" defined by extension"); + + values[2] = CStringGetTextDatum(buf); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + pfree(waitevententries); + + return (Datum) 0; +} diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..1a942c678c 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5417,6 +5417,12 @@ proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}', proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}', prosrc => 'pg_stat_get_activity' }, +{ oid => '8403', descr => 'describe wait events', + proname => 'pg_get_wait_events', procost => '10', prorows => '100', + proretset => 't', provolatile => 's', prorettype => 'record', + proargtypes => '', proallargtypes => '{text,text,text}', + proargmodes => '{o,o,o}', proargnames => '{type,name,description}', + prosrc => 'pg_get_wait_events' }, { oid => '3318', descr => 'statistics: information about progress of backends running maintenance command', proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't', diff --git a/src/include/utils/meson.build b/src/include/utils/meson.build index 6de5d93799..c179478611 100644 --- a/src/include/utils/meson.build +++ b/src/include/utils/meson.build @@ -1,6 +1,6 @@ # Copyright (c) 2022-2023, PostgreSQL Global Development Group -wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c'] +wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c', 'wait_event_funcs_data.c'] wait_event_target = custom_target('wait_event_names', input: files('../../backend/utils/activity/wait_event_names.txt'), output: wait_event_output, @@ -11,7 +11,7 @@ wait_event_target = custom_target('wait_event_names', ], build_by_default: true, install: true, - install_dir: [dir_include_server / 'utils', false], + install_dir: [dir_include_server / 'utils', false, false], ) wait_event_types_h = wait_event_target[0] diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h index 3eebdfad38..5dd17d33ee 100644 --- a/src/include/utils/wait_event.h +++ b/src/include/utils/wait_event.h @@ -59,10 +59,17 @@ typedef enum WAIT_EVENT_EXTENSION_FIRST_USER_DEFINED } WaitEventExtension; +typedef struct WaitEventExtensionEntryByName +{ + char wait_event_name[NAMEDATALEN]; /* hash key */ + uint16 event_id; /* wait event ID */ +} WaitEventExtensionEntryByName; + extern void WaitEventExtensionShmemInit(void); extern Size WaitEventExtensionShmemSize(void); extern uint32 WaitEventExtensionNew(const char *wait_event_name); +extern WaitEventExtensionEntryByName *GetWaitEventExtensionEntries(int *nwaitevents); /* ---------- * pgstat_report_wait_start() - diff --git a/src/test/modules/worker_spi/t/001_worker_spi.pl b/src/test/modules/worker_spi/t/001_worker_spi.pl index 26b8a49bec..cd48f42269 100644 --- a/src/test/modules/worker_spi/t/001_worker_spi.pl +++ b/src/test/modules/worker_spi/t/001_worker_spi.pl @@ -47,6 +47,12 @@ $result = $node->poll_query_until( is($result, 1, 'dynamic bgworker has reported "worker_spi_main" as wait event'); +# Check the wait event used by the dynamic bgworker appears in pg_wait_event +$result = $node->safe_psql('postgres', + q[SELECT count(*) > 0 from pg_wait_event where type = 'Extension' and name = 'worker_spi_main';] +); +is($result, 't', '"worker_spi_main" is reported in pg_wait_event'); + note "testing bgworkers loaded with shared_preload_libraries"; # Create the database first so as the workers can connect to it when diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e07afcd4aa..d69202991d 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2631,6 +2631,10 @@ pg_views| SELECT n.nspname AS schemaname, FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char"); +pg_wait_event| SELECT type, + name, + description + FROM pg_get_wait_events() we(type, name, description); SELECT tablename, rulename, definition FROM pg_rules WHERE schemaname = 'pg_catalog' ORDER BY tablename, rulename; diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 001c6e7eb9..c1a7944c7a 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -134,6 +134,22 @@ select name, setting from pg_settings where name like 'enable%'; enable_tidscan | on (21 rows) +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_event + group by type order by type COLLATE "C"; + type | ok +-----------+---- + Activity | t + BufferPin | t + Client | t + Extension | t + IO | t + IPC | t + LWLock | t + Lock | t + Timeout | t +(9 rows) + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 351e469c77..3d43108aae 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -55,6 +55,10 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- a regression test run. select name, setting from pg_settings where name like 'enable%'; +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_event + group by type order by type COLLATE "C"; + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index c98a1e9f9a..a50f730260 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -588,7 +588,8 @@ sub GenerateFiles 'src/include/utils/wait_event_types.h', 'src/backend/utils/activity/wait_event_names.txt')) { - print "Generating pgstat_wait_event.c and wait_event_types.h...\n"; + print + "Generating pgstat_wait_event.c, wait_event_types.h and wait_event_funcs_data.c...\n"; my $activ = 'src/backend/utils/activity'; system( "perl $activ/generate-wait_event_types.pl --outdir $activ --code $activ/wait_event_names.txt" diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index 7cb23ea894..ac8da581e4 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -55,6 +55,7 @@ if exist src\include\catalog\header-stamp del /q src\include\catalog\header-stam if exist doc\src\sgml\version.sgml del /q doc\src\sgml\version.sgml if %DIST%==1 if exist src\backend\utils\activity\pgstat_wait_event.c del /q src\backend\utils\activity\pgstat_wait_event.c +if %DIST%==1 if exist src\backend\utils\activity\wait_event_funcs_data.c del /q src\backend\utils\activity\wait_event_funcs_data.c if %DIST%==1 if exist src\backend\utils\activity\wait_event_types.h del /q src\backend\utils\activity\wait_event_types.h if %DIST%==1 if exist src\backend\utils\fmgroids.h del /q src\backend\utils\fmgroids.h if %DIST%==1 if exist src\backend\utils\fmgrprotos.h del /q src\backend\utils\fmgrprotos.h -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-16 12:08                           ` Michael Paquier <michael@paquier.xyz>
  2023-08-17 05:51                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  1 sibling, 1 reply; 34+ messages in thread

From: Michael Paquier @ 2023-08-16 12:08 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Wed, Aug 16, 2023 at 01:43:35PM +0200, Drouvot, Bertrand wrote:
> Yeah, agree, done that way in v6 (also added a test in 001_worker_spi.pl
> to ensure that "worker_spi_main" is reported in pg_wait_event).

-typedef struct WaitEventExtensionEntryByName
-{
-   char        wait_event_name[NAMEDATALEN];   /* hash key */
-   uint16      event_id;       /* wait event ID */
-} WaitEventExtensionEntryByName;

I'd rather keep all these structures local to wait_event.c, as these
cover the internals of the hash tables.

Could you switch GetWaitEventExtensionEntries() so as it returns a
list of strings or an array of char*?  We probably can live with the
list for that.

+       char        buf[NAMEDATALEN + 44]; //"Custom wait event \"%Name%\" defined by extension" + 
Incorrect comment.  This would be simpler as a StringInfo.

Thanks for the extra test in worker_spi looking at the contents of the
catalog.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 12:08                           ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-17 05:51                             ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 0 replies; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-17 05:51 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/16/23 2:08 PM, Michael Paquier wrote:
> On Wed, Aug 16, 2023 at 01:43:35PM +0200, Drouvot, Bertrand wrote:
>> Yeah, agree, done that way in v6 (also added a test in 001_worker_spi.pl
>> to ensure that "worker_spi_main" is reported in pg_wait_event).
> 
> -typedef struct WaitEventExtensionEntryByName
> -{
> -   char        wait_event_name[NAMEDATALEN];   /* hash key */
> -   uint16      event_id;       /* wait event ID */
> -} WaitEventExtensionEntryByName;
> 
> I'd rather keep all these structures local to wait_event.c, as these
> cover the internals of the hash tables.
> 
> Could you switch GetWaitEventExtensionEntries() so as it returns a
> list of strings or an array of char*?  We probably can live with the
> list for that.
> 

Yeah, I was not sure about this (returning a list of WaitEventExtensionEntryByName
or a list of wait event names) while working on v6.

That's true that the only need here is to get the names of the custom wait events.
Returning only the names would allow us to move the WaitEventExtensionEntryByName definition back
to the wait_event.c file.

It makes sense to me, done in v7 attached and renamed the function to GetWaitEventExtensionNames().

> +       char        buf[NAMEDATALEN + 44]; //"Custom wait event \"%Name%\" defined by extension" +
> Incorrect comment.  This would be simpler as a StringInfo.

Yeah and probably less error prone: done in v7.

While at it, v7 is deliberately not calling "pfree(waiteventnames)" and "resetStringInfo(&buf)" in
pg_get_wait_events(): reason is that they are palloc in a short-lived memory context while executing
pg_get_wait_events().

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v7-0001-Add-catalog-pg_wait_event.patch (text/plain, 21.9K) download From 43baf166f14aeca4ef0979d93e9ed34fdc323a09 Mon Sep 17 00:00:00 2001 From: bdrouvotAWS <bdrouvot@amazon.com> Date: Sat, 5 Aug 2023 12:39:42 +0000 Subject: [PATCH v7] Add catalog pg_wait_event Adding a new system view, namely pg_wait_event, that describes the wait events. --- doc/src/sgml/system-views.sgml | 64 +++++++++++++ src/backend/Makefile | 3 +- src/backend/catalog/system_views.sql | 3 + src/backend/utils/activity/.gitignore | 1 + src/backend/utils/activity/Makefile | 8 +- .../activity/generate-wait_event_types.pl | 54 ++++++++++- src/backend/utils/activity/meson.build | 1 + src/backend/utils/activity/wait_event.c | 44 +++++++++ src/backend/utils/activity/wait_event_funcs.c | 93 +++++++++++++++++++ src/include/catalog/pg_proc.dat | 6 ++ src/include/utils/meson.build | 4 +- src/include/utils/wait_event.h | 1 + .../modules/worker_spi/t/001_worker_spi.pl | 6 ++ src/test/regress/expected/rules.out | 4 + src/test/regress/expected/sysviews.out | 16 ++++ src/test/regress/sql/sysviews.sql | 4 + src/tools/msvc/Solution.pm | 3 +- src/tools/msvc/clean.bat | 1 + 18 files changed, 306 insertions(+), 10 deletions(-) 16.3% doc/src/sgml/ 63.1% src/backend/utils/activity/ 3.7% src/include/catalog/ 3.0% src/test/modules/worker_spi/t/ 4.8% src/test/regress/expected/ 3.2% src/tools/msvc/ 5.5% src/ diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 57b228076e..b2ed309fe2 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -221,6 +221,11 @@ <entry>views</entry> </row> + <row> + <entry><link linkend="view-pg-wait-event"><structname>pg_wait_event</structname></link></entry> + <entry>wait events</entry> + </row> + </tbody> </tgroup> </table> @@ -4825,4 +4830,63 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx </table> </sect1> + + <sect1 id="view-pg-wait-event"> + <title><structname>pg_wait_event</structname></title> + + <indexterm zone="view-pg-wait-event"> + <primary>pg_wait_event</primary> + </indexterm> + + <para> + The view <structname>pg_wait_event</structname> provides description about the + wait events. + </para> + + <table> + <title><structname>pg_wait_event</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>type</structfield> <type>text</type> + </para> + <para> + Wait event type + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>text</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/backend/Makefile b/src/backend/Makefile index 1c929383c4..3e275ac759 100644 --- a/src/backend/Makefile +++ b/src/backend/Makefile @@ -134,7 +134,7 @@ storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lw $(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c utils/activity/wait_event_types.h: utils/activity/generate-wait_event_types.pl utils/activity/wait_event_names.txt - $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c + $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c # run this unconditionally to avoid needing to know its dependencies here: submake-catalog-headers: @@ -311,6 +311,7 @@ maintainer-clean: distclean storage/lmgr/lwlocknames.c \ storage/lmgr/lwlocknames.h \ utils/activity/pgstat_wait_event.c \ + utils/activity/wait_event_funcs_data.c \ utils/activity/wait_event_types.h \ utils/adt/jsonpath_gram.c \ utils/adt/jsonpath_gram.h \ diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index af65af6bdd..f86a4dd770 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1342,3 +1342,6 @@ CREATE VIEW pg_stat_subscription_stats AS ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; + +CREATE VIEW pg_wait_event AS + SELECT * FROM pg_get_wait_events() AS we; diff --git a/src/backend/utils/activity/.gitignore b/src/backend/utils/activity/.gitignore index d77079285b..bd0c0c7772 100644 --- a/src/backend/utils/activity/.gitignore +++ b/src/backend/utils/activity/.gitignore @@ -1,2 +1,3 @@ /pgstat_wait_event.c /wait_event_types.h +/wait_event_funcs_data.c diff --git a/src/backend/utils/activity/Makefile b/src/backend/utils/activity/Makefile index f1117745d4..f57cf3958c 100644 --- a/src/backend/utils/activity/Makefile +++ b/src/backend/utils/activity/Makefile @@ -32,10 +32,14 @@ OBJS = \ pgstat_subscription.o \ pgstat_wal.o \ pgstat_xact.o \ - wait_event.o + wait_event.o \ + wait_event_funcs.o include $(top_srcdir)/src/backend/common.mk +wait_event_funcs.o: wait_event_funcs_data.c +wait_event_funcs_data.c: wait_event_types.h + wait_event.o: pgstat_wait_event.c pgstat_wait_event.c: wait_event_types.h touch $@ @@ -44,4 +48,4 @@ wait_event_types.h: $(top_srcdir)/src/backend/utils/activity/wait_event_names.tx $(PERL) $(srcdir)/generate-wait_event_types.pl --code $< maintainer-clean: clean - rm -f wait_event_types.h pgstat_wait_event.c + rm -f wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..9d5bfd2145 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -4,6 +4,7 @@ # Generate wait events support files from wait_event_names.txt: # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) +# - wait_event_funcs_data.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -98,8 +99,10 @@ if ($gen_code) # multiple times. my $htmp = "$output_path/wait_event_types.h.tmp$$"; my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$"; + my $wctmp = "$output_path/wait_event_funcs_data.c.tmp$$"; open my $h, '>', $htmp or die "Could not open $htmp: $!"; open my $c, '>', $ctmp or die "Could not open $ctmp: $!"; + open my $wc, '>', $wctmp or die "Could not open $wctmp: $!"; my $header_comment = '/*------------------------------------------------------------------------- @@ -129,12 +132,14 @@ if ($gen_code) printf $c $header_comment, 'pgstat_wait_event.c'; + printf $wc $header_comment, 'wait_event_funcs_data.c'; + + # Generate the pgstat_wait_event.c and wait_event_types.h files # uc() is being used to force the comparison to be case-insensitive. foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) { - - # Don't generate .c and .h files for Extension, LWLock and - # Lock, these are handled independently. + # Don't generate the pgstat_wait_event.c and wait_event_types.h files + # for Extension, LWLock and Lock, these are handled independently. next if ( $waitclass eq 'WaitEventExtension' || $waitclass eq 'WaitEventLWLock' @@ -183,14 +188,55 @@ if ($gen_code) printf $c "}\n\n"; } + # Generate wait_event_funcs_data.c, for the contents of static C + # structure holding all the information about the wait events. + # uc() is being used to force the comparison to be case-insensitive, + # even though it is not strictly mandatory here. + foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) + { + my $last = $waitclass; + $last =~ s/^WaitEvent//; + + foreach my $wev (@{ $hashwe{$waitclass} }) + { + my $new_desc = substr $wev->[2], 1, -2; + # Escape single quotes. + $new_desc =~ s/'/\\'/g; + + # Replace the "quote" markups by real ones. + $new_desc =~ s/<quote>(.*?)<\/quote>/\\"$1\\"/g; + + # Remove SGML markups. + $new_desc =~ s/<.*?>(.*?)<.*?>/$1/g; + + # Tweak contents about links <xref linkend="text"/> on GUCs, + while (my ($capture) = + $new_desc =~ m/<xref linkend="guc-(.*?)"\/>/g) + { + $capture =~ s/-/_/g; + $new_desc =~ s/<xref linkend="guc-.*?"\/>/$capture/g; + } + # Then remove any reference to "see <xref linkend="text"/>". + $new_desc =~ s/; see.*$//; + + # One element to the C structure holding the wait event + # info, as of (type, name, description). + printf $wc "\t{\"%s\", \"%s\", \"%s\"},\n", $last, $wev->[1], + $new_desc; + } + } + printf $h "#endif /* WAIT_EVENT_TYPES_H */\n"; close $h; close $c; + close $wc; rename($htmp, "$output_path/wait_event_types.h") || die "rename: $htmp to $output_path/wait_event_types.h: $!"; rename($ctmp, "$output_path/pgstat_wait_event.c") || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!"; + rename($wctmp, "$output_path/wait_event_funcs_data.c") + || die "rename: $ctmp to $output_path/wait_event_funcs_data.c: $!"; } # Generate the .sgml file. elsif ($gen_docs) @@ -249,7 +295,7 @@ Usage: perl [--output <path>] [--code ] [ --sgml ] input_file Options: --outdir Output directory (default '.') - --code Generate wait_event_types.h and pgstat_wait_event.c. + --code Generate C and header files. --sgml Generate wait_event_types.sgml. generate-wait_event_types.pl generates the SGML documentation and code diff --git a/src/backend/utils/activity/meson.build b/src/backend/utils/activity/meson.build index 9633f3623c..46a27e7548 100644 --- a/src/backend/utils/activity/meson.build +++ b/src/backend/utils/activity/meson.build @@ -23,6 +23,7 @@ backend_sources += files( # seems nicer to not add that as an include path for the whole backend. waitevent_sources = files( 'wait_event.c', + 'wait_event_funcs.c', ) wait_event = static_library('wait_event_names', diff --git a/src/backend/utils/activity/wait_event.c b/src/backend/utils/activity/wait_event.c index 4b9b5c01cb..d31767df80 100644 --- a/src/backend/utils/activity/wait_event.c +++ b/src/backend/utils/activity/wait_event.c @@ -264,6 +264,50 @@ GetWaitEventExtensionIdentifier(uint16 eventId) } +/* + * Returns a list of currently defined custom wait event names for extensions. + * The result is a palloc'd array, with the number of elements returned into + * *nwaitevents. + */ +char ** +GetWaitEventExtensionNames(int *nwaitevents) +{ + char **waiteventnames; + WaitEventExtensionEntryByName *hentry; + HASH_SEQ_STATUS hash_seq; + int index; + int els; + + LWLockAcquire(WaitEventExtensionLock, LW_SHARED); + + /* Now we can safely count the number of entries */ + els = hash_get_num_entries(WaitEventExtensionHashByName); + + /* Allocate enough space for all entries */ + waiteventnames = palloc(els * sizeof(char[NAMEDATALEN])); + + /* Now scan the hash table to copy the data */ + hash_seq_init(&hash_seq, WaitEventExtensionHashByName); + + index = 0; + while ((hentry = (WaitEventExtensionEntryByName *) hash_seq_search(&hash_seq)) != NULL) + { + + waiteventnames[index] = palloc(sizeof(char[NAMEDATALEN])); + strlcpy(waiteventnames[index], hentry->wait_event_name, + sizeof(char[NAMEDATALEN])); + + index++; + } + + LWLockRelease(WaitEventExtensionLock); + + Assert(index == els); + + *nwaitevents = index; + return waiteventnames; +} + /* * Configure wait event reporting to report wait events to *wait_event_info. * *wait_event_info needs to be valid until pgstat_reset_wait_event_storage() diff --git a/src/backend/utils/activity/wait_event_funcs.c b/src/backend/utils/activity/wait_event_funcs.c new file mode 100644 index 0000000000..74506f989e --- /dev/null +++ b/src/backend/utils/activity/wait_event_funcs.c @@ -0,0 +1,93 @@ +/*------------------------------------------------------------------------ + * + * wait_event_funcs.c + * Functions for accessing wait event data. + * + * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/utils/activity/wait_event_funcs.c + * + *------------------------------------------------------------------------ + */ +#include "postgres.h" + +#include "funcapi.h" +#include "utils/builtins.h" +#include "utils/wait_event.h" + +/* + * Each wait event has one corresponding entry in this structure, fed to + * the SQL function of this file. + */ +static const struct +{ + const char *type; + const char *name; + const char *description; +} + +waitEventData[] = +{ +#include "wait_event_funcs_data.c" + /* end of list */ + {NULL, NULL, NULL} +}; + + +/* + * pg_get_wait_events + * + * List all the wait events (type, name) and their descriptions. + */ +Datum +pg_get_wait_events(PG_FUNCTION_ARGS) +{ +#define PG_GET_WAIT_EVENTS_COLS 3 + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + char **waiteventnames; + int nbextwaitevents; + + /* Build tuplestore to hold the result rows */ + InitMaterializedSRF(fcinfo, 0); + + /* Iterate over the list of wait events */ + for (int idx = 0; waitEventData[idx].type != NULL; idx++) + { + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + values[0] = CStringGetTextDatum(waitEventData[idx].type); + values[1] = CStringGetTextDatum(waitEventData[idx].name); + values[2] = CStringGetTextDatum(waitEventData[idx].description); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + /* Handling extension custom wait events */ + waiteventnames = GetWaitEventExtensionNames(&nbextwaitevents); + + for (int idx = 0; idx < nbextwaitevents; idx++) + { + StringInfoData buf; + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + + values[0] = CStringGetTextDatum("Extension"); + values[1] = CStringGetTextDatum(waiteventnames[idx]); + + initStringInfo(&buf); + appendStringInfoString(&buf, "Custom wait event \""); + appendStringInfoString(&buf, waiteventnames[idx]); + appendStringInfoString(&buf, "\" defined by extension"); + + values[2] = CStringGetTextDatum(buf.data); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + return (Datum) 0; +} diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..1a942c678c 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5417,6 +5417,12 @@ proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}', proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}', prosrc => 'pg_stat_get_activity' }, +{ oid => '8403', descr => 'describe wait events', + proname => 'pg_get_wait_events', procost => '10', prorows => '100', + proretset => 't', provolatile => 's', prorettype => 'record', + proargtypes => '', proallargtypes => '{text,text,text}', + proargmodes => '{o,o,o}', proargnames => '{type,name,description}', + prosrc => 'pg_get_wait_events' }, { oid => '3318', descr => 'statistics: information about progress of backends running maintenance command', proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't', diff --git a/src/include/utils/meson.build b/src/include/utils/meson.build index 6de5d93799..c179478611 100644 --- a/src/include/utils/meson.build +++ b/src/include/utils/meson.build @@ -1,6 +1,6 @@ # Copyright (c) 2022-2023, PostgreSQL Global Development Group -wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c'] +wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c', 'wait_event_funcs_data.c'] wait_event_target = custom_target('wait_event_names', input: files('../../backend/utils/activity/wait_event_names.txt'), output: wait_event_output, @@ -11,7 +11,7 @@ wait_event_target = custom_target('wait_event_names', ], build_by_default: true, install: true, - install_dir: [dir_include_server / 'utils', false], + install_dir: [dir_include_server / 'utils', false, false], ) wait_event_types_h = wait_event_target[0] diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h index 3eebdfad38..009b03a520 100644 --- a/src/include/utils/wait_event.h +++ b/src/include/utils/wait_event.h @@ -63,6 +63,7 @@ extern void WaitEventExtensionShmemInit(void); extern Size WaitEventExtensionShmemSize(void); extern uint32 WaitEventExtensionNew(const char *wait_event_name); +extern char **GetWaitEventExtensionNames(int *nwaitevents); /* ---------- * pgstat_report_wait_start() - diff --git a/src/test/modules/worker_spi/t/001_worker_spi.pl b/src/test/modules/worker_spi/t/001_worker_spi.pl index 26b8a49bec..cd48f42269 100644 --- a/src/test/modules/worker_spi/t/001_worker_spi.pl +++ b/src/test/modules/worker_spi/t/001_worker_spi.pl @@ -47,6 +47,12 @@ $result = $node->poll_query_until( is($result, 1, 'dynamic bgworker has reported "worker_spi_main" as wait event'); +# Check the wait event used by the dynamic bgworker appears in pg_wait_event +$result = $node->safe_psql('postgres', + q[SELECT count(*) > 0 from pg_wait_event where type = 'Extension' and name = 'worker_spi_main';] +); +is($result, 't', '"worker_spi_main" is reported in pg_wait_event'); + note "testing bgworkers loaded with shared_preload_libraries"; # Create the database first so as the workers can connect to it when diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e07afcd4aa..d69202991d 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2631,6 +2631,10 @@ pg_views| SELECT n.nspname AS schemaname, FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char"); +pg_wait_event| SELECT type, + name, + description + FROM pg_get_wait_events() we(type, name, description); SELECT tablename, rulename, definition FROM pg_rules WHERE schemaname = 'pg_catalog' ORDER BY tablename, rulename; diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 001c6e7eb9..c1a7944c7a 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -134,6 +134,22 @@ select name, setting from pg_settings where name like 'enable%'; enable_tidscan | on (21 rows) +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_event + group by type order by type COLLATE "C"; + type | ok +-----------+---- + Activity | t + BufferPin | t + Client | t + Extension | t + IO | t + IPC | t + LWLock | t + Lock | t + Timeout | t +(9 rows) + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 351e469c77..3d43108aae 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -55,6 +55,10 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- a regression test run. select name, setting from pg_settings where name like 'enable%'; +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_event + group by type order by type COLLATE "C"; + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index c98a1e9f9a..a50f730260 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -588,7 +588,8 @@ sub GenerateFiles 'src/include/utils/wait_event_types.h', 'src/backend/utils/activity/wait_event_names.txt')) { - print "Generating pgstat_wait_event.c and wait_event_types.h...\n"; + print + "Generating pgstat_wait_event.c, wait_event_types.h and wait_event_funcs_data.c...\n"; my $activ = 'src/backend/utils/activity'; system( "perl $activ/generate-wait_event_types.pl --outdir $activ --code $activ/wait_event_names.txt" diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index 7cb23ea894..ac8da581e4 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -55,6 +55,7 @@ if exist src\include\catalog\header-stamp del /q src\include\catalog\header-stam if exist doc\src\sgml\version.sgml del /q doc\src\sgml\version.sgml if %DIST%==1 if exist src\backend\utils\activity\pgstat_wait_event.c del /q src\backend\utils\activity\pgstat_wait_event.c +if %DIST%==1 if exist src\backend\utils\activity\wait_event_funcs_data.c del /q src\backend\utils\activity\wait_event_funcs_data.c if %DIST%==1 if exist src\backend\utils\activity\wait_event_types.h del /q src\backend\utils\activity\wait_event_types.h if %DIST%==1 if exist src\backend\utils\fmgroids.h del /q src\backend\utils\fmgroids.h if %DIST%==1 if exist src\backend\utils\fmgrprotos.h del /q src\backend\utils\fmgrprotos.h -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-17 01:53                           ` Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 01:57                             ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  1 sibling, 2 replies; 34+ messages in thread

From: Masahiro Ikeda @ 2023-08-17 01:53 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

Thank you for creating the patch!
I think it is a very useful view as a user.

I will share some thoughts about the v6 patch.

1)

The regular expression needs to be changed in 
generate-wait_event_types.pl.
I have compared the documentation with the output of the pg_wait_event
view and found the following differences.

* For parameter names, the substitution for underscore is missing.
-ArchiveCleanupCommand Waiting for archive_cleanup_command to complete
-ArchiveCommand Waiting for archive_command to complete
+ArchiveCleanupCommand Waiting for archive-cleanup-command to complete
+ArchiveCommand Waiting for archive-command to complete
-RecoveryEndCommand Waiting for recovery_end_command to complete
+RecoveryEndCommand Waiting for recovery-end-command to complete
-RestoreCommand Waiting for restore_command to complete
+RestoreCommand Waiting for restore-command to complete

* The HTML tag match is not shortest match.
-frozenid Waiting to update pg_database.datfrozenxid and 
pg_database.datminmxid
+frozenid Waiting to update datminmxid

* There are two blanks before "about". Also " for heavy weight is
   removed (is it intended?)
-LockManager Waiting to read or update information about "heavyweight" 
locks
+LockManager Waiting to read or update information  about heavyweight 
locks

* Do we need "worker_spi_main" in the description? The name column
   shows the same one, so it could be omitted.
> pg_wait_event
> worker_spi_main Custom wait event "worker_spi_main" defined by 
> extension

2)

Would it be better to add "extension" meaning unassigned?

> Extensions can add Extension and LWLock types to the list shown in 
> Table 28.8 and Table 28.12. In some cases, the name of LWLock assigned 
> by an extension will not be available in all server processes; It might 
> be reported as just “extension” rather than the extension-assigned 
> name.

3)

Would index == els be better for the following Assert?
+	Assert(index <= els);

4)

There is a typo. alll -> all
+	/* Allocate enough space for alll entries */

5)

BTW, although I think this is outside the scope of this patch,
it might be a good idea to be able to add a description to the
API for custom wait events.

Regards,
-- 
Masahiro Ikeda
NTT DATA CORPORATION





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
@ 2023-08-17 01:57                             ` Michael Paquier <michael@paquier.xyz>
  2023-08-17 02:35                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:57                               ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  1 sibling, 2 replies; 34+ messages in thread

From: Michael Paquier @ 2023-08-17 01:57 UTC (permalink / raw)
  To: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; +Cc: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Thu, Aug 17, 2023 at 10:53:02AM +0900, Masahiro Ikeda wrote:
> BTW, although I think this is outside the scope of this patch,
> it might be a good idea to be able to add a description to the
> API for custom wait events.

Somebody on twitter has raised this point.  I am not sure that we need
to go down to that for the sake of this view, but I'm OK to..
Disagree and Commit to any consensus reached on this matter.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 01:57                             ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-17 02:35                               ` Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  1 sibling, 0 replies; 34+ messages in thread

From: Masahiro Ikeda @ 2023-08-17 02:35 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On 2023-08-17 10:57, Michael Paquier wrote:
> On Thu, Aug 17, 2023 at 10:53:02AM +0900, Masahiro Ikeda wrote:
>> BTW, although I think this is outside the scope of this patch,
>> it might be a good idea to be able to add a description to the
>> API for custom wait events.
> 
> Somebody on twitter has raised this point.  I am not sure that we need
> to go down to that for the sake of this view, but I'm OK to..
> Disagree and Commit to any consensus reached on this matter.

Oh, okay. Thanks for sharing the information.

Regards,
-- 
Masahiro Ikeda
NTT DATA CORPORATION





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 01:57                             ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-17 05:57                               ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  1 sibling, 0 replies; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-17 05:57 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; Masahiro Ikeda <ikedamsh@oss.nttdata.com>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/17/23 3:57 AM, Michael Paquier wrote:
> On Thu, Aug 17, 2023 at 10:53:02AM +0900, Masahiro Ikeda wrote:
>> BTW, although I think this is outside the scope of this patch,
>> it might be a good idea to be able to add a description to the
>> API for custom wait events.
> 
> Somebody on twitter has raised this point.

I think I know him ;-)

>  I am not sure that we need
> to go down to that for the sake of this view, but I'm OK to..
> Disagree and Commit to any consensus reached on this matter.

Yeah, I changed my mind of this. I think it's better to keep the
"control" about what is displayed in the description field for
this new system view.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
@ 2023-08-17 05:53                             ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  1 sibling, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-17 05:53 UTC (permalink / raw)
  To: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/17/23 3:53 AM, Masahiro Ikeda wrote:
> Hi,
> 
> Thank you for creating the patch!
> I think it is a very useful view as a user.
> 
> I will share some thoughts about the v6 patch.
> 

Thanks for looking at it!

> 1)
> 
> The regular expression needs to be changed in generate-wait_event_types.pl.
> I have compared the documentation with the output of the pg_wait_event
> view and found the following differences.
> 
> * For parameter names, the substitution for underscore is missing.
> -ArchiveCleanupCommand Waiting for archive_cleanup_command to complete
> -ArchiveCommand Waiting for archive_command to complete
> +ArchiveCleanupCommand Waiting for archive-cleanup-command to complete
> +ArchiveCommand Waiting for archive-command to complete
> -RecoveryEndCommand Waiting for recovery_end_command to complete
> +RecoveryEndCommand Waiting for recovery-end-command to complete
> -RestoreCommand Waiting for restore_command to complete
> +RestoreCommand Waiting for restore-command to complete
> 

Yeah, nice catch. v7 just shared up-thread replace "-" by "_"
for such a case. But I'm not sure the pg_wait_event description field needs
to be 100% aligned with the documentation: wouldn't be better to replace
"-" by " " in such cases in pg_wait_event?

> * The HTML tag match is not shortest match.
> -frozenid Waiting to update pg_database.datfrozenxid and pg_database.datminmxid
> +frozenid Waiting to update datminmxid
> 

Nice catch, fixed in v7.

> * There are two blanks before "about".

This is coming from wait_event_names.txt, thanks for pointing out.
I just submitted a patch [1] to fix that.

> Also " for heavy weight is
>    removed (is it intended?)
> -LockManager Waiting to read or update information about "heavyweight" locks
> +LockManager Waiting to read or update information  about heavyweight locks
> 

Not intended, fixed in v7.

> * Do we need "worker_spi_main" in the description? The name column
>    shows the same one, so it could be omitted.
>> pg_wait_event
>> worker_spi_main Custom wait event "worker_spi_main" defined by extension
> 

Do you mean remove the wait event name from the description in case of custom
extension wait events? I'd prefer to keep it, if not the descriptions would be
all the same for custom wait events.

> 2)
> 
> Would it be better to add "extension" meaning unassigned?
> 
>> Extensions can add Extension and LWLock types to the list shown in Table 28.8 and Table 28.12. In some cases, the name of LWLock assigned by an extension will not be available in all server processes; It might be reported as just “extension” rather than the extension-assigned name.
> 

Yeah, could make sense but I think that should be a dedicated patch for the documentation.

> 3)
> 
> Would index == els be better for the following Assert?
> +    Assert(index <= els);
> 

Agree, done in v7.

> 4)
> 
> There is a typo. alll -> all
> +    /* Allocate enough space for alll entries */
> 

Thanks! Fixed in v7.

[1]: https://www.postgresql.org/message-id/dd836027-2e9e-4df9-9fd9-7527cd1757e1%40gmail.com

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-17 07:37                               ` Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 22:37                                 ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-18 08:56                                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 2 replies; 34+ messages in thread

From: Masahiro Ikeda @ 2023-08-17 07:37 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 2023-08-17 14:53, Drouvot, Bertrand wrote:
> On 8/17/23 3:53 AM, Masahiro Ikeda wrote:
>> 1)
>> 
>> The regular expression needs to be changed in 
>> generate-wait_event_types.pl.
>> I have compared the documentation with the output of the pg_wait_event
>> view and found the following differences.
>> 
>> * For parameter names, the substitution for underscore is missing.
>> -ArchiveCleanupCommand Waiting for archive_cleanup_command to complete
>> -ArchiveCommand Waiting for archive_command to complete
>> +ArchiveCleanupCommand Waiting for archive-cleanup-command to complete
>> +ArchiveCommand Waiting for archive-command to complete
>> -RecoveryEndCommand Waiting for recovery_end_command to complete
>> +RecoveryEndCommand Waiting for recovery-end-command to complete
>> -RestoreCommand Waiting for restore_command to complete
>> +RestoreCommand Waiting for restore-command to complete
>> 
> 
> Yeah, nice catch. v7 just shared up-thread replace "-" by "_"
> for such a case. But I'm not sure the pg_wait_event description field 
> needs
> to be 100% aligned with the documentation: wouldn't be better to 
> replace
> "-" by " " in such cases in pg_wait_event?
> 
>> * The HTML tag match is not shortest match.
>> -frozenid Waiting to update pg_database.datfrozenxid and 
>> pg_database.datminmxid
>> +frozenid Waiting to update datminmxid
>> 
> 
> Nice catch, fixed in v7.

Thanks, I confirmed.

>> * There are two blanks before "about".
> 
> This is coming from wait_event_names.txt, thanks for pointing out.
> I just submitted a patch [1] to fix that.

Thanks. I agree it's better to be treated as a separated patch.

>> Also " for heavy weight is
>>    removed (is it intended?)
>> -LockManager Waiting to read or update information about "heavyweight" 
>> locks
>> +LockManager Waiting to read or update information  about heavyweight 
>> locks
>> 
> 
> Not intended, fixed in v7.

OK, I confirmed it's fixed.

>> * Do we need "worker_spi_main" in the description? The name column
>>    shows the same one, so it could be omitted.
>>> pg_wait_event
>>> worker_spi_main Custom wait event "worker_spi_main" defined by 
>>> extension
>> 
> 
> Do you mean remove the wait event name from the description in case of 
> custom
> extension wait events? I'd prefer to keep it, if not the descriptions 
> would be
> all the same for custom wait events.

OK, I thought it's redundant.

BTW, is it better to start with "Waiting" like any other lines?
For example, "Waiting for custom event \"worker_spi_main\" defined by an 
extension".

>> 2)
>> 
>> Would it be better to add "extension" meaning unassigned?
>> 
>>> Extensions can add Extension and LWLock types to the list shown in 
>>> Table 28.8 and Table 28.12. In some cases, the name of LWLock 
>>> assigned by an extension will not be available in all server 
>>> processes; It might be reported as just “extension” rather than the 
>>> extension-assigned name.
>> 
> 
> Yeah, could make sense but I think that should be a dedicated patch
> for the documentation.

OK, make sense.

>> 3)
>> 
>> Would index == els be better for the following Assert?
>> +    Assert(index <= els);
>> 
> 
> Agree, done in v7.
> 
>> 4)
>> 
>> There is a typo. alll -> all
>> +    /* Allocate enough space for alll entries */

Thanks, I confirmed it's fixed.


The followings are additional comments for v7.

1)

>> I am not sure that "pg_wait_event" is a good idea for the name if the
>> new view.  How about "pg_wait_events" instead, in plural form?  There
>> is more than one wait event listed.
> I'd prefer the singular form. There is a lot of places where it's 
> already used
> (pg_database, pg_user, pg_namespace...to name a few) and it looks like 
> that using
> the plural form are exceptions.

Since I got the same feeling as Michael-san that "pg_wait_events" would 
be better,
I check the names of all system views. I think the singular form seems 
to be
exceptions for views though the singular form is used usually for system 
catalogs.

https://www.postgresql.org/docs/devel/views.html
https://www.postgresql.org/docs/devel/catalogs.html

2)

$ctmp must be $wctmp?

+	rename($wctmp, "$output_path/wait_event_funcs_data.c")
+	  || die "rename: $ctmp to $output_path/wait_event_funcs_data.c: $!";


3)

"List all the wait events type, name and descriptions" is enough?

+ * List all the wait events (type, name) and their descriptions.

4)

Why don't you add the usage of the view in the monitoring.sgml?

```
    <para>
      Here is an example of how wait events can be viewed:

<programlisting>
SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE 
wait_event is NOT NULL;
  pid  | wait_event_type | wait_event
------+-----------------+------------
  2540 | Lock            | relation
  6644 | LWLock          | ProcArray
(2 rows)
</programlisting>
    </para>
```

ex.
postgres(3789417)=# SELECT a.pid, a.wait_event_type, a.wait_event, 
w.description FROM pg_stat_activity a JOIN pg_wait_event w ON 
(a.wait_event_type = w.type AND a.wait_event = w.name) WHERE wait_event 
is NOT NULL;
    pid   | wait_event_type |    wait_event     |                      
description
---------+-----------------+-------------------+--------------------------------------------------------
  3783759 | Activity        | AutoVacuumMain    | Waiting in main loop of 
autovacuum launcher process
  3812866 | Extension       | WorkerSpiMain     | Custom wait event 
"WorkerSpiMain" defined by extension
  3783756 | Activity        | BgWriterHibernate | Waiting in background 
writer process, hibernating
  3783760 | Activity        | ArchiverMain      | Waiting in main loop of 
archiver process
  3783755 | Activity        | CheckpointerMain  | Waiting in main loop of 
checkpointer process
  3783758 | Activity        | WalWriterMain     | Waiting in main loop of 
WAL writer process
(6 rows)

5)

Though I'm not familiar the value, do we need procost?

+{ oid => '8403', descr => 'describe wait events',
+  proname => 'pg_get_wait_events', procost => '10', prorows => '100',
+  proretset => 't', provolatile => 's', prorettype => 'record',
+  proargtypes => '', proallargtypes => '{text,text,text}',
+  proargmodes => '{o,o,o}', proargnames => '{type,name,description}',
+  prosrc => 'pg_get_wait_events' },


Regards,
-- 
Masahiro Ikeda
NTT DATA CORPORATION





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
@ 2023-08-17 22:37                                 ` Michael Paquier <michael@paquier.xyz>
  2023-08-18 08:57                                   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  1 sibling, 1 reply; 34+ messages in thread

From: Michael Paquier @ 2023-08-17 22:37 UTC (permalink / raw)
  To: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; +Cc: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Thu, Aug 17, 2023 at 04:37:22PM +0900, Masahiro Ikeda wrote:
> On 2023-08-17 14:53, Drouvot, Bertrand wrote:
> BTW, is it better to start with "Waiting" like any other lines?
> For example, "Waiting for custom event \"worker_spi_main\" defined by an
> extension".

Using "Waiting for custom event" is a good term here.  I am wondering
if this should be s/extension/module/, as an event could be set by a
loaded library, which may not be set with CREATE EXTENSION.

> ex.
> postgres(3789417)=# SELECT a.pid, a.wait_event_type, a.wait_event,
> w.description FROM pg_stat_activity a JOIN pg_wait_event w ON
> (a.wait_event_type = w.type AND a.wait_event = w.name) WHERE wait_event is
> NOT NULL;
>    pid   | wait_event_type |    wait_event     |
> description
> ---------+-----------------+-------------------+--------------------------------------------------------
>  3783759 | Activity        | AutoVacuumMain    | Waiting in main loop of
> autovacuum launcher process
>  3812866 | Extension       | WorkerSpiMain     | Custom wait event
> "WorkerSpiMain" defined by extension
>  3783756 | Activity        | BgWriterHibernate | Waiting in background
> writer process, hibernating
>  3783760 | Activity        | ArchiverMain      | Waiting in main loop of
> archiver process
>  3783755 | Activity        | CheckpointerMain  | Waiting in main loop of
> checkpointer process
>  3783758 | Activity        | WalWriterMain     | Waiting in main loop of WAL
> writer process
> (6 rows)

You need to think about the PDF being generated from the SGML docs, as
well, so this should not be too large.  I don't think we need to care
about wait_event_type for this example, so it can be removed.  If the
tuple generated is still too large, showing one tuple under \x with a
filter on backend_type would be enough. 
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 22:37                                 ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-18 08:57                                   ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-18 10:28                                     ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-18 08:57 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; Masahiro Ikeda <ikedamsh@oss.nttdata.com>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/18/23 12:37 AM, Michael Paquier wrote:
> On Thu, Aug 17, 2023 at 04:37:22PM +0900, Masahiro Ikeda wrote:
>> On 2023-08-17 14:53, Drouvot, Bertrand wrote:
>> BTW, is it better to start with "Waiting" like any other lines?
>> For example, "Waiting for custom event \"worker_spi_main\" defined by an
>> extension".
> 
> Using "Waiting for custom event" is a good term here. 

Yeah, done in v8 just shared up-thread.

> I am wondering
> if this should be s/extension/module/, as an event could be set by a
> loaded library, which may not be set with CREATE EXTENSION.
> 

I think that removing "extension" sounds weird given the fact that the
wait event type is "Extension" (that could lead to confusion).

I did use "defined by an extension module" in v8 (also that's aligned with
the wording used in wait_event.h).

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 22:37                                 ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-18 08:57                                   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-18 10:28                                     ` Michael Paquier <michael@paquier.xyz>
  0 siblings, 0 replies; 34+ messages in thread

From: Michael Paquier @ 2023-08-18 10:28 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Fri, Aug 18, 2023 at 10:57:28AM +0200, Drouvot, Bertrand wrote:
> I did use "defined by an extension module" in v8 (also that's aligned with
> the wording used in wait_event.h).

WFM.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
@ 2023-08-18 08:56                                 ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-18 10:31                                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  1 sibling, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-18 08:56 UTC (permalink / raw)
  To: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/17/23 9:37 AM, Masahiro Ikeda wrote:
> Hi,
> 
> On 2023-08-17 14:53, Drouvot, Bertrand wrote:
> 
> The followings are additional comments for v7.
> 
> 1)
> 
>>> I am not sure that "pg_wait_event" is a good idea for the name if the
>>> new view.  How about "pg_wait_events" instead, in plural form?  There
>>> is more than one wait event listed.
>> I'd prefer the singular form. There is a lot of places where it's already used
>> (pg_database, pg_user, pg_namespace...to name a few) and it looks like that using
>> the plural form are exceptions.
> 
> Since I got the same feeling as Michael-san that "pg_wait_events" would be better,
> I check the names of all system views. I think the singular form seems to be
> exceptions for views though the singular form is used usually for system catalogs.
> 
> https://www.postgresql.org/docs/devel/views.html
> https://www.postgresql.org/docs/devel/catalogs.html

Okay, using the plural form in v8 attached.

> 
> 2)
> 
> $ctmp must be $wctmp?
> 
> +    rename($wctmp, "$output_path/wait_event_funcs_data.c")
> +      || die "rename: $ctmp to $output_path/wait_event_funcs_data.c: $!";
> 

Nice catch, thanks, fixed in v8.

> 
> 3)
> 
> "List all the wait events type, name and descriptions" is enough?
> 
> + * List all the wait events (type, name) and their descriptions.
> 

Agree, used "List all the wait events type, name and description" in v8
(using description in singular as compared to your proposal)

> 4)
> 
> Why don't you add the usage of the view in the monitoring.sgml?
> 
> ```
>     <para>
>       Here is an example of how wait events can be viewed:
> 
> <programlisting>
> SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event is NOT NULL;
>   pid  | wait_event_type | wait_event
> ------+-----------------+------------
>   2540 | Lock            | relation
>   6644 | LWLock          | ProcArray
> (2 rows)
> </programlisting>
>     </para>
> ```
> 
> ex.
> postgres(3789417)=# SELECT a.pid, a.wait_event_type, a.wait_event, w.description FROM pg_stat_activity a JOIN pg_wait_event w ON (a.wait_event_type = w.type AND a.wait_event = w.name) WHERE wait_event is NOT NULL;

Agree, I think that's a good idea.

I'd prefer to add also a filtering on "state='active'" for this example (I think that would provide
a "real" use case as the sessions are not waiting if they are not active).

Done in v8.

> 5)
> 
> Though I'm not familiar the value, do we need procost?
> 

I think it makes sense to add a "small" one as it's done.
BTW, while looking at it, I changed prorows to a more "accurate"
value.


Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v8-0001-Add-catalog-pg_wait_events.patch (text/plain, 23.0K) download From 027a83d37d52eee52e3c82bcf814c6d5949f7ccf Mon Sep 17 00:00:00 2001 From: bdrouvotAWS <bdrouvot@amazon.com> Date: Sat, 5 Aug 2023 12:39:42 +0000 Subject: [PATCH v8] Add catalog pg_wait_events Adding a new system view, namely pg_wait_events, that describes the wait events. --- doc/src/sgml/monitoring.sgml | 13 ++- doc/src/sgml/system-views.sgml | 64 +++++++++++++ src/backend/Makefile | 3 +- src/backend/catalog/system_views.sql | 3 + src/backend/utils/activity/.gitignore | 1 + src/backend/utils/activity/Makefile | 8 +- .../activity/generate-wait_event_types.pl | 54 ++++++++++- src/backend/utils/activity/meson.build | 1 + src/backend/utils/activity/wait_event.c | 44 +++++++++ src/backend/utils/activity/wait_event_funcs.c | 93 +++++++++++++++++++ src/include/catalog/pg_proc.dat | 6 ++ src/include/utils/meson.build | 4 +- src/include/utils/wait_event.h | 1 + .../modules/worker_spi/t/001_worker_spi.pl | 6 ++ src/test/regress/expected/rules.out | 4 + src/test/regress/expected/sysviews.out | 16 ++++ src/test/regress/sql/sysviews.sql | 4 + src/tools/msvc/Solution.pm | 3 +- src/tools/msvc/clean.bat | 1 + 19 files changed, 318 insertions(+), 11 deletions(-) create mode 100644 src/backend/utils/activity/wait_event_funcs.c diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 70511a2388..bfa658f7a8 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -1103,7 +1103,7 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser &wait_event_types; <para> - Here is an example of how wait events can be viewed: + Here is are examples of how wait events can be viewed: <programlisting> SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event is NOT NULL; @@ -1112,6 +1112,17 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i 2540 | Lock | relation 6644 | LWLock | ProcArray (2 rows) +</programlisting> + +<programlisting> +SELECT a.pid, a.wait_event, w.description +FROM pg_stat_activity a JOIN pg_wait_events w +ON (a.wait_event_type = w.type AND a.wait_event = w.name) +WHERE wait_event is NOT NULL and a.state = 'active'; +-[ RECORD 1 ]------------------------------------------------------------------ +pid | 686674 +wait_event | WALInitSync +description | Waiting for a newly initialized WAL file to reach durable storage </programlisting> </para> diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 57b228076e..2b35c2f91b 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -221,6 +221,11 @@ <entry>views</entry> </row> + <row> + <entry><link linkend="view-pg-wait-events"><structname>pg_wait_events</structname></link></entry> + <entry>wait events</entry> + </row> + </tbody> </tgroup> </table> @@ -4825,4 +4830,63 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx </table> </sect1> + + <sect1 id="view-pg-wait-events"> + <title><structname>pg_wait_events</structname></title> + + <indexterm zone="view-pg-wait-events"> + <primary>pg_wait_events</primary> + </indexterm> + + <para> + The view <structname>pg_wait_events</structname> provides description about the + wait events. + </para> + + <table> + <title><structname>pg_wait_events</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>type</structfield> <type>text</type> + </para> + <para> + Wait event type + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>text</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/backend/Makefile b/src/backend/Makefile index 1c929383c4..3e275ac759 100644 --- a/src/backend/Makefile +++ b/src/backend/Makefile @@ -134,7 +134,7 @@ storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lw $(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c utils/activity/wait_event_types.h: utils/activity/generate-wait_event_types.pl utils/activity/wait_event_names.txt - $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c + $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c # run this unconditionally to avoid needing to know its dependencies here: submake-catalog-headers: @@ -311,6 +311,7 @@ maintainer-clean: distclean storage/lmgr/lwlocknames.c \ storage/lmgr/lwlocknames.h \ utils/activity/pgstat_wait_event.c \ + utils/activity/wait_event_funcs_data.c \ utils/activity/wait_event_types.h \ utils/adt/jsonpath_gram.c \ utils/adt/jsonpath_gram.h \ diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index af65af6bdd..362b1ea8d5 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1342,3 +1342,6 @@ CREATE VIEW pg_stat_subscription_stats AS ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; + +CREATE VIEW pg_wait_events AS + SELECT * FROM pg_get_wait_events() AS we; diff --git a/src/backend/utils/activity/.gitignore b/src/backend/utils/activity/.gitignore index d77079285b..bd0c0c7772 100644 --- a/src/backend/utils/activity/.gitignore +++ b/src/backend/utils/activity/.gitignore @@ -1,2 +1,3 @@ /pgstat_wait_event.c /wait_event_types.h +/wait_event_funcs_data.c diff --git a/src/backend/utils/activity/Makefile b/src/backend/utils/activity/Makefile index f1117745d4..f57cf3958c 100644 --- a/src/backend/utils/activity/Makefile +++ b/src/backend/utils/activity/Makefile @@ -32,10 +32,14 @@ OBJS = \ pgstat_subscription.o \ pgstat_wal.o \ pgstat_xact.o \ - wait_event.o + wait_event.o \ + wait_event_funcs.o include $(top_srcdir)/src/backend/common.mk +wait_event_funcs.o: wait_event_funcs_data.c +wait_event_funcs_data.c: wait_event_types.h + wait_event.o: pgstat_wait_event.c pgstat_wait_event.c: wait_event_types.h touch $@ @@ -44,4 +48,4 @@ wait_event_types.h: $(top_srcdir)/src/backend/utils/activity/wait_event_names.tx $(PERL) $(srcdir)/generate-wait_event_types.pl --code $< maintainer-clean: clean - rm -f wait_event_types.h pgstat_wait_event.c + rm -f wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..9c4557d903 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -4,6 +4,7 @@ # Generate wait events support files from wait_event_names.txt: # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) +# - wait_event_funcs_data.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -98,8 +99,10 @@ if ($gen_code) # multiple times. my $htmp = "$output_path/wait_event_types.h.tmp$$"; my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$"; + my $wctmp = "$output_path/wait_event_funcs_data.c.tmp$$"; open my $h, '>', $htmp or die "Could not open $htmp: $!"; open my $c, '>', $ctmp or die "Could not open $ctmp: $!"; + open my $wc, '>', $wctmp or die "Could not open $wctmp: $!"; my $header_comment = '/*------------------------------------------------------------------------- @@ -129,12 +132,14 @@ if ($gen_code) printf $c $header_comment, 'pgstat_wait_event.c'; + printf $wc $header_comment, 'wait_event_funcs_data.c'; + + # Generate the pgstat_wait_event.c and wait_event_types.h files # uc() is being used to force the comparison to be case-insensitive. foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) { - - # Don't generate .c and .h files for Extension, LWLock and - # Lock, these are handled independently. + # Don't generate the pgstat_wait_event.c and wait_event_types.h files + # for Extension, LWLock and Lock, these are handled independently. next if ( $waitclass eq 'WaitEventExtension' || $waitclass eq 'WaitEventLWLock' @@ -183,14 +188,55 @@ if ($gen_code) printf $c "}\n\n"; } + # Generate wait_event_funcs_data.c, for the contents of static C + # structure holding all the information about the wait events. + # uc() is being used to force the comparison to be case-insensitive, + # even though it is not strictly mandatory here. + foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) + { + my $last = $waitclass; + $last =~ s/^WaitEvent//; + + foreach my $wev (@{ $hashwe{$waitclass} }) + { + my $new_desc = substr $wev->[2], 1, -2; + # Escape single quotes. + $new_desc =~ s/'/\\'/g; + + # Replace the "quote" markups by real ones. + $new_desc =~ s/<quote>(.*?)<\/quote>/\\"$1\\"/g; + + # Remove SGML markups. + $new_desc =~ s/<.*?>(.*?)<.*?>/$1/g; + + # Tweak contents about links <xref linkend="text"/> on GUCs, + while (my ($capture) = + $new_desc =~ m/<xref linkend="guc-(.*?)"\/>/g) + { + $capture =~ s/-/_/g; + $new_desc =~ s/<xref linkend="guc-.*?"\/>/$capture/g; + } + # Then remove any reference to "see <xref linkend="text"/>". + $new_desc =~ s/; see.*$//; + + # One element to the C structure holding the wait event + # info, as of (type, name, description). + printf $wc "\t{\"%s\", \"%s\", \"%s\"},\n", $last, $wev->[1], + $new_desc; + } + } + printf $h "#endif /* WAIT_EVENT_TYPES_H */\n"; close $h; close $c; + close $wc; rename($htmp, "$output_path/wait_event_types.h") || die "rename: $htmp to $output_path/wait_event_types.h: $!"; rename($ctmp, "$output_path/pgstat_wait_event.c") || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!"; + rename($wctmp, "$output_path/wait_event_funcs_data.c") + || die "rename: $wctmp to $output_path/wait_event_funcs_data.c: $!"; } # Generate the .sgml file. elsif ($gen_docs) @@ -249,7 +295,7 @@ Usage: perl [--output <path>] [--code ] [ --sgml ] input_file Options: --outdir Output directory (default '.') - --code Generate wait_event_types.h and pgstat_wait_event.c. + --code Generate C and header files. --sgml Generate wait_event_types.sgml. generate-wait_event_types.pl generates the SGML documentation and code diff --git a/src/backend/utils/activity/meson.build b/src/backend/utils/activity/meson.build index 9633f3623c..46a27e7548 100644 --- a/src/backend/utils/activity/meson.build +++ b/src/backend/utils/activity/meson.build @@ -23,6 +23,7 @@ backend_sources += files( # seems nicer to not add that as an include path for the whole backend. waitevent_sources = files( 'wait_event.c', + 'wait_event_funcs.c', ) wait_event = static_library('wait_event_names', diff --git a/src/backend/utils/activity/wait_event.c b/src/backend/utils/activity/wait_event.c index 4b9b5c01cb..d31767df80 100644 --- a/src/backend/utils/activity/wait_event.c +++ b/src/backend/utils/activity/wait_event.c @@ -264,6 +264,50 @@ GetWaitEventExtensionIdentifier(uint16 eventId) } +/* + * Returns a list of currently defined custom wait event names for extensions. + * The result is a palloc'd array, with the number of elements returned into + * *nwaitevents. + */ +char ** +GetWaitEventExtensionNames(int *nwaitevents) +{ + char **waiteventnames; + WaitEventExtensionEntryByName *hentry; + HASH_SEQ_STATUS hash_seq; + int index; + int els; + + LWLockAcquire(WaitEventExtensionLock, LW_SHARED); + + /* Now we can safely count the number of entries */ + els = hash_get_num_entries(WaitEventExtensionHashByName); + + /* Allocate enough space for all entries */ + waiteventnames = palloc(els * sizeof(char[NAMEDATALEN])); + + /* Now scan the hash table to copy the data */ + hash_seq_init(&hash_seq, WaitEventExtensionHashByName); + + index = 0; + while ((hentry = (WaitEventExtensionEntryByName *) hash_seq_search(&hash_seq)) != NULL) + { + + waiteventnames[index] = palloc(sizeof(char[NAMEDATALEN])); + strlcpy(waiteventnames[index], hentry->wait_event_name, + sizeof(char[NAMEDATALEN])); + + index++; + } + + LWLockRelease(WaitEventExtensionLock); + + Assert(index == els); + + *nwaitevents = index; + return waiteventnames; +} + /* * Configure wait event reporting to report wait events to *wait_event_info. * *wait_event_info needs to be valid until pgstat_reset_wait_event_storage() diff --git a/src/backend/utils/activity/wait_event_funcs.c b/src/backend/utils/activity/wait_event_funcs.c new file mode 100644 index 0000000000..06992f04a9 --- /dev/null +++ b/src/backend/utils/activity/wait_event_funcs.c @@ -0,0 +1,93 @@ +/*------------------------------------------------------------------------ + * + * wait_event_funcs.c + * Functions for accessing wait event data. + * + * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/utils/activity/wait_event_funcs.c + * + *------------------------------------------------------------------------ + */ +#include "postgres.h" + +#include "funcapi.h" +#include "utils/builtins.h" +#include "utils/wait_event.h" + +/* + * Each wait event has one corresponding entry in this structure, fed to + * the SQL function of this file. + */ +static const struct +{ + const char *type; + const char *name; + const char *description; +} + +waitEventData[] = +{ +#include "wait_event_funcs_data.c" + /* end of list */ + {NULL, NULL, NULL} +}; + + +/* + * pg_get_wait_events + * + * List all the wait events type, name and description. + */ +Datum +pg_get_wait_events(PG_FUNCTION_ARGS) +{ +#define PG_GET_WAIT_EVENTS_COLS 3 + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + char **waiteventnames; + int nbextwaitevents; + + /* Build tuplestore to hold the result rows */ + InitMaterializedSRF(fcinfo, 0); + + /* Iterate over the list of wait events */ + for (int idx = 0; waitEventData[idx].type != NULL; idx++) + { + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + values[0] = CStringGetTextDatum(waitEventData[idx].type); + values[1] = CStringGetTextDatum(waitEventData[idx].name); + values[2] = CStringGetTextDatum(waitEventData[idx].description); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + /* Handling extension custom wait events */ + waiteventnames = GetWaitEventExtensionNames(&nbextwaitevents); + + for (int idx = 0; idx < nbextwaitevents; idx++) + { + StringInfoData buf; + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + + values[0] = CStringGetTextDatum("Extension"); + values[1] = CStringGetTextDatum(waiteventnames[idx]); + + initStringInfo(&buf); + appendStringInfoString(&buf, "Waiting for custom wait event \""); + appendStringInfoString(&buf, waiteventnames[idx]); + appendStringInfoString(&buf, "\" defined by an extension module"); + + values[2] = CStringGetTextDatum(buf.data); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + return (Datum) 0; +} diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..b2dab98a8d 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5417,6 +5417,12 @@ proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}', proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}', prosrc => 'pg_stat_get_activity' }, +{ oid => '8403', descr => 'describe wait events', + proname => 'pg_get_wait_events', procost => '10', prorows => '250', + proretset => 't', provolatile => 's', prorettype => 'record', + proargtypes => '', proallargtypes => '{text,text,text}', + proargmodes => '{o,o,o}', proargnames => '{type,name,description}', + prosrc => 'pg_get_wait_events' }, { oid => '3318', descr => 'statistics: information about progress of backends running maintenance command', proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't', diff --git a/src/include/utils/meson.build b/src/include/utils/meson.build index 6de5d93799..c179478611 100644 --- a/src/include/utils/meson.build +++ b/src/include/utils/meson.build @@ -1,6 +1,6 @@ # Copyright (c) 2022-2023, PostgreSQL Global Development Group -wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c'] +wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c', 'wait_event_funcs_data.c'] wait_event_target = custom_target('wait_event_names', input: files('../../backend/utils/activity/wait_event_names.txt'), output: wait_event_output, @@ -11,7 +11,7 @@ wait_event_target = custom_target('wait_event_names', ], build_by_default: true, install: true, - install_dir: [dir_include_server / 'utils', false], + install_dir: [dir_include_server / 'utils', false, false], ) wait_event_types_h = wait_event_target[0] diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h index 3eebdfad38..009b03a520 100644 --- a/src/include/utils/wait_event.h +++ b/src/include/utils/wait_event.h @@ -63,6 +63,7 @@ extern void WaitEventExtensionShmemInit(void); extern Size WaitEventExtensionShmemSize(void); extern uint32 WaitEventExtensionNew(const char *wait_event_name); +extern char **GetWaitEventExtensionNames(int *nwaitevents); /* ---------- * pgstat_report_wait_start() - diff --git a/src/test/modules/worker_spi/t/001_worker_spi.pl b/src/test/modules/worker_spi/t/001_worker_spi.pl index 26b8a49bec..2965acd789 100644 --- a/src/test/modules/worker_spi/t/001_worker_spi.pl +++ b/src/test/modules/worker_spi/t/001_worker_spi.pl @@ -47,6 +47,12 @@ $result = $node->poll_query_until( is($result, 1, 'dynamic bgworker has reported "worker_spi_main" as wait event'); +# Check the wait event used by the dynamic bgworker appears in pg_wait_events +$result = $node->safe_psql('postgres', + q[SELECT count(*) > 0 from pg_wait_events where type = 'Extension' and name = 'worker_spi_main';] +); +is($result, 't', '"worker_spi_main" is reported in pg_wait_events'); + note "testing bgworkers loaded with shared_preload_libraries"; # Create the database first so as the workers can connect to it when diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e07afcd4aa..55447d8090 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2631,6 +2631,10 @@ pg_views| SELECT n.nspname AS schemaname, FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char"); +pg_wait_events| SELECT type, + name, + description + FROM pg_get_wait_events() we(type, name, description); SELECT tablename, rulename, definition FROM pg_rules WHERE schemaname = 'pg_catalog' ORDER BY tablename, rulename; diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 001c6e7eb9..aae5d51e1c 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -134,6 +134,22 @@ select name, setting from pg_settings where name like 'enable%'; enable_tidscan | on (21 rows) +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_events + group by type order by type COLLATE "C"; + type | ok +-----------+---- + Activity | t + BufferPin | t + Client | t + Extension | t + IO | t + IPC | t + LWLock | t + Lock | t + Timeout | t +(9 rows) + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 351e469c77..6b4e24601d 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -55,6 +55,10 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- a regression test run. select name, setting from pg_settings where name like 'enable%'; +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_events + group by type order by type COLLATE "C"; + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index c98a1e9f9a..a50f730260 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -588,7 +588,8 @@ sub GenerateFiles 'src/include/utils/wait_event_types.h', 'src/backend/utils/activity/wait_event_names.txt')) { - print "Generating pgstat_wait_event.c and wait_event_types.h...\n"; + print + "Generating pgstat_wait_event.c, wait_event_types.h and wait_event_funcs_data.c...\n"; my $activ = 'src/backend/utils/activity'; system( "perl $activ/generate-wait_event_types.pl --outdir $activ --code $activ/wait_event_names.txt" diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index 7cb23ea894..ac8da581e4 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -55,6 +55,7 @@ if exist src\include\catalog\header-stamp del /q src\include\catalog\header-stam if exist doc\src\sgml\version.sgml del /q doc\src\sgml\version.sgml if %DIST%==1 if exist src\backend\utils\activity\pgstat_wait_event.c del /q src\backend\utils\activity\pgstat_wait_event.c +if %DIST%==1 if exist src\backend\utils\activity\wait_event_funcs_data.c del /q src\backend\utils\activity\wait_event_funcs_data.c if %DIST%==1 if exist src\backend\utils\activity\wait_event_types.h del /q src\backend\utils\activity\wait_event_types.h if %DIST%==1 if exist src\backend\utils\fmgroids.h del /q src\backend\utils\fmgroids.h if %DIST%==1 if exist src\backend\utils\fmgrprotos.h del /q src\backend\utils\fmgrprotos.h -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-18 08:56                                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-18 10:31                                   ` Michael Paquier <michael@paquier.xyz>
  2023-08-19 09:35                                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Michael Paquier @ 2023-08-18 10:31 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Fri, Aug 18, 2023 at 10:56:55AM +0200, Drouvot, Bertrand wrote:
> Okay, using the plural form in v8 attached.

Noting in passing:
-     Here is an example of how wait events can be viewed:
+     Here is are examples of how wait events can be viewed:
s/is are/are/.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-18 08:56                                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-18 10:31                                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-19 09:35                                     ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-19 10:00                                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-19 09:35 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/18/23 12:31 PM, Michael Paquier wrote:
> On Fri, Aug 18, 2023 at 10:56:55AM +0200, Drouvot, Bertrand wrote:
>> Okay, using the plural form in v8 attached.
> 
> Noting in passing:
> -     Here is an example of how wait events can be viewed:
> +     Here is are examples of how wait events can be viewed:
> s/is are/are/.

Thanks! Please find attached v9 fixing this typo.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v9-0001-Add-catalog-pg_wait_events.patch (text/plain, 23.1K) download From 430cc8cb79c7cf3ea811760e8aadd53cab7ff9e3 Mon Sep 17 00:00:00 2001 From: bdrouvotAWS <bdrouvot@amazon.com> Date: Sat, 5 Aug 2023 12:39:42 +0000 Subject: [PATCH v9] Add catalog pg_wait_events Adding a new system view, namely pg_wait_events, that describes the wait events. --- doc/src/sgml/monitoring.sgml | 13 ++- doc/src/sgml/system-views.sgml | 64 +++++++++++++ src/backend/Makefile | 3 +- src/backend/catalog/system_views.sql | 3 + src/backend/utils/activity/.gitignore | 1 + src/backend/utils/activity/Makefile | 8 +- .../activity/generate-wait_event_types.pl | 54 ++++++++++- src/backend/utils/activity/meson.build | 1 + src/backend/utils/activity/wait_event.c | 44 +++++++++ src/backend/utils/activity/wait_event_funcs.c | 93 +++++++++++++++++++ src/include/catalog/pg_proc.dat | 6 ++ src/include/utils/meson.build | 4 +- src/include/utils/wait_event.h | 1 + .../modules/worker_spi/t/001_worker_spi.pl | 6 ++ src/test/regress/expected/rules.out | 4 + src/test/regress/expected/sysviews.out | 16 ++++ src/test/regress/sql/sysviews.sql | 4 + src/tools/msvc/Solution.pm | 3 +- src/tools/msvc/clean.bat | 1 + 19 files changed, 318 insertions(+), 11 deletions(-) 21.0% doc/src/sgml/ 59.5% src/backend/utils/activity/ 3.5% src/include/catalog/ 4.5% src/test/regress/expected/ 4.5% src/test/ 3.0% src/tools/msvc/ 3.6% src/ diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 70511a2388..6c53c1ed91 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -1103,7 +1103,7 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser &wait_event_types; <para> - Here is an example of how wait events can be viewed: + Here are examples of how wait events can be viewed: <programlisting> SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event is NOT NULL; @@ -1112,6 +1112,17 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i 2540 | Lock | relation 6644 | LWLock | ProcArray (2 rows) +</programlisting> + +<programlisting> +SELECT a.pid, a.wait_event, w.description +FROM pg_stat_activity a JOIN pg_wait_events w +ON (a.wait_event_type = w.type AND a.wait_event = w.name) +WHERE wait_event is NOT NULL and a.state = 'active'; +-[ RECORD 1 ]------------------------------------------------------------------ +pid | 686674 +wait_event | WALInitSync +description | Waiting for a newly initialized WAL file to reach durable storage </programlisting> </para> diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 57b228076e..2b35c2f91b 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -221,6 +221,11 @@ <entry>views</entry> </row> + <row> + <entry><link linkend="view-pg-wait-events"><structname>pg_wait_events</structname></link></entry> + <entry>wait events</entry> + </row> + </tbody> </tgroup> </table> @@ -4825,4 +4830,63 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx </table> </sect1> + + <sect1 id="view-pg-wait-events"> + <title><structname>pg_wait_events</structname></title> + + <indexterm zone="view-pg-wait-events"> + <primary>pg_wait_events</primary> + </indexterm> + + <para> + The view <structname>pg_wait_events</structname> provides description about the + wait events. + </para> + + <table> + <title><structname>pg_wait_events</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>type</structfield> <type>text</type> + </para> + <para> + Wait event type + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>text</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/backend/Makefile b/src/backend/Makefile index 1c929383c4..3e275ac759 100644 --- a/src/backend/Makefile +++ b/src/backend/Makefile @@ -134,7 +134,7 @@ storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lw $(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c utils/activity/wait_event_types.h: utils/activity/generate-wait_event_types.pl utils/activity/wait_event_names.txt - $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c + $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c # run this unconditionally to avoid needing to know its dependencies here: submake-catalog-headers: @@ -311,6 +311,7 @@ maintainer-clean: distclean storage/lmgr/lwlocknames.c \ storage/lmgr/lwlocknames.h \ utils/activity/pgstat_wait_event.c \ + utils/activity/wait_event_funcs_data.c \ utils/activity/wait_event_types.h \ utils/adt/jsonpath_gram.c \ utils/adt/jsonpath_gram.h \ diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index af65af6bdd..362b1ea8d5 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1342,3 +1342,6 @@ CREATE VIEW pg_stat_subscription_stats AS ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; + +CREATE VIEW pg_wait_events AS + SELECT * FROM pg_get_wait_events() AS we; diff --git a/src/backend/utils/activity/.gitignore b/src/backend/utils/activity/.gitignore index d77079285b..bd0c0c7772 100644 --- a/src/backend/utils/activity/.gitignore +++ b/src/backend/utils/activity/.gitignore @@ -1,2 +1,3 @@ /pgstat_wait_event.c /wait_event_types.h +/wait_event_funcs_data.c diff --git a/src/backend/utils/activity/Makefile b/src/backend/utils/activity/Makefile index f1117745d4..f57cf3958c 100644 --- a/src/backend/utils/activity/Makefile +++ b/src/backend/utils/activity/Makefile @@ -32,10 +32,14 @@ OBJS = \ pgstat_subscription.o \ pgstat_wal.o \ pgstat_xact.o \ - wait_event.o + wait_event.o \ + wait_event_funcs.o include $(top_srcdir)/src/backend/common.mk +wait_event_funcs.o: wait_event_funcs_data.c +wait_event_funcs_data.c: wait_event_types.h + wait_event.o: pgstat_wait_event.c pgstat_wait_event.c: wait_event_types.h touch $@ @@ -44,4 +48,4 @@ wait_event_types.h: $(top_srcdir)/src/backend/utils/activity/wait_event_names.tx $(PERL) $(srcdir)/generate-wait_event_types.pl --code $< maintainer-clean: clean - rm -f wait_event_types.h pgstat_wait_event.c + rm -f wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..9c4557d903 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -4,6 +4,7 @@ # Generate wait events support files from wait_event_names.txt: # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) +# - wait_event_funcs_data.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -98,8 +99,10 @@ if ($gen_code) # multiple times. my $htmp = "$output_path/wait_event_types.h.tmp$$"; my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$"; + my $wctmp = "$output_path/wait_event_funcs_data.c.tmp$$"; open my $h, '>', $htmp or die "Could not open $htmp: $!"; open my $c, '>', $ctmp or die "Could not open $ctmp: $!"; + open my $wc, '>', $wctmp or die "Could not open $wctmp: $!"; my $header_comment = '/*------------------------------------------------------------------------- @@ -129,12 +132,14 @@ if ($gen_code) printf $c $header_comment, 'pgstat_wait_event.c'; + printf $wc $header_comment, 'wait_event_funcs_data.c'; + + # Generate the pgstat_wait_event.c and wait_event_types.h files # uc() is being used to force the comparison to be case-insensitive. foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) { - - # Don't generate .c and .h files for Extension, LWLock and - # Lock, these are handled independently. + # Don't generate the pgstat_wait_event.c and wait_event_types.h files + # for Extension, LWLock and Lock, these are handled independently. next if ( $waitclass eq 'WaitEventExtension' || $waitclass eq 'WaitEventLWLock' @@ -183,14 +188,55 @@ if ($gen_code) printf $c "}\n\n"; } + # Generate wait_event_funcs_data.c, for the contents of static C + # structure holding all the information about the wait events. + # uc() is being used to force the comparison to be case-insensitive, + # even though it is not strictly mandatory here. + foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) + { + my $last = $waitclass; + $last =~ s/^WaitEvent//; + + foreach my $wev (@{ $hashwe{$waitclass} }) + { + my $new_desc = substr $wev->[2], 1, -2; + # Escape single quotes. + $new_desc =~ s/'/\\'/g; + + # Replace the "quote" markups by real ones. + $new_desc =~ s/<quote>(.*?)<\/quote>/\\"$1\\"/g; + + # Remove SGML markups. + $new_desc =~ s/<.*?>(.*?)<.*?>/$1/g; + + # Tweak contents about links <xref linkend="text"/> on GUCs, + while (my ($capture) = + $new_desc =~ m/<xref linkend="guc-(.*?)"\/>/g) + { + $capture =~ s/-/_/g; + $new_desc =~ s/<xref linkend="guc-.*?"\/>/$capture/g; + } + # Then remove any reference to "see <xref linkend="text"/>". + $new_desc =~ s/; see.*$//; + + # One element to the C structure holding the wait event + # info, as of (type, name, description). + printf $wc "\t{\"%s\", \"%s\", \"%s\"},\n", $last, $wev->[1], + $new_desc; + } + } + printf $h "#endif /* WAIT_EVENT_TYPES_H */\n"; close $h; close $c; + close $wc; rename($htmp, "$output_path/wait_event_types.h") || die "rename: $htmp to $output_path/wait_event_types.h: $!"; rename($ctmp, "$output_path/pgstat_wait_event.c") || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!"; + rename($wctmp, "$output_path/wait_event_funcs_data.c") + || die "rename: $wctmp to $output_path/wait_event_funcs_data.c: $!"; } # Generate the .sgml file. elsif ($gen_docs) @@ -249,7 +295,7 @@ Usage: perl [--output <path>] [--code ] [ --sgml ] input_file Options: --outdir Output directory (default '.') - --code Generate wait_event_types.h and pgstat_wait_event.c. + --code Generate C and header files. --sgml Generate wait_event_types.sgml. generate-wait_event_types.pl generates the SGML documentation and code diff --git a/src/backend/utils/activity/meson.build b/src/backend/utils/activity/meson.build index 9633f3623c..46a27e7548 100644 --- a/src/backend/utils/activity/meson.build +++ b/src/backend/utils/activity/meson.build @@ -23,6 +23,7 @@ backend_sources += files( # seems nicer to not add that as an include path for the whole backend. waitevent_sources = files( 'wait_event.c', + 'wait_event_funcs.c', ) wait_event = static_library('wait_event_names', diff --git a/src/backend/utils/activity/wait_event.c b/src/backend/utils/activity/wait_event.c index 4b9b5c01cb..d31767df80 100644 --- a/src/backend/utils/activity/wait_event.c +++ b/src/backend/utils/activity/wait_event.c @@ -264,6 +264,50 @@ GetWaitEventExtensionIdentifier(uint16 eventId) } +/* + * Returns a list of currently defined custom wait event names for extensions. + * The result is a palloc'd array, with the number of elements returned into + * *nwaitevents. + */ +char ** +GetWaitEventExtensionNames(int *nwaitevents) +{ + char **waiteventnames; + WaitEventExtensionEntryByName *hentry; + HASH_SEQ_STATUS hash_seq; + int index; + int els; + + LWLockAcquire(WaitEventExtensionLock, LW_SHARED); + + /* Now we can safely count the number of entries */ + els = hash_get_num_entries(WaitEventExtensionHashByName); + + /* Allocate enough space for all entries */ + waiteventnames = palloc(els * sizeof(char[NAMEDATALEN])); + + /* Now scan the hash table to copy the data */ + hash_seq_init(&hash_seq, WaitEventExtensionHashByName); + + index = 0; + while ((hentry = (WaitEventExtensionEntryByName *) hash_seq_search(&hash_seq)) != NULL) + { + + waiteventnames[index] = palloc(sizeof(char[NAMEDATALEN])); + strlcpy(waiteventnames[index], hentry->wait_event_name, + sizeof(char[NAMEDATALEN])); + + index++; + } + + LWLockRelease(WaitEventExtensionLock); + + Assert(index == els); + + *nwaitevents = index; + return waiteventnames; +} + /* * Configure wait event reporting to report wait events to *wait_event_info. * *wait_event_info needs to be valid until pgstat_reset_wait_event_storage() diff --git a/src/backend/utils/activity/wait_event_funcs.c b/src/backend/utils/activity/wait_event_funcs.c new file mode 100644 index 0000000000..06992f04a9 --- /dev/null +++ b/src/backend/utils/activity/wait_event_funcs.c @@ -0,0 +1,93 @@ +/*------------------------------------------------------------------------ + * + * wait_event_funcs.c + * Functions for accessing wait event data. + * + * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/utils/activity/wait_event_funcs.c + * + *------------------------------------------------------------------------ + */ +#include "postgres.h" + +#include "funcapi.h" +#include "utils/builtins.h" +#include "utils/wait_event.h" + +/* + * Each wait event has one corresponding entry in this structure, fed to + * the SQL function of this file. + */ +static const struct +{ + const char *type; + const char *name; + const char *description; +} + +waitEventData[] = +{ +#include "wait_event_funcs_data.c" + /* end of list */ + {NULL, NULL, NULL} +}; + + +/* + * pg_get_wait_events + * + * List all the wait events type, name and description. + */ +Datum +pg_get_wait_events(PG_FUNCTION_ARGS) +{ +#define PG_GET_WAIT_EVENTS_COLS 3 + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + char **waiteventnames; + int nbextwaitevents; + + /* Build tuplestore to hold the result rows */ + InitMaterializedSRF(fcinfo, 0); + + /* Iterate over the list of wait events */ + for (int idx = 0; waitEventData[idx].type != NULL; idx++) + { + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + values[0] = CStringGetTextDatum(waitEventData[idx].type); + values[1] = CStringGetTextDatum(waitEventData[idx].name); + values[2] = CStringGetTextDatum(waitEventData[idx].description); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + /* Handling extension custom wait events */ + waiteventnames = GetWaitEventExtensionNames(&nbextwaitevents); + + for (int idx = 0; idx < nbextwaitevents; idx++) + { + StringInfoData buf; + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + + values[0] = CStringGetTextDatum("Extension"); + values[1] = CStringGetTextDatum(waiteventnames[idx]); + + initStringInfo(&buf); + appendStringInfoString(&buf, "Waiting for custom wait event \""); + appendStringInfoString(&buf, waiteventnames[idx]); + appendStringInfoString(&buf, "\" defined by an extension module"); + + values[2] = CStringGetTextDatum(buf.data); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + return (Datum) 0; +} diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..b2dab98a8d 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5417,6 +5417,12 @@ proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}', proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}', prosrc => 'pg_stat_get_activity' }, +{ oid => '8403', descr => 'describe wait events', + proname => 'pg_get_wait_events', procost => '10', prorows => '250', + proretset => 't', provolatile => 's', prorettype => 'record', + proargtypes => '', proallargtypes => '{text,text,text}', + proargmodes => '{o,o,o}', proargnames => '{type,name,description}', + prosrc => 'pg_get_wait_events' }, { oid => '3318', descr => 'statistics: information about progress of backends running maintenance command', proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't', diff --git a/src/include/utils/meson.build b/src/include/utils/meson.build index 6de5d93799..c179478611 100644 --- a/src/include/utils/meson.build +++ b/src/include/utils/meson.build @@ -1,6 +1,6 @@ # Copyright (c) 2022-2023, PostgreSQL Global Development Group -wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c'] +wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c', 'wait_event_funcs_data.c'] wait_event_target = custom_target('wait_event_names', input: files('../../backend/utils/activity/wait_event_names.txt'), output: wait_event_output, @@ -11,7 +11,7 @@ wait_event_target = custom_target('wait_event_names', ], build_by_default: true, install: true, - install_dir: [dir_include_server / 'utils', false], + install_dir: [dir_include_server / 'utils', false, false], ) wait_event_types_h = wait_event_target[0] diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h index 3eebdfad38..009b03a520 100644 --- a/src/include/utils/wait_event.h +++ b/src/include/utils/wait_event.h @@ -63,6 +63,7 @@ extern void WaitEventExtensionShmemInit(void); extern Size WaitEventExtensionShmemSize(void); extern uint32 WaitEventExtensionNew(const char *wait_event_name); +extern char **GetWaitEventExtensionNames(int *nwaitevents); /* ---------- * pgstat_report_wait_start() - diff --git a/src/test/modules/worker_spi/t/001_worker_spi.pl b/src/test/modules/worker_spi/t/001_worker_spi.pl index 26b8a49bec..2965acd789 100644 --- a/src/test/modules/worker_spi/t/001_worker_spi.pl +++ b/src/test/modules/worker_spi/t/001_worker_spi.pl @@ -47,6 +47,12 @@ $result = $node->poll_query_until( is($result, 1, 'dynamic bgworker has reported "worker_spi_main" as wait event'); +# Check the wait event used by the dynamic bgworker appears in pg_wait_events +$result = $node->safe_psql('postgres', + q[SELECT count(*) > 0 from pg_wait_events where type = 'Extension' and name = 'worker_spi_main';] +); +is($result, 't', '"worker_spi_main" is reported in pg_wait_events'); + note "testing bgworkers loaded with shared_preload_libraries"; # Create the database first so as the workers can connect to it when diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e07afcd4aa..55447d8090 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2631,6 +2631,10 @@ pg_views| SELECT n.nspname AS schemaname, FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char"); +pg_wait_events| SELECT type, + name, + description + FROM pg_get_wait_events() we(type, name, description); SELECT tablename, rulename, definition FROM pg_rules WHERE schemaname = 'pg_catalog' ORDER BY tablename, rulename; diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 001c6e7eb9..aae5d51e1c 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -134,6 +134,22 @@ select name, setting from pg_settings where name like 'enable%'; enable_tidscan | on (21 rows) +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_events + group by type order by type COLLATE "C"; + type | ok +-----------+---- + Activity | t + BufferPin | t + Client | t + Extension | t + IO | t + IPC | t + LWLock | t + Lock | t + Timeout | t +(9 rows) + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 351e469c77..6b4e24601d 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -55,6 +55,10 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- a regression test run. select name, setting from pg_settings where name like 'enable%'; +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_events + group by type order by type COLLATE "C"; + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index c98a1e9f9a..a50f730260 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -588,7 +588,8 @@ sub GenerateFiles 'src/include/utils/wait_event_types.h', 'src/backend/utils/activity/wait_event_names.txt')) { - print "Generating pgstat_wait_event.c and wait_event_types.h...\n"; + print + "Generating pgstat_wait_event.c, wait_event_types.h and wait_event_funcs_data.c...\n"; my $activ = 'src/backend/utils/activity'; system( "perl $activ/generate-wait_event_types.pl --outdir $activ --code $activ/wait_event_names.txt" diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index 7cb23ea894..ac8da581e4 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -55,6 +55,7 @@ if exist src\include\catalog\header-stamp del /q src\include\catalog\header-stam if exist doc\src\sgml\version.sgml del /q doc\src\sgml\version.sgml if %DIST%==1 if exist src\backend\utils\activity\pgstat_wait_event.c del /q src\backend\utils\activity\pgstat_wait_event.c +if %DIST%==1 if exist src\backend\utils\activity\wait_event_funcs_data.c del /q src\backend\utils\activity\wait_event_funcs_data.c if %DIST%==1 if exist src\backend\utils\activity\wait_event_types.h del /q src\backend\utils\activity\wait_event_types.h if %DIST%==1 if exist src\backend\utils\fmgroids.h del /q src\backend\utils\fmgroids.h if %DIST%==1 if exist src\backend\utils\fmgrprotos.h del /q src\backend\utils\fmgrprotos.h -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-18 08:56                                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-18 10:31                                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 09:35                                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-19 10:00                                       ` Michael Paquier <michael@paquier.xyz>
  2023-08-19 16:30                                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Michael Paquier @ 2023-08-19 10:00 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Sat, Aug 19, 2023 at 11:35:01AM +0200, Drouvot, Bertrand wrote:
> Hi,
> 
> On 8/18/23 12:31 PM, Michael Paquier wrote:
> Thanks! Please find attached v9 fixing this typo.

I was looking at v8, and this looks pretty good to me.  I have
spotted a few minor things.

+  proretset => 't', provolatile => 's', prorettype => 'record',
This function should be volatile.  For example, a backend could add a
new wait event while a different backend queries twice this view in
the same transaction, resulting in a different set of rows sent back.

By the way, shouldn't the results of GetWaitEventExtensionNames() in
the SQL function be freed?  I mean that:
    for (int idx = 0; idx < nbextwaitevents; idx++)
        pfree(waiteventnames[idx]);
    pfree(waiteventnames);

+   /* Handling extension custom wait events */
Nit warning: s/Handling/Handle/, or "Handling of".
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-18 08:56                                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-18 10:31                                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 09:35                                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-19 10:00                                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-19 16:30                                         ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-20 08:07                                           ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-19 16:30 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/19/23 12:00 PM, Michael Paquier wrote:
> On Sat, Aug 19, 2023 at 11:35:01AM +0200, Drouvot, Bertrand wrote:
>> Hi,
>>
>> On 8/18/23 12:31 PM, Michael Paquier wrote:
>> Thanks! Please find attached v9 fixing this typo.
> 
> I was looking at v8, and this looks pretty good to me. 

Great!

> I have
> spotted a few minor things.
> 
> +  proretset => 't', provolatile => 's', prorettype => 'record',
> This function should be volatile.  

Oh right, I copied/pasted this and should have paid more attention to
that part. Fixed in v10 attached.

> By the way, shouldn't the results of GetWaitEventExtensionNames() in
> the SQL function be freed?  I mean that:
>      for (int idx = 0; idx < nbextwaitevents; idx++)
>          pfree(waiteventnames[idx]);
>      pfree(waiteventnames);
> 

I don't think it's needed. The reason is that they are palloc in a
short-lived memory context while executing pg_get_wait_events().

> +   /* Handling extension custom wait events */
> Nit warning: s/Handling/Handle/, or "Handling of".

Thanks, fixed in v10.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v10-0001-Add-catalog-pg_wait_events.patch (text/plain, 23.1K) download From 40eae29b80920d3859ec78bd2485036ca4ab4f6d Mon Sep 17 00:00:00 2001 From: bdrouvotAWS <bdrouvot@amazon.com> Date: Sat, 5 Aug 2023 12:39:42 +0000 Subject: [PATCH v10] Add catalog pg_wait_events Adding a new system view, namely pg_wait_events, that describes the wait events. --- doc/src/sgml/monitoring.sgml | 13 ++- doc/src/sgml/system-views.sgml | 64 +++++++++++++ src/backend/Makefile | 3 +- src/backend/catalog/system_views.sql | 3 + src/backend/utils/activity/.gitignore | 1 + src/backend/utils/activity/Makefile | 8 +- .../activity/generate-wait_event_types.pl | 54 ++++++++++- src/backend/utils/activity/meson.build | 1 + src/backend/utils/activity/wait_event.c | 44 +++++++++ src/backend/utils/activity/wait_event_funcs.c | 93 +++++++++++++++++++ src/include/catalog/pg_proc.dat | 6 ++ src/include/utils/meson.build | 4 +- src/include/utils/wait_event.h | 1 + .../modules/worker_spi/t/001_worker_spi.pl | 6 ++ src/test/regress/expected/rules.out | 4 + src/test/regress/expected/sysviews.out | 16 ++++ src/test/regress/sql/sysviews.sql | 4 + src/tools/msvc/Solution.pm | 3 +- src/tools/msvc/clean.bat | 1 + 19 files changed, 318 insertions(+), 11 deletions(-) 21.1% doc/src/sgml/ 59.5% src/backend/utils/activity/ 3.5% src/include/catalog/ 4.5% src/test/regress/expected/ 4.5% src/test/ 3.0% src/tools/msvc/ 3.6% src/ diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 70511a2388..6c53c1ed91 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -1103,7 +1103,7 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser &wait_event_types; <para> - Here is an example of how wait events can be viewed: + Here are examples of how wait events can be viewed: <programlisting> SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event is NOT NULL; @@ -1112,6 +1112,17 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i 2540 | Lock | relation 6644 | LWLock | ProcArray (2 rows) +</programlisting> + +<programlisting> +SELECT a.pid, a.wait_event, w.description +FROM pg_stat_activity a JOIN pg_wait_events w +ON (a.wait_event_type = w.type AND a.wait_event = w.name) +WHERE wait_event is NOT NULL and a.state = 'active'; +-[ RECORD 1 ]------------------------------------------------------------------ +pid | 686674 +wait_event | WALInitSync +description | Waiting for a newly initialized WAL file to reach durable storage </programlisting> </para> diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 57b228076e..2b35c2f91b 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -221,6 +221,11 @@ <entry>views</entry> </row> + <row> + <entry><link linkend="view-pg-wait-events"><structname>pg_wait_events</structname></link></entry> + <entry>wait events</entry> + </row> + </tbody> </tgroup> </table> @@ -4825,4 +4830,63 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx </table> </sect1> + + <sect1 id="view-pg-wait-events"> + <title><structname>pg_wait_events</structname></title> + + <indexterm zone="view-pg-wait-events"> + <primary>pg_wait_events</primary> + </indexterm> + + <para> + The view <structname>pg_wait_events</structname> provides description about the + wait events. + </para> + + <table> + <title><structname>pg_wait_events</structname> Columns</title> + <tgroup cols="1"> + <thead> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + Column Type + </para> + <para> + Description + </para></entry> + </row> + </thead> + + <tbody> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>type</structfield> <type>text</type> + </para> + <para> + Wait event type + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>name</structfield> <type>text</type> + </para> + <para> + Wait event name + </para></entry> + </row> + + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>description</structfield> <type>text</type> + </para> + <para> + Wait event description + </para></entry> + </row> + </tbody> + </tgroup> + </table> + </sect1> + </chapter> diff --git a/src/backend/Makefile b/src/backend/Makefile index 1c929383c4..3e275ac759 100644 --- a/src/backend/Makefile +++ b/src/backend/Makefile @@ -134,7 +134,7 @@ storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lw $(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c utils/activity/wait_event_types.h: utils/activity/generate-wait_event_types.pl utils/activity/wait_event_names.txt - $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c + $(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c # run this unconditionally to avoid needing to know its dependencies here: submake-catalog-headers: @@ -311,6 +311,7 @@ maintainer-clean: distclean storage/lmgr/lwlocknames.c \ storage/lmgr/lwlocknames.h \ utils/activity/pgstat_wait_event.c \ + utils/activity/wait_event_funcs_data.c \ utils/activity/wait_event_types.h \ utils/adt/jsonpath_gram.c \ utils/adt/jsonpath_gram.h \ diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index af65af6bdd..362b1ea8d5 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1342,3 +1342,6 @@ CREATE VIEW pg_stat_subscription_stats AS ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; + +CREATE VIEW pg_wait_events AS + SELECT * FROM pg_get_wait_events() AS we; diff --git a/src/backend/utils/activity/.gitignore b/src/backend/utils/activity/.gitignore index d77079285b..bd0c0c7772 100644 --- a/src/backend/utils/activity/.gitignore +++ b/src/backend/utils/activity/.gitignore @@ -1,2 +1,3 @@ /pgstat_wait_event.c /wait_event_types.h +/wait_event_funcs_data.c diff --git a/src/backend/utils/activity/Makefile b/src/backend/utils/activity/Makefile index f1117745d4..f57cf3958c 100644 --- a/src/backend/utils/activity/Makefile +++ b/src/backend/utils/activity/Makefile @@ -32,10 +32,14 @@ OBJS = \ pgstat_subscription.o \ pgstat_wal.o \ pgstat_xact.o \ - wait_event.o + wait_event.o \ + wait_event_funcs.o include $(top_srcdir)/src/backend/common.mk +wait_event_funcs.o: wait_event_funcs_data.c +wait_event_funcs_data.c: wait_event_types.h + wait_event.o: pgstat_wait_event.c pgstat_wait_event.c: wait_event_types.h touch $@ @@ -44,4 +48,4 @@ wait_event_types.h: $(top_srcdir)/src/backend/utils/activity/wait_event_names.tx $(PERL) $(srcdir)/generate-wait_event_types.pl --code $< maintainer-clean: clean - rm -f wait_event_types.h pgstat_wait_event.c + rm -f wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c diff --git a/src/backend/utils/activity/generate-wait_event_types.pl b/src/backend/utils/activity/generate-wait_event_types.pl index 56335e8730..9c4557d903 100644 --- a/src/backend/utils/activity/generate-wait_event_types.pl +++ b/src/backend/utils/activity/generate-wait_event_types.pl @@ -4,6 +4,7 @@ # Generate wait events support files from wait_event_names.txt: # - wait_event_types.h (if --code is passed) # - pgstat_wait_event.c (if --code is passed) +# - wait_event_funcs_data.c (if --code is passed) # - wait_event_types.sgml (if --docs is passed) # # Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -98,8 +99,10 @@ if ($gen_code) # multiple times. my $htmp = "$output_path/wait_event_types.h.tmp$$"; my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$"; + my $wctmp = "$output_path/wait_event_funcs_data.c.tmp$$"; open my $h, '>', $htmp or die "Could not open $htmp: $!"; open my $c, '>', $ctmp or die "Could not open $ctmp: $!"; + open my $wc, '>', $wctmp or die "Could not open $wctmp: $!"; my $header_comment = '/*------------------------------------------------------------------------- @@ -129,12 +132,14 @@ if ($gen_code) printf $c $header_comment, 'pgstat_wait_event.c'; + printf $wc $header_comment, 'wait_event_funcs_data.c'; + + # Generate the pgstat_wait_event.c and wait_event_types.h files # uc() is being used to force the comparison to be case-insensitive. foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) { - - # Don't generate .c and .h files for Extension, LWLock and - # Lock, these are handled independently. + # Don't generate the pgstat_wait_event.c and wait_event_types.h files + # for Extension, LWLock and Lock, these are handled independently. next if ( $waitclass eq 'WaitEventExtension' || $waitclass eq 'WaitEventLWLock' @@ -183,14 +188,55 @@ if ($gen_code) printf $c "}\n\n"; } + # Generate wait_event_funcs_data.c, for the contents of static C + # structure holding all the information about the wait events. + # uc() is being used to force the comparison to be case-insensitive, + # even though it is not strictly mandatory here. + foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe) + { + my $last = $waitclass; + $last =~ s/^WaitEvent//; + + foreach my $wev (@{ $hashwe{$waitclass} }) + { + my $new_desc = substr $wev->[2], 1, -2; + # Escape single quotes. + $new_desc =~ s/'/\\'/g; + + # Replace the "quote" markups by real ones. + $new_desc =~ s/<quote>(.*?)<\/quote>/\\"$1\\"/g; + + # Remove SGML markups. + $new_desc =~ s/<.*?>(.*?)<.*?>/$1/g; + + # Tweak contents about links <xref linkend="text"/> on GUCs, + while (my ($capture) = + $new_desc =~ m/<xref linkend="guc-(.*?)"\/>/g) + { + $capture =~ s/-/_/g; + $new_desc =~ s/<xref linkend="guc-.*?"\/>/$capture/g; + } + # Then remove any reference to "see <xref linkend="text"/>". + $new_desc =~ s/; see.*$//; + + # One element to the C structure holding the wait event + # info, as of (type, name, description). + printf $wc "\t{\"%s\", \"%s\", \"%s\"},\n", $last, $wev->[1], + $new_desc; + } + } + printf $h "#endif /* WAIT_EVENT_TYPES_H */\n"; close $h; close $c; + close $wc; rename($htmp, "$output_path/wait_event_types.h") || die "rename: $htmp to $output_path/wait_event_types.h: $!"; rename($ctmp, "$output_path/pgstat_wait_event.c") || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!"; + rename($wctmp, "$output_path/wait_event_funcs_data.c") + || die "rename: $wctmp to $output_path/wait_event_funcs_data.c: $!"; } # Generate the .sgml file. elsif ($gen_docs) @@ -249,7 +295,7 @@ Usage: perl [--output <path>] [--code ] [ --sgml ] input_file Options: --outdir Output directory (default '.') - --code Generate wait_event_types.h and pgstat_wait_event.c. + --code Generate C and header files. --sgml Generate wait_event_types.sgml. generate-wait_event_types.pl generates the SGML documentation and code diff --git a/src/backend/utils/activity/meson.build b/src/backend/utils/activity/meson.build index 9633f3623c..46a27e7548 100644 --- a/src/backend/utils/activity/meson.build +++ b/src/backend/utils/activity/meson.build @@ -23,6 +23,7 @@ backend_sources += files( # seems nicer to not add that as an include path for the whole backend. waitevent_sources = files( 'wait_event.c', + 'wait_event_funcs.c', ) wait_event = static_library('wait_event_names', diff --git a/src/backend/utils/activity/wait_event.c b/src/backend/utils/activity/wait_event.c index 4b9b5c01cb..d31767df80 100644 --- a/src/backend/utils/activity/wait_event.c +++ b/src/backend/utils/activity/wait_event.c @@ -264,6 +264,50 @@ GetWaitEventExtensionIdentifier(uint16 eventId) } +/* + * Returns a list of currently defined custom wait event names for extensions. + * The result is a palloc'd array, with the number of elements returned into + * *nwaitevents. + */ +char ** +GetWaitEventExtensionNames(int *nwaitevents) +{ + char **waiteventnames; + WaitEventExtensionEntryByName *hentry; + HASH_SEQ_STATUS hash_seq; + int index; + int els; + + LWLockAcquire(WaitEventExtensionLock, LW_SHARED); + + /* Now we can safely count the number of entries */ + els = hash_get_num_entries(WaitEventExtensionHashByName); + + /* Allocate enough space for all entries */ + waiteventnames = palloc(els * sizeof(char[NAMEDATALEN])); + + /* Now scan the hash table to copy the data */ + hash_seq_init(&hash_seq, WaitEventExtensionHashByName); + + index = 0; + while ((hentry = (WaitEventExtensionEntryByName *) hash_seq_search(&hash_seq)) != NULL) + { + + waiteventnames[index] = palloc(sizeof(char[NAMEDATALEN])); + strlcpy(waiteventnames[index], hentry->wait_event_name, + sizeof(char[NAMEDATALEN])); + + index++; + } + + LWLockRelease(WaitEventExtensionLock); + + Assert(index == els); + + *nwaitevents = index; + return waiteventnames; +} + /* * Configure wait event reporting to report wait events to *wait_event_info. * *wait_event_info needs to be valid until pgstat_reset_wait_event_storage() diff --git a/src/backend/utils/activity/wait_event_funcs.c b/src/backend/utils/activity/wait_event_funcs.c new file mode 100644 index 0000000000..81ce8166b6 --- /dev/null +++ b/src/backend/utils/activity/wait_event_funcs.c @@ -0,0 +1,93 @@ +/*------------------------------------------------------------------------ + * + * wait_event_funcs.c + * Functions for accessing wait event data. + * + * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/utils/activity/wait_event_funcs.c + * + *------------------------------------------------------------------------ + */ +#include "postgres.h" + +#include "funcapi.h" +#include "utils/builtins.h" +#include "utils/wait_event.h" + +/* + * Each wait event has one corresponding entry in this structure, fed to + * the SQL function of this file. + */ +static const struct +{ + const char *type; + const char *name; + const char *description; +} + +waitEventData[] = +{ +#include "wait_event_funcs_data.c" + /* end of list */ + {NULL, NULL, NULL} +}; + + +/* + * pg_get_wait_events + * + * List all the wait events type, name and description. + */ +Datum +pg_get_wait_events(PG_FUNCTION_ARGS) +{ +#define PG_GET_WAIT_EVENTS_COLS 3 + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + char **waiteventnames; + int nbextwaitevents; + + /* Build tuplestore to hold the result rows */ + InitMaterializedSRF(fcinfo, 0); + + /* Iterate over the list of wait events */ + for (int idx = 0; waitEventData[idx].type != NULL; idx++) + { + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + values[0] = CStringGetTextDatum(waitEventData[idx].type); + values[1] = CStringGetTextDatum(waitEventData[idx].name); + values[2] = CStringGetTextDatum(waitEventData[idx].description); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + /* Handle extension custom wait events */ + waiteventnames = GetWaitEventExtensionNames(&nbextwaitevents); + + for (int idx = 0; idx < nbextwaitevents; idx++) + { + StringInfoData buf; + Datum values[PG_GET_WAIT_EVENTS_COLS] = {0}; + bool nulls[PG_GET_WAIT_EVENTS_COLS] = {0}; + + + values[0] = CStringGetTextDatum("Extension"); + values[1] = CStringGetTextDatum(waiteventnames[idx]); + + initStringInfo(&buf); + appendStringInfoString(&buf, "Waiting for custom wait event \""); + appendStringInfoString(&buf, waiteventnames[idx]); + appendStringInfoString(&buf, "\" defined by an extension module"); + + values[2] = CStringGetTextDatum(buf.data); + + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + return (Datum) 0; +} diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..12fac15ceb 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5417,6 +5417,12 @@ proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}', proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}', prosrc => 'pg_stat_get_activity' }, +{ oid => '8403', descr => 'describe wait events', + proname => 'pg_get_wait_events', procost => '10', prorows => '250', + proretset => 't', provolatile => 'v', prorettype => 'record', + proargtypes => '', proallargtypes => '{text,text,text}', + proargmodes => '{o,o,o}', proargnames => '{type,name,description}', + prosrc => 'pg_get_wait_events' }, { oid => '3318', descr => 'statistics: information about progress of backends running maintenance command', proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't', diff --git a/src/include/utils/meson.build b/src/include/utils/meson.build index 6de5d93799..c179478611 100644 --- a/src/include/utils/meson.build +++ b/src/include/utils/meson.build @@ -1,6 +1,6 @@ # Copyright (c) 2022-2023, PostgreSQL Global Development Group -wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c'] +wait_event_output = ['wait_event_types.h', 'pgstat_wait_event.c', 'wait_event_funcs_data.c'] wait_event_target = custom_target('wait_event_names', input: files('../../backend/utils/activity/wait_event_names.txt'), output: wait_event_output, @@ -11,7 +11,7 @@ wait_event_target = custom_target('wait_event_names', ], build_by_default: true, install: true, - install_dir: [dir_include_server / 'utils', false], + install_dir: [dir_include_server / 'utils', false, false], ) wait_event_types_h = wait_event_target[0] diff --git a/src/include/utils/wait_event.h b/src/include/utils/wait_event.h index 3eebdfad38..009b03a520 100644 --- a/src/include/utils/wait_event.h +++ b/src/include/utils/wait_event.h @@ -63,6 +63,7 @@ extern void WaitEventExtensionShmemInit(void); extern Size WaitEventExtensionShmemSize(void); extern uint32 WaitEventExtensionNew(const char *wait_event_name); +extern char **GetWaitEventExtensionNames(int *nwaitevents); /* ---------- * pgstat_report_wait_start() - diff --git a/src/test/modules/worker_spi/t/001_worker_spi.pl b/src/test/modules/worker_spi/t/001_worker_spi.pl index 26b8a49bec..2965acd789 100644 --- a/src/test/modules/worker_spi/t/001_worker_spi.pl +++ b/src/test/modules/worker_spi/t/001_worker_spi.pl @@ -47,6 +47,12 @@ $result = $node->poll_query_until( is($result, 1, 'dynamic bgworker has reported "worker_spi_main" as wait event'); +# Check the wait event used by the dynamic bgworker appears in pg_wait_events +$result = $node->safe_psql('postgres', + q[SELECT count(*) > 0 from pg_wait_events where type = 'Extension' and name = 'worker_spi_main';] +); +is($result, 't', '"worker_spi_main" is reported in pg_wait_events'); + note "testing bgworkers loaded with shared_preload_libraries"; # Create the database first so as the workers can connect to it when diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e07afcd4aa..55447d8090 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2631,6 +2631,10 @@ pg_views| SELECT n.nspname AS schemaname, FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char"); +pg_wait_events| SELECT type, + name, + description + FROM pg_get_wait_events() we(type, name, description); SELECT tablename, rulename, definition FROM pg_rules WHERE schemaname = 'pg_catalog' ORDER BY tablename, rulename; diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 001c6e7eb9..aae5d51e1c 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -134,6 +134,22 @@ select name, setting from pg_settings where name like 'enable%'; enable_tidscan | on (21 rows) +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_events + group by type order by type COLLATE "C"; + type | ok +-----------+---- + Activity | t + BufferPin | t + Client | t + Extension | t + IO | t + IPC | t + LWLock | t + Lock | t + Timeout | t +(9 rows) + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 351e469c77..6b4e24601d 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -55,6 +55,10 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- a regression test run. select name, setting from pg_settings where name like 'enable%'; +-- There are always wait event descriptions for various types. +select type, count(*) > 0 as ok FROM pg_wait_events + group by type order by type COLLATE "C"; + -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail -- without the outputs changing anytime IANA updates the underlying data, diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index c98a1e9f9a..a50f730260 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -588,7 +588,8 @@ sub GenerateFiles 'src/include/utils/wait_event_types.h', 'src/backend/utils/activity/wait_event_names.txt')) { - print "Generating pgstat_wait_event.c and wait_event_types.h...\n"; + print + "Generating pgstat_wait_event.c, wait_event_types.h and wait_event_funcs_data.c...\n"; my $activ = 'src/backend/utils/activity'; system( "perl $activ/generate-wait_event_types.pl --outdir $activ --code $activ/wait_event_names.txt" diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index 7cb23ea894..ac8da581e4 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -55,6 +55,7 @@ if exist src\include\catalog\header-stamp del /q src\include\catalog\header-stam if exist doc\src\sgml\version.sgml del /q doc\src\sgml\version.sgml if %DIST%==1 if exist src\backend\utils\activity\pgstat_wait_event.c del /q src\backend\utils\activity\pgstat_wait_event.c +if %DIST%==1 if exist src\backend\utils\activity\wait_event_funcs_data.c del /q src\backend\utils\activity\wait_event_funcs_data.c if %DIST%==1 if exist src\backend\utils\activity\wait_event_types.h del /q src\backend\utils\activity\wait_event_types.h if %DIST%==1 if exist src\backend\utils\fmgroids.h del /q src\backend\utils\fmgroids.h if %DIST%==1 if exist src\backend\utils\fmgrprotos.h del /q src\backend\utils\fmgrprotos.h -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-18 08:56                                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-18 10:31                                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 09:35                                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-19 10:00                                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 16:30                                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-08-20 08:07                                           ` Michael Paquier <michael@paquier.xyz>
  2023-08-21 07:33                                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 34+ messages in thread

From: Michael Paquier @ 2023-08-20 08:07 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; +Cc: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Sat, Aug 19, 2023 at 06:30:12PM +0200, Drouvot, Bertrand wrote:
> Thanks, fixed in v10.

Okay.  I have done an extra review of it, simplifying a few things in
the function, the comments and the formatting, and applied the patch.
Thanks!

I have somewhat managed to miss the catalog version in the initial
commit, but fixed that quickly.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-18 08:56                                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-18 10:31                                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 09:35                                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-19 10:00                                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 16:30                                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-20 08:07                                           ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
@ 2023-08-21 07:33                                             ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-10-23 10:17                                               ` Re: WIP: new system catalog pg_wait_event Pavel Luzanov <p.luzanov@postgrespro.ru>
  0 siblings, 1 reply; 34+ messages in thread

From: Drouvot, Bertrand @ 2023-08-21 07:33 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Hi,

On 8/20/23 10:07 AM, Michael Paquier wrote:
> On Sat, Aug 19, 2023 at 06:30:12PM +0200, Drouvot, Bertrand wrote:
>> Thanks, fixed in v10.
> 
> Okay.  I have done an extra review of it, simplifying a few things in
> the function, the comments and the formatting, and applied the patch.

Thanks!

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-18 08:56                                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-18 10:31                                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 09:35                                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-19 10:00                                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 16:30                                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-20 08:07                                           ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-21 07:33                                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
@ 2023-10-23 10:17                                               ` Pavel Luzanov <p.luzanov@postgrespro.ru>
  2023-10-23 23:41                                                 ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 34+ messages in thread

From: Pavel Luzanov @ 2023-10-23 10:17 UTC (permalink / raw)
  To: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; Michael Paquier <michael@paquier.xyz>; +Cc: Masahiro Ikeda <ikedamsh@oss.nttdata.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

Please, consider this small fix for the query example in the docs[1]:

SELECT a.pid, a.wait_event, w.description
   FROM pg_stat_activity a JOIN
        pg_wait_events w ON (a.wait_event_type = w.type AND
                             a.wait_event = w.name)
   WHERE wait_event is NOT NULL and a.state = 'active';

I propose to add a table alias for the wait_event column in the WHERE clause for consistency.

1.https://www.postgresql.org/docs/devel/monitoring-stats.html#MONITORING-PG-STAT-ACTIVITY-VIEW

-- 
Pavel Luzanov
Postgres Professional:https://postgrespro.com

Attachments:

0001-Fix-pg_wait_events-usage-example.patch (text/x-patch, 1015B) download From 64e9aa8ddde392b97bff66d53a5d09e0a820380c Mon Sep 17 00:00:00 2001 From: Pavel Luzanov <p.luzanov@postgrespro.ru> Date: Mon, 23 Oct 2023 13:00:16 +0300 Subject: [PATCH] Fix pg_wait_events usage example This makes the use of column aliases consistent throughout the query. --- doc/src/sgml/monitoring.sgml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 1149093a8a..3f49ff79f3 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -1119,7 +1119,7 @@ SELECT a.pid, a.wait_event, w.description FROM pg_stat_activity a JOIN pg_wait_events w ON (a.wait_event_type = w.type AND a.wait_event = w.name) - WHERE wait_event is NOT NULL and a.state = 'active'; + WHERE a.wait_event is NOT NULL and a.state = 'active'; -[ RECORD 1 ]------------------------------------------------------&zwsp;------------ pid | 686674 wait_event | WALInitSync -- 2.34.1
^ permalink raw reply [nested|flat] 34+ messages in thread

* Re: WIP: new system catalog pg_wait_event
  2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-04 15:08 ` Re: WIP: new system catalog pg_wait_event Tom Lane <tgl@sss.pgh.pa.us>
  2023-08-07 08:23   ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-07 15:11     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 02:53       ` Re: WIP: new system catalog pg_wait_event Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2023-08-08 03:05         ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-08 05:37           ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-08 08:16             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-09 07:56               ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-10 18:09                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-14 04:37                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 05:04                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-16 06:22                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-16 11:43                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 01:53                           ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-17 05:53                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-17 07:37                               ` Re: WIP: new system catalog pg_wait_event Masahiro Ikeda <ikedamsh@oss.nttdata.com>
  2023-08-18 08:56                                 ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-18 10:31                                   ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 09:35                                     ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-19 10:00                                       ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-19 16:30                                         ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-08-20 08:07                                           ` Re: WIP: new system catalog pg_wait_event Michael Paquier <michael@paquier.xyz>
  2023-08-21 07:33                                             ` Re: WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
  2023-10-23 10:17                                               ` Re: WIP: new system catalog pg_wait_event Pavel Luzanov <p.luzanov@postgrespro.ru>
@ 2023-10-23 23:41                                                 ` Michael Paquier <michael@paquier.xyz>
  0 siblings, 0 replies; 34+ messages in thread

From: Michael Paquier @ 2023-10-23 23:41 UTC (permalink / raw)
  To: Pavel Luzanov <p.luzanov@postgrespro.ru>; +Cc: Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>; Masahiro Ikeda <ikedamsh@oss.nttdata.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; tgl@sss.pgh.pa.us; pgsql-hackers@lists.postgresql.org

On Mon, Oct 23, 2023 at 01:17:42PM +0300, Pavel Luzanov wrote:
> I propose to add a table alias for the wait_event column in the
> WHERE clause for consistency.

Okay by me that it looks like an improvement to understand where this
attribute is from, so applied on HEAD.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 34+ messages in thread


end of thread, other threads:[~2023-10-23 23:41 UTC | newest]

Thread overview: 34+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-08-04 14:22 WIP: new system catalog pg_wait_event Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-04 15:08 ` Tom Lane <tgl@sss.pgh.pa.us>
2023-08-07 08:23   ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-07 15:11     ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-08 02:53       ` Kyotaro Horiguchi <horikyota.ntt@gmail.com>
2023-08-08 03:05         ` Michael Paquier <michael@paquier.xyz>
2023-08-08 05:37           ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-08 08:16             ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-09 07:56               ` Michael Paquier <michael@paquier.xyz>
2023-08-10 18:09                 ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-14 04:37                   ` Michael Paquier <michael@paquier.xyz>
2023-08-16 05:04                     ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-16 06:22                       ` Michael Paquier <michael@paquier.xyz>
2023-08-16 11:43                         ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-16 12:08                           ` Michael Paquier <michael@paquier.xyz>
2023-08-17 05:51                             ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-17 01:53                           ` Masahiro Ikeda <ikedamsh@oss.nttdata.com>
2023-08-17 01:57                             ` Michael Paquier <michael@paquier.xyz>
2023-08-17 02:35                               ` Masahiro Ikeda <ikedamsh@oss.nttdata.com>
2023-08-17 05:57                               ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-17 05:53                             ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-17 07:37                               ` Masahiro Ikeda <ikedamsh@oss.nttdata.com>
2023-08-17 22:37                                 ` Michael Paquier <michael@paquier.xyz>
2023-08-18 08:57                                   ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-18 10:28                                     ` Michael Paquier <michael@paquier.xyz>
2023-08-18 08:56                                 ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-18 10:31                                   ` Michael Paquier <michael@paquier.xyz>
2023-08-19 09:35                                     ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-19 10:00                                       ` Michael Paquier <michael@paquier.xyz>
2023-08-19 16:30                                         ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-08-20 08:07                                           ` Michael Paquier <michael@paquier.xyz>
2023-08-21 07:33                                             ` Drouvot, Bertrand <bertranddrouvot.pg@gmail.com>
2023-10-23 10:17                                               ` Pavel Luzanov <p.luzanov@postgrespro.ru>
2023-10-23 23:41                                                 ` Michael Paquier <michael@paquier.xyz>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox