PostgreSQL Source Code git master
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

HV * plperl_spi_exec (char *, int)
 
void plperl_return_next (SV *)
 
SV * plperl_spi_query (char *)
 
SV * plperl_spi_fetchrow (char *)
 
SV * plperl_spi_prepare (char *, int, SV **)
 
HV * plperl_spi_exec_prepared (char *, HV *, int, SV **)
 
SV * plperl_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)
 
char * plperl_sv_to_literal (SV *, char *)
 
void plperl_util_elog (int level, SV *msg)
 
static char * utf_u2e (char *utf8_str, size_t len)
 
static char * utf_e2u (const char *str)
 
static char * sv2cstr (SV *sv)
 
static SV * cstr2sv (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 */
181 croak_sv(sv_2mortal(cstr2sv(str)));
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
198 pfree(utf8_str);
199
200 sv_setsv(errsv, ssv);
201
202 croak(NULL);
203#endif /* croak_sv */
204}
const char * str
void pfree(void *pointer)
Definition: mcxt.c:1524
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

References croak_sv, cstr2sv(), dTHX, 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

Definition at line 147 of file plperl.h.

148{
149 dTHX;
150 SV *sv;
151 char *utf8_str;
152
153 /* no conversion when SQL_ASCII */
155 return newSVpv(str, 0);
156
157 utf8_str = utf_e2u(str);
158
159 sv = newSVpv(utf8_str, 0);
160 SvUTF8_on(sv);
161 pfree(utf8_str);
162
163 return sv;
164}
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
@ PG_SQL_ASCII
Definition: pg_wchar.h:226

References dTHX, GetDatabaseEncoding(), pfree(), PG_SQL_ASCII, str, and utf_e2u().

Referenced by croak_cstr(), hstore_to_plperl(), JsonbValue_to_SV(), make_array_ref(), plperl_call_perl_func(), plperl_call_perl_trigger_func(), plperl_create_sub(), plperl_event_trigger_build_args(), plperl_hash_from_tuple(), plperl_spi_execute_fetch_result(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), and plperl_trigger_build_args().

◆ plperl_return_next()

void plperl_return_next ( SV *  sv)

Definition at line 3243 of file plperl.c.

3244{
3246
3248
3249 PG_TRY();
3250 {
3252 }
3253 PG_CATCH();
3254 {
3255 ErrorData *edata;
3256
3257 /* Must reset elog.c's state */
3258 MemoryContextSwitchTo(oldcontext);
3259 edata = CopyErrorData();
3261
3262 /* Punt the error to Perl */
3263 croak_cstr(edata->message);
3264 }
3265 PG_END_TRY();
3266}
ErrorData * CopyErrorData(void)
Definition: elog.c:1746
void FlushErrorState(void)
Definition: elog.c:1867
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define PG_CATCH(...)
Definition: elog.h:381
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static void check_spi_usage_allowed(void)
Definition: plperl.c:3104
static void plperl_return_next_internal(SV *sv)
Definition: plperl.c:3273
static void croak_cstr(const char *str)
Definition: plperl.h:175
char * message
Definition: elog.h:431

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

◆ plperl_spi_commit()

void plperl_spi_commit ( void  )

Definition at line 3989 of file plperl.c.

3990{
3992
3994
3995 PG_TRY();
3996 {
3997 SPI_commit();
3998 }
3999 PG_CATCH();
4000 {
4001 ErrorData *edata;
4002
4003 /* Save error info */
4004 MemoryContextSwitchTo(oldcontext);
4005 edata = CopyErrorData();
4007
4008 /* Punt the error to Perl */
4009 croak_cstr(edata->message);
4010 }
4011 PG_END_TRY();
4012}
void SPI_commit(void)
Definition: spi.c:321

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

◆ plperl_spi_cursor_close()

void plperl_spi_cursor_close ( char *  cursor)

Definition at line 3549 of file plperl.c.

3550{
3551 Portal p;
3552
3554
3556
3557 if (p)
3558 {
3559 UnpinPortal(p);
3561 }
3562}
void UnpinPortal(Portal portal)
Definition: portalmem.c:382
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1796
void SPI_cursor_close(Portal portal)
Definition: spi.c:1864
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 3131 of file plperl.c.

3132{
3133 HV *ret_hv;
3134
3135 /*
3136 * Execute the query inside a sub-transaction, so we can cope with errors
3137 * sanely
3138 */
3141
3143
3145 /* Want to run inside function's memory context */
3146 MemoryContextSwitchTo(oldcontext);
3147
3148 PG_TRY();
3149 {
3150 int spi_rv;
3151
3152 pg_verifymbstr(query, strlen(query), false);
3153
3155 limit);
3157 spi_rv);
3158
3159 /* Commit the inner transaction, return to outer xact context */
3161 MemoryContextSwitchTo(oldcontext);
3162 CurrentResourceOwner = oldowner;
3163 }
3164 PG_CATCH();
3165 {
3166 ErrorData *edata;
3167
3168 /* Save error info */
3169 MemoryContextSwitchTo(oldcontext);
3170 edata = CopyErrorData();
3172
3173 /* Abort the inner transaction */
3175 MemoryContextSwitchTo(oldcontext);
3176 CurrentResourceOwner = oldowner;
3177
3178 /* Punt the error to Perl */
3179 croak_cstr(edata->message);
3180
3181 /* Can't get here, but keep compiler quiet */
3182 return NULL;
3183 }
3184 PG_END_TRY();
3185
3186 return ret_hv;
3187}
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1556
static HV * plperl_spi_execute_fetch_result(SPITupleTable *, uint64, int)
Definition: plperl.c:3191
static plperl_call_data * current_call_data
Definition: plperl.c:240
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
uint64 SPI_processed
Definition: spi.c:44
SPITupleTable * SPI_tuptable
Definition: spi.c:45
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:597
plperl_proc_desc * prodesc
Definition: plperl.c:172
bool fn_readonly
Definition: plperl.c:108
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4694
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4796
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4768

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), current_call_data, CurrentMemoryContext, CurrentResourceOwner, FlushErrorState(), plperl_proc_desc::fn_readonly, MemoryContextSwitchTo(), ErrorData::message, 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 3713 of file plperl.c.

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

References plperl_query_desc::arginfuncs, plperl_query_desc::argtypes, plperl_query_desc::argtypioparams, BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), current_call_data, CurrentMemoryContext, CurrentResourceOwner, dTHX, elog, ERROR, FlushErrorState(), plperl_proc_desc::fn_readonly, HASH_FIND, hash_search(), hv_fetch_string(), i, MemoryContextSwitchTo(), ErrorData::message, plperl_query_desc::nargs, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, plperl_query_desc::plan, plperl_active_interp, plperl_spi_execute_fetch_result(), plperl_sv_to_datum(), plperl_call_data::prodesc, plperl_query_entry::query_data, 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 3474 of file plperl.c.

3475{
3476 SV *row;
3477
3478 /*
3479 * Execute the FETCH inside a sub-transaction, so we can cope with errors
3480 * sanely
3481 */
3484
3486
3488 /* Want to run inside function's memory context */
3489 MemoryContextSwitchTo(oldcontext);
3490
3491 PG_TRY();
3492 {
3493 dTHX;
3495
3496 if (!p)
3497 {
3498 row = &PL_sv_undef;
3499 }
3500 else
3501 {
3502 SPI_cursor_fetch(p, true, 1);
3503 if (SPI_processed == 0)
3504 {
3505 UnpinPortal(p);
3507 row = &PL_sv_undef;
3508 }
3509 else
3510 {
3513 true);
3514 }
3516 }
3517
3518 /* Commit the inner transaction, return to outer xact context */
3520 MemoryContextSwitchTo(oldcontext);
3521 CurrentResourceOwner = oldowner;
3522 }
3523 PG_CATCH();
3524 {
3525 ErrorData *edata;
3526
3527 /* Save error info */
3528 MemoryContextSwitchTo(oldcontext);
3529 edata = CopyErrorData();
3531
3532 /* Abort the inner transaction */
3534 MemoryContextSwitchTo(oldcontext);
3535 CurrentResourceOwner = oldowner;
3536
3537 /* Punt the error to Perl */
3538 croak_cstr(edata->message);
3539
3540 /* Can't get here, but keep compiler quiet */
3541 return NULL;
3542 }
3543 PG_END_TRY();
3544
3545 return row;
3546}
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc, bool include_generated)
Definition: plperl.c:3021
#define PL_sv_undef
Definition: ppport.h:11780
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1808
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, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, 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 3958 of file plperl.c.

3959{
3961 plperl_query_desc *qdesc;
3962 plperl_query_entry *hash_entry;
3963
3965
3966 hash_entry = hash_search(plperl_active_interp->query_hash, query,
3967 HASH_FIND, NULL);
3968 if (hash_entry == NULL)
3969 elog(ERROR, "spi_freeplan: Invalid prepared query passed");
3970
3971 qdesc = hash_entry->query_data;
3972 if (qdesc == NULL)
3973 elog(ERROR, "spi_freeplan: plperl query_hash value vanished");
3974 plan = qdesc->plan;
3975
3976 /*
3977 * free all memory before SPI_freeplan, so if it dies, nothing will be
3978 * left over
3979 */
3981 HASH_REMOVE, NULL);
3982
3984
3986}
@ HASH_REMOVE
Definition: hsearch.h:115
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define plan(x)
Definition: pg_regress.c:161
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:1026
MemoryContext plan_cxt
Definition: plperl.c:188

References check_spi_usage_allowed(), elog, ERROR, HASH_FIND, HASH_REMOVE, hash_search(), MemoryContextDelete(), plperl_query_desc::plan, plan, plperl_query_desc::plan_cxt, plperl_active_interp, plperl_query_entry::query_data, plperl_interp_desc::query_hash, and SPI_freeplan().

◆ plperl_spi_prepare()

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

Definition at line 3565 of file plperl.c.

3566{
3567 volatile SPIPlanPtr plan = NULL;
3568 volatile MemoryContext plan_cxt = NULL;
3569 plperl_query_desc *volatile qdesc = NULL;
3570 plperl_query_entry *volatile hash_entry = NULL;
3573 MemoryContext work_cxt;
3574 bool found;
3575 int i;
3576
3578
3580 MemoryContextSwitchTo(oldcontext);
3581
3582 PG_TRY();
3583 {
3585
3586 /************************************************************
3587 * Allocate the new querydesc structure
3588 *
3589 * The qdesc struct, as well as all its subsidiary data, lives in its
3590 * plan_cxt. But note that the SPIPlan does not.
3591 ************************************************************/
3593 "PL/Perl spi_prepare query",
3595 MemoryContextSwitchTo(plan_cxt);
3596 qdesc = (plperl_query_desc *) palloc0(sizeof(plperl_query_desc));
3597 snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
3598 qdesc->plan_cxt = plan_cxt;
3599 qdesc->nargs = argc;
3600 qdesc->argtypes = (Oid *) palloc(argc * sizeof(Oid));
3601 qdesc->arginfuncs = (FmgrInfo *) palloc(argc * sizeof(FmgrInfo));
3602 qdesc->argtypioparams = (Oid *) palloc(argc * sizeof(Oid));
3603 MemoryContextSwitchTo(oldcontext);
3604
3605 /************************************************************
3606 * Do the following work in a short-lived context so that we don't
3607 * leak a lot of memory in the PL/Perl function's SPI Proc context.
3608 ************************************************************/
3610 "PL/Perl spi_prepare workspace",
3612 MemoryContextSwitchTo(work_cxt);
3613
3614 /************************************************************
3615 * Resolve argument type names and then look them up by oid
3616 * in the system cache, and remember the required information
3617 * for input conversion.
3618 ************************************************************/
3619 for (i = 0; i < argc; i++)
3620 {
3621 Oid typId,
3622 typInput,
3623 typIOParam;
3624 int32 typmod;
3625 char *typstr;
3626
3627 typstr = sv2cstr(argv[i]);
3628 (void) parseTypeString(typstr, &typId, &typmod, NULL);
3629 pfree(typstr);
3630
3631 getTypeInputInfo(typId, &typInput, &typIOParam);
3632
3633 qdesc->argtypes[i] = typId;
3634 fmgr_info_cxt(typInput, &(qdesc->arginfuncs[i]), plan_cxt);
3635 qdesc->argtypioparams[i] = typIOParam;
3636 }
3637
3638 /* Make sure the query is validly encoded */
3639 pg_verifymbstr(query, strlen(query), false);
3640
3641 /************************************************************
3642 * Prepare the plan and check for errors
3643 ************************************************************/
3644 plan = SPI_prepare(query, argc, qdesc->argtypes);
3645
3646 if (plan == NULL)
3647 elog(ERROR, "SPI_prepare() failed:%s",
3649
3650 /************************************************************
3651 * Save the plan into permanent memory (right now it's in the
3652 * SPI procCxt, which will go away at function end).
3653 ************************************************************/
3654 if (SPI_keepplan(plan))
3655 elog(ERROR, "SPI_keepplan() failed");
3656 qdesc->plan = plan;
3657
3658 /************************************************************
3659 * Insert a hashtable entry for the plan.
3660 ************************************************************/
3662 qdesc->qname,
3663 HASH_ENTER, &found);
3664 hash_entry->query_data = qdesc;
3665
3666 /* Get rid of workspace */
3667 MemoryContextDelete(work_cxt);
3668
3669 /* Commit the inner transaction, return to outer xact context */
3671 MemoryContextSwitchTo(oldcontext);
3672 CurrentResourceOwner = oldowner;
3673 }
3674 PG_CATCH();
3675 {
3676 ErrorData *edata;
3677
3678 /* Save error info */
3679 MemoryContextSwitchTo(oldcontext);
3680 edata = CopyErrorData();
3682
3683 /* Drop anything we managed to allocate */
3684 if (hash_entry)
3686 qdesc->qname,
3687 HASH_REMOVE, NULL);
3688 if (plan_cxt)
3689 MemoryContextDelete(plan_cxt);
3690 if (plan)
3692
3693 /* Abort the inner transaction */
3695 MemoryContextSwitchTo(oldcontext);
3696 CurrentResourceOwner = oldowner;
3697
3698 /* Punt the error to Perl */
3699 croak_cstr(edata->message);
3700
3701 /* Can't get here, but keep compiler quiet */
3702 return NULL;
3703 }
3704 PG_END_TRY();
3705
3706 /************************************************************
3707 * Return the query's hash key to the caller.
3708 ************************************************************/
3709 return cstr2sv(qdesc->qname);
3710}
int32_t int32
Definition: c.h:498
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
@ HASH_ENTER
Definition: hsearch.h:114
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2957
void * palloc0(Size size)
Definition: mcxt.c:1347
MemoryContext TopMemoryContext
Definition: mcxt.c:149
#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:122
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:239
unsigned int Oid
Definition: postgres_ext.h:32
const char * SPI_result_code_string(int code)
Definition: spi.c:1974
int SPI_result
Definition: spi.c:46
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:861
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:977
Definition: fmgr.h:57
char qname[24]
Definition: plperl.c:187

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, plperl_query_desc::arginfuncs, plperl_query_desc::argtypes, plperl_query_desc::argtypioparams, BeginInternalSubTransaction(), CHECK_FOR_INTERRUPTS, check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), cstr2sv(), CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, FlushErrorState(), fmgr_info_cxt(), getTypeInputInfo(), HASH_ENTER, HASH_REMOVE, hash_search(), i, MemoryContextDelete(), MemoryContextSwitchTo(), ErrorData::message, plperl_query_desc::nargs, palloc(), palloc0(), parseTypeString(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, pg_verifymbstr(), plperl_query_desc::plan, plan, plperl_query_desc::plan_cxt, plperl_active_interp, plperl_query_desc::qname, plperl_query_entry::query_data, 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 3402 of file plperl.c.

3403{
3404 SV *cursor;
3405
3406 /*
3407 * Execute the query inside a sub-transaction, so we can cope with errors
3408 * sanely
3409 */
3412
3414
3416 /* Want to run inside function's memory context */
3417 MemoryContextSwitchTo(oldcontext);
3418
3419 PG_TRY();
3420 {
3422 Portal portal;
3423
3424 /* Make sure the query is validly encoded */
3425 pg_verifymbstr(query, strlen(query), false);
3426
3427 /* Create a cursor for the query */
3428 plan = SPI_prepare(query, 0, NULL);
3429 if (plan == NULL)
3430 elog(ERROR, "SPI_prepare() failed:%s",
3432
3433 portal = SPI_cursor_open(NULL, plan, NULL, NULL, false);
3435 if (portal == NULL)
3436 elog(ERROR, "SPI_cursor_open() failed:%s",
3438 cursor = cstr2sv(portal->name);
3439
3440 PinPortal(portal);
3441
3442 /* Commit the inner transaction, return to outer xact context */
3444 MemoryContextSwitchTo(oldcontext);
3445 CurrentResourceOwner = oldowner;
3446 }
3447 PG_CATCH();
3448 {
3449 ErrorData *edata;
3450
3451 /* Save error info */
3452 MemoryContextSwitchTo(oldcontext);
3453 edata = CopyErrorData();
3455
3456 /* Abort the inner transaction */
3458 MemoryContextSwitchTo(oldcontext);
3459 CurrentResourceOwner = oldowner;
3460
3461 /* Punt the error to Perl */
3462 croak_cstr(edata->message);
3463
3464 /* Can't get here, but keep compiler quiet */
3465 return NULL;
3466 }
3467 PG_END_TRY();
3468
3469 return cursor;
3470}
void PinPortal(Portal portal)
Definition: portalmem.c:373
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, 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, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, 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 3840 of file plperl.c.

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

References plperl_query_desc::arginfuncs, plperl_query_desc::argtypes, plperl_query_desc::argtypioparams, BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), cstr2sv(), current_call_data, CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, FlushErrorState(), plperl_proc_desc::fn_readonly, HASH_FIND, hash_search(), i, MemoryContextSwitchTo(), ErrorData::message, PortalData::name, plperl_query_desc::nargs, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, PinPortal(), plperl_query_desc::plan, plperl_active_interp, plperl_sv_to_datum(), plperl_call_data::prodesc, plperl_query_entry::query_data, 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 4015 of file plperl.c.

4016{
4018
4020
4021 PG_TRY();
4022 {
4023 SPI_rollback();
4024 }
4025 PG_CATCH();
4026 {
4027 ErrorData *edata;
4028
4029 /* Save error info */
4030 MemoryContextSwitchTo(oldcontext);
4031 edata = CopyErrorData();
4033
4034 /* Punt the error to Perl */
4035 croak_cstr(edata->message);
4036 }
4037 PG_END_TRY();
4038}
void SPI_rollback(void)
Definition: spi.c:414

References check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, 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 1443 of file plperl.c.

1444{
1445 Oid typid;
1446 Oid typoutput;
1447 Datum datum;
1448 bool typisvarlena,
1449 isnull;
1450
1452
1453 typid = DirectFunctionCall1(regtypein, CStringGetDatum(fqtypename));
1454 if (!OidIsValid(typid))
1455 ereport(ERROR,
1456 (errcode(ERRCODE_UNDEFINED_OBJECT),
1457 errmsg("lookup failed for type %s", fqtypename)));
1458
1459 datum = plperl_sv_to_datum(sv,
1460 typid, -1,
1461 NULL, NULL, InvalidOid,
1462 &isnull);
1463
1464 if (isnull)
1465 return NULL;
1466
1467 getTypeOutputInfo(typid,
1468 &typoutput, &typisvarlena);
1469
1470 return OidOutputFunctionCall(typoutput, datum);
1471}
#define OidIsValid(objectId)
Definition: c.h:746
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2990
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
#define InvalidOid
Definition: postgres_ext.h:37
Datum regtypein(PG_FUNCTION_ARGS)
Definition: regproc.c:1176

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

◆ plperl_util_elog()

void plperl_util_elog ( int  level,
SV *  msg 
)

Definition at line 4052 of file plperl.c.

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

References CopyErrorData(), croak_cstr(), CurrentMemoryContext, elog, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, 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 */
137 SvREFCNT_dec(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, 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
74 ret = pg_server_to_any(str, strlen(str), PG_UTF8);
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}
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:749
char * pstrdup(const char *in)
Definition: mcxt.c:1699
@ PG_UTF8
Definition: pg_wchar.h:232

References 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
55 ret = pg_any_to_server(utf8_str, len, PG_UTF8);
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:676

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

Referenced by sv2cstr().