Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression tests #22

Merged
merged 5 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
results/*
munakoiso marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ Run psql command:

```
postgres=# select * from pg_log_errors_stats();
time_interval | type | message | count
munakoiso marked this conversation as resolved.
Show resolved Hide resolved
---------------+---------+----------------------+-------
| 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()`:

Expand Down
60 changes: 60 additions & 0 deletions expected/logerrors.out
Original file line number Diff line number Diff line change
@@ -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)

9 changes: 2 additions & 7 deletions logerrors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions logerrors.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shared_preload_libraries='logerrors'
17 changes: 17 additions & 0 deletions sql/logerrors.sql
Original file line number Diff line number Diff line change
@@ -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();