From: Michael Paquier Date: Fri, 5 Jul 2019 03:21:11 +0000 (+0900) Subject: Add min() and max() aggregates for pg_lsn X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=313f87a17155a6dbd27a3ce687cf59bd171fe75e;p=postgresql Add min() and max() aggregates for pg_lsn This is useful for monitoring, when it comes for example to calculations of WAL retention with replication slots and delays with a set of standbys. Bump catalog version. Author: Fabrízio de Royes Mello Reviewed-by: Surafel Temesgen Discussion: https://postgr.es/m/CAFcNs+oc8ZoHhowA4rR1GGCgG8QNgK_TOwPRVYQo5rYy8_PXzA@mail.gmail.com --- diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 3a8581d205..b7f746ba3b 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -14804,7 +14804,7 @@ NULL baz(3 rows) max(expression) - any numeric, string, date/time, network, or enum type, + any numeric, string, date/time, network, pg_lsn, or enum type, or arrays of these types same as argument type Yes @@ -14822,7 +14822,7 @@ NULL baz(3 rows) min(expression) - any numeric, string, date/time, network, or enum type, + any numeric, string, date/time, network, pg_lsn, or enum type, or arrays of these types same as argument type Yes diff --git a/src/backend/utils/adt/pg_lsn.c b/src/backend/utils/adt/pg_lsn.c index 4c329a8d8f..b4c6c2309c 100644 --- a/src/backend/utils/adt/pg_lsn.c +++ b/src/backend/utils/adt/pg_lsn.c @@ -171,6 +171,24 @@ pg_lsn_ge(PG_FUNCTION_ARGS) PG_RETURN_BOOL(lsn1 >= lsn2); } +Datum +pg_lsn_larger(PG_FUNCTION_ARGS) +{ + XLogRecPtr lsn1 = PG_GETARG_LSN(0); + XLogRecPtr lsn2 = PG_GETARG_LSN(1); + + PG_RETURN_LSN((lsn1 > lsn2) ? lsn1 : lsn2); +} + +Datum +pg_lsn_smaller(PG_FUNCTION_ARGS) +{ + XLogRecPtr lsn1 = PG_GETARG_LSN(0); + XLogRecPtr lsn2 = PG_GETARG_LSN(1); + + PG_RETURN_LSN((lsn1 < lsn2) ? lsn1 : lsn2); +} + /* btree index opclass support */ Datum pg_lsn_cmp(PG_FUNCTION_ARGS) diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 9436e0b5ec..bd2f23e602 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 201907052 +#define CATALOG_VERSION_NO 201907053 #endif diff --git a/src/include/catalog/pg_aggregate.dat b/src/include/catalog/pg_aggregate.dat index 044695a046..242d843347 100644 --- a/src/include/catalog/pg_aggregate.dat +++ b/src/include/catalog/pg_aggregate.dat @@ -146,6 +146,9 @@ { aggfnoid => 'max(inet)', aggtransfn => 'network_larger', aggcombinefn => 'network_larger', aggsortop => '>(inet,inet)', aggtranstype => 'inet' }, +{ aggfnoid => 'max(pg_lsn)', aggtransfn => 'pg_lsn_larger', + aggcombinefn => 'pg_lsn_larger', aggsortop => '>(pg_lsn,pg_lsn)', + aggtranstype => 'pg_lsn' }, # min { aggfnoid => 'min(int8)', aggtransfn => 'int8smaller', @@ -208,6 +211,9 @@ { aggfnoid => 'min(inet)', aggtransfn => 'network_smaller', aggcombinefn => 'network_smaller', aggsortop => '<(inet,inet)', aggtranstype => 'inet' }, +{ aggfnoid => 'min(pg_lsn)', aggtransfn => 'pg_lsn_smaller', + aggcombinefn => 'pg_lsn_smaller', aggsortop => '<(pg_lsn,pg_lsn)', + aggtranstype => 'pg_lsn' }, # count { aggfnoid => 'count(any)', aggtransfn => 'int8inc_any', diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 36ae2ac9d8..29f0944774 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -6219,6 +6219,9 @@ { oid => '3564', descr => 'maximum value of all inet input values', proname => 'max', prokind => 'a', proisstrict => 'f', prorettype => 'inet', proargtypes => 'inet', prosrc => 'aggregate_dummy' }, +{ oid => '4189', descr => 'maximum value of all pg_lsn input values', + proname => 'max', prokind => 'a', proisstrict => 'f', prorettype => 'pg_lsn', + proargtypes => 'pg_lsn', prosrc => 'aggregate_dummy' }, { oid => '2131', descr => 'minimum value of all bigint input values', proname => 'min', prokind => 'a', proisstrict => 'f', prorettype => 'int8', @@ -6283,6 +6286,9 @@ { oid => '3565', descr => 'minimum value of all inet input values', proname => 'min', prokind => 'a', proisstrict => 'f', prorettype => 'inet', proargtypes => 'inet', prosrc => 'aggregate_dummy' }, +{ oid => '4190', descr => 'minimum value of all pg_lsn input values', + proname => 'min', prokind => 'a', proisstrict => 'f', prorettype => 'pg_lsn', + proargtypes => 'pg_lsn', prosrc => 'aggregate_dummy' }, # count has two forms: count(any) and count(*) { oid => '2147', @@ -8385,6 +8391,12 @@ { oid => '3413', descr => 'hash', proname => 'pg_lsn_hash_extended', prorettype => 'int8', proargtypes => 'pg_lsn int8', prosrc => 'pg_lsn_hash_extended' }, +{ oid => '4187', descr => 'larger of two', + proname => 'pg_lsn_larger', prorettype => 'pg_lsn', + proargtypes => 'pg_lsn pg_lsn', prosrc => 'pg_lsn_larger' }, +{ oid => '4188', descr => 'smaller of two', + proname => 'pg_lsn_smaller', prorettype => 'pg_lsn', + proargtypes => 'pg_lsn pg_lsn', prosrc => 'pg_lsn_smaller' }, # enum related procs { oid => '3504', descr => 'I/O', diff --git a/src/test/regress/expected/pg_lsn.out b/src/test/regress/expected/pg_lsn.out index 2854cfd7b9..64d41dfdad 100644 --- a/src/test/regress/expected/pg_lsn.out +++ b/src/test/regress/expected/pg_lsn.out @@ -26,6 +26,13 @@ INSERT INTO PG_LSN_TBL VALUES ('/ABCD'); ERROR: invalid input syntax for type pg_lsn: "/ABCD" LINE 1: INSERT INTO PG_LSN_TBL VALUES ('/ABCD'); ^ +-- Min/Max aggregation +SELECT MIN(f1), MAX(f1) FROM PG_LSN_TBL; + min | max +-----+------------------- + 0/0 | FFFFFFFF/FFFFFFFF +(1 row) + DROP TABLE PG_LSN_TBL; -- Operators SELECT '0/16AE7F8' = '0/16AE7F8'::pg_lsn; diff --git a/src/test/regress/sql/pg_lsn.sql b/src/test/regress/sql/pg_lsn.sql index 746f720d69..2c143c82ff 100644 --- a/src/test/regress/sql/pg_lsn.sql +++ b/src/test/regress/sql/pg_lsn.sql @@ -14,6 +14,10 @@ INSERT INTO PG_LSN_TBL VALUES ('-1/0'); INSERT INTO PG_LSN_TBL VALUES (' 0/12345678'); INSERT INTO PG_LSN_TBL VALUES ('ABCD/'); INSERT INTO PG_LSN_TBL VALUES ('/ABCD'); + +-- Min/Max aggregation +SELECT MIN(f1), MAX(f1) FROM PG_LSN_TBL; + DROP TABLE PG_LSN_TBL; -- Operators