PostgreSQL Source Code git master
Loading...
Searching...
No Matches
plperl.h File Reference
#include "mb/pg_wchar.h"
#include "plperl_system.h"
Include dependency graph for plperl.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

HVplperl_spi_exec (char *, int)
 
void plperl_return_next (SV *)
 
SVplperl_spi_query (char *)
 
SVplperl_spi_fetchrow (char *)
 
SVplperl_spi_prepare (char *, int, SV **)
 
HVplperl_spi_exec_prepared (char *, HV *, int, SV **)
 
SVplperl_spi_query_prepared (char *, int, SV **)
 
void plperl_spi_freeplan (char *)
 
void plperl_spi_cursor_close (char *)
 
void plperl_spi_commit (void)
 
void plperl_spi_rollback (void)
 
charplperl_sv_to_literal (SV *, char *)
 
void plperl_util_elog (int level, SV *msg)
 
static charutf_u2e (char *utf8_str, size_t len)
 
static charutf_e2u (const char *str)
 
static charsv2cstr (SV *sv)
 
static SVcstr2sv (const char *str)
 
static void croak_cstr (const char *str)
 

Function Documentation

◆ croak_cstr()

static void croak_cstr ( const char str)
inlinestatic

Definition at line 175 of file plperl.h.

176{
177 dTHX;
178
179#ifdef croak_sv
180 /* Use sv_2mortal() to be sure the transient SV gets freed */
182#else
183
184 /*
185 * The older way to do this is to assign a UTF8-marked value to ERRSV and
186 * then call croak(NULL). But if we leave it to croak() to append the
187 * error location, it does so too late (only after popping the stack) in
188 * some Perl versions. Hence, use mess() to create an SV with the error
189 * location info already appended.
190 */
191 SV *errsv = get_sv("@", GV_ADD);
192 char *utf8_str = utf_e2u(str);
193 SV *ssv;
194
195 ssv = mess("%s", utf8_str);
196 SvUTF8_on(ssv);
197
199
201
202 croak(NULL);
203#endif /* croak_sv */
204}
const char * str
void pfree(void *pointer)
Definition mcxt.c:1616
static char * utf_e2u(const char *str)
Definition plperl.h:70
static SV * cstr2sv(const char *str)
Definition plperl.h:147
#define get_sv
Definition ppport.h:12463
#define dTHX
Definition ppport.h:11306
#define croak_sv(sv)
Definition ppport.h:14714
static int fb(int x)

References croak_sv, cstr2sv(), dTHX, fb(), get_sv, pfree(), str, and utf_e2u().

Referenced by plperl_return_next(), plperl_spi_commit(), plperl_spi_exec(), plperl_spi_exec_prepared(), plperl_spi_fetchrow(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), plperl_spi_rollback(), and plperl_util_elog().

◆ cstr2sv()

static SV * cstr2sv ( const char str)
inlinestatic

◆ plperl_return_next()

void plperl_return_next ( SV sv)

Definition at line 3250 of file plperl.c.

3251{
3253
3255
3256 PG_TRY();
3257 {
3259 }
3260 PG_CATCH();
3261 {
3263
3264 /* Must reset elog.c's state */
3265 MemoryContextSwitchTo(oldcontext);
3266 edata = CopyErrorData();
3268
3269 /* Punt the error to Perl */
3270 croak_cstr(edata->message);
3271 }
3272 PG_END_TRY();
3273}
ErrorData * CopyErrorData(void)
Definition elog.c:1941
void FlushErrorState(void)
Definition elog.c:2062
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define PG_CATCH(...)
Definition elog.h:384
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static void check_spi_usage_allowed(void)
Definition plperl.c:3111
static void plperl_return_next_internal(SV *sv)
Definition plperl.c:3280
static void croak_cstr(const char *str)
Definition plperl.h:175

References check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, fb(), FlushErrorState(), MemoryContextSwitchTo(), PG_CATCH, PG_END_TRY, PG_TRY, and plperl_return_next_internal().

◆ plperl_spi_commit()

void plperl_spi_commit ( void  )

Definition at line 3996 of file plperl.c.

3997{
3999
4001
4002 PG_TRY();
4003 {
4004 SPI_commit();
4005 }
4006 PG_CATCH();
4007 {
4009
4010 /* Save error info */
4011 MemoryContextSwitchTo(oldcontext);
4012 edata = CopyErrorData();
4014
4015 /* Punt the error to Perl */
4016 croak_cstr(edata->message);
4017 }
4018 PG_END_TRY();
4019}
void SPI_commit(void)
Definition spi.c:321

References check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, fb(), FlushErrorState(), MemoryContextSwitchTo(), PG_CATCH, PG_END_TRY, PG_TRY, and SPI_commit().

◆ plperl_spi_cursor_close()

void plperl_spi_cursor_close ( char cursor)

Definition at line 3556 of file plperl.c.

3557{
3558 Portal p;
3559
3561
3563
3564 if (p)
3565 {
3566 UnpinPortal(p);
3568 }
3569}
void UnpinPortal(Portal portal)
Definition portalmem.c:381
Portal SPI_cursor_find(const char *name)
Definition spi.c:1795
void SPI_cursor_close(Portal portal)
Definition spi.c:1863
Definition type.h:138

References check_spi_usage_allowed(), SPI_cursor_close(), SPI_cursor_find(), and UnpinPortal().

◆ plperl_spi_exec()

HV * plperl_spi_exec ( char query,
int  limit 
)

Definition at line 3138 of file plperl.c.

3139{
3140 HV *ret_hv;
3141
3142 /*
3143 * Execute the query inside a sub-transaction, so we can cope with errors
3144 * sanely
3145 */
3148
3150
3152 /* Want to run inside function's memory context */
3153 MemoryContextSwitchTo(oldcontext);
3154
3155 PG_TRY();
3156 {
3157 int spi_rv;
3158
3159 pg_verifymbstr(query, strlen(query), false);
3160
3162 limit);
3164 spi_rv);
3165
3166 /* Commit the inner transaction, return to outer xact context */
3168 MemoryContextSwitchTo(oldcontext);
3169 CurrentResourceOwner = oldowner;
3170 }
3171 PG_CATCH();
3172 {
3174
3175 /* Save error info */
3176 MemoryContextSwitchTo(oldcontext);
3177 edata = CopyErrorData();
3179
3180 /* Abort the inner transaction */
3182 MemoryContextSwitchTo(oldcontext);
3183 CurrentResourceOwner = oldowner;
3184
3185 /* Punt the error to Perl */
3186 croak_cstr(edata->message);
3187
3188 /* Can't get here, but keep compiler quiet */
3189 return NULL;
3190 }
3191 PG_END_TRY();
3192
3193 return ret_hv;
3194}
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition mbutils.c:1682
static HV * plperl_spi_execute_fetch_result(SPITupleTable *, uint64, int)
Definition plperl.c:3198
static plperl_call_data * current_call_data
Definition plperl.c:244
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
uint64 SPI_processed
Definition spi.c:45
SPITupleTable * SPI_tuptable
Definition spi.c:46
int SPI_execute(const char *src, bool read_only, long tcount)
Definition spi.c:597
plperl_proc_desc * prodesc
Definition plperl.c:176
void BeginInternalSubTransaction(const char *name)
Definition xact.c:4745
void RollbackAndReleaseCurrentSubTransaction(void)
Definition xact.c:4847
void ReleaseCurrentSubTransaction(void)
Definition xact.c:4819

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), current_call_data, CurrentMemoryContext, CurrentResourceOwner, fb(), FlushErrorState(), plperl_proc_desc::fn_readonly, MemoryContextSwitchTo(), PG_CATCH, PG_END_TRY, PG_TRY, pg_verifymbstr(), plperl_spi_execute_fetch_result(), plperl_call_data::prodesc, ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_execute(), SPI_processed, and SPI_tuptable.

◆ plperl_spi_exec_prepared()

HV * plperl_spi_exec_prepared ( char query,
HV attr,
int  argc,
SV **  argv 
)

Definition at line 3720 of file plperl.c.

3721{
3722 HV *ret_hv;
3723 SV **sv;
3724 int i,
3725 limit,
3726 spi_rv;
3727 char *nulls;
3731
3732 /*
3733 * Execute the query inside a sub-transaction, so we can cope with errors
3734 * sanely
3735 */
3738
3740
3742 /* Want to run inside function's memory context */
3743 MemoryContextSwitchTo(oldcontext);
3744
3745 PG_TRY();
3746 {
3747 dTHX;
3748
3749 /************************************************************
3750 * Fetch the saved plan descriptor, see if it's o.k.
3751 ************************************************************/
3753 HASH_FIND, NULL);
3754 if (hash_entry == NULL)
3755 elog(ERROR, "spi_exec_prepared: Invalid prepared query passed");
3756
3757 qdesc = hash_entry->query_data;
3758 if (qdesc == NULL)
3759 elog(ERROR, "spi_exec_prepared: plperl query_hash value vanished");
3760
3761 if (qdesc->nargs != argc)
3762 elog(ERROR, "spi_exec_prepared: expected %d argument(s), %d passed",
3763 qdesc->nargs, argc);
3764
3765 /************************************************************
3766 * Parse eventual attributes
3767 ************************************************************/
3768 limit = 0;
3769 if (attr != NULL)
3770 {
3771 sv = hv_fetch_string(attr, "limit");
3772 if (sv && *sv && SvIOK(*sv))
3773 limit = SvIV(*sv);
3774 }
3775 /************************************************************
3776 * Set up arguments
3777 ************************************************************/
3778 if (argc > 0)
3779 {
3780 nulls = (char *) palloc(argc);
3781 argvalues = (Datum *) palloc(argc * sizeof(Datum));
3782 }
3783 else
3784 {
3785 nulls = NULL;
3786 argvalues = NULL;
3787 }
3788
3789 for (i = 0; i < argc; i++)
3790 {
3791 bool isnull;
3792
3794 qdesc->argtypes[i],
3795 -1,
3796 NULL,
3797 &qdesc->arginfuncs[i],
3798 qdesc->argtypioparams[i],
3799 &isnull);
3800 nulls[i] = isnull ? 'n' : ' ';
3801 }
3802
3803 /************************************************************
3804 * go
3805 ************************************************************/
3806 spi_rv = SPI_execute_plan(qdesc->plan, argvalues, nulls,
3809 spi_rv);
3810 if (argc > 0)
3811 {
3813 pfree(nulls);
3814 }
3815
3816 /* Commit the inner transaction, return to outer xact context */
3818 MemoryContextSwitchTo(oldcontext);
3819 CurrentResourceOwner = oldowner;
3820 }
3821 PG_CATCH();
3822 {
3824
3825 /* Save error info */
3826 MemoryContextSwitchTo(oldcontext);
3827 edata = CopyErrorData();
3829
3830 /* Abort the inner transaction */
3832 MemoryContextSwitchTo(oldcontext);
3833 CurrentResourceOwner = oldowner;
3834
3835 /* Punt the error to Perl */
3836 croak_cstr(edata->message);
3837
3838 /* Can't get here, but keep compiler quiet */
3839 return NULL;
3840 }
3841 PG_END_TRY();
3842
3843 return ret_hv;
3844}
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:889
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
@ HASH_FIND
Definition hsearch.h:108
int i
Definition isn.c:77
void * palloc(Size size)
Definition mcxt.c:1387
static plperl_interp_desc * plperl_active_interp
Definition plperl.c:228
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition plperl.c:1327
static SV ** hv_fetch_string(HV *hv, const char *key)
Definition plperl.c:4125
uint64_t Datum
Definition postgres.h:70
int SPI_execute_plan(SPIPlanPtr plan, const Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition spi.c:673
HTAB * query_hash
Definition plperl.c:90
Definition plperl.c:203

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), current_call_data, CurrentMemoryContext, CurrentResourceOwner, dTHX, elog, ERROR, fb(), FlushErrorState(), plperl_proc_desc::fn_readonly, HASH_FIND, hash_search(), hv_fetch_string(), i, MemoryContextSwitchTo(), palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, plperl_active_interp, plperl_spi_execute_fetch_result(), plperl_sv_to_datum(), plperl_call_data::prodesc, plperl_interp_desc::query_hash, ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_execute_plan(), SPI_processed, and SPI_tuptable.

◆ plperl_spi_fetchrow()

SV * plperl_spi_fetchrow ( char cursor)

Definition at line 3481 of file plperl.c.

3482{
3483 SV *row;
3484
3485 /*
3486 * Execute the FETCH inside a sub-transaction, so we can cope with errors
3487 * sanely
3488 */
3491
3493
3495 /* Want to run inside function's memory context */
3496 MemoryContextSwitchTo(oldcontext);
3497
3498 PG_TRY();
3499 {
3500 dTHX;
3502
3503 if (!p)
3504 {
3505 row = &PL_sv_undef;
3506 }
3507 else
3508 {
3509 SPI_cursor_fetch(p, true, 1);
3510 if (SPI_processed == 0)
3511 {
3512 UnpinPortal(p);
3514 row = &PL_sv_undef;
3515 }
3516 else
3517 {
3520 true);
3521 }
3523 }
3524
3525 /* Commit the inner transaction, return to outer xact context */
3527 MemoryContextSwitchTo(oldcontext);
3528 CurrentResourceOwner = oldowner;
3529 }
3530 PG_CATCH();
3531 {
3533
3534 /* Save error info */
3535 MemoryContextSwitchTo(oldcontext);
3536 edata = CopyErrorData();
3538
3539 /* Abort the inner transaction */
3541 MemoryContextSwitchTo(oldcontext);
3542 CurrentResourceOwner = oldowner;
3543
3544 /* Punt the error to Perl */
3545 croak_cstr(edata->message);
3546
3547 /* Can't get here, but keep compiler quiet */
3548 return NULL;
3549 }
3550 PG_END_TRY();
3551
3552 return row;
3553}
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc, bool include_generated)
Definition plperl.c:3028
#define PL_sv_undef
Definition ppport.h:11780
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition spi.c:1807
void SPI_freetuptable(SPITupleTable *tuptable)
Definition spi.c:1387
TupleDesc tupdesc
Definition spi.h:25
HeapTuple * vals
Definition spi.h:26

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, CurrentResourceOwner, dTHX, fb(), FlushErrorState(), MemoryContextSwitchTo(), PG_CATCH, PG_END_TRY, PG_TRY, PL_sv_undef, plperl_hash_from_tuple(), ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_cursor_close(), SPI_cursor_fetch(), SPI_cursor_find(), SPI_freetuptable(), SPI_processed, SPI_tuptable, SPITupleTable::tupdesc, UnpinPortal(), and SPITupleTable::vals.

◆ plperl_spi_freeplan()

void plperl_spi_freeplan ( char query)

Definition at line 3965 of file plperl.c.

3966{
3970
3972
3974 HASH_FIND, NULL);
3975 if (hash_entry == NULL)
3976 elog(ERROR, "spi_freeplan: Invalid prepared query passed");
3977
3978 qdesc = hash_entry->query_data;
3979 if (qdesc == NULL)
3980 elog(ERROR, "spi_freeplan: plperl query_hash value vanished");
3981 plan = qdesc->plan;
3982
3983 /*
3984 * free all memory before SPI_freeplan, so if it dies, nothing will be
3985 * left over
3986 */
3988 HASH_REMOVE, NULL);
3989
3990 MemoryContextDelete(qdesc->plan_cxt);
3991
3993}
@ HASH_REMOVE
Definition hsearch.h:110
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
#define plan(x)
Definition pg_regress.c:164
int SPI_freeplan(SPIPlanPtr plan)
Definition spi.c:1026

References check_spi_usage_allowed(), elog, ERROR, fb(), HASH_FIND, HASH_REMOVE, hash_search(), MemoryContextDelete(), plan, plperl_active_interp, plperl_interp_desc::query_hash, and SPI_freeplan().

◆ plperl_spi_prepare()

SV * plperl_spi_prepare ( char query,
int  argc,
SV **  argv 
)

Definition at line 3572 of file plperl.c.

3573{
3574 volatile SPIPlanPtr plan = NULL;
3575 volatile MemoryContext plan_cxt = NULL;
3576 plperl_query_desc *volatile qdesc = NULL;
3581 bool found;
3582 int i;
3583
3585
3587 MemoryContextSwitchTo(oldcontext);
3588
3589 PG_TRY();
3590 {
3592
3593 /************************************************************
3594 * Allocate the new querydesc structure
3595 *
3596 * The qdesc struct, as well as all its subsidiary data, lives in its
3597 * plan_cxt. But note that the SPIPlan does not.
3598 ************************************************************/
3600 "PL/Perl spi_prepare query",
3602 MemoryContextSwitchTo(plan_cxt);
3604 snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
3605 qdesc->plan_cxt = plan_cxt;
3606 qdesc->nargs = argc;
3607 qdesc->argtypes = (Oid *) palloc(argc * sizeof(Oid));
3608 qdesc->arginfuncs = (FmgrInfo *) palloc(argc * sizeof(FmgrInfo));
3609 qdesc->argtypioparams = (Oid *) palloc(argc * sizeof(Oid));
3610 MemoryContextSwitchTo(oldcontext);
3611
3612 /************************************************************
3613 * Do the following work in a short-lived context so that we don't
3614 * leak a lot of memory in the PL/Perl function's SPI Proc context.
3615 ************************************************************/
3617 "PL/Perl spi_prepare workspace",
3620
3621 /************************************************************
3622 * Resolve argument type names and then look them up by oid
3623 * in the system cache, and remember the required information
3624 * for input conversion.
3625 ************************************************************/
3626 for (i = 0; i < argc; i++)
3627 {
3628 Oid typId,
3629 typInput,
3630 typIOParam;
3631 int32 typmod;
3632 char *typstr;
3633
3634 typstr = sv2cstr(argv[i]);
3635 (void) parseTypeString(typstr, &typId, &typmod, NULL);
3636 pfree(typstr);
3637
3639
3640 qdesc->argtypes[i] = typId;
3641 fmgr_info_cxt(typInput, &(qdesc->arginfuncs[i]), plan_cxt);
3642 qdesc->argtypioparams[i] = typIOParam;
3643 }
3644
3645 /* Make sure the query is validly encoded */
3646 pg_verifymbstr(query, strlen(query), false);
3647
3648 /************************************************************
3649 * Prepare the plan and check for errors
3650 ************************************************************/
3651 plan = SPI_prepare(query, argc, qdesc->argtypes);
3652
3653 if (plan == NULL)
3654 elog(ERROR, "SPI_prepare() failed:%s",
3656
3657 /************************************************************
3658 * Save the plan into permanent memory (right now it's in the
3659 * SPI procCxt, which will go away at function end).
3660 ************************************************************/
3661 if (SPI_keepplan(plan))
3662 elog(ERROR, "SPI_keepplan() failed");
3663 qdesc->plan = plan;
3664
3665 /************************************************************
3666 * Insert a hashtable entry for the plan.
3667 ************************************************************/
3669 qdesc->qname,
3670 HASH_ENTER, &found);
3671 hash_entry->query_data = qdesc;
3672
3673 /* Get rid of workspace */
3675
3676 /* Commit the inner transaction, return to outer xact context */
3678 MemoryContextSwitchTo(oldcontext);
3679 CurrentResourceOwner = oldowner;
3680 }
3681 PG_CATCH();
3682 {
3684
3685 /* Save error info */
3686 MemoryContextSwitchTo(oldcontext);
3687 edata = CopyErrorData();
3689
3690 /* Drop anything we managed to allocate */
3691 if (hash_entry)
3693 qdesc->qname,
3694 HASH_REMOVE, NULL);
3695 if (plan_cxt)
3696 MemoryContextDelete(plan_cxt);
3697 if (plan)
3699
3700 /* Abort the inner transaction */
3702 MemoryContextSwitchTo(oldcontext);
3703 CurrentResourceOwner = oldowner;
3704
3705 /* Punt the error to Perl */
3706 croak_cstr(edata->message);
3707
3708 /* Can't get here, but keep compiler quiet */
3709 return NULL;
3710 }
3711 PG_END_TRY();
3712
3713 /************************************************************
3714 * Return the query's hash key to the caller.
3715 ************************************************************/
3716 return cstr2sv(qdesc->qname);
3717}
int32_t int32
Definition c.h:620
#define palloc0_object(type)
Definition fe_memutils.h:75
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition fmgr.c:139
@ HASH_ENTER
Definition hsearch.h:109
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition lsyscache.c:3096
MemoryContext TopMemoryContext
Definition mcxt.c:166
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define ALLOCSET_SMALL_SIZES
Definition memutils.h:170
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
bool parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p, Node *escontext)
Definition parse_type.c:785
static char * sv2cstr(SV *sv)
Definition plperl.h:89
#define snprintf
Definition port.h:260
unsigned int Oid
const char * SPI_result_code_string(int code)
Definition spi.c:1973
int SPI_result
Definition spi.c:47
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition spi.c:861
int SPI_keepplan(SPIPlanPtr plan)
Definition spi.c:977

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, BeginInternalSubTransaction(), CHECK_FOR_INTERRUPTS, check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), cstr2sv(), CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, fb(), FlushErrorState(), fmgr_info_cxt(), getTypeInputInfo(), HASH_ENTER, HASH_REMOVE, hash_search(), i, MemoryContextDelete(), MemoryContextSwitchTo(), palloc(), palloc0_object, parseTypeString(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, pg_verifymbstr(), plan, plperl_active_interp, plperl_interp_desc::query_hash, ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), snprintf, SPI_freeplan(), SPI_keepplan(), SPI_prepare(), SPI_result, SPI_result_code_string(), sv2cstr(), and TopMemoryContext.

◆ plperl_spi_query()

SV * plperl_spi_query ( char query)

Definition at line 3409 of file plperl.c.

3410{
3411 SV *cursor;
3412
3413 /*
3414 * Execute the query inside a sub-transaction, so we can cope with errors
3415 * sanely
3416 */
3419
3421
3423 /* Want to run inside function's memory context */
3424 MemoryContextSwitchTo(oldcontext);
3425
3426 PG_TRY();
3427 {
3429 Portal portal;
3430
3431 /* Make sure the query is validly encoded */
3432 pg_verifymbstr(query, strlen(query), false);
3433
3434 /* Create a cursor for the query */
3435 plan = SPI_prepare(query, 0, NULL);
3436 if (plan == NULL)
3437 elog(ERROR, "SPI_prepare() failed:%s",
3439
3440 portal = SPI_cursor_open(NULL, plan, NULL, NULL, false);
3442 if (portal == NULL)
3443 elog(ERROR, "SPI_cursor_open() failed:%s",
3445 cursor = cstr2sv(portal->name);
3446
3447 PinPortal(portal);
3448
3449 /* Commit the inner transaction, return to outer xact context */
3451 MemoryContextSwitchTo(oldcontext);
3452 CurrentResourceOwner = oldowner;
3453 }
3454 PG_CATCH();
3455 {
3457
3458 /* Save error info */
3459 MemoryContextSwitchTo(oldcontext);
3460 edata = CopyErrorData();
3462
3463 /* Abort the inner transaction */
3465 MemoryContextSwitchTo(oldcontext);
3466 CurrentResourceOwner = oldowner;
3467
3468 /* Punt the error to Perl */
3469 croak_cstr(edata->message);
3470
3471 /* Can't get here, but keep compiler quiet */
3472 return NULL;
3473 }
3474 PG_END_TRY();
3475
3476 return cursor;
3477}
void PinPortal(Portal portal)
Definition portalmem.c:372
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, const Datum *Values, const char *Nulls, bool read_only)
Definition spi.c:1446
const char * name
Definition portal.h:118

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), cstr2sv(), CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, fb(), FlushErrorState(), MemoryContextSwitchTo(), PortalData::name, PG_CATCH, PG_END_TRY, PG_TRY, pg_verifymbstr(), PinPortal(), plan, ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_cursor_open(), SPI_freeplan(), SPI_prepare(), SPI_result, and SPI_result_code_string().

◆ plperl_spi_query_prepared()

SV * plperl_spi_query_prepared ( char query,
int  argc,
SV **  argv 
)

Definition at line 3847 of file plperl.c.

3848{
3849 int i;
3850 char *nulls;
3854 SV *cursor;
3855 Portal portal = NULL;
3856
3857 /*
3858 * Execute the query inside a sub-transaction, so we can cope with errors
3859 * sanely
3860 */
3863
3865
3867 /* Want to run inside function's memory context */
3868 MemoryContextSwitchTo(oldcontext);
3869
3870 PG_TRY();
3871 {
3872 /************************************************************
3873 * Fetch the saved plan descriptor, see if it's o.k.
3874 ************************************************************/
3876 HASH_FIND, NULL);
3877 if (hash_entry == NULL)
3878 elog(ERROR, "spi_query_prepared: Invalid prepared query passed");
3879
3880 qdesc = hash_entry->query_data;
3881 if (qdesc == NULL)
3882 elog(ERROR, "spi_query_prepared: plperl query_hash value vanished");
3883
3884 if (qdesc->nargs != argc)
3885 elog(ERROR, "spi_query_prepared: expected %d argument(s), %d passed",
3886 qdesc->nargs, argc);
3887
3888 /************************************************************
3889 * Set up arguments
3890 ************************************************************/
3891 if (argc > 0)
3892 {
3893 nulls = (char *) palloc(argc);
3894 argvalues = (Datum *) palloc(argc * sizeof(Datum));
3895 }
3896 else
3897 {
3898 nulls = NULL;
3899 argvalues = NULL;
3900 }
3901
3902 for (i = 0; i < argc; i++)
3903 {
3904 bool isnull;
3905
3907 qdesc->argtypes[i],
3908 -1,
3909 NULL,
3910 &qdesc->arginfuncs[i],
3911 qdesc->argtypioparams[i],
3912 &isnull);
3913 nulls[i] = isnull ? 'n' : ' ';
3914 }
3915
3916 /************************************************************
3917 * go
3918 ************************************************************/
3919 portal = SPI_cursor_open(NULL, qdesc->plan, argvalues, nulls,
3921 if (argc > 0)
3922 {
3924 pfree(nulls);
3925 }
3926 if (portal == NULL)
3927 elog(ERROR, "SPI_cursor_open() failed:%s",
3929
3930 cursor = cstr2sv(portal->name);
3931
3932 PinPortal(portal);
3933
3934 /* Commit the inner transaction, return to outer xact context */
3936 MemoryContextSwitchTo(oldcontext);
3937 CurrentResourceOwner = oldowner;
3938 }
3939 PG_CATCH();
3940 {
3942
3943 /* Save error info */
3944 MemoryContextSwitchTo(oldcontext);
3945 edata = CopyErrorData();
3947
3948 /* Abort the inner transaction */
3950 MemoryContextSwitchTo(oldcontext);
3951 CurrentResourceOwner = oldowner;
3952
3953 /* Punt the error to Perl */
3954 croak_cstr(edata->message);
3955
3956 /* Can't get here, but keep compiler quiet */
3957 return NULL;
3958 }
3959 PG_END_TRY();
3960
3961 return cursor;
3962}

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), cstr2sv(), current_call_data, CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, fb(), FlushErrorState(), plperl_proc_desc::fn_readonly, HASH_FIND, hash_search(), i, MemoryContextSwitchTo(), PortalData::name, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, PinPortal(), plperl_active_interp, plperl_sv_to_datum(), plperl_call_data::prodesc, plperl_interp_desc::query_hash, ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_cursor_open(), SPI_result, and SPI_result_code_string().

◆ plperl_spi_rollback()

void plperl_spi_rollback ( void  )

Definition at line 4022 of file plperl.c.

4023{
4025
4027
4028 PG_TRY();
4029 {
4030 SPI_rollback();
4031 }
4032 PG_CATCH();
4033 {
4035
4036 /* Save error info */
4037 MemoryContextSwitchTo(oldcontext);
4038 edata = CopyErrorData();
4040
4041 /* Punt the error to Perl */
4042 croak_cstr(edata->message);
4043 }
4044 PG_END_TRY();
4045}
void SPI_rollback(void)
Definition spi.c:414

References check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, fb(), FlushErrorState(), MemoryContextSwitchTo(), PG_CATCH, PG_END_TRY, PG_TRY, and SPI_rollback().

◆ plperl_sv_to_literal()

char * plperl_sv_to_literal ( SV sv,
char fqtypename 
)

Definition at line 1448 of file plperl.c.

1449{
1450 Oid typid;
1451 Oid typoutput;
1452 Datum datum;
1453 bool typisvarlena,
1454 isnull;
1455
1457
1459 if (!OidIsValid(typid))
1460 ereport(ERROR,
1462 errmsg("lookup failed for type %s", fqtypename)));
1463
1464 datum = plperl_sv_to_datum(sv,
1465 typid, -1,
1467 &isnull);
1468
1469 if (isnull)
1470 return NULL;
1471
1472 getTypeOutputInfo(typid,
1473 &typoutput, &typisvarlena);
1474
1475 return OidOutputFunctionCall(typoutput, datum);
1476}
#define OidIsValid(objectId)
Definition c.h:858
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereport(elevel,...)
Definition elog.h:152
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition fmgr.c:1764
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3129
static char * errmsg
static Oid DatumGetObjectId(Datum X)
Definition postgres.h:242
static Datum CStringGetDatum(const char *X)
Definition postgres.h:370
#define InvalidOid
Datum regtypein(PG_FUNCTION_ARGS)
Definition regproc.c:1184

References check_spi_usage_allowed(), CStringGetDatum(), DatumGetObjectId(), DirectFunctionCall1, ereport, errcode(), errmsg, ERROR, fb(), getTypeOutputInfo(), InvalidOid, OidIsValid, OidOutputFunctionCall(), plperl_sv_to_datum(), and regtypein().

◆ plperl_util_elog()

void plperl_util_elog ( int  level,
SV msg 
)

Definition at line 4059 of file plperl.c.

4060{
4062 char *volatile cmsg = NULL;
4063
4064 /*
4065 * We intentionally omit check_spi_usage_allowed() here, as this seems
4066 * safe to allow even in the contexts that that function rejects.
4067 */
4068
4069 PG_TRY();
4070 {
4071 cmsg = sv2cstr(msg);
4072 elog(level, "%s", cmsg);
4073 pfree(cmsg);
4074 }
4075 PG_CATCH();
4076 {
4078
4079 /* Must reset elog.c's state */
4080 MemoryContextSwitchTo(oldcontext);
4081 edata = CopyErrorData();
4083
4084 if (cmsg)
4085 pfree(cmsg);
4086
4087 /* Punt the error to Perl */
4088 croak_cstr(edata->message);
4089 }
4090 PG_END_TRY();
4091}

References CopyErrorData(), croak_cstr(), CurrentMemoryContext, elog, fb(), FlushErrorState(), MemoryContextSwitchTo(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, and sv2cstr().

◆ sv2cstr()

static char * sv2cstr ( SV sv)
inlinestatic

Definition at line 89 of file plperl.h.

90{
91 dTHX;
92 char *val,
93 *res;
94 STRLEN len;
95
96 /*
97 * get a utf8 encoded char * out of perl. *note* it may not be valid utf8!
98 */
99
100 /*
101 * SvPVutf8() croaks nastily on certain things, like typeglobs and
102 * readonly objects such as $^V. That's a perl bug - it's not supposed to
103 * happen. To avoid crashing the backend, we make a copy of the sv before
104 * passing it to SvPVutf8(). The copy is garbage collected when we're done
105 * with it.
106 */
107 if (SvREADONLY(sv) ||
108 isGV_with_GP(sv) ||
109 (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM))
110 sv = newSVsv(sv);
111 else
112 {
113 /*
114 * increase the reference count so we can just SvREFCNT_dec() it when
115 * we are done
116 */
118 }
119
120 /*
121 * Request the string from Perl, in UTF-8 encoding; but if we're in a
122 * SQL_ASCII database, just request the byte soup without trying to make
123 * it UTF8, because that might fail.
124 */
126 val = SvPV(sv, len);
127 else
128 val = SvPVutf8(sv, len);
129
130 /*
131 * Now convert to database encoding. We use perl's length in the event we
132 * had an embedded null byte to ensure we error out properly.
133 */
134 res = utf_u2e(val, len);
135
136 /* safe now to garbage collect the new SV */
138
139 return res;
140}
long val
Definition informix.c:689
const void size_t len
static char * utf_u2e(char *utf8_str, size_t len)
Definition plperl.h:51
#define SvREFCNT_inc_simple_void(sv)
Definition ppport.h:15412
#define isGV_with_GP(gv)
Definition ppport.h:15703

References dTHX, fb(), GetDatabaseEncoding(), isGV_with_GP, len, PG_SQL_ASCII, SvREFCNT_inc_simple_void, utf_u2e(), and val.

Referenced by hek2cstr(), plperl_call_perl_event_trigger_func(), plperl_call_perl_func(), plperl_call_perl_trigger_func(), plperl_create_sub(), plperl_init_interp(), plperl_spi_prepare(), plperl_sv_to_datum(), plperl_to_hstore(), plperl_trigger_handler(), plperl_trusted_init(), plperl_untrusted_init(), plperl_util_elog(), select_perl_context(), and SV_to_JsonbValue().

◆ utf_e2u()

static char * utf_e2u ( const char str)
inlinestatic

Definition at line 70 of file plperl.h.

71{
72 char *ret;
73
75
76 /* ensure we have a copy even if no conversion happened */
77 if (ret == str)
78 ret = pstrdup(ret);
79
80 return ret;
81}
#define PG_UTF8
Definition mbprint.c:43
char * pg_server_to_any(const char *s, int len, int encoding)
Definition mbutils.c:760
char * pstrdup(const char *in)
Definition mcxt.c:1781

References fb(), pg_server_to_any(), PG_UTF8, pstrdup(), and str.

Referenced by croak_cstr(), and cstr2sv().

◆ utf_u2e()

static char * utf_u2e ( char utf8_str,
size_t  len 
)
inlinestatic

Definition at line 51 of file plperl.h.

52{
53 char *ret;
54
56
57 /* ensure we have a copy even if no conversion happened */
58 if (ret == utf8_str)
59 ret = pstrdup(ret);
60
61 return ret;
62}
char * pg_any_to_server(const char *s, int len, int encoding)
Definition mbutils.c:687

References fb(), len, pg_any_to_server(), PG_UTF8, and pstrdup().

Referenced by sv2cstr().