summaryrefslogtreecommitdiff
path: root/src/pl
diff options
context:
space:
mode:
authorTom Lane2006-08-08 19:15:09 +0000
committerTom Lane2006-08-08 19:15:09 +0000
commitb09bfcaa576c1a3e0c34a747a502bae909b984a8 (patch)
treefe1c80a8cf23b37d2ea78345cd3580ac2c588eed /src/pl
parente00664da48cc31575c7105bbeff9e298a1ab1827 (diff)
Add a feature for automatic initialization and finalization of dynamically
loaded libraries: call functions _PG_init() and _PG_fini() if the library defines such symbols. Hence we no longer need to specify an initialization function in preload_libraries: we can assume that the library used the _PG_init() convention, instead. This removes one source of pilot error in use of preloaded libraries. Original patch by Ralf Engelschall, preload_libraries changes by me.
Diffstat (limited to 'src/pl')
-rw-r--r--src/pl/plperl/plperl.c48
-rw-r--r--src/pl/plpgsql/src/pl_handler.c40
-rw-r--r--src/pl/plpgsql/src/plpgsql.h4
-rw-r--r--src/pl/plpython/plpython.c47
-rw-r--r--src/pl/tcl/pltcl.c38
5 files changed, 54 insertions, 123 deletions
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index 2ed16b12cbe..8e0f309d056 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -1,7 +1,7 @@
/**********************************************************************
* plperl.c - perl as a procedural language for PostgreSQL
*
- * $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.112 2006/06/16 18:42:23 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.113 2006/08/08 19:15:09 tgl Exp $
*
**********************************************************************/
@@ -87,7 +87,6 @@ typedef struct plperl_query_desc
/**********************************************************************
* Global data
**********************************************************************/
-static bool plperl_firstcall = true;
static bool plperl_safe_init_done = false;
static PerlInterpreter *plperl_interp = NULL;
static HV *plperl_proc_hash = NULL;
@@ -101,12 +100,11 @@ static plperl_call_data *current_call_data = NULL;
/**********************************************************************
* Forward declarations
**********************************************************************/
-static void plperl_init_all(void);
-static void plperl_init_interp(void);
-
Datum plperl_call_handler(PG_FUNCTION_ARGS);
Datum plperl_validator(PG_FUNCTION_ARGS);
-void plperl_init(void);
+void _PG_init(void);
+
+static void plperl_init_interp(void);
static Datum plperl_func_handler(PG_FUNCTION_ARGS);
@@ -135,16 +133,21 @@ perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
}
-/* Perform initialization during postmaster startup. */
-
+/*
+ * _PG_init() - library load-time initialization
+ *
+ * DO NOT make this static nor change its name!
+ */
void
-plperl_init(void)
+_PG_init(void)
{
- if (!plperl_firstcall)
+ /* Be sure we do initialization only once (should be redundant now) */
+ static bool inited = false;
+
+ if (inited)
return;
- DefineCustomBoolVariable(
- "plperl.use_strict",
+ DefineCustomBoolVariable("plperl.use_strict",
"If true, will compile trusted and untrusted perl code in strict mode",
NULL,
&plperl_use_strict,
@@ -154,19 +157,8 @@ plperl_init(void)
EmitWarningsOnPlaceholders("plperl");
plperl_init_interp();
- plperl_firstcall = false;
-}
-
-
-/* Perform initialization during backend startup. */
-static void
-plperl_init_all(void)
-{
- if (plperl_firstcall)
- plperl_init();
-
- /* We don't need to do anything yet when a new backend starts. */
+ inited = true;
}
/* Each of these macros must represent a single string literal */
@@ -657,8 +649,6 @@ plperl_call_handler(PG_FUNCTION_ARGS)
Datum retval;
plperl_call_data *save_call_data;
- plperl_init_all();
-
save_call_data = current_call_data;
PG_TRY();
{
@@ -741,11 +731,7 @@ plperl_validator(PG_FUNCTION_ARGS)
/* Postpone body checks if !check_function_bodies */
if (check_function_bodies)
{
- plperl_proc_desc *prodesc;
-
- plperl_init_all();
-
- prodesc = compile_plperl_function(funcoid, istrigger);
+ (void) compile_plperl_function(funcoid, istrigger);
}
/* the result of a validator is ignored */
diff --git a/src/pl/plpgsql/src/pl_handler.c b/src/pl/plpgsql/src/pl_handler.c
index d8eb22f995e..22264f5a283 100644
--- a/src/pl/plpgsql/src/pl_handler.c
+++ b/src/pl/plpgsql/src/pl_handler.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_handler.c,v 1.29 2006/05/30 22:12:16 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_handler.c,v 1.30 2006/08/08 19:15:09 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -28,41 +28,25 @@ extern DLLIMPORT bool check_function_bodies;
PG_MODULE_MAGIC;
-static bool plpgsql_firstcall = true;
-
-static void plpgsql_init_all(void);
-
/*
- * plpgsql_init() - postmaster-startup safe initialization
+ * _PG_init() - library load-time initialization
*
- * DO NOT make this static --- it has to be callable by preload
+ * DO NOT make this static nor change its name!
*/
void
-plpgsql_init(void)
+_PG_init(void)
{
- /* Do initialization only once */
- if (!plpgsql_firstcall)
+ /* Be sure we do initialization only once (should be redundant now) */
+ static bool inited = false;
+
+ if (inited)
return;
plpgsql_HashTableInit();
RegisterXactCallback(plpgsql_xact_cb, NULL);
- plpgsql_firstcall = false;
-}
-
-/*
- * plpgsql_init_all() - Initialize all
- */
-static void
-plpgsql_init_all(void)
-{
- /* Execute any postmaster-startup safe initialization */
- plpgsql_init();
- /*
- * Any other initialization that must be done each time a new backend
- * starts -- currently none
- */
+ inited = true;
}
/* ----------
@@ -81,9 +65,6 @@ plpgsql_call_handler(PG_FUNCTION_ARGS)
Datum retval;
int rc;
- /* perform initialization */
- plpgsql_init_all();
-
/*
* Connect to SPI manager
*/
@@ -135,9 +116,6 @@ plpgsql_validator(PG_FUNCTION_ARGS)
bool istrigger = false;
int i;
- /* perform initialization */
- plpgsql_init_all();
-
/* Get the new function's pg_proc entry */
tuple = SearchSysCache(PROCOID,
ObjectIdGetDatum(funcoid),
diff --git a/src/pl/plpgsql/src/plpgsql.h b/src/pl/plpgsql/src/plpgsql.h
index 32d7fbaf696..268fc13821e 100644
--- a/src/pl/plpgsql/src/plpgsql.h
+++ b/src/pl/plpgsql/src/plpgsql.h
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.77 2006/07/11 17:26:59 momjian Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.78 2006/08/08 19:15:09 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -678,7 +678,7 @@ extern void plpgsql_compile_error_callback(void *arg);
* Functions in pl_handler.c
* ----------
*/
-extern void plpgsql_init(void);
+extern void _PG_init(void);
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
diff --git a/src/pl/plpython/plpython.c b/src/pl/plpython/plpython.c
index 164df875a5a..289ab2e7b73 100644
--- a/src/pl/plpython/plpython.c
+++ b/src/pl/plpython/plpython.c
@@ -1,7 +1,7 @@
/**********************************************************************
* plpython.c - python as a procedural language for PostgreSQL
*
- * $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.84 2006/07/06 01:55:51 momjian Exp $
+ * $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.85 2006/08/08 19:15:09 tgl Exp $
*
*********************************************************************
*/
@@ -155,11 +155,11 @@ typedef struct PLyResultObject
/* function declarations */
/* Two exported functions: first is the magic telling Postgresql
- * what function call interface it implements. Second allows
- * preinitialization of the interpreter during postmaster startup.
+ * what function call interface it implements. Second is for
+ * initialization of the interpreter during library load.
*/
Datum plpython_call_handler(PG_FUNCTION_ARGS);
-void plpython_init(void);
+void _PG_init(void);
PG_FUNCTION_INFO_V1(plpython_call_handler);
@@ -169,7 +169,6 @@ PG_FUNCTION_INFO_V1(plpython_call_handler);
* of plpython_call_handler. initialize the python interpreter
* and global data.
*/
-static void PLy_init_all(void);
static void PLy_init_interp(void);
static void PLy_init_plpy(void);
@@ -233,9 +232,6 @@ static PyObject *PLyLong_FromString(const char *);
static PyObject *PLyString_FromString(const char *);
-/* global data */
-static bool PLy_first_call = true;
-
/*
* Currently active plpython function
*/
@@ -301,8 +297,6 @@ plpython_call_handler(PG_FUNCTION_ARGS)
PLyProcedure *save_curr_proc;
PLyProcedure *volatile proc = NULL;
- PLy_init_all();
-
if (SPI_connect() != SPI_OK_CONNECT)
elog(ERROR, "could not connect to SPI manager");
@@ -2263,25 +2257,19 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
*/
/*
- * plpython_init() - Initialize everything that can be
- * safely initialized during postmaster
- * startup.
+ * _PG_init() - library load-time initialization
*
- * DO NOT make this static --- it has to be callable by preload
+ * DO NOT make this static nor change its name!
*/
void
-plpython_init(void)
+_PG_init(void)
{
- static volatile bool init_active = false;
+ /* Be sure we do initialization only once (should be redundant now) */
+ static bool inited = false;
- /* Do initialization only once */
- if (!PLy_first_call)
+ if (inited)
return;
- if (init_active)
- elog(FATAL, "initialization of language module failed");
- init_active = true;
-
Py_Initialize();
PLy_init_interp();
PLy_init_plpy();
@@ -2291,20 +2279,7 @@ plpython_init(void)
if (PLy_procedure_cache == NULL)
PLy_elog(ERROR, "could not create procedure cache");
- PLy_first_call = false;
-}
-
-static void
-PLy_init_all(void)
-{
- /* Execute postmaster-startup safe initialization */
- if (PLy_first_call)
- plpython_init();
-
- /*
- * Any other initialization that must be done each time a new backend
- * starts -- currently none
- */
+ inited = true;
}
static void
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index 75964b25684..54abd096722 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -2,7 +2,7 @@
* pltcl.c - PostgreSQL support for Tcl as
* procedural language (PL)
*
- * $PostgreSQL: pgsql/src/pl/tcl/pltcl.c,v 1.105 2006/06/16 18:42:24 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/tcl/pltcl.c,v 1.106 2006/08/08 19:15:09 tgl Exp $
*
**********************************************************************/
@@ -120,15 +120,14 @@ static pltcl_proc_desc *pltcl_current_prodesc = NULL;
/**********************************************************************
* Forward declarations
**********************************************************************/
+Datum pltcl_call_handler(PG_FUNCTION_ARGS);
+Datum pltclu_call_handler(PG_FUNCTION_ARGS);
+void _PG_init(void);
+
static void pltcl_init_all(void);
static void pltcl_init_interp(Tcl_Interp *interp);
-
static void pltcl_init_load_unknown(Tcl_Interp *interp);
-Datum pltcl_call_handler(PG_FUNCTION_ARGS);
-Datum pltclu_call_handler(PG_FUNCTION_ARGS);
-void pltcl_init(void);
-
static Datum pltcl_func_handler(PG_FUNCTION_ARGS);
static HeapTuple pltcl_trigger_handler(PG_FUNCTION_ARGS);
@@ -182,17 +181,15 @@ perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
fmgr_info_cxt(functionId, finfo, TopMemoryContext);
}
-/**********************************************************************
- * pltcl_init() - Initialize all that's safe to do in the postmaster
+/*
+ * _PG_init() - library load-time initialization
*
- * DO NOT make this static --- it has to be callable by preload
- **********************************************************************/
+ * DO NOT make this static nor change its name!
+ */
void
-pltcl_init(void)
+_PG_init(void)
{
- /************************************************************
- * Do initialization only once
- ************************************************************/
+ /* Be sure we do initialization only once (should be redundant now) */
if (pltcl_pm_init_done)
return;
@@ -236,20 +233,15 @@ pltcl_init(void)
/**********************************************************************
* pltcl_init_all() - Initialize all
+ *
+ * This does initialization that can't be done in the postmaster, and
+ * hence is not safe to do at library load time.
**********************************************************************/
static void
pltcl_init_all(void)
{
/************************************************************
- * Execute postmaster-startup safe initialization
- ************************************************************/
- if (!pltcl_pm_init_done)
- pltcl_init();
-
- /************************************************************
- * Any other initialization that must be done each time a new
- * backend starts:
- * - Try to load the unknown procedure from pltcl_modules
+ * Try to load the unknown procedure from pltcl_modules
************************************************************/
if (!pltcl_be_init_done)
{