0002-session_end_hook-v5.patch
text/x-patch
Filename: 0002-session_end_hook-v5.patch
Type: text/x-patch
Part: 1
Message:
Re: [PATCH] A hook for session start
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
Series: patch v5-0002
| File | + | − |
|---|---|---|
| src/backend/utils/init/postinit.c | 12 | 0 |
| src/include/tcop/tcopprot.h | 5 | 1 |
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 20f1d27..029eeb4 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -76,6 +76,8 @@ static bool ThereIsAtLeastOneRole(void);
static void process_startup_options(Port *port, bool am_superuser);
static void process_settings(Oid databaseid, Oid roleid);
+/* Hook for plugins to get control at end of session */
+session_end_hook_type session_end_hook = NULL;
/*** InitPostgres support ***/
@@ -1154,6 +1156,16 @@ ShutdownPostgres(int code, Datum arg)
* them explicitly.
*/
LockReleaseAll(USER_LOCKMETHOD, true);
+
+ /* Hook just normal backends */
+ if (session_end_hook && MyBackendId != InvalidBackendId)
+ {
+ (*session_end_hook) (MyProcPort->database_name, MyProcPort->user_name);
+
+ /* Make don't leave any active transactions and/or locks behind */
+ AbortOutOfAnyTransaction();
+ LockReleaseAll(USER_LOCKMETHOD, true);
+ }
}
diff --git a/src/include/tcop/tcopprot.h b/src/include/tcop/tcopprot.h
index d349592..b7fb8c3 100644
--- a/src/include/tcop/tcopprot.h
+++ b/src/include/tcop/tcopprot.h
@@ -35,10 +35,14 @@ extern PGDLLIMPORT const char *debug_query_string;
extern int max_stack_depth;
extern int PostAuthDelay;
-/* Hook for plugins to get control at start of session */
+/* Hook for plugins to get control at start and end of session */
typedef void (*session_start_hook_type) (const char *dbname,
const char *username);
+typedef void (*session_end_hook_type) (const char *dbname,
+ const char *username);
+
extern PGDLLIMPORT session_start_hook_type session_start_hook;
+extern PGDLLIMPORT session_end_hook_type session_end_hook;
/* GUC-configurable parameters */