From 775a43077051a9c9e2900ff4ce34602caebd547c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Klonfar?= Date: Fri, 21 Apr 2023 23:09:53 +0200 Subject: [PATCH 1/5] Slightly changed output: * no explicit error code when NOT_KNOWN_ERROR * even for NOT_KNOWN_ERROR sqlstate is filled as this happens for custom sql states raised from plpgsql code --- logerrors.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/logerrors.c b/logerrors.c index 69e233f..5aaed76 100644 --- a/logerrors.c +++ b/logerrors.c @@ -556,7 +556,7 @@ put_values_to_tuple( if (found) long_interval_values[2] = CStringGetTextDatum(err_name->name); else { - sprintf(err_name_str, "NOT_KNOWN_ERROR: %d", key.error_code); + sprintf(err_name_str, "NOT_KNOWN_ERROR"); long_interval_values[2] = CStringGetTextDatum(err_name_str); } /* Count */ @@ -575,12 +575,7 @@ put_values_to_tuple( long_interval_values[5] = CStringGetTextDatum(db_name); /* SQLState */ - if (found) { - long_interval_values[6] = CStringGetTextDatum(unpack_sql_state(err_code.num)); - } - else { - long_interval_nulls[6] = true; - } + long_interval_values[6] = CStringGetTextDatum(unpack_sql_state(err_code.num)); if (elem->counter > 0) { tuplestore_putvalues(tupstore, tupdesc, long_interval_values, long_interval_nulls); From 0fb50e0af24c4302c0d3582165d0de4b1bc7c389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Klonfar?= Date: Fri, 21 Apr 2023 23:13:51 +0200 Subject: [PATCH 2/5] PGXS regression tests enabled --- .gitignore | 1 + Makefile | 4 ++- expected/logerrors.out | 60 ++++++++++++++++++++++++++++++++++++++++++ logerrors.conf | 1 + sql/logerrors.sql | 17 ++++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 expected/logerrors.out create mode 100644 logerrors.conf create mode 100644 sql/logerrors.sql diff --git a/.gitignore b/.gitignore index 485dee6..1646cf8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +results/* diff --git a/Makefile b/Makefile index 7b90062..baa2bd9 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,6 @@ DATA = logerrors--1.0.sql logerrors--1.0--1.1.sql logerrors--1.1--2.0.sql logerr OBJS = logerrors.o PG_CONFIG = pg_config PGXS := $(shell $(PG_CONFIG) --pgxs) -include $(PGXS) +REGRESS = logerrors +REGRESS_OPTS = --create-role=postgres --temp-config logerrors.conf --load-extension=logerrors --temp-instance=./temp-check +include $(PGXS) diff --git a/expected/logerrors.out b/expected/logerrors.out new file mode 100644 index 0000000..ad7e9da --- /dev/null +++ b/expected/logerrors.out @@ -0,0 +1,60 @@ +SET ROLE postgres; +SELECT pg_log_errors_reset(); + pg_log_errors_reset +--------------------- + +(1 row) + +SELECT blah(); +ERROR: function blah() does not exist +LINE 1: SELECT blah(); + ^ +HINT: No function matches the given name and argument types. You might need to add explicit type casts. +SELECT pg_sleep(10); + pg_sleep +---------- + +(1 row) + +SELECT * FROM pg_log_errors_stats(); + time_interval | type | message | count | username | database | sqlstate +---------------+---------+----------------------------+-------+----------+--------------------+---------- + | WARNING | TOTAL | 0 | | | + | ERROR | TOTAL | 1 | | | + | FATAL | TOTAL | 0 | | | + 600 | ERROR | ERRCODE_UNDEFINED_FUNCTION | 1 | postgres | contrib_regression | 42883 +(4 rows) + +DO LANGUAGE plpgsql $$ +BEGIN + RAISE SQLSTATE 'XXXXX'; +END; +$$; +ERROR: XXXXX +CONTEXT: PL/pgSQL function inline_code_block line 3 at RAISE +DO LANGUAGE plpgsql $$ +BEGIN + RAISE SQLSTATE 'XXXXY'; +END; +$$; +ERROR: XXXXY +CONTEXT: PL/pgSQL function inline_code_block line 3 at RAISE +SELECT pg_sleep(5); + pg_sleep +---------- + +(1 row) + +SELECT * FROM pg_log_errors_stats(); + time_interval | type | message | count | username | database | sqlstate +---------------+---------+----------------------------+-------+----------+--------------------+---------- + | WARNING | TOTAL | 0 | | | + | ERROR | TOTAL | 3 | | | + | FATAL | TOTAL | 0 | | | + 5 | ERROR | NOT_KNOWN_ERROR | 1 | postgres | contrib_regression | XXXXX + 5 | ERROR | NOT_KNOWN_ERROR | 1 | postgres | contrib_regression | XXXXY + 600 | ERROR | ERRCODE_UNDEFINED_FUNCTION | 1 | postgres | contrib_regression | 42883 + 600 | ERROR | NOT_KNOWN_ERROR | 1 | postgres | contrib_regression | XXXXX + 600 | ERROR | NOT_KNOWN_ERROR | 1 | postgres | contrib_regression | XXXXY +(8 rows) + diff --git a/logerrors.conf b/logerrors.conf new file mode 100644 index 0000000..3899aaf --- /dev/null +++ b/logerrors.conf @@ -0,0 +1 @@ +shared_preload_libraries='logerrors' diff --git a/sql/logerrors.sql b/sql/logerrors.sql new file mode 100644 index 0000000..51a006e --- /dev/null +++ b/sql/logerrors.sql @@ -0,0 +1,17 @@ +SET ROLE postgres; +SELECT pg_log_errors_reset(); +SELECT blah(); +SELECT pg_sleep(10); +SELECT * FROM pg_log_errors_stats(); +DO LANGUAGE plpgsql $$ +BEGIN + RAISE SQLSTATE 'XXXXX'; +END; +$$; +DO LANGUAGE plpgsql $$ +BEGIN + RAISE SQLSTATE 'XXXXY'; +END; +$$; +SELECT pg_sleep(5); +SELECT * FROM pg_log_errors_stats(); From cf566a923dcf44ebed3edaf130b6dbcfe9d049db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Klonfar?= Date: Fri, 21 Apr 2023 23:28:09 +0200 Subject: [PATCH 3/5] documentation update --- README.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index dcf0aee..2e172a6 100644 --- a/README.md +++ b/README.md @@ -19,20 +19,23 @@ Run psql command: ``` postgres=# select * from pg_log_errors_stats(); - time_interval | type | message | count - ---------------+---------+----------------------+------- - | WARNING | TOTAL | 0 - | ERROR | TOTAL | 3 - 600 | ERROR | ERRCODE_SYNTAX_ERROR | 3 - 5 | ERROR | ERRCODE_SYNTAX_ERROR | 2 - | FATAL | TOTAL | 0 + time_interval | type | message | count | username | database | sqlstate + ---------------+---------+----------------------+-------+----------+----------+---------- + | WARNING | TOTAL | 0 | | | + | ERROR | TOTAL | 1 | | | + | FATAL | TOTAL | 0 | | | + 5 | ERROR | ERRCODE_SYNTAX_ERROR | 1 | postgres | postgres | 42601 + 600 | ERROR | ERRCODE_SYNTAX_ERROR | 1 | postgres | postgres | 42601 ``` -In output you can see 4 columns: +In output you can see 7 columns: time_interval: how long (in seconds) has statistics been collected. type: postgresql type of message (now supports only these: warning, error, fatal). message: code of message from log_hook. (or 'TOTAL' for total count of that type messages) count: count of messages of this type at this time_interval in log. + username: effective role causing the message + database: database where the message comes from + sqlstate: code of the message transformed to the form of sqlstate To get number of lines in slow log call `pg_slow_log_stats()`: From fe37e82ee883995e8f2d3e79452b2df13ab952af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Klonfar?= Date: Tue, 9 May 2023 23:33:41 +0200 Subject: [PATCH 4/5] files created by installcheck added to gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 1646cf8..c5e7447 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ .idea results/* +log/* +logerrors.o +logerrors.so +regression-test/* From 199d59427936ba12ae279720e96860e470925ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Klonfar?= Date: Tue, 9 May 2023 23:34:31 +0200 Subject: [PATCH 5/5] basic description of pgxs regression tests added to Readme --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 2e172a6..c3f4e93 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,41 @@ Run psql command: $ CREATE EXTENSION logerrors; +## Tests + +The extension uses standard pgxs regression tests. Run `make installcheck` to run all psql scripts defined in `sql` directory. Output of each is then evaluated by `diff` with corresponding expected output stored in the `expected` directory. + +``` + $ make installcheck + +++ regress install-check in +++ + ============== creating temporary instance ============== + ============== initializing database system ============== + ============== starting postmaster ============== + running on port 51698 with PID 472134 + ============== creating database "contrib_regression" ============== + CREATE DATABASE + ALTER DATABASE + ALTER DATABASE + ALTER DATABASE + ALTER DATABASE + ALTER DATABASE + ALTER DATABASE + ============== installing logerrors ============== + CREATE EXTENSION + ============== creating role "postgres" ============== + CREATE ROLE + GRANT + ============== running regression test queries ============== + test logerrors ... ok 15017 ms + ============== shutting down postmaster ============== + ============== removing temporary instance ============== + +===================== + All 1 tests passed. +===================== + +``` + ## Usage After creating extension you can call `pg_log_errors_stats()` function in psql (without any arguments).