Skip to content

Commit

Permalink
Add max() and min() aggregate functions for pg_lsn data type.
Browse files Browse the repository at this point in the history
  • Loading branch information
MasaoFujii committed Oct 5, 2016
1 parent 2e9e767 commit e13ceba
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REGRESS = pg_cheat_funcs pg_eucjp
MAJORVERSION_INT = $(shell echo $(MAJORVERSION).0 | cut -d . -f 1-2 | tr -d .)
REGRESS += $(shell if [ $(MAJORVERSION_INT) -ge 96 ]; then echo pg_stat_get_memory_context; fi)
REGRESS += $(shell if [ $(MAJORVERSION_INT) -ge 95 ]; then echo pglz_compress; fi)
REGRESS += $(shell if [ $(MAJORVERSION_INT) -ge 94 ]; then echo pg_chr; else echo pg_chr_91_93; fi)
REGRESS += $(shell if [ $(MAJORVERSION_INT) -ge 94 ]; then echo pg_chr pg_lsn; else echo pg_chr_91_93; fi)

PGFILEDESC = "pg_cheat_funcs - provides cheat (but useful) functions"

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ That is, this function can return bogus WAL file name.
For details of this conversion, please see [PostgreSQL document](http://www.postgresql.org/docs/devel/static/functions-admin.html#FUNCTIONS-ADMIN-BACKUP).
This function is available only in PostgreSQL 9.4 or later.

### pg_lsn max(SETOF pg_lsn)
This is max() aggregate function for pg_lsn data type.
This aggregate function computes and returns a maximum pg_lsn value
from a set of pg_lsn input values.

### pg_lsn min(SETOF pg_lsn)
This is min() aggregate function for pg_lsn data type.
This aggregate function computes and returns a minimum pg_lsn value
from a set of pg_lsn input values.

### SETOF record pg_stat_get_syncrep_waiters()
Return statistics about all server processes waiting for
synchronous replication.
Expand Down
27 changes: 26 additions & 1 deletion pg_cheat_funcs--1.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ AS 'MODULE_PATHNAME'
LANGUAGE C STRICT VOLATILE;
REVOKE ALL ON FUNCTION pg_process_config_file() FROM PUBLIC;

/* pg_xlogfile_name function is available only in 9.4 or later */
/* Any functions using pg_lsn data type are available only in 9.4 or later */
DO $$
DECLARE
pgversion INTEGER;
Expand All @@ -55,6 +55,31 @@ BEGIN
RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT VOLATILE;

-- Aggregate functions for pg_lsn data type.
CREATE FUNCTION pg_lsn_larger(pg_lsn, pg_lsn)
RETURNS pg_lsn
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE FUNCTION pg_lsn_smaller(pg_lsn, pg_lsn)
RETURNS pg_lsn
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE AGGREGATE max(pg_lsn) (
SFUNC = pg_lsn_larger,
STYPE = pg_lsn,
SORTOP = >,
COMBINEFUNC = pg_lsn_larger
);

CREATE AGGREGATE min(pg_lsn) (
SFUNC = pg_lsn_smaller,
STYPE = pg_lsn,
SORTOP = <,
COMBINEFUNC = pg_lsn_smaller
);
END IF;
END;
$$;
Expand Down
32 changes: 32 additions & 0 deletions pg_cheat_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ PG_FUNCTION_INFO_V1(pg_signal_process);
PG_FUNCTION_INFO_V1(pg_process_config_file);
#if PG_VERSION_NUM >= 90400
PG_FUNCTION_INFO_V1(pg_xlogfile_name);
PG_FUNCTION_INFO_V1(pg_lsn_larger);
PG_FUNCTION_INFO_V1(pg_lsn_smaller);
PG_FUNCTION_INFO_V1(pg_stat_get_syncrep_waiters);
PG_FUNCTION_INFO_V1(pg_wait_syncrep);
#endif
Expand Down Expand Up @@ -581,6 +583,36 @@ pg_xlogfile_name(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
}

/*
* Return larger pg_lsn value.
*/
Datum
pg_lsn_larger(PG_FUNCTION_ARGS)
{
XLogRecPtr lsn1 = PG_GETARG_LSN(0);
XLogRecPtr lsn2 = PG_GETARG_LSN(1);
XLogRecPtr result;

result = ((lsn1 > lsn2) ? lsn1 : lsn2);

PG_RETURN_LSN(result);
}

/*
* Return smaller pg_lsn value.
*/
Datum
pg_lsn_smaller(PG_FUNCTION_ARGS)
{
XLogRecPtr lsn1 = PG_GETARG_LSN(0);
XLogRecPtr lsn2 = PG_GETARG_LSN(1);
XLogRecPtr result;

result = ((lsn1 < lsn2) ? lsn1 : lsn2);

PG_RETURN_LSN(result);
}

/*
* Return statistics about all syncrep waiters.
*/
Expand Down

0 comments on commit e13ceba

Please sign in to comment.