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]

 





2015-09-27 13:33 GMT+02:00 Nikolai Zhubr <n-a-zhubr@xxxxxxxxx>:
Hi,
27.09.2015 8:29, Pavel Stehule:
    I'll check it.


It is working. Patch attached

Oh, brilliant! This is a _huge_ help actually!

If I understand it correctly, any unhandled SQL-level exceptions will essentially be ignored there, so that the session will continue regardless?

And maybe it could even be proposed for upstream?
It is so wonderfully small and looks not too much intrusive.



updated patch - fixed error reporting

Regards

Pavel
 

--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

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 ...15cd5d9
*** a/contrib/session_exec/session_exec.c
--- b/contrib/session_exec/session_exec.c
***************
*** 0 ****
--- 1,94 ----
+ /*-------------------------------------------------------------------------
+  *
+  * 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();
+ 
+ 			ereport(FATAL,
+ 					(errcode(ERRCODE_CONNECTION_FAILURE),
+ 					 errmsg("unhandled exception in login function \"%s\"",
+ 						    session_login_function_name),
+ 					 errdetail("%s", edata->message),
+ 					 errcontext("session_exec: perform login function \"%s\"",
+ 						    session_login_function_name),
+ 					 errcontext("%s", edata->context)));
+ 		}
+ 		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