Skip to content

Commit 68daba6

Browse files
author
Craig Ringer
committed
Add a demo ext showing that ereport doesn't eval its args on errlevel
1 parent f4d4088 commit 68daba6

File tree

10 files changed

+110
-0
lines changed

10 files changed

+110
-0
lines changed

postgresql/ereport_skip/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
log
2+
regress.*
3+
tmp_check
4+
*.so
5+
*.o

postgresql/ereport_skip/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# src/test/modules/ereport_skip/Makefile
2+
3+
MODULES = ereport_skip
4+
PGFILEDESC = "ereport_skip"
5+
6+
EXTENSION = ereport_skip
7+
DATA = ereport_skip--1.0.sql
8+
9+
REGRESS = ereport_skip_constexpr \
10+
ereport_skip_nonconst
11+
12+
ifdef USE_PGXS
13+
PG_CONFIG = pg_config
14+
PGXS := $(shell $(PG_CONFIG) --pgxs)
15+
include $(PGXS)
16+
else
17+
subdir = src/test/modules/ereport_skip
18+
top_builddir = ../../../..
19+
include $(top_builddir)/src/Makefile.global
20+
include $(top_srcdir)/contrib/contrib-global.mk
21+
endif

postgresql/ereport_skip/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `ereport_skip`
2+
3+
Demo extension showing that evaluation of ereport() arguments is bypassed
4+
when elevel is low enough that they are not required.
5+
6+
Put it in src/test/modules and "make check", or run with PGXS using
7+
installcheck.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE FUNCTION ereport_skip_test() RETURNS void LANGUAGE c AS 'MODULE_PATHNAME','ereport_skip_test';
2+
CREATE FUNCTION ereport_skip_test2() RETURNS void LANGUAGE c AS 'MODULE_PATHNAME','ereport_skip_test2';
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "postgres.h"
2+
3+
#include "commands/seclabel.h"
4+
#include "fmgr.h"
5+
#include "miscadmin.h"
6+
#include "utils/guc.h"
7+
8+
PG_MODULE_MAGIC;
9+
10+
static const char *
11+
expensive_function(void)
12+
{
13+
pg_usleep(2000000L);
14+
return "expensive";
15+
}
16+
17+
PG_FUNCTION_INFO_V1(ereport_skip_test);
18+
19+
Datum
20+
ereport_skip_test(PG_FUNCTION_ARGS)
21+
{
22+
/* Constant expression */
23+
ereport(DEBUG1,
24+
(errmsg("%s", expensive_function())));
25+
PG_RETURN_VOID();
26+
}
27+
28+
PG_FUNCTION_INFO_V1(ereport_skip_test2);
29+
30+
Datum
31+
ereport_skip_test2(PG_FUNCTION_ARGS)
32+
{
33+
/* Non-constant expression */
34+
ereport(client_min_messages,
35+
(errmsg("%s", expensive_function())));
36+
PG_RETURN_VOID();
37+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
comment = 'Test code for ereport skip'
2+
default_version = '1.0'
3+
module_pathname = '$libdir/ereport_skip'
4+
relocatable = true

postgresql/ereport_skip/expected/ereport_skip_constexpr.sql

Whitespace-only changes.

postgresql/ereport_skip/expected/ereport_skip_nonconst.sql

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CREATE EXTENSION ereport_skip;
2+
3+
SET client_min_messages = 'info';
4+
5+
SELECT clock_timestamp() AS before_ts \gset
6+
SELECT ereport_skip_test();
7+
SELECT clock_timestamp() AS after_ts \gset
8+
9+
SELECT (TIMESTAMP :'after_ts' - TIMESTAMP :'before_ts') > INTERVAL '1' SECOND AS did_delay_when_no_output;
10+
11+
SET client_min_messages = 'debug1';
12+
13+
SELECT clock_timestamp() AS before_ts \gset
14+
SELECT ereport_skip_test();
15+
SELECT clock_timestamp() AS after_ts \gset
16+
17+
SELECT (TIMESTAMP :'after_ts' - TIMESTAMP :'before_ts') > INTERVAL '1' SECOND AS did_delay_when_output;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Test with non-constexpr elevel to make sure we still elide calls.
2+
3+
SET client_min_messages = 'info';
4+
5+
SELECT clock_timestamp() AS before_ts \gset
6+
SELECT ereport_skip_test2();
7+
SELECT clock_timestamp() AS after_ts \gset
8+
9+
SELECT (TIMESTAMP :'after_ts' - TIMESTAMP :'before_ts') > INTERVAL '1' SECOND AS did_delay_when_no_output;
10+
11+
SET client_min_messages = 'debug1';
12+
13+
SELECT clock_timestamp() AS before_ts \gset
14+
SELECT ereport_skip_test2();
15+
SELECT clock_timestamp() AS after_ts \gset
16+
17+
SELECT (TIMESTAMP :'after_ts' - TIMESTAMP :'before_ts') > INTERVAL '1' SECOND AS did_delay_when_output;

0 commit comments

Comments
 (0)