1
1
/**********************************************************************
2
2
* plperl.c - perl as a procedural language for PostgreSQL
3
3
*
4
- * $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.150 2009/06/11 14:49:14 momjian Exp $
4
+ * $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.151 2009/09/16 06:06:12 petere Exp $
5
5
*
6
6
**********************************************************************/
7
7
@@ -162,6 +162,8 @@ static SV **hv_store_string(HV *hv, const char *key, SV *val);
162
162
static SV * * hv_fetch_string (HV * hv , const char * key );
163
163
static SV * plperl_create_sub (char * proname , char * s , bool trusted );
164
164
static SV * plperl_call_perl_func (plperl_proc_desc * desc , FunctionCallInfo fcinfo );
165
+ static void plperl_compile_callback (void * arg );
166
+ static void plperl_exec_callback (void * arg );
165
167
166
168
/*
167
169
* This routine is a crock, and so is everyplace that calls it. The problem
@@ -1019,9 +1021,7 @@ plperl_create_sub(char *proname, char *s, bool trusted)
1019
1021
LEAVE ;
1020
1022
ereport (ERROR ,
1021
1023
(errcode (ERRCODE_SYNTAX_ERROR ),
1022
- errmsg ("creation of Perl function \"%s\" failed: %s" ,
1023
- proname ,
1024
- strip_trailing_ws (SvPV (ERRSV , PL_na )))));
1024
+ errmsg ("%s" , strip_trailing_ws (SvPV (ERRSV , PL_na )))));
1025
1025
}
1026
1026
1027
1027
/*
@@ -1149,9 +1149,7 @@ plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
1149
1149
LEAVE ;
1150
1150
/* XXX need to find a way to assign an errcode here */
1151
1151
ereport (ERROR ,
1152
- (errmsg ("error from Perl function \"%s\": %s" ,
1153
- desc -> proname ,
1154
- strip_trailing_ws (SvPV (ERRSV , PL_na )))));
1152
+ (errmsg ("%s" , strip_trailing_ws (SvPV (ERRSV , PL_na )))));
1155
1153
}
1156
1154
1157
1155
retval = newSVsv (POPs );
@@ -1207,9 +1205,7 @@ plperl_call_perl_trigger_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo,
1207
1205
LEAVE ;
1208
1206
/* XXX need to find a way to assign an errcode here */
1209
1207
ereport (ERROR ,
1210
- (errmsg ("error from Perl function \"%s\": %s" ,
1211
- desc -> proname ,
1212
- strip_trailing_ws (SvPV (ERRSV , PL_na )))));
1208
+ (errmsg ("%s" , strip_trailing_ws (SvPV (ERRSV , PL_na )))));
1213
1209
}
1214
1210
1215
1211
retval = newSVsv (POPs );
@@ -1231,6 +1227,7 @@ plperl_func_handler(PG_FUNCTION_ARGS)
1231
1227
ReturnSetInfo * rsi ;
1232
1228
SV * array_ret = NULL ;
1233
1229
bool oldcontext = trusted_context ;
1230
+ ErrorContextCallback pl_error_context ;
1234
1231
1235
1232
/*
1236
1233
* Create the call_data beforing connecting to SPI, so that it is not
@@ -1245,6 +1242,12 @@ plperl_func_handler(PG_FUNCTION_ARGS)
1245
1242
prodesc = compile_plperl_function (fcinfo -> flinfo -> fn_oid , false);
1246
1243
current_call_data -> prodesc = prodesc ;
1247
1244
1245
+ /* Set a callback for error reporting */
1246
+ pl_error_context .callback = plperl_exec_callback ;
1247
+ pl_error_context .previous = error_context_stack ;
1248
+ pl_error_context .arg = prodesc -> proname ;
1249
+ error_context_stack = & pl_error_context ;
1250
+
1248
1251
rsi = (ReturnSetInfo * ) fcinfo -> resultinfo ;
1249
1252
1250
1253
if (prodesc -> fn_retisset )
@@ -1367,6 +1370,9 @@ plperl_func_handler(PG_FUNCTION_ARGS)
1367
1370
prodesc -> result_typioparam , -1 );
1368
1371
}
1369
1372
1373
+ /* Restore the previous error callback */
1374
+ error_context_stack = pl_error_context .previous ;
1375
+
1370
1376
if (array_ret == NULL )
1371
1377
SvREFCNT_dec (perlret );
1372
1378
@@ -1386,6 +1392,7 @@ plperl_trigger_handler(PG_FUNCTION_ARGS)
1386
1392
SV * svTD ;
1387
1393
HV * hvTD ;
1388
1394
bool oldcontext = trusted_context ;
1395
+ ErrorContextCallback pl_error_context ;
1389
1396
1390
1397
/*
1391
1398
* Create the call_data beforing connecting to SPI, so that it is not
@@ -1402,6 +1409,12 @@ plperl_trigger_handler(PG_FUNCTION_ARGS)
1402
1409
prodesc = compile_plperl_function (fcinfo -> flinfo -> fn_oid , true);
1403
1410
current_call_data -> prodesc = prodesc ;
1404
1411
1412
+ /* Set a callback for error reporting */
1413
+ pl_error_context .callback = plperl_exec_callback ;
1414
+ pl_error_context .previous = error_context_stack ;
1415
+ pl_error_context .arg = prodesc -> proname ;
1416
+ error_context_stack = & pl_error_context ;
1417
+
1405
1418
check_interp (prodesc -> lanpltrusted );
1406
1419
1407
1420
svTD = plperl_trigger_build_args (fcinfo );
@@ -1471,6 +1484,9 @@ plperl_trigger_handler(PG_FUNCTION_ARGS)
1471
1484
retval = PointerGetDatum (trv );
1472
1485
}
1473
1486
1487
+ /* Restore the previous error callback */
1488
+ error_context_stack = pl_error_context .previous ;
1489
+
1474
1490
SvREFCNT_dec (svTD );
1475
1491
if (perlret )
1476
1492
SvREFCNT_dec (perlret );
@@ -1492,6 +1508,7 @@ compile_plperl_function(Oid fn_oid, bool is_trigger)
1492
1508
plperl_proc_entry * hash_entry ;
1493
1509
bool found ;
1494
1510
bool oldcontext = trusted_context ;
1511
+ ErrorContextCallback plperl_error_context ;
1495
1512
1496
1513
/* We'll need the pg_proc tuple in any case... */
1497
1514
procTup = SearchSysCache (PROCOID ,
@@ -1501,6 +1518,12 @@ compile_plperl_function(Oid fn_oid, bool is_trigger)
1501
1518
elog (ERROR , "cache lookup failed for function %u" , fn_oid );
1502
1519
procStruct = (Form_pg_proc ) GETSTRUCT (procTup );
1503
1520
1521
+ /* Set a callback for reporting compilation errors */
1522
+ plperl_error_context .callback = plperl_compile_callback ;
1523
+ plperl_error_context .previous = error_context_stack ;
1524
+ plperl_error_context .arg = NameStr (procStruct -> proname );
1525
+ error_context_stack = & plperl_error_context ;
1526
+
1504
1527
/************************************************************
1505
1528
* Build our internal proc name from the function's Oid
1506
1529
************************************************************/
@@ -1731,6 +1754,9 @@ compile_plperl_function(Oid fn_oid, bool is_trigger)
1731
1754
hash_entry -> proc_data = prodesc ;
1732
1755
}
1733
1756
1757
+ /* restore previous error callback */
1758
+ error_context_stack = plperl_error_context .previous ;
1759
+
1734
1760
ReleaseSysCache (procTup );
1735
1761
1736
1762
return prodesc ;
@@ -2683,3 +2709,25 @@ hv_fetch_string(HV *hv, const char *key)
2683
2709
#endif
2684
2710
return hv_fetch (hv , key , klen , 0 );
2685
2711
}
2712
+
2713
+ /*
2714
+ * Provide function name for PL/Perl execution errors
2715
+ */
2716
+ static void
2717
+ plperl_exec_callback (void * arg )
2718
+ {
2719
+ char * procname = (char * ) arg ;
2720
+ if (procname )
2721
+ errcontext ("PL/Perl function \"%s\"" , procname );
2722
+ }
2723
+
2724
+ /*
2725
+ * Provide function name for PL/Perl compilation errors
2726
+ */
2727
+ static void
2728
+ plperl_compile_callback (void * arg )
2729
+ {
2730
+ char * procname = (char * ) arg ;
2731
+ if (procname )
2732
+ errcontext ("compilation of PL/Perl function \"%s\"" , procname );
2733
+ }
0 commit comments