Search Postgresql Archives

Re: Server-side hooks for user session start and session end

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





> years ago I tried it, if I remember well. I had a problems with SPI calls,
> because some caches was not initialized. I am not sure, and I didn't test
> last time.

You'd have to start your own transaction if you wanted one, and any
uncaught error would effectively be FATAL because it would terminate the
session, but otherwise I don't see why that wouldn't work.

Probably I didn't start transaction.

I'll check it.

It is working. Patch attached

Regards

Pavel

diff --git a/contrib/session_exec/Makefile b/contrib/session_exec/Makefile
new file mode 100644
index ...fd21d87
*** a/contrib/session_exec/Makefile
--- b/contrib/session_exec/Makefile
***************
*** 0 ****
--- 1,16 ----
+ # contrib/session_exec/Makefile
+ 
+ MODULE_big = session_exec
+ OBJS = session_exec.o $(WIN32RES)
+ PGFILEDESC = "session_exec - logging facility for execution plans"
+ 
+ ifdef USE_PGXS
+ PG_CONFIG = pg_config
+ PGXS := $(shell $(PG_CONFIG) --pgxs)
+ include $(PGXS)
+ else
+ subdir = contrib/session_exec
+ top_builddir = ../..
+ include $(top_builddir)/src/Makefile.global
+ include $(top_srcdir)/contrib/contrib-global.mk
+ endif
diff --git a/contrib/session_exec/session_exec.c b/contrib/session_exec/session_exec.c
new file mode 100644
index ...3bdead5
*** a/contrib/session_exec/session_exec.c
--- b/contrib/session_exec/session_exec.c
***************
*** 0 ****
--- 1,89 ----
+ /*-------------------------------------------------------------------------
+  *
+  * session_exec.c
+  *
+  *
+  * Copyright (c) 2008-2015, PostgreSQL Global Development Group
+  *
+  * IDENTIFICATION
+  *	  contrib/session_exec/session_exec.c
+  *
+  *-------------------------------------------------------------------------
+  */
+ #include "postgres.h"
+ #include "fmgr.h"
+ #include "access/xact.h"
+ #include "catalog/namespace.h"
+ #include "storage/ipc.h"
+ #include "utils/builtins.h"
+ #include "utils/guc.h"
+ 
+ PG_MODULE_MAGIC;
+ 
+ void		_PG_init(void);
+ void		_PG_fini(void);
+ 
+ char *session_login_function_name = NULL;
+ 
+ /*
+  * Execute function named funcname. This function
+  * has not to have any parameters. This routine will
+  * be used for execution login/logout functions.
+  */
+ static void
+ exec_function(char *funcname)
+ {
+ 	FuncCandidateList clist;
+ 	List *names;
+ 
+ 	names = stringToQualifiedNameList(funcname);
+ 	clist = FuncnameGetCandidates(names, 0, NIL, false, false, true);
+ 
+ 	if (clist == NULL)
+ 		elog(WARNING, "function \"%s()\" does not exist", funcname);
+ 	else
+ 	{
+ 		/* execute function */
+ 		OidFunctionCall0(clist->oid);
+ 	}
+ }
+ 
+ void
+ _PG_init(void)
+ {
+ 	DefineCustomStringVariable("session_exec.login_name",
+ 	                           "Define function that will be executed on login",
+ 	                           "It is undefined by default",
+ 	                           &session_login_function_name,
+ 	                           NULL,
+ 	                           PGC_USERSET,
+ 	                           0, NULL, NULL, NULL);
+ 
+ 	if (session_login_function_name != NULL && *session_login_function_name != '\0')
+ 	{
+ 		MemoryContext	oldCtx = CurrentMemoryContext;
+ 
+ 		PG_TRY();
+ 		{
+ 			SetCurrentStatementStartTimestamp();
+ 			StartTransactionCommand();
+ 
+ 			exec_function(session_login_function_name);
+ 
+ 			CommitTransactionCommand();
+ 		}
+ 		PG_CATCH();
+ 		{
+ 			ErrorData	*edata;
+ 
+ 			MemoryContextSwitchTo(oldCtx);
+ 			edata = CopyErrorData();
+ 			FlushErrorState();
+ 
+ 			elog(FATAL, "exception in login function \"%s\"",
+ 								edata->message);
+ 		}
+ 		PG_END_TRY();
+ 	}
+ }
+ 
-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux