33
33
* ENHANCEMENTS, OR MODIFICATIONS.
34
34
*
35
35
* IDENTIFICATION
36
- * $Header: /cvsroot/pgsql/src/pl/plperl/plperl.c,v 1.35 2002/09/21 18:39:26 tgl Exp $
36
+ * $Header: /cvsroot/pgsql/src/pl/plperl/plperl.c,v 1.36 2003/04/20 21:15:34 tgl Exp $
37
37
*
38
38
**********************************************************************/
39
39
@@ -92,8 +92,6 @@ typedef struct plperl_proc_desc
92
92
* Global data
93
93
**********************************************************************/
94
94
static int plperl_firstcall = 1 ;
95
- static int plperl_call_level = 0 ;
96
- static int plperl_restart_in_progress = 0 ;
97
95
static PerlInterpreter * plperl_interp = NULL ;
98
96
static HV * plperl_proc_hash = NULL ;
99
97
@@ -143,6 +141,15 @@ plperl_init_all(void)
143
141
if (!plperl_firstcall )
144
142
return ;
145
143
144
+ /************************************************************
145
+ * Free the proc hash table
146
+ ************************************************************/
147
+ if (plperl_proc_hash != NULL )
148
+ {
149
+ hv_undef (plperl_proc_hash );
150
+ SvREFCNT_dec ((SV * ) plperl_proc_hash );
151
+ plperl_proc_hash = NULL ;
152
+ }
146
153
147
154
/************************************************************
148
155
* Destroy the existing Perl interpreter
@@ -154,16 +161,6 @@ plperl_init_all(void)
154
161
plperl_interp = NULL ;
155
162
}
156
163
157
- /************************************************************
158
- * Free the proc hash table
159
- ************************************************************/
160
- if (plperl_proc_hash != NULL )
161
- {
162
- hv_undef (plperl_proc_hash );
163
- SvREFCNT_dec ((SV * ) plperl_proc_hash );
164
- plperl_proc_hash = NULL ;
165
- }
166
-
167
164
/************************************************************
168
165
* Now recreate a new Perl interpreter
169
166
************************************************************/
@@ -202,8 +199,6 @@ plperl_init_interp(void)
202
199
perl_parse (plperl_interp , plperl_init_shared_libs , 3 , embedding , NULL );
203
200
perl_run (plperl_interp );
204
201
205
-
206
-
207
202
/************************************************************
208
203
* Initialize the proc and query hash tables
209
204
************************************************************/
@@ -212,7 +207,6 @@ plperl_init_interp(void)
212
207
}
213
208
214
209
215
-
216
210
/**********************************************************************
217
211
* plperl_call_handler - This is the only visible function
218
212
* of the PL interpreter. The PostgreSQL
@@ -229,7 +223,7 @@ plperl_call_handler(PG_FUNCTION_ARGS)
229
223
Datum retval ;
230
224
231
225
/************************************************************
232
- * Initialize interpreters on first call
226
+ * Initialize interpreter on first call
233
227
************************************************************/
234
228
if (plperl_firstcall )
235
229
plperl_init_all ();
@@ -239,10 +233,6 @@ plperl_call_handler(PG_FUNCTION_ARGS)
239
233
************************************************************/
240
234
if (SPI_connect () != SPI_OK_CONNECT )
241
235
elog (ERROR , "plperl: cannot connect to SPI manager" );
242
- /************************************************************
243
- * Keep track about the nesting of Perl-SPI-Perl-... calls
244
- ************************************************************/
245
- plperl_call_level ++ ;
246
236
247
237
/************************************************************
248
238
* Determine if called as function or trigger and
@@ -261,8 +251,6 @@ plperl_call_handler(PG_FUNCTION_ARGS)
261
251
else
262
252
retval = plperl_func_handler (fcinfo );
263
253
264
- plperl_call_level -- ;
265
-
266
254
return retval ;
267
255
}
268
256
@@ -272,24 +260,35 @@ plperl_call_handler(PG_FUNCTION_ARGS)
272
260
* create the anonymous subroutine whose text is in the SV.
273
261
* Returns the SV containing the RV to the closure.
274
262
**********************************************************************/
275
- static
276
- SV *
263
+ static SV *
277
264
plperl_create_sub (char * s , bool trusted )
278
265
{
279
266
dSP ;
280
-
281
- SV * subref = NULL ;
267
+ SV * subref ;
282
268
int count ;
283
269
284
270
ENTER ;
285
271
SAVETMPS ;
286
272
PUSHMARK (SP );
287
273
XPUSHs (sv_2mortal (newSVpv (s , 0 )));
288
274
PUTBACK ;
275
+ /*
276
+ * G_KEEPERR seems to be needed here, else we don't recognize compile
277
+ * errors properly. Perhaps it's because there's another level of eval
278
+ * inside mksafefunc?
279
+ */
289
280
count = perl_call_pv ((trusted ? "mksafefunc" : "mkunsafefunc" ),
290
281
G_SCALAR | G_EVAL | G_KEEPERR );
291
282
SPAGAIN ;
292
283
284
+ if (count != 1 )
285
+ {
286
+ PUTBACK ;
287
+ FREETMPS ;
288
+ LEAVE ;
289
+ elog (ERROR , "plperl: didn't get a return item from mksafefunc" );
290
+ }
291
+
293
292
if (SvTRUE (ERRSV ))
294
293
{
295
294
POPs ;
@@ -299,9 +298,6 @@ plperl_create_sub(char *s, bool trusted)
299
298
elog (ERROR , "creation of function failed: %s" , SvPV (ERRSV , PL_na ));
300
299
}
301
300
302
- if (count != 1 )
303
- elog (ERROR , "creation of function failed - no return from mksafefunc" );
304
-
305
301
/*
306
302
* need to make a deep copy of the return. it comes off the stack as a
307
303
* temporary.
@@ -324,6 +320,7 @@ plperl_create_sub(char *s, bool trusted)
324
320
PUTBACK ;
325
321
FREETMPS ;
326
322
LEAVE ;
323
+
327
324
return subref ;
328
325
}
329
326
@@ -352,21 +349,18 @@ plperl_init_shared_libs(pTHX)
352
349
* plperl_call_perl_func() - calls a perl function through the RV
353
350
* stored in the prodesc structure. massages the input parms properly
354
351
**********************************************************************/
355
- static
356
- SV *
352
+ static SV *
357
353
plperl_call_perl_func (plperl_proc_desc * desc , FunctionCallInfo fcinfo )
358
354
{
359
355
dSP ;
360
-
361
356
SV * retval ;
362
357
int i ;
363
358
int count ;
364
359
365
-
366
360
ENTER ;
367
361
SAVETMPS ;
368
362
369
- PUSHMARK (sp );
363
+ PUSHMARK (SP );
370
364
for (i = 0 ; i < desc -> nargs ; i ++ )
371
365
{
372
366
if (desc -> arg_is_rel [i ])
@@ -401,7 +395,9 @@ plperl_call_perl_func(plperl_proc_desc * desc, FunctionCallInfo fcinfo)
401
395
}
402
396
}
403
397
PUTBACK ;
404
- count = perl_call_sv (desc -> reference , G_SCALAR | G_EVAL | G_KEEPERR );
398
+
399
+ /* Do NOT use G_KEEPERR here */
400
+ count = perl_call_sv (desc -> reference , G_SCALAR | G_EVAL );
405
401
406
402
SPAGAIN ;
407
403
@@ -424,16 +420,14 @@ plperl_call_perl_func(plperl_proc_desc * desc, FunctionCallInfo fcinfo)
424
420
425
421
retval = newSVsv (POPs );
426
422
427
-
428
423
PUTBACK ;
429
424
FREETMPS ;
430
425
LEAVE ;
431
426
432
427
return retval ;
433
-
434
-
435
428
}
436
429
430
+
437
431
/**********************************************************************
438
432
* plperl_func_handler() - Handler for regular function calls
439
433
**********************************************************************/
@@ -443,23 +437,10 @@ plperl_func_handler(PG_FUNCTION_ARGS)
443
437
plperl_proc_desc * prodesc ;
444
438
SV * perlret ;
445
439
Datum retval ;
446
- sigjmp_buf save_restart ;
447
440
448
441
/* Find or compile the function */
449
442
prodesc = compile_plperl_function (fcinfo -> flinfo -> fn_oid , false);
450
443
451
- /* Set up error handling */
452
- memcpy (& save_restart , & Warn_restart , sizeof (save_restart ));
453
-
454
- if (sigsetjmp (Warn_restart , 1 ) != 0 )
455
- {
456
- memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
457
- plperl_restart_in_progress = 1 ;
458
- if (-- plperl_call_level == 0 )
459
- plperl_restart_in_progress = 0 ;
460
- siglongjmp (Warn_restart , 1 );
461
- }
462
-
463
444
/************************************************************
464
445
* Call the Perl function
465
446
************************************************************/
@@ -490,14 +471,6 @@ plperl_func_handler(PG_FUNCTION_ARGS)
490
471
491
472
SvREFCNT_dec (perlret );
492
473
493
- memcpy (& Warn_restart , & save_restart , sizeof (Warn_restart ));
494
- if (plperl_restart_in_progress )
495
- {
496
- if (-- plperl_call_level == 0 )
497
- plperl_restart_in_progress = 0 ;
498
- siglongjmp (Warn_restart , 1 );
499
- }
500
-
501
474
return retval ;
502
475
}
503
476
@@ -734,7 +707,7 @@ compile_plperl_function(Oid fn_oid, bool is_trigger)
734
707
* plperl_build_tuple_argument() - Build a string for a ref to a hash
735
708
* from all attributes of a given tuple
736
709
**********************************************************************/
737
- static SV *
710
+ static SV *
738
711
plperl_build_tuple_argument (HeapTuple tuple , TupleDesc tupdesc )
739
712
{
740
713
int i ;
0 commit comments