Hi, hackers
I see [1] has already implemented on login event trigger, why not implement
the logoff event trigger?
My friend Song Jinzhou and I try to implement the logoff event trigger, so
attach it.
Here is a problem with the regression test when using \c to create a new
session, because it might be running concurrently, which may lead to the
checking being unstable.
Any thoughts?
[1]
https://www.postgresql.org/message-id/0d46d29f-4558-3af9-9c85-7774e14a7709%40postgrespro.ru
--
Regards,
Japin Li
>From ef7c6aa408d0a6a97d7b0d2e093b71e279b1b0dc Mon Sep 17 00:00:00 2001
From: Japin Li
Date: Mon, 15 Apr 2024 09:11:41 +0800
Subject: [PATCH v1 1/1] Add support event triggers for logoff
---
doc/src/sgml/catalogs.sgml| 13 ++
doc/src/sgml/ecpg.sgml| 2 +
doc/src/sgml/event-trigger.sgml | 5 +
src/backend/commands/dbcommands.c | 17 +-
src/backend/commands/event_trigger.c | 181 +-
src/backend/tcop/postgres.c | 8 +
src/backend/utils/cache/evtcache.c| 2 +
src/backend/utils/init/globals.c | 1 +
src/backend/utils/init/postinit.c | 1 +
src/bin/psql/tab-complete.c | 2 +-
src/include/catalog/pg_database.dat | 2 +-
src/include/catalog/pg_database.h | 3 +
src/include/commands/event_trigger.h | 1 +
src/include/miscadmin.h | 1 +
src/include/tcop/cmdtaglist.h | 1 +
src/include/utils/evtcache.h | 1 +
.../regress/expected/event_trigger_logoff.out | 49 +
src/test/regress/parallel_schedule| 2 +
src/test/regress/sql/event_trigger_logoff.sql | 26 +++
19 files changed, 309 insertions(+), 9 deletions(-)
create mode 100644 src/test/regress/expected/event_trigger_logoff.out
create mode 100644 src/test/regress/sql/event_trigger_logoff.sql
diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml
index 2907079e2a..621fbfde98 100644
--- a/doc/src/sgml/catalogs.sgml
+++ b/doc/src/sgml/catalogs.sgml
@@ -3076,6 +3076,19 @@ SCRAM-SHA-256$iteration count:
+
+
+ dathaslogoffevt bool
+
+
+Indicates that there are logoff event triggers defined for this database.
+This flag is used to avoid extra lookups on the
+pg_event_trigger table during each backend
+startup. This flag is used internally by PostgreSQL
+and should not be manually altered or read for monitoring purposes.
+
+
+
datconnlimit int4
diff --git a/doc/src/sgml/ecpg.sgml b/doc/src/sgml/ecpg.sgml
index e7a53f3c9d..d223843157 100644
--- a/doc/src/sgml/ecpg.sgml
+++ b/doc/src/sgml/ecpg.sgml
@@ -4765,6 +4765,7 @@ encoding = 0 (type: 5)
datistemplate = t (type: 1)
datallowconn = t (type: 1)
dathasloginevt = f (type: 1)
+dathaslogoffevt = f (type: 1)
datconnlimit = -1 (type: 5)
datfrozenxid = 379 (type: 1)
dattablespace = 1663 (type: 1)
@@ -4790,6 +4791,7 @@ encoding = 0 (type: 5)
datistemplate = f (type: 1)
datallowconn = t (type: 1)
dathasloginevt = f (type: 1)
+dathaslogoffevt = f (type: 1)
datconnlimit = -1 (type: 5)
datfrozenxid = 379 (type: 1)
dattablespace = 1663 (type: 1)
diff --git a/doc/src/sgml/event-trigger.sgml b/doc/src/sgml/event-trigger.sgml
index 8e009cca05..ed996a456e 100644
--- a/doc/src/sgml/event-trigger.sgml
+++ b/doc/src/sgml/event-trigger.sgml
@@ -29,6 +29,7 @@
occurs in the database in which it is defined. Currently, the only
supported events are
login,
+ logoff,
ddl_command_start,
ddl_command_end,
table_rewrite
@@ -54,6 +55,10 @@
the in-progress login trigger.
+
+ The logoff event occurs when a user logs off the system.
+
+
The ddl_command_start event occurs just before the
execution of a CREATE, ALTER, DROP,
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 8229dfa1f2..a6784a0ea6 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -116,7 +116,7 @@ static void movedb_failure_callback(int code, Datum arg);
static bool get_db_info(const char *name, LOCKMODE lockmode,
Oid *dbIdP, Oid *ownerIdP,
int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP, bool *dbHasLoginEvtP,
- TransactionId *dbFrozenXidP, MultiXactId *dbMinMultiP,
+ bool *dbHasLogoffEvtP, TransactionId *dbFrozenXidP, MultiXactId *dbMinMultiP,
Oid *dbTablespace, char **dbCollate, char **dbCtype, char **dbLocale,
char **dbIcurules,
char *dbLocProvider,
@@ -680,6 +680,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
char *src_collversion = NULL;
bool src_istemplate;
bool src_hasloginevt = false;
+ bool src_haslogoffevt = false;
bool src_allowconn;
TransactionId src_frozenxid = InvalidTransactionId;
MultiXactId