Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd8ce3a

Browse files
committed
Add hooks for session start and session end
These hooks can be used in loadable modules. A simple test module is included. Discussion: https://postgr.es/m/[email protected] Fabrízio de Royes Mello and Yugo Nagata Reviewed by Michael Paquier and Aleksandr Parfenov
1 parent ebc189e commit cd8ce3a

File tree

13 files changed

+233
-0
lines changed

13 files changed

+233
-0
lines changed

src/backend/tcop/postgres.c

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

172+
/* Hook for plugins to get control at start of session */
173+
session_start_hook_type session_start_hook = NULL;
174+
172175
/* ----------------------------------------------------------------
173176
* decls for routines only used in this file
174177
* ----------------------------------------------------------------
@@ -3857,6 +3860,9 @@ PostgresMain(int argc, char *argv[],
38573860
if (!IsUnderPostmaster)
38583861
PgStartTime = GetCurrentTimestamp();
38593862

3863+
if (session_start_hook)
3864+
(*session_start_hook) ();
3865+
38603866
/*
38613867
* POSTGRES main processing loop begins here
38623868
*

src/backend/utils/init/postinit.c

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

79+
/* Hook for plugins to get control at end of session */
80+
session_end_hook_type session_end_hook = NULL;
7981

8082
/*** InitPostgres support ***/
8183

@@ -1154,6 +1156,10 @@ ShutdownPostgres(int code, Datum arg)
11541156
* them explicitly.
11551157
*/
11561158
LockReleaseAll(USER_LOCKMETHOD, true);
1159+
1160+
/* Hook at session end */
1161+
if (session_end_hook)
1162+
(*session_end_hook) ();
11571163
}
11581164

11591165

src/include/tcop/tcopprot.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ extern PGDLLIMPORT const char *debug_query_string;
3535
extern int max_stack_depth;
3636
extern int PostAuthDelay;
3737

38+
/* Hook for plugins to get control at start and end of session */
39+
typedef void (*session_start_hook_type) (void);
40+
typedef void (*session_end_hook_type) (void);
41+
42+
extern PGDLLIMPORT session_start_hook_type session_start_hook;
43+
extern PGDLLIMPORT session_end_hook_type session_end_hook;
44+
3845
/* GUC-configurable parameters */
3946

4047
typedef enum

src/test/modules/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ SUBDIRS = \
1515
test_pg_dump \
1616
test_rbtree \
1717
test_rls_hooks \
18+
test_session_hooks \
1819
test_shm_mq \
1920
worker_spi
2021

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# src/test/modules/test_session_hooks/Makefile
2+
3+
MODULES = test_session_hooks
4+
PGFILEDESC = "test_session_hooks - Test session hooks with an extension"
5+
6+
EXTENSION = test_session_hooks
7+
DATA = test_session_hooks--1.0.sql
8+
9+
REGRESS = test_session_hooks
10+
REGRESS_OPTS = --temp-config=$(top_srcdir)/src/test/modules/test_session_hooks/session_hooks.conf
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/test_session_hooks
18+
top_builddir = ../../../..
19+
include $(top_builddir)/src/Makefile.global
20+
include $(top_srcdir)/contrib/contrib-global.mk
21+
endif
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test_session_hooks is an example of how to use session start and end
2+
hooks.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
CREATE ROLE regress_sess_hook_usr1 SUPERUSER LOGIN;
2+
CREATE ROLE regress_sess_hook_usr2 SUPERUSER LOGIN;
3+
\set prevdb :DBNAME
4+
\set prevusr :USER
5+
CREATE TABLE session_hook_log(id SERIAL, dbname TEXT, username TEXT, hook_at TEXT);
6+
SELECT * FROM session_hook_log ORDER BY id;
7+
id | dbname | username | hook_at
8+
----+--------+----------+---------
9+
(0 rows)
10+
11+
\c :prevdb regress_sess_hook_usr1
12+
SELECT * FROM session_hook_log ORDER BY id;
13+
id | dbname | username | hook_at
14+
----+--------+----------+---------
15+
(0 rows)
16+
17+
\c :prevdb regress_sess_hook_usr2
18+
SELECT * FROM session_hook_log ORDER BY id;
19+
id | dbname | username | hook_at
20+
----+--------------------+------------------------+---------
21+
1 | contrib_regression | regress_sess_hook_usr2 | START
22+
(1 row)
23+
24+
\c :prevdb :prevusr
25+
SELECT * FROM session_hook_log ORDER BY id;
26+
id | dbname | username | hook_at
27+
----+--------------------+------------------------+---------
28+
1 | contrib_regression | regress_sess_hook_usr2 | START
29+
2 | contrib_regression | regress_sess_hook_usr2 | END
30+
(2 rows)
31+
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE ROLE regress_sess_hook_usr1 SUPERUSER LOGIN;
2+
CREATE ROLE regress_sess_hook_usr2 SUPERUSER LOGIN;
3+
\set prevdb :DBNAME
4+
\set prevusr :USER
5+
CREATE TABLE session_hook_log(id SERIAL, dbname TEXT, username TEXT, hook_at TEXT);
6+
SELECT * FROM session_hook_log ORDER BY id;
7+
\c :prevdb regress_sess_hook_usr1
8+
SELECT * FROM session_hook_log ORDER BY id;
9+
\c :prevdb regress_sess_hook_usr2
10+
SELECT * FROM session_hook_log ORDER BY id;
11+
\c :prevdb :prevusr
12+
SELECT * FROM session_hook_log ORDER BY id;

0 commit comments

Comments
 (0)