Skip to content

Commit e788bd9

Browse files
committed
Add hooks for session start and session end, take two
These hooks can be used in loadable modules. A simple test module is included. The first attempt was done with cd8ce3a but we lacked handling for NO_INSTALLCHECK in the MSVC scripts (problem solved afterwards by 431f159) so the buildfarm got angry. This also fixes a couple of issues noticed upon review compared to the first attempt, so the code has slightly changed, resulting in a more simple test module. Author: Fabrízio de Royes Mello, Yugo Nagata Reviewed-by: Andrew Dunstan, Michael Paquier, Aleksandr Parfenov Discussion: https://postgr.es/m/20170720204733.40f2b7eb.nagata@sraoss.co.jp Discussion: https://postgr.es/m/20190823042602.GB5275@paquier.xyz
1 parent 41a6de4 commit e788bd9

File tree

11 files changed

+262
-0
lines changed

11 files changed

+262
-0
lines changed

src/backend/tcop/postgres.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ static ProcSignalReason RecoveryConflictReason;
171171
static MemoryContext row_description_context = NULL;
172172
static StringInfoData row_description_buf;
173173

174+
/* Hook for plugins to get control at start of session */
175+
session_start_hook_type session_start_hook = NULL;
176+
174177
/* ----------------------------------------------------------------
175178
* decls for routines only used in this file
176179
* ----------------------------------------------------------------
@@ -3968,6 +3971,9 @@ PostgresMain(int argc, char *argv[],
39683971
if (!IsUnderPostmaster)
39693972
PgStartTime = GetCurrentTimestamp();
39703973

3974+
if (session_start_hook)
3975+
(*session_start_hook) ();
3976+
39713977
/*
39723978
* POSTGRES main processing loop begins here
39733979
*

src/backend/utils/init/postinit.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ static bool ThereIsAtLeastOneRole(void);
7878
static void process_startup_options(Port *port, bool am_superuser);
7979
static void process_settings(Oid databaseid, Oid roleid);
8080

81+
/* Hook for plugins to get control at end of session */
82+
session_end_hook_type session_end_hook = NULL;
8183

8284
/*** InitPostgres support ***/
8385

@@ -1195,6 +1197,10 @@ ShutdownPostgres(int code, Datum arg)
11951197
* them explicitly.
11961198
*/
11971199
LockReleaseAll(USER_LOCKMETHOD, true);
1200+
1201+
/* Hook at session end */
1202+
if (session_end_hook)
1203+
(*session_end_hook) ();
11981204
}
11991205

12001206

src/include/tcop/tcopprot.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ extern PGDLLIMPORT const char *debug_query_string;
3030
extern int max_stack_depth;
3131
extern int PostAuthDelay;
3232

33+
/* Hook for plugins to get control at start and end of session */
34+
typedef void (*session_start_hook_type) (void);
35+
typedef void (*session_end_hook_type) (void);
36+
37+
extern PGDLLIMPORT session_start_hook_type session_start_hook;
38+
extern PGDLLIMPORT session_end_hook_type session_end_hook;
39+
3340
/* GUC-configurable parameters */
3441

3542
typedef enum

src/test/modules/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SUBDIRS = \
2121
test_predtest \
2222
test_rbtree \
2323
test_rls_hooks \
24+
test_session_hooks \
2425
test_shm_mq \
2526
unsafe_tests \
2627
worker_spi
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Generated subdirectories
2+
/log/
3+
/results/
4+
/tmp_check/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# src/test/modules/test_session_hooks/Makefile
2+
3+
MODULE_big = test_session_hooks
4+
OBJS = test_session_hooks.o $(WIN32RES)
5+
PGFILEDESC = "test_session_hooks - tests for start and end session hooks"
6+
7+
REGRESS = test_session_hooks
8+
REGRESS_OPTS = --temp-config=$(top_srcdir)/src/test/modules/test_session_hooks/session_hooks.conf
9+
# Disabled because these tests require extra configuration with
10+
# "shared_preload_libraries=test_session_hooks", which typical
11+
# installcheck users do not have (e.g. buildfarm clients).
12+
NO_INSTALLCHECK = 1
13+
14+
ifdef USE_PGXS
15+
PG_CONFIG = pg_config
16+
PGXS := $(shell $(PG_CONFIG) --pgxs)
17+
include $(PGXS)
18+
else
19+
subdir = src/test/modules/test_session_hooks
20+
top_builddir = ../../../..
21+
include $(top_builddir)/src/Makefile.global
22+
include $(top_srcdir)/contrib/contrib-global.mk
23+
endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
test_session_hooks
2+
==================
3+
4+
test_session_hooks is an example of how to use session start and end
5+
hooks.
6+
7+
This module will insert into a pre-existing table called "session_hook_log"
8+
a log activity which happens at session start and end. It is possible
9+
to control which user information is logged when using the configuration
10+
parameter "test_session_hooks.username". If set, the hooks will log only
11+
information of the session user matching the parameter value.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--
2+
-- Tests for start and end session hooks
3+
--
4+
-- Only activity from role regress_sess_hook_usr2 is logged.
5+
CREATE ROLE regress_sess_hook_usr1 SUPERUSER LOGIN;
6+
CREATE ROLE regress_sess_hook_usr2 SUPERUSER LOGIN;
7+
\set prevdb :DBNAME
8+
\set prevusr :USER
9+
CREATE TABLE session_hook_log(id SERIAL, dbname TEXT, username TEXT, hook_at TEXT);
10+
SELECT * FROM session_hook_log ORDER BY id;
11+
id | dbname | username | hook_at
12+
----+--------+----------+---------
13+
(0 rows)
14+
15+
\c :prevdb regress_sess_hook_usr1
16+
SELECT * FROM session_hook_log ORDER BY id;
17+
id | dbname | username | hook_at
18+
----+--------+----------+---------
19+
(0 rows)
20+
21+
\c :prevdb regress_sess_hook_usr2
22+
SELECT * FROM session_hook_log ORDER BY id;
23+
id | dbname | username | hook_at
24+
----+--------------------+------------------------+---------
25+
1 | contrib_regression | regress_sess_hook_usr2 | START
26+
(1 row)
27+
28+
\c :prevdb :prevusr
29+
SELECT * FROM session_hook_log ORDER BY id;
30+
id | dbname | username | hook_at
31+
----+--------------------+------------------------+---------
32+
1 | contrib_regression | regress_sess_hook_usr2 | START
33+
2 | contrib_regression | regress_sess_hook_usr2 | END
34+
(2 rows)
35+
36+
DROP ROLE regress_sess_hook_usr1;
37+
DROP ROLE regress_sess_hook_usr2;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
shared_preload_libraries = 'test_session_hooks'
2+
test_session_hooks.username = regress_sess_hook_usr2
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--
2+
-- Tests for start and end session hooks
3+
--
4+
5+
-- Only activity from role regress_sess_hook_usr2 is logged.
6+
CREATE ROLE regress_sess_hook_usr1 SUPERUSER LOGIN;
7+
CREATE ROLE regress_sess_hook_usr2 SUPERUSER LOGIN;
8+
\set prevdb :DBNAME
9+
\set prevusr :USER
10+
CREATE TABLE session_hook_log(id SERIAL, dbname TEXT, username TEXT, hook_at TEXT);
11+
SELECT * FROM session_hook_log ORDER BY id;
12+
\c :prevdb regress_sess_hook_usr1
13+
SELECT * FROM session_hook_log ORDER BY id;
14+
\c :prevdb regress_sess_hook_usr2
15+
SELECT * FROM session_hook_log ORDER BY id;
16+
\c :prevdb :prevusr
17+
SELECT * FROM session_hook_log ORDER BY id;
18+
DROP ROLE regress_sess_hook_usr1;
19+
DROP ROLE regress_sess_hook_usr2;

0 commit comments

Comments
 (0)