Re: Feedback on writing extensible modules - Mailing list pgsql-hackers

From Dimitri Fontaine
Subject Re: Feedback on writing extensible modules
Date
Msg-id 8763fp70l1.fsf@hi-media-techno.com
Whole thread Raw
In response to Re: Feedback on writing extensible modules  (Dimitri Fontaine <dfontaine@hi-media.com>)
Responses Re: Feedback on writing extensible modules  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Dimitri Fontaine <dfontaine@hi-media.com> writes:
> And currently calling SPI_connect() from _PG_init will crash the
> backend. I'll try to obtain a gdb backtrace, I've just been told about
> pre_auth_delay and post_auth_delay parameters.

Here we go:

(gdb) handle SIGABRT nopass
Signal        Stop      Print   Pass to program Description
SIGABRT       Yes       Yes     No              Aborted
(gdb) continue
Program received signal SIGABRT, Aborted.
0xb802d424 in __kernel_vsyscall ()
(gdb) bt
#0  0xb802d424 in __kernel_vsyscall ()
#1  0xb7e7c640 in raise () from /lib/i686/cmov/libc.so.6
#2  0xb7e7dfa1 in abort () from /lib/i686/cmov/libc.so.6
#3  0x082dadde in ExceptionalCondition (conditionName=0x83cbfe0 "!(((context) != ((void *)0) &&
(((((Node*)((context)))->type)== T_AllocSetContext))))",  
    errorType=0x830bc09 "BadArgument", fileName=0x83be166 "mcxt.c", lineNumber=507) at assert.c:57
#4  0x082f8abb in MemoryContextAlloc (context=0x0, size=448) at mcxt.c:507
#5  0x081a93a3 in SPI_connect () at spi.c:81
#6  0xb582cf15 in _PG_init () at pre_prepare.c:150
#7  0x082df913 in internal_load_library (libname=0x9808da4 "/home/dim/pgsql/8.3/lib/plugins/pre_prepare.so") at
dfmgr.c:296
#8  0x082dfc38 in load_file (filename=0x9809d00 "$libdir/plugins/pre_prepare", restricted=1 '\001') at dfmgr.c:153
#9  0x082e7554 in load_libraries (libraries=<value optimized out>, gucname=0x9809d00 "$libdir/plugins/pre_prepare",
restricted=1'\001') at miscinit.c:1185 
#10 0x08233ce2 in PostgresMain (argc=4, argv=0x9807fb8, username=0x9807f90 "dim") at postgres.c:3314
#11 0x0820054c in ServerLoop () at postmaster.c:3207
#12 0x0820124b in PostmasterMain (argc=3, argv=0x97f1bd8) at postmaster.c:1029
#13 0x081b2b39 in main (argc=3, argv=0x97f1bd8) at main.c:188

And I'm runnin a CVS version of 8.3 I'm not sure is the last update in
the branch, so here's what I have at mcxt.c:507

504 void *
505 MemoryContextAlloc(MemoryContext context, Size size)
506 {
507    AssertArg(MemoryContextIsValid(context));
508
509    if (!AllocSizeIsValid(size))
510        elog(ERROR, "invalid memory alloc request size %lu",
511             (unsigned long) size);

That's with attached patch to pre_prepare.c from pgfoundry:
  http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/preprepare/preprepare/

If you need any more information from me, or for me to rerun with
another server version, please ask. I'm very interrested in being able
to prepare a query at local_preload_libraries time, if possible in 8.3
and following releases.

Regards,
--
dim

Index: pre_prepare.c
===================================================================
RCS file: /cvsroot/preprepare/preprepare/pre_prepare.c,v
retrieving revision 1.1
diff -p -u -r1.1 pre_prepare.c
--- pre_prepare.c    13 May 2009 20:54:04 -0000    1.1
+++ pre_prepare.c    25 May 2009 13:37:52 -0000
@@ -35,6 +35,7 @@

 PG_MODULE_MAGIC;

+static bool  pre_prepare_at_init  = NULL;
 static char *pre_prepare_relation = NULL;

 void _PG_init(void);
@@ -125,6 +126,15 @@ int pre_prepare_all() {
  */
 void
 _PG_init(void) {
+  DefineCustomBoolVariable("preprepare.at_init",
+               "Do we prepare the statements at backend init start",
+               "You have to setup local_preload_libraries too",
+               &pre_prepare_at_init,
+               PGC_USERSET,
+               NULL,
+               NULL);
+  EmitWarningsOnPlaceholders("prepare.at_init");
+
   DefineCustomStringVariable("preprepare.relation",
                  "Table name where to find statements to prepare",
                  "Can be schema qualified, must have columns \"name\" and \"statement\"",
@@ -132,8 +142,21 @@ _PG_init(void) {
                  PGC_USERSET,
                  NULL,
                  NULL);
-
   EmitWarningsOnPlaceholders("prepare.relation");
+
+  if( pre_prepare_at_init ) {
+    int err;
+
+    err = SPI_connect();
+    if (err != SPI_OK_CONNECT)
+      elog(ERROR, "SPI_connect: %s", SPI_result_code_string(err));
+
+    pre_prepare_all();
+
+    err = SPI_finish();
+    if (err != SPI_OK_FINISH)
+      elog(ERROR, "SPI_finish: %s", SPI_result_code_string(err));
+  }
 }

 /*

pgsql-hackers by date:

Previous
From: Dimitri Fontaine
Date:
Subject: Re: [PATCH] cleanup hashindex for pg_migrator hashindex compat mode (for 8.4)
Next
From: Tom Lane
Date:
Subject: Re: [PATCH] cleanup hashindex for pg_migrator hashindex compat mode (for 8.4)