LCOV - differential code coverage report
Current view: top level - src/backend/executor - spi.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 72.7 % 1258 915 1 342 915 1
Current Date: 2025-09-06 07:49:51 +0900 Functions: 83.3 % 84 70 14 1 69
Baseline: lcov-20250906-005545-baseline Branches: 55.3 % 805 445 360 445
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 0.0 % 1 0 1
(30,360] days: 87.5 % 8 7 1 7
(360..) days: 72.7 % 1249 908 341 908
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 83.1 % 83 69 14 1 68
Branch coverage date bins:
(30,360] days: 80.0 % 10 8 2 8
(360..) days: 55.0 % 795 437 358 437

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * spi.c
                                  4                 :                :  *              Server Programming Interface
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/executor/spi.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include "access/htup_details.h"
                                 18                 :                : #include "access/printtup.h"
                                 19                 :                : #include "access/sysattr.h"
                                 20                 :                : #include "access/xact.h"
                                 21                 :                : #include "catalog/heap.h"
                                 22                 :                : #include "catalog/pg_type.h"
                                 23                 :                : #include "commands/trigger.h"
                                 24                 :                : #include "executor/executor.h"
                                 25                 :                : #include "executor/spi_priv.h"
                                 26                 :                : #include "tcop/pquery.h"
                                 27                 :                : #include "tcop/utility.h"
                                 28                 :                : #include "utils/builtins.h"
                                 29                 :                : #include "utils/datum.h"
                                 30                 :                : #include "utils/lsyscache.h"
                                 31                 :                : #include "utils/memutils.h"
                                 32                 :                : #include "utils/rel.h"
                                 33                 :                : #include "utils/snapmgr.h"
                                 34                 :                : #include "utils/syscache.h"
                                 35                 :                : #include "utils/typcache.h"
                                 36                 :                : 
                                 37                 :                : 
                                 38                 :                : /*
                                 39                 :                :  * These global variables are part of the API for various SPI functions
                                 40                 :                :  * (a horrible API choice, but it's too late now).  To reduce the risk of
                                 41                 :                :  * interference between different SPI callers, we save and restore them
                                 42                 :                :  * when entering/exiting a SPI nesting level.
                                 43                 :                :  */
                                 44                 :                : uint64      SPI_processed = 0;
                                 45                 :                : SPITupleTable *SPI_tuptable = NULL;
                                 46                 :                : int         SPI_result = 0;
                                 47                 :                : 
                                 48                 :                : static _SPI_connection *_SPI_stack = NULL;
                                 49                 :                : static _SPI_connection *_SPI_current = NULL;
                                 50                 :                : static int  _SPI_stack_depth = 0;   /* allocated size of _SPI_stack */
                                 51                 :                : static int  _SPI_connected = -1;    /* current stack index */
                                 52                 :                : 
                                 53                 :                : typedef struct SPICallbackArg
                                 54                 :                : {
                                 55                 :                :     const char *query;
                                 56                 :                :     RawParseMode mode;
                                 57                 :                : } SPICallbackArg;
                                 58                 :                : 
                                 59                 :                : static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
                                 60                 :                :                                        ParamListInfo paramLI, bool read_only);
                                 61                 :                : 
                                 62                 :                : static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan);
                                 63                 :                : 
                                 64                 :                : static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan);
                                 65                 :                : 
                                 66                 :                : static int  _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
                                 67                 :                :                               Snapshot snapshot, Snapshot crosscheck_snapshot,
                                 68                 :                :                               bool fire_triggers);
                                 69                 :                : 
                                 70                 :                : static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes,
                                 71                 :                :                                          Datum *Values, const char *Nulls);
                                 72                 :                : 
                                 73                 :                : static int  _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount);
                                 74                 :                : 
                                 75                 :                : static void _SPI_error_callback(void *arg);
                                 76                 :                : 
                                 77                 :                : static void _SPI_cursor_operation(Portal portal,
                                 78                 :                :                                   FetchDirection direction, long count,
                                 79                 :                :                                   DestReceiver *dest);
                                 80                 :                : 
                                 81                 :                : static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan);
                                 82                 :                : static SPIPlanPtr _SPI_save_plan(SPIPlanPtr plan);
                                 83                 :                : 
                                 84                 :                : static int  _SPI_begin_call(bool use_exec);
                                 85                 :                : static int  _SPI_end_call(bool use_exec);
                                 86                 :                : static MemoryContext _SPI_execmem(void);
                                 87                 :                : static MemoryContext _SPI_procmem(void);
                                 88                 :                : static bool _SPI_checktuples(void);
                                 89                 :                : 
                                 90                 :                : 
                                 91                 :                : /* =================== interface functions =================== */
                                 92                 :                : 
                                 93                 :                : int
 8863 tgl@sss.pgh.pa.us          94                 :CBC        8624 : SPI_connect(void)
                                 95                 :                : {
 2784 peter_e@gmx.net            96                 :           8624 :     return SPI_connect_ext(0);
                                 97                 :                : }
                                 98                 :                : 
                                 99                 :                : int
                                100                 :          51523 : SPI_connect_ext(int options)
                                101                 :                : {
                                102                 :                :     int         newdepth;
                                103                 :                : 
                                104                 :                :     /* Enlarge stack if necessary */
10226 bruce@momjian.us          105         [ +  + ]:          51523 :     if (_SPI_stack == NULL)
                                106                 :                :     {
 7737 tgl@sss.pgh.pa.us         107   [ +  -  -  + ]:            977 :         if (_SPI_connected != -1 || _SPI_stack_depth != 0)
 8083 tgl@sss.pgh.pa.us         108         [ #  # ]:UBC           0 :             elog(ERROR, "SPI stack corrupted");
 7737 tgl@sss.pgh.pa.us         109                 :CBC         977 :         newdepth = 16;
                                110                 :            977 :         _SPI_stack = (_SPI_connection *)
 2784 peter_e@gmx.net           111                 :            977 :             MemoryContextAlloc(TopMemoryContext,
                                112                 :                :                                newdepth * sizeof(_SPI_connection));
 7737 tgl@sss.pgh.pa.us         113                 :            977 :         _SPI_stack_depth = newdepth;
                                114                 :                :     }
                                115                 :                :     else
                                116                 :                :     {
                                117   [ +  -  -  + ]:          50546 :         if (_SPI_stack_depth <= 0 || _SPI_stack_depth <= _SPI_connected)
 8083 tgl@sss.pgh.pa.us         118         [ #  # ]:UBC           0 :             elog(ERROR, "SPI stack corrupted");
 7737 tgl@sss.pgh.pa.us         119         [ +  + ]:CBC       50546 :         if (_SPI_stack_depth == _SPI_connected + 1)
                                120                 :                :         {
                                121                 :             13 :             newdepth = _SPI_stack_depth * 2;
                                122                 :             13 :             _SPI_stack = (_SPI_connection *)
                                123                 :             13 :                 repalloc(_SPI_stack,
                                124                 :                :                          newdepth * sizeof(_SPI_connection));
                                125                 :             13 :             _SPI_stack_depth = newdepth;
                                126                 :                :         }
                                127                 :                :     }
                                128                 :                : 
                                129                 :                :     /* Enter new stack level */
10226 bruce@momjian.us          130                 :          51523 :     _SPI_connected++;
 7737 tgl@sss.pgh.pa.us         131   [ +  -  -  + ]:          51523 :     Assert(_SPI_connected >= 0 && _SPI_connected < _SPI_stack_depth);
                                132                 :                : 
10226 bruce@momjian.us          133                 :          51523 :     _SPI_current = &(_SPI_stack[_SPI_connected]);
                                134                 :          51523 :     _SPI_current->processed = 0;
                                135                 :          51523 :     _SPI_current->tuptable = NULL;
 2892 tgl@sss.pgh.pa.us         136                 :          51523 :     _SPI_current->execSubid = InvalidSubTransactionId;
 4426                           137                 :          51523 :     slist_init(&_SPI_current->tuptables);
 2999                           138                 :          51523 :     _SPI_current->procCxt = NULL;    /* in case we fail to create 'em */
 7660                           139                 :          51523 :     _SPI_current->execCxt = NULL;
                                140                 :          51523 :     _SPI_current->connectSubid = GetCurrentSubTransactionId();
 3081 kgrittn@postgresql.o      141                 :          51523 :     _SPI_current->queryEnv = NULL;
 2784 peter_e@gmx.net           142                 :          51523 :     _SPI_current->atomic = (options & SPI_OPT_NONATOMIC ? false : true);
                                143                 :          51523 :     _SPI_current->internal_xact = false;
 2556 tgl@sss.pgh.pa.us         144                 :          51523 :     _SPI_current->outer_processed = SPI_processed;
                                145                 :          51523 :     _SPI_current->outer_tuptable = SPI_tuptable;
                                146                 :          51523 :     _SPI_current->outer_result = SPI_result;
                                147                 :                : 
                                148                 :                :     /*
                                149                 :                :      * Create memory contexts for this procedure
                                150                 :                :      *
                                151                 :                :      * In atomic contexts (the normal case), we use TopTransactionContext,
                                152                 :                :      * otherwise PortalContext, so that it lives across transaction
                                153                 :                :      * boundaries.
                                154                 :                :      *
                                155                 :                :      * XXX It could be better to use PortalContext as the parent context in
                                156                 :                :      * all cases, but we may not be inside a portal (consider deferred-trigger
                                157                 :                :      * execution).  Perhaps CurTransactionContext could be an option?  For now
                                158                 :                :      * it doesn't matter because we clean up explicitly in AtEOSubXact_SPI();
                                159                 :                :      * but see also AtEOXact_SPI().
                                160                 :                :      */
 2784 peter_e@gmx.net           161         [ +  + ]:          51523 :     _SPI_current->procCxt = AllocSetContextCreate(_SPI_current->atomic ? TopTransactionContext : PortalContext,
                                162                 :                :                                                   "SPI Proc",
                                163                 :                :                                                   ALLOCSET_DEFAULT_SIZES);
                                164         [ +  + ]:          51523 :     _SPI_current->execCxt = AllocSetContextCreate(_SPI_current->atomic ? TopTransactionContext : _SPI_current->procCxt,
                                165                 :                :                                                   "SPI Exec",
                                166                 :                :                                                   ALLOCSET_DEFAULT_SIZES);
                                167                 :                :     /* ... and switch to procedure's context */
 9201 tgl@sss.pgh.pa.us         168                 :          51523 :     _SPI_current->savedcxt = MemoryContextSwitchTo(_SPI_current->procCxt);
                                169                 :                : 
                                170                 :                :     /*
                                171                 :                :      * Reset API global variables so that current caller cannot accidentally
                                172                 :                :      * depend on state of an outer caller.
                                173                 :                :      */
 2556                           174                 :          51523 :     SPI_processed = 0;
                                175                 :          51523 :     SPI_tuptable = NULL;
                                176                 :          51523 :     SPI_result = 0;
                                177                 :                : 
 9867 bruce@momjian.us          178                 :          51523 :     return SPI_OK_CONNECT;
                                179                 :                : }
                                180                 :                : 
                                181                 :                : int
 8863 tgl@sss.pgh.pa.us         182                 :          50111 : SPI_finish(void)
                                183                 :                : {
                                184                 :                :     int         res;
                                185                 :                : 
 2892                           186                 :          50111 :     res = _SPI_begin_call(false);   /* just check we're connected */
10226 bruce@momjian.us          187         [ -  + ]:          50111 :     if (res < 0)
 9867 bruce@momjian.us          188                 :UBC           0 :         return res;
                                189                 :                : 
                                190                 :                :     /* Restore memory context as it was before procedure call */
10226 bruce@momjian.us          191                 :CBC       50111 :     MemoryContextSwitchTo(_SPI_current->savedcxt);
                                192                 :                : 
                                193                 :                :     /* Release memory used in procedure call (including tuptables) */
 9201 tgl@sss.pgh.pa.us         194                 :          50111 :     MemoryContextDelete(_SPI_current->execCxt);
 7660                           195                 :          50111 :     _SPI_current->execCxt = NULL;
 9201                           196                 :          50111 :     MemoryContextDelete(_SPI_current->procCxt);
 7660                           197                 :          50111 :     _SPI_current->procCxt = NULL;
                                198                 :                : 
                                199                 :                :     /*
                                200                 :                :      * Restore outer API variables, especially SPI_tuptable which is probably
                                201                 :                :      * pointing at a just-deleted tuptable
                                202                 :                :      */
 2556                           203                 :          50111 :     SPI_processed = _SPI_current->outer_processed;
                                204                 :          50111 :     SPI_tuptable = _SPI_current->outer_tuptable;
                                205                 :          50111 :     SPI_result = _SPI_current->outer_result;
                                206                 :                : 
                                207                 :                :     /* Exit stack level */
10226 bruce@momjian.us          208                 :          50111 :     _SPI_connected--;
 3224 tgl@sss.pgh.pa.us         209         [ +  + ]:          50111 :     if (_SPI_connected < 0)
 9201                           210                 :          43967 :         _SPI_current = NULL;
                                211                 :                :     else
10226 bruce@momjian.us          212                 :           6144 :         _SPI_current = &(_SPI_stack[_SPI_connected]);
                                213                 :                : 
 9867                           214                 :          50111 :     return SPI_OK_FINISH;
                                215                 :                : }
                                216                 :                : 
                                217                 :                : /*
                                218                 :                :  * SPI_start_transaction is a no-op, kept for backwards compatibility.
                                219                 :                :  * SPI callers are *always* inside a transaction.
                                220                 :                :  */
                                221                 :                : void
 2784 peter_e@gmx.net           222                 :UBC           0 : SPI_start_transaction(void)
                                223                 :                : {
                                224                 :              0 : }
                                225                 :                : 
                                226                 :                : static void
 2358 peter@eisentraut.org      227                 :CBC        2144 : _SPI_commit(bool chain)
                                228                 :                : {
 2784 peter_e@gmx.net           229                 :           2144 :     MemoryContext oldcontext = CurrentMemoryContext;
                                230                 :                :     SavedTransactionCharacteristics savetc;
                                231                 :                : 
                                232                 :                :     /*
                                233                 :                :      * Complain if we are in a context that doesn't permit transaction
                                234                 :                :      * termination.  (Note: here and _SPI_rollback should be the only places
                                235                 :                :      * that throw ERRCODE_INVALID_TRANSACTION_TERMINATION, so that callers can
                                236                 :                :      * test for that with security that they know what happened.)
                                237                 :                :      */
                                238         [ +  + ]:           2144 :     if (_SPI_current->atomic)
                                239         [ +  - ]:             16 :         ereport(ERROR,
                                240                 :                :                 (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
                                241                 :                :                  errmsg("invalid transaction termination")));
                                242                 :                : 
                                243                 :                :     /*
                                244                 :                :      * This restriction is required by PLs implemented on top of SPI.  They
                                245                 :                :      * use subtransactions to establish exception blocks that are supposed to
                                246                 :                :      * be rolled back together if there is an error.  Terminating the
                                247                 :                :      * top-level transaction in such a block violates that idea.  A future PL
                                248                 :                :      * implementation might have different ideas about this, in which case
                                249                 :                :      * this restriction would have to be refined or the check possibly be
                                250                 :                :      * moved out of SPI into the PLs.  Note however that the code below relies
                                251                 :                :      * on not being within a subtransaction.
                                252                 :                :      */
                                253         [ +  + ]:           2128 :     if (IsSubTransaction())
                                254         [ +  - ]:              3 :         ereport(ERROR,
                                255                 :                :                 (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
                                256                 :                :                  errmsg("cannot commit while a subtransaction is active")));
                                257                 :                : 
 1286 tgl@sss.pgh.pa.us         258         [ +  + ]:           2125 :     if (chain)
                                259                 :              2 :         SaveTransactionCharacteristics(&savetc);
                                260                 :                : 
                                261                 :                :     /* Catch any error occurring during the COMMIT */
                                262         [ +  + ]:           2125 :     PG_TRY();
                                263                 :                :     {
                                264                 :                :         /* Protect current SPI stack entry against deletion */
                                265                 :           2125 :         _SPI_current->internal_xact = true;
                                266                 :                : 
                                267                 :                :         /*
                                268                 :                :          * Hold any pinned portals that any PLs might be using.  We have to do
                                269                 :                :          * this before changing transaction state, since this will run
                                270                 :                :          * user-defined code that might throw an error.
                                271                 :                :          */
                                272                 :           2125 :         HoldPinnedPortals();
                                273                 :                : 
                                274                 :                :         /* Release snapshots associated with portals */
                                275                 :           2124 :         ForgetPortalSnapshots();
                                276                 :                : 
                                277                 :                :         /* Do the deed */
                                278                 :           2124 :         CommitTransactionCommand();
                                279                 :                : 
                                280                 :                :         /* Immediately start a new transaction */
 2358 peter@eisentraut.org      281                 :           2117 :         StartTransactionCommand();
 1286 tgl@sss.pgh.pa.us         282         [ +  + ]:           2117 :         if (chain)
                                283                 :              2 :             RestoreTransactionCharacteristics(&savetc);
                                284                 :                : 
                                285                 :           2117 :         MemoryContextSwitchTo(oldcontext);
                                286                 :                : 
                                287                 :           2117 :         _SPI_current->internal_xact = false;
                                288                 :                :     }
                                289                 :              8 :     PG_CATCH();
                                290                 :                :     {
                                291                 :                :         ErrorData  *edata;
                                292                 :                : 
                                293                 :                :         /* Save error info in caller's context */
                                294                 :              8 :         MemoryContextSwitchTo(oldcontext);
                                295                 :              8 :         edata = CopyErrorData();
                                296                 :              8 :         FlushErrorState();
                                297                 :                : 
                                298                 :                :         /*
                                299                 :                :          * Abort the failed transaction.  If this fails too, we'll just
                                300                 :                :          * propagate the error out ... there's not that much we can do.
                                301                 :                :          */
                                302                 :              8 :         AbortCurrentTransaction();
                                303                 :                : 
                                304                 :                :         /* ... and start a new one */
                                305                 :              8 :         StartTransactionCommand();
                                306         [ -  + ]:              8 :         if (chain)
 1286 tgl@sss.pgh.pa.us         307                 :UBC           0 :             RestoreTransactionCharacteristics(&savetc);
                                308                 :                : 
 1286 tgl@sss.pgh.pa.us         309                 :CBC           8 :         MemoryContextSwitchTo(oldcontext);
                                310                 :                : 
                                311                 :              8 :         _SPI_current->internal_xact = false;
                                312                 :                : 
                                313                 :                :         /* Now that we've cleaned up the transaction, re-throw the error */
                                314                 :              8 :         ReThrowError(edata);
                                315                 :                :     }
                                316         [ -  + ]:           2117 :     PG_END_TRY();
 2784 peter_e@gmx.net           317                 :           2117 : }
                                318                 :                : 
                                319                 :                : void
 2358 peter@eisentraut.org      320                 :           2142 : SPI_commit(void)
                                321                 :                : {
                                322                 :           2142 :     _SPI_commit(false);
                                323                 :           2115 : }
                                324                 :                : 
                                325                 :                : void
                                326                 :              2 : SPI_commit_and_chain(void)
                                327                 :                : {
                                328                 :              2 :     _SPI_commit(true);
                                329                 :              2 : }
                                330                 :                : 
                                331                 :                : static void
                                332                 :             84 : _SPI_rollback(bool chain)
                                333                 :                : {
 2784 peter_e@gmx.net           334                 :             84 :     MemoryContext oldcontext = CurrentMemoryContext;
                                335                 :                :     SavedTransactionCharacteristics savetc;
                                336                 :                : 
                                337                 :                :     /* see comments in _SPI_commit() */
                                338         [ -  + ]:             84 :     if (_SPI_current->atomic)
 2784 peter_e@gmx.net           339         [ #  # ]:UBC           0 :         ereport(ERROR,
                                340                 :                :                 (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
                                341                 :                :                  errmsg("invalid transaction termination")));
                                342                 :                : 
                                343                 :                :     /* see comments in _SPI_commit() */
 2784 peter_e@gmx.net           344         [ +  + ]:CBC          84 :     if (IsSubTransaction())
                                345         [ +  - ]:              2 :         ereport(ERROR,
                                346                 :                :                 (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
                                347                 :                :                  errmsg("cannot roll back while a subtransaction is active")));
                                348                 :                : 
 1286 tgl@sss.pgh.pa.us         349         [ +  + ]:             82 :     if (chain)
                                350                 :              2 :         SaveTransactionCharacteristics(&savetc);
                                351                 :                : 
                                352                 :                :     /* Catch any error occurring during the ROLLBACK */
                                353         [ +  + ]:             82 :     PG_TRY();
                                354                 :                :     {
                                355                 :                :         /* Protect current SPI stack entry against deletion */
                                356                 :             82 :         _SPI_current->internal_xact = true;
                                357                 :                : 
                                358                 :                :         /*
                                359                 :                :          * Hold any pinned portals that any PLs might be using.  We have to do
                                360                 :                :          * this before changing transaction state, since this will run
                                361                 :                :          * user-defined code that might throw an error, and in any case
                                362                 :                :          * couldn't be run in an already-aborted transaction.
                                363                 :                :          */
                                364                 :             82 :         HoldPinnedPortals();
                                365                 :                : 
                                366                 :                :         /* Release snapshots associated with portals */
                                367                 :             80 :         ForgetPortalSnapshots();
                                368                 :                : 
                                369                 :                :         /* Do the deed */
                                370                 :             80 :         AbortCurrentTransaction();
                                371                 :                : 
                                372                 :                :         /* Immediately start a new transaction */
 2358 peter@eisentraut.org      373                 :             80 :         StartTransactionCommand();
 1286 tgl@sss.pgh.pa.us         374         [ +  + ]:             80 :         if (chain)
                                375                 :              2 :             RestoreTransactionCharacteristics(&savetc);
                                376                 :                : 
                                377                 :             80 :         MemoryContextSwitchTo(oldcontext);
                                378                 :                : 
                                379                 :             80 :         _SPI_current->internal_xact = false;
                                380                 :                :     }
                                381                 :              2 :     PG_CATCH();
                                382                 :                :     {
                                383                 :                :         ErrorData  *edata;
                                384                 :                : 
                                385                 :                :         /* Save error info in caller's context */
                                386                 :              2 :         MemoryContextSwitchTo(oldcontext);
                                387                 :              2 :         edata = CopyErrorData();
                                388                 :              2 :         FlushErrorState();
                                389                 :                : 
                                390                 :                :         /*
                                391                 :                :          * Try again to abort the failed transaction.  If this fails too,
                                392                 :                :          * we'll just propagate the error out ... there's not that much we can
                                393                 :                :          * do.
                                394                 :                :          */
                                395                 :              2 :         AbortCurrentTransaction();
                                396                 :                : 
                                397                 :                :         /* ... and start a new one */
                                398                 :              2 :         StartTransactionCommand();
                                399         [ -  + ]:              2 :         if (chain)
 1286 tgl@sss.pgh.pa.us         400                 :UBC           0 :             RestoreTransactionCharacteristics(&savetc);
                                401                 :                : 
 1286 tgl@sss.pgh.pa.us         402                 :CBC           2 :         MemoryContextSwitchTo(oldcontext);
                                403                 :                : 
                                404                 :              2 :         _SPI_current->internal_xact = false;
                                405                 :                : 
                                406                 :                :         /* Now that we've cleaned up the transaction, re-throw the error */
                                407                 :              2 :         ReThrowError(edata);
                                408                 :                :     }
                                409         [ -  + ]:             80 :     PG_END_TRY();
 2784 peter_e@gmx.net           410                 :             80 : }
                                411                 :                : 
                                412                 :                : void
 2358 peter@eisentraut.org      413                 :             82 : SPI_rollback(void)
                                414                 :                : {
                                415                 :             82 :     _SPI_rollback(false);
                                416                 :             78 : }
                                417                 :                : 
                                418                 :                : void
                                419                 :              2 : SPI_rollback_and_chain(void)
                                420                 :                : {
                                421                 :              2 :     _SPI_rollback(true);
                                422                 :              2 : }
                                423                 :                : 
                                424                 :                : /*
                                425                 :                :  * Clean up SPI state at transaction commit or abort.
                                426                 :                :  */
                                427                 :                : void
 7949 mail@joeconway.com        428                 :         317128 : AtEOXact_SPI(bool isCommit)
                                429                 :                : {
 1286 tgl@sss.pgh.pa.us         430                 :         317128 :     bool        found = false;
                                431                 :                : 
                                432                 :                :     /*
                                433                 :                :      * Pop stack entries, stopping if we find one marked internal_xact (that
                                434                 :                :      * one belongs to the caller of SPI_commit or SPI_rollback).
                                435                 :                :      */
                                436         [ +  + ]:         318429 :     while (_SPI_connected >= 0)
                                437                 :                :     {
                                438                 :           3508 :         _SPI_connection *connection = &(_SPI_stack[_SPI_connected]);
                                439                 :                : 
                                440         [ +  + ]:           3508 :         if (connection->internal_xact)
                                441                 :           2207 :             break;
                                442                 :                : 
                                443                 :           1301 :         found = true;
                                444                 :                : 
                                445                 :                :         /*
                                446                 :                :          * We need not release the procedure's memory contexts explicitly, as
                                447                 :                :          * they'll go away automatically when their parent context does; see
                                448                 :                :          * notes in SPI_connect_ext.
                                449                 :                :          */
                                450                 :                : 
                                451                 :                :         /*
                                452                 :                :          * Restore outer global variables and pop the stack entry.  Unlike
                                453                 :                :          * SPI_finish(), we don't risk switching to memory contexts that might
                                454                 :                :          * be already gone.
                                455                 :                :          */
                                456                 :           1301 :         SPI_processed = connection->outer_processed;
                                457                 :           1301 :         SPI_tuptable = connection->outer_tuptable;
                                458                 :           1301 :         SPI_result = connection->outer_result;
                                459                 :                : 
                                460                 :           1301 :         _SPI_connected--;
                                461         [ +  + ]:           1301 :         if (_SPI_connected < 0)
                                462                 :           1269 :             _SPI_current = NULL;
                                463                 :                :         else
                                464                 :             32 :             _SPI_current = &(_SPI_stack[_SPI_connected]);
                                465                 :                :     }
                                466                 :                : 
                                467                 :                :     /* We should only find entries to pop during an ABORT. */
                                468   [ +  +  -  + ]:         317128 :     if (found && isCommit)
 7737 tgl@sss.pgh.pa.us         469         [ #  # ]:UBC           0 :         ereport(WARNING,
                                470                 :                :                 (errcode(ERRCODE_WARNING),
                                471                 :                :                  errmsg("transaction left non-empty SPI stack"),
                                472                 :                :                  errhint("Check for missing \"SPI_finish\" calls.")));
 9201 tgl@sss.pgh.pa.us         473                 :CBC      317128 : }
                                474                 :                : 
                                475                 :                : /*
                                476                 :                :  * Clean up SPI state at subtransaction commit or abort.
                                477                 :                :  *
                                478                 :                :  * During commit, there shouldn't be any unclosed entries remaining from
                                479                 :                :  * the current subtransaction; we emit a warning if any are found.
                                480                 :                :  */
                                481                 :                : void
 7660                           482                 :           9090 : AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
                                483                 :                : {
 7678 bruce@momjian.us          484                 :           9090 :     bool        found = false;
                                485                 :                : 
 7737 tgl@sss.pgh.pa.us         486         [ +  + ]:           9201 :     while (_SPI_connected >= 0)
                                487                 :                :     {
                                488                 :           6955 :         _SPI_connection *connection = &(_SPI_stack[_SPI_connected]);
                                489                 :                : 
 7660                           490         [ +  + ]:           6955 :         if (connection->connectSubid != mySubid)
 7737                           491                 :           6844 :             break;              /* couldn't be any underneath it either */
                                492                 :                : 
 2784 peter_e@gmx.net           493         [ -  + ]:            111 :         if (connection->internal_xact)
 2784 peter_e@gmx.net           494                 :UBC           0 :             break;
                                495                 :                : 
 7737 tgl@sss.pgh.pa.us         496                 :CBC         111 :         found = true;
                                497                 :                : 
                                498                 :                :         /*
                                499                 :                :          * Release procedure memory explicitly (see note in SPI_connect)
                                500                 :                :          */
 7660                           501         [ +  - ]:            111 :         if (connection->execCxt)
                                502                 :                :         {
                                503                 :            111 :             MemoryContextDelete(connection->execCxt);
                                504                 :            111 :             connection->execCxt = NULL;
                                505                 :                :         }
                                506         [ +  - ]:            111 :         if (connection->procCxt)
                                507                 :                :         {
                                508                 :            111 :             MemoryContextDelete(connection->procCxt);
                                509                 :            111 :             connection->procCxt = NULL;
                                510                 :                :         }
                                511                 :                : 
                                512                 :                :         /*
                                513                 :                :          * Restore outer global variables and pop the stack entry.  Unlike
                                514                 :                :          * SPI_finish(), we don't risk switching to memory contexts that might
                                515                 :                :          * be already gone.
                                516                 :                :          */
 2556                           517                 :            111 :         SPI_processed = connection->outer_processed;
                                518                 :            111 :         SPI_tuptable = connection->outer_tuptable;
                                519                 :            111 :         SPI_result = connection->outer_result;
                                520                 :                : 
 7737                           521                 :            111 :         _SPI_connected--;
 3224                           522         [ +  + ]:            111 :         if (_SPI_connected < 0)
 7737                           523                 :             39 :             _SPI_current = NULL;
                                524                 :                :         else
                                525                 :             72 :             _SPI_current = &(_SPI_stack[_SPI_connected]);
                                526                 :                :     }
                                527                 :                : 
                                528   [ +  +  -  + ]:           9090 :     if (found && isCommit)
 7737 tgl@sss.pgh.pa.us         529         [ #  # ]:UBC           0 :         ereport(WARNING,
                                530                 :                :                 (errcode(ERRCODE_WARNING),
                                531                 :                :                  errmsg("subtransaction left non-empty SPI stack"),
                                532                 :                :                  errhint("Check for missing \"SPI_finish\" calls.")));
                                533                 :                : 
                                534                 :                :     /*
                                535                 :                :      * If we are aborting a subtransaction and there is an open SPI context
                                536                 :                :      * surrounding the subxact, clean up to prevent memory leakage.
                                537                 :                :      */
 6864 tgl@sss.pgh.pa.us         538   [ +  +  +  + ]:CBC        9090 :     if (_SPI_current && !isCommit)
                                539                 :                :     {
                                540                 :                :         slist_mutable_iter siter;
                                541                 :                : 
                                542                 :                :         /*
                                543                 :                :          * Throw away executor state if current executor operation was started
                                544                 :                :          * within current subxact (essentially, force a _SPI_end_call(true)).
                                545                 :                :          */
 2892                           546         [ +  + ]:           3179 :         if (_SPI_current->execSubid >= mySubid)
                                547                 :                :         {
                                548                 :           2783 :             _SPI_current->execSubid = InvalidSubTransactionId;
  661 nathan@postgresql.or      549                 :           2783 :             MemoryContextReset(_SPI_current->execCxt);
                                550                 :                :         }
                                551                 :                : 
                                552                 :                :         /* throw away any tuple tables created within current subxact */
 4426 tgl@sss.pgh.pa.us         553   [ +  +  +  +  :           7701 :         slist_foreach_modify(siter, &_SPI_current->tuptables)
                                              +  + ]
                                554                 :                :         {
                                555                 :                :             SPITupleTable *tuptable;
                                556                 :                : 
                                557                 :           4522 :             tuptable = slist_container(SPITupleTable, next, siter.cur);
                                558         [ +  + ]:           4522 :             if (tuptable->subid >= mySubid)
                                559                 :                :             {
                                560                 :                :                 /*
                                561                 :                :                  * If we used SPI_freetuptable() here, its internal search of
                                562                 :                :                  * the tuptables list would make this operation O(N^2).
                                563                 :                :                  * Instead, just free the tuptable manually.  This should
                                564                 :                :                  * match what SPI_freetuptable() does.
                                565                 :                :                  */
                                566                 :           2668 :                 slist_delete_current(&siter);
                                567         [ +  + ]:           2668 :                 if (tuptable == _SPI_current->tuptable)
                                568                 :           2665 :                     _SPI_current->tuptable = NULL;
                                569         [ +  + ]:           2668 :                 if (tuptable == SPI_tuptable)
                                570                 :              3 :                     SPI_tuptable = NULL;
                                571                 :           2668 :                 MemoryContextDelete(tuptable->tuptabcxt);
                                572                 :                :             }
                                573                 :                :         }
                                574                 :                :     }
 7737                           575                 :           9090 : }
                                576                 :                : 
                                577                 :                : /*
                                578                 :                :  * Are we executing inside a procedure (that is, a nonatomic SPI context)?
                                579                 :                :  */
                                580                 :                : bool
 2525                           581                 :         312994 : SPI_inside_nonatomic_context(void)
                                582                 :                : {
                                583         [ +  + ]:         312994 :     if (_SPI_current == NULL)
                                584                 :         310787 :         return false;           /* not in any SPI context at all */
                                585                 :                :     /* these tests must match _SPI_commit's opinion of what's atomic: */
                                586         [ -  + ]:           2207 :     if (_SPI_current->atomic)
 2525 tgl@sss.pgh.pa.us         587                 :UBC           0 :         return false;           /* it's atomic (ie function not procedure) */
  325 tgl@sss.pgh.pa.us         588         [ -  + ]:CBC        2207 :     if (IsSubTransaction())
  325 tgl@sss.pgh.pa.us         589                 :UBC           0 :         return false;           /* if within subtransaction, it's atomic */
 2525 tgl@sss.pgh.pa.us         590                 :CBC        2207 :     return true;
                                591                 :                : }
                                592                 :                : 
                                593                 :                : 
                                594                 :                : /* Parse, plan, and execute a query string */
                                595                 :                : int
 7432 neilc@samurai.com         596                 :            744 : SPI_execute(const char *src, bool read_only, long tcount)
                                597                 :                : {
                                598                 :                :     _SPI_plan   plan;
                                599                 :                :     SPIExecuteOptions options;
                                600                 :                :     int         res;
                                601                 :                : 
10226 bruce@momjian.us          602   [ +  -  -  + ]:            744 :     if (src == NULL || tcount < 0)
 9867 bruce@momjian.us          603                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                604                 :                : 
10226 bruce@momjian.us          605                 :CBC         744 :     res = _SPI_begin_call(true);
                                606         [ -  + ]:            744 :     if (res < 0)
 9867 bruce@momjian.us          607                 :UBC           0 :         return res;
                                608                 :                : 
 6750 tgl@sss.pgh.pa.us         609                 :CBC         744 :     memset(&plan, 0, sizeof(_SPI_plan));
                                610                 :            744 :     plan.magic = _SPI_PLAN_MAGIC;
 1706                           611                 :            744 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 3088 rhaas@postgresql.org      612                 :            744 :     plan.cursor_options = CURSOR_OPT_PARALLEL_OK;
                                613                 :                : 
 4628 tgl@sss.pgh.pa.us         614                 :            744 :     _SPI_prepare_oneshot_plan(src, &plan);
                                615                 :                : 
 1434                           616                 :            738 :     memset(&options, 0, sizeof(options));
                                617                 :            738 :     options.read_only = read_only;
                                618                 :            738 :     options.tcount = tcount;
                                619                 :                : 
                                620                 :            738 :     res = _SPI_execute_plan(&plan, &options,
                                621                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                622                 :                :                             true);
                                623                 :                : 
10226 bruce@momjian.us          624                 :            712 :     _SPI_end_call(true);
 9867                           625                 :            712 :     return res;
                                626                 :                : }
                                627                 :                : 
                                628                 :                : /* Obsolete version of SPI_execute */
                                629                 :                : int
 7432 neilc@samurai.com         630                 :            265 : SPI_exec(const char *src, long tcount)
                                631                 :                : {
 7663 tgl@sss.pgh.pa.us         632                 :            265 :     return SPI_execute(src, false, tcount);
                                633                 :                : }
                                634                 :                : 
                                635                 :                : /* Parse, plan, and execute a query string, with extensible options */
                                636                 :                : int
 1684                           637                 :           7257 : SPI_execute_extended(const char *src,
                                638                 :                :                      const SPIExecuteOptions *options)
                                639                 :                : {
                                640                 :                :     int         res;
                                641                 :                :     _SPI_plan   plan;
                                642                 :                : 
                                643   [ +  -  -  + ]:           7257 :     if (src == NULL || options == NULL)
 1684 tgl@sss.pgh.pa.us         644                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                645                 :                : 
 1684 tgl@sss.pgh.pa.us         646                 :CBC        7257 :     res = _SPI_begin_call(true);
                                647         [ -  + ]:           7257 :     if (res < 0)
 1684 tgl@sss.pgh.pa.us         648                 :UBC           0 :         return res;
                                649                 :                : 
 1684 tgl@sss.pgh.pa.us         650                 :CBC        7257 :     memset(&plan, 0, sizeof(_SPI_plan));
                                651                 :           7257 :     plan.magic = _SPI_PLAN_MAGIC;
                                652                 :           7257 :     plan.parse_mode = RAW_PARSE_DEFAULT;
                                653                 :           7257 :     plan.cursor_options = CURSOR_OPT_PARALLEL_OK;
                                654         [ +  + ]:           7257 :     if (options->params)
                                655                 :                :     {
                                656                 :            285 :         plan.parserSetup = options->params->parserSetup;
                                657                 :            285 :         plan.parserSetupArg = options->params->parserSetupArg;
                                658                 :                :     }
                                659                 :                : 
                                660                 :           7257 :     _SPI_prepare_oneshot_plan(src, &plan);
                                661                 :                : 
 1434                           662                 :           7257 :     res = _SPI_execute_plan(&plan, options,
                                663                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                664                 :                :                             true);
                                665                 :                : 
 1684                           666                 :           7150 :     _SPI_end_call(true);
                                667                 :           7150 :     return res;
                                668                 :                : }
                                669                 :                : 
                                670                 :                : /* Execute a previously prepared plan */
                                671                 :                : int
 6750                           672                 :           2295 : SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls,
                                673                 :                :                  bool read_only, long tcount)
                                674                 :                : {
                                675                 :                :     SPIExecuteOptions options;
                                676                 :                :     int         res;
                                677                 :                : 
                                678   [ +  -  +  -  :           2295 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
                                              -  + ]
 9867 bruce@momjian.us          679                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                680                 :                : 
 6750 tgl@sss.pgh.pa.us         681   [ +  +  -  + ]:CBC        2295 :     if (plan->nargs > 0 && Values == NULL)
 9867 bruce@momjian.us          682                 :UBC           0 :         return SPI_ERROR_PARAM;
                                683                 :                : 
10226 bruce@momjian.us          684                 :CBC        2295 :     res = _SPI_begin_call(true);
                                685         [ -  + ]:           2295 :     if (res < 0)
 9867 bruce@momjian.us          686                 :UBC           0 :         return res;
                                687                 :                : 
 1434 tgl@sss.pgh.pa.us         688                 :CBC        2295 :     memset(&options, 0, sizeof(options));
                                689                 :           2295 :     options.params = _SPI_convert_params(plan->nargs, plan->argtypes,
                                690                 :                :                                          Values, Nulls);
                                691                 :           2295 :     options.read_only = read_only;
                                692                 :           2295 :     options.tcount = tcount;
                                693                 :                : 
                                694                 :           2295 :     res = _SPI_execute_plan(plan, &options,
                                695                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                696                 :                :                             true);
                                697                 :                : 
 8017                           698                 :           2294 :     _SPI_end_call(true);
                                699                 :           2294 :     return res;
                                700                 :                : }
                                701                 :                : 
                                702                 :                : /* Obsolete version of SPI_execute_plan */
                                703                 :                : int
 6750                           704                 :             27 : SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls, long tcount)
                                705                 :                : {
 7663                           706                 :             27 :     return SPI_execute_plan(plan, Values, Nulls, false, tcount);
                                707                 :                : }
                                708                 :                : 
                                709                 :                : /* Execute a previously prepared plan */
                                710                 :                : int
 1685                           711                 :           1271 : SPI_execute_plan_extended(SPIPlanPtr plan,
                                712                 :                :                           const SPIExecuteOptions *options)
                                713                 :                : {
                                714                 :                :     int         res;
                                715                 :                : 
                                716   [ +  -  +  -  :           1271 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || options == NULL)
                                              -  + ]
 5785 tgl@sss.pgh.pa.us         717                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                718                 :                : 
 5785 tgl@sss.pgh.pa.us         719                 :CBC        1271 :     res = _SPI_begin_call(true);
                                720         [ -  + ]:           1271 :     if (res < 0)
 5785 tgl@sss.pgh.pa.us         721                 :UBC           0 :         return res;
                                722                 :                : 
 1434 tgl@sss.pgh.pa.us         723                 :CBC        1271 :     res = _SPI_execute_plan(plan, options,
                                724                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                725                 :                :                             true);
                                726                 :                : 
 1912                           727                 :           1266 :     _SPI_end_call(true);
                                728                 :           1266 :     return res;
                                729                 :                : }
                                730                 :                : 
                                731                 :                : /* Execute a previously prepared plan */
                                732                 :                : int
 1685                           733                 :          36715 : SPI_execute_plan_with_paramlist(SPIPlanPtr plan, ParamListInfo params,
                                734                 :                :                                 bool read_only, long tcount)
                                735                 :                : {
                                736                 :                :     SPIExecuteOptions options;
                                737                 :                :     int         res;
                                738                 :                : 
 1912                           739   [ +  -  +  -  :          36715 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
                                              -  + ]
 1912 tgl@sss.pgh.pa.us         740                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                741                 :                : 
 1912 tgl@sss.pgh.pa.us         742                 :CBC       36715 :     res = _SPI_begin_call(true);
                                743         [ -  + ]:          36715 :     if (res < 0)
 1912 tgl@sss.pgh.pa.us         744                 :UBC           0 :         return res;
                                745                 :                : 
 1434 tgl@sss.pgh.pa.us         746                 :CBC       36715 :     memset(&options, 0, sizeof(options));
                                747                 :          36715 :     options.params = params;
                                748                 :          36715 :     options.read_only = read_only;
                                749                 :          36715 :     options.tcount = tcount;
                                750                 :                : 
                                751                 :          36715 :     res = _SPI_execute_plan(plan, &options,
                                752                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                753                 :                :                             true);
                                754                 :                : 
 5785                           755                 :          34012 :     _SPI_end_call(true);
                                756                 :          34012 :     return res;
                                757                 :                : }
                                758                 :                : 
                                759                 :                : /*
                                760                 :                :  * SPI_execute_snapshot -- identical to SPI_execute_plan, except that we allow
                                761                 :                :  * the caller to specify exactly which snapshots to use, which will be
                                762                 :                :  * registered here.  Also, the caller may specify that AFTER triggers should be
                                763                 :                :  * queued as part of the outer query rather than being fired immediately at the
                                764                 :                :  * end of the command.
                                765                 :                :  *
                                766                 :                :  * This is currently not documented in spi.sgml because it is only intended
                                767                 :                :  * for use by RI triggers.
                                768                 :                :  *
                                769                 :                :  * Passing snapshot == InvalidSnapshot will select the normal behavior of
                                770                 :                :  * fetching a new snapshot for each query.
                                771                 :                :  */
                                772                 :                : int
 6750                           773                 :           3881 : SPI_execute_snapshot(SPIPlanPtr plan,
                                774                 :                :                      Datum *Values, const char *Nulls,
                                775                 :                :                      Snapshot snapshot, Snapshot crosscheck_snapshot,
                                776                 :                :                      bool read_only, bool fire_triggers, long tcount)
                                777                 :                : {
                                778                 :                :     SPIExecuteOptions options;
                                779                 :                :     int         res;
                                780                 :                : 
                                781   [ +  -  +  -  :           3881 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
                                              -  + ]
 8017 tgl@sss.pgh.pa.us         782                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                783                 :                : 
 6750 tgl@sss.pgh.pa.us         784   [ +  +  -  + ]:CBC        3881 :     if (plan->nargs > 0 && Values == NULL)
 8017 tgl@sss.pgh.pa.us         785                 :UBC           0 :         return SPI_ERROR_PARAM;
                                786                 :                : 
 8017 tgl@sss.pgh.pa.us         787                 :CBC        3881 :     res = _SPI_begin_call(true);
                                788         [ -  + ]:           3881 :     if (res < 0)
 8017 tgl@sss.pgh.pa.us         789                 :UBC           0 :         return res;
                                790                 :                : 
 1434 tgl@sss.pgh.pa.us         791                 :CBC        3881 :     memset(&options, 0, sizeof(options));
                                792                 :           3881 :     options.params = _SPI_convert_params(plan->nargs, plan->argtypes,
                                793                 :                :                                          Values, Nulls);
                                794                 :           3881 :     options.read_only = read_only;
                                795                 :           3881 :     options.tcount = tcount;
                                796                 :                : 
                                797                 :           3881 :     res = _SPI_execute_plan(plan, &options,
                                798                 :                :                             snapshot, crosscheck_snapshot,
                                799                 :                :                             fire_triggers);
                                800                 :                : 
10226 bruce@momjian.us          801                 :           3874 :     _SPI_end_call(true);
 9867                           802                 :           3874 :     return res;
                                803                 :                : }
                                804                 :                : 
                                805                 :                : /*
                                806                 :                :  * SPI_execute_with_args -- plan and execute a query with supplied arguments
                                807                 :                :  *
                                808                 :                :  * This is functionally equivalent to SPI_prepare followed by
                                809                 :                :  * SPI_execute_plan.
                                810                 :                :  */
                                811                 :                : int
 6367 tgl@sss.pgh.pa.us         812                 :UBC           0 : SPI_execute_with_args(const char *src,
                                813                 :                :                       int nargs, Oid *argtypes,
                                814                 :                :                       Datum *Values, const char *Nulls,
                                815                 :                :                       bool read_only, long tcount)
                                816                 :                : {
                                817                 :                :     int         res;
                                818                 :                :     _SPI_plan   plan;
                                819                 :                :     ParamListInfo paramLI;
                                820                 :                :     SPIExecuteOptions options;
                                821                 :                : 
                                822   [ #  #  #  #  :              0 :     if (src == NULL || nargs < 0 || tcount < 0)
                                              #  # ]
                                823                 :              0 :         return SPI_ERROR_ARGUMENT;
                                824                 :                : 
                                825   [ #  #  #  #  :              0 :     if (nargs > 0 && (argtypes == NULL || Values == NULL))
                                              #  # ]
                                826                 :              0 :         return SPI_ERROR_PARAM;
                                827                 :                : 
                                828                 :              0 :     res = _SPI_begin_call(true);
                                829         [ #  # ]:              0 :     if (res < 0)
                                830                 :              0 :         return res;
                                831                 :                : 
                                832                 :              0 :     memset(&plan, 0, sizeof(_SPI_plan));
                                833                 :              0 :     plan.magic = _SPI_PLAN_MAGIC;
 1706                           834                 :              0 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 3088 rhaas@postgresql.org      835                 :              0 :     plan.cursor_options = CURSOR_OPT_PARALLEL_OK;
 6367 tgl@sss.pgh.pa.us         836                 :              0 :     plan.nargs = nargs;
                                837                 :              0 :     plan.argtypes = argtypes;
 5785                           838                 :              0 :     plan.parserSetup = NULL;
                                839                 :              0 :     plan.parserSetupArg = NULL;
                                840                 :                : 
 6367                           841                 :              0 :     paramLI = _SPI_convert_params(nargs, argtypes,
                                842                 :                :                                   Values, Nulls);
                                843                 :                : 
 4628                           844                 :              0 :     _SPI_prepare_oneshot_plan(src, &plan);
                                845                 :                : 
 1434                           846                 :              0 :     memset(&options, 0, sizeof(options));
                                847                 :              0 :     options.params = paramLI;
                                848                 :              0 :     options.read_only = read_only;
                                849                 :              0 :     options.tcount = tcount;
                                850                 :                : 
                                851                 :              0 :     res = _SPI_execute_plan(&plan, &options,
                                852                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                853                 :                :                             true);
                                854                 :                : 
 1912                           855                 :              0 :     _SPI_end_call(true);
                                856                 :              0 :     return res;
                                857                 :                : }
                                858                 :                : 
                                859                 :                : SPIPlanPtr
 8286 tgl@sss.pgh.pa.us         860                 :CBC        2610 : SPI_prepare(const char *src, int nargs, Oid *argtypes)
                                861                 :                : {
 6718                           862                 :           2610 :     return SPI_prepare_cursor(src, nargs, argtypes, 0);
                                863                 :                : }
                                864                 :                : 
                                865                 :                : SPIPlanPtr
                                866                 :           2610 : SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes,
                                867                 :                :                    int cursorOptions)
                                868                 :                : {
                                869                 :                :     _SPI_plan   plan;
                                870                 :                :     SPIPlanPtr  result;
                                871                 :                : 
10207 vadim4o@yahoo.com         872   [ +  -  +  -  :           2610 :     if (src == NULL || nargs < 0 || (nargs > 0 && argtypes == NULL))
                                        +  +  -  + ]
                                873                 :                :     {
10226 bruce@momjian.us          874                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
 9867                           875                 :              0 :         return NULL;
                                876                 :                :     }
                                877                 :                : 
10226 bruce@momjian.us          878                 :CBC        2610 :     SPI_result = _SPI_begin_call(true);
                                879         [ -  + ]:           2610 :     if (SPI_result < 0)
 9867 bruce@momjian.us          880                 :UBC           0 :         return NULL;
                                881                 :                : 
 6750 tgl@sss.pgh.pa.us         882                 :CBC        2610 :     memset(&plan, 0, sizeof(_SPI_plan));
                                883                 :           2610 :     plan.magic = _SPI_PLAN_MAGIC;
 1706                           884                 :           2610 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 6718                           885                 :           2610 :     plan.cursor_options = cursorOptions;
 7839                           886                 :           2610 :     plan.nargs = nargs;
                                887                 :           2610 :     plan.argtypes = argtypes;
 5785                           888                 :           2610 :     plan.parserSetup = NULL;
                                889                 :           2610 :     plan.parserSetupArg = NULL;
                                890                 :                : 
 4628                           891                 :           2610 :     _SPI_prepare_plan(src, &plan);
                                892                 :                : 
                                893                 :                :     /* copy plan to procedure context */
 5104                           894                 :           2609 :     result = _SPI_make_plan_non_temp(&plan);
                                895                 :                : 
 5785                           896                 :           2609 :     _SPI_end_call(true);
                                897                 :                : 
                                898                 :           2609 :     return result;
                                899                 :                : }
                                900                 :                : 
                                901                 :                : SPIPlanPtr
 1706                           902                 :          14602 : SPI_prepare_extended(const char *src,
                                903                 :                :                      const SPIPrepareOptions *options)
                                904                 :                : {
                                905                 :                :     _SPI_plan   plan;
                                906                 :                :     SPIPlanPtr  result;
                                907                 :                : 
                                908   [ +  -  -  + ]:          14602 :     if (src == NULL || options == NULL)
                                909                 :                :     {
 1706 tgl@sss.pgh.pa.us         910                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
                                911                 :              0 :         return NULL;
                                912                 :                :     }
                                913                 :                : 
 1706 tgl@sss.pgh.pa.us         914                 :CBC       14602 :     SPI_result = _SPI_begin_call(true);
                                915         [ -  + ]:          14602 :     if (SPI_result < 0)
 1706 tgl@sss.pgh.pa.us         916                 :UBC           0 :         return NULL;
                                917                 :                : 
 1706 tgl@sss.pgh.pa.us         918                 :CBC       14602 :     memset(&plan, 0, sizeof(_SPI_plan));
                                919                 :          14602 :     plan.magic = _SPI_PLAN_MAGIC;
                                920                 :          14602 :     plan.parse_mode = options->parseMode;
                                921                 :          14602 :     plan.cursor_options = options->cursorOptions;
                                922                 :          14602 :     plan.nargs = 0;
                                923                 :          14602 :     plan.argtypes = NULL;
                                924                 :          14602 :     plan.parserSetup = options->parserSetup;
                                925                 :          14602 :     plan.parserSetupArg = options->parserSetupArg;
                                926                 :                : 
                                927                 :          14602 :     _SPI_prepare_plan(src, &plan);
                                928                 :                : 
                                929                 :                :     /* copy plan to procedure context */
                                930                 :          14554 :     result = _SPI_make_plan_non_temp(&plan);
                                931                 :                : 
                                932                 :          14554 :     _SPI_end_call(true);
                                933                 :                : 
                                934                 :          14554 :     return result;
                                935                 :                : }
                                936                 :                : 
                                937                 :                : SPIPlanPtr
 5785 tgl@sss.pgh.pa.us         938                 :UBC           0 : SPI_prepare_params(const char *src,
                                939                 :                :                    ParserSetupHook parserSetup,
                                940                 :                :                    void *parserSetupArg,
                                941                 :                :                    int cursorOptions)
                                942                 :                : {
                                943                 :                :     _SPI_plan   plan;
                                944                 :                :     SPIPlanPtr  result;
                                945                 :                : 
                                946         [ #  # ]:              0 :     if (src == NULL)
                                947                 :                :     {
                                948                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
                                949                 :              0 :         return NULL;
                                950                 :                :     }
                                951                 :                : 
                                952                 :              0 :     SPI_result = _SPI_begin_call(true);
                                953         [ #  # ]:              0 :     if (SPI_result < 0)
                                954                 :              0 :         return NULL;
                                955                 :                : 
                                956                 :              0 :     memset(&plan, 0, sizeof(_SPI_plan));
                                957                 :              0 :     plan.magic = _SPI_PLAN_MAGIC;
 1706                           958                 :              0 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 5785                           959                 :              0 :     plan.cursor_options = cursorOptions;
                                960                 :              0 :     plan.nargs = 0;
                                961                 :              0 :     plan.argtypes = NULL;
                                962                 :              0 :     plan.parserSetup = parserSetup;
                                963                 :              0 :     plan.parserSetupArg = parserSetupArg;
                                964                 :                : 
 4628                           965                 :              0 :     _SPI_prepare_plan(src, &plan);
                                966                 :                : 
                                967                 :                :     /* copy plan to procedure context */
 5104                           968                 :              0 :     result = _SPI_make_plan_non_temp(&plan);
                                969                 :                : 
10226 bruce@momjian.us          970                 :              0 :     _SPI_end_call(true);
                                971                 :                : 
 6750 tgl@sss.pgh.pa.us         972                 :              0 :     return result;
                                973                 :                : }
                                974                 :                : 
                                975                 :                : int
 5104 tgl@sss.pgh.pa.us         976                 :CBC       16404 : SPI_keepplan(SPIPlanPtr plan)
                                977                 :                : {
                                978                 :                :     ListCell   *lc;
                                979                 :                : 
 4628                           980   [ +  -  +  - ]:          16404 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
                                981   [ +  -  -  + ]:          16404 :         plan->saved || plan->oneshot)
 5104 tgl@sss.pgh.pa.us         982                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                983                 :                : 
                                984                 :                :     /*
                                985                 :                :      * Mark it saved, reparent it under CacheMemoryContext, and mark all the
                                986                 :                :      * component CachedPlanSources as saved.  This sequence cannot fail
                                987                 :                :      * partway through, so there's no risk of long-term memory leakage.
                                988                 :                :      */
 5104 tgl@sss.pgh.pa.us         989                 :CBC       16404 :     plan->saved = true;
                                990                 :          16404 :     MemoryContextSetParent(plan->plancxt, CacheMemoryContext);
                                991                 :                : 
                                992   [ +  -  +  +  :          32808 :     foreach(lc, plan->plancache_list)
                                              +  + ]
                                993                 :                :     {
                                994                 :          16404 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                                995                 :                : 
                                996                 :          16404 :         SaveCachedPlan(plansource);
                                997                 :                :     }
                                998                 :                : 
                                999                 :          16404 :     return 0;
                               1000                 :                : }
                               1001                 :                : 
                               1002                 :                : SPIPlanPtr
 6750 tgl@sss.pgh.pa.us        1003                 :UBC           0 : SPI_saveplan(SPIPlanPtr plan)
                               1004                 :                : {
                               1005                 :                :     SPIPlanPtr  newplan;
                               1006                 :                : 
 5104                          1007   [ #  #  #  # ]:              0 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
                               1008                 :                :     {
10226 bruce@momjian.us         1009                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
 9867                          1010                 :              0 :         return NULL;
                               1011                 :                :     }
                               1012                 :                : 
 2999 tgl@sss.pgh.pa.us        1013                 :              0 :     SPI_result = _SPI_begin_call(false);    /* don't change context */
10226 bruce@momjian.us         1014         [ #  # ]:              0 :     if (SPI_result < 0)
 9867                          1015                 :              0 :         return NULL;
                               1016                 :                : 
 6750 tgl@sss.pgh.pa.us        1017                 :              0 :     newplan = _SPI_save_plan(plan);
                               1018                 :                : 
 5104                          1019                 :              0 :     SPI_result = _SPI_end_call(false);
                               1020                 :                : 
 6750                          1021                 :              0 :     return newplan;
                               1022                 :                : }
                               1023                 :                : 
                               1024                 :                : int
 6750 tgl@sss.pgh.pa.us        1025                 :CBC        3812 : SPI_freeplan(SPIPlanPtr plan)
                               1026                 :                : {
                               1027                 :                :     ListCell   *lc;
                               1028                 :                : 
                               1029   [ +  -  -  + ]:           3812 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
 8874 JanWieck@Yahoo.com       1030                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                               1031                 :                : 
                               1032                 :                :     /* Release the plancache entries */
 5104 tgl@sss.pgh.pa.us        1033   [ +  -  +  +  :CBC        7624 :     foreach(lc, plan->plancache_list)
                                              +  + ]
                               1034                 :                :     {
                               1035                 :           3812 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               1036                 :                : 
                               1037                 :           3812 :         DropCachedPlan(plansource);
                               1038                 :                :     }
                               1039                 :                : 
                               1040                 :                :     /* Now get rid of the _SPI_plan and subsidiary data in its plancxt */
 6750                          1041                 :           3812 :     MemoryContextDelete(plan->plancxt);
                               1042                 :                : 
 8874 JanWieck@Yahoo.com       1043                 :           3812 :     return 0;
                               1044                 :                : }
                               1045                 :                : 
                               1046                 :                : HeapTuple
10221 vadim4o@yahoo.com        1047                 :           1092 : SPI_copytuple(HeapTuple tuple)
                               1048                 :                : {
                               1049                 :                :     MemoryContext oldcxt;
                               1050                 :                :     HeapTuple   ctuple;
                               1051                 :                : 
                               1052         [ -  + ]:           1092 :     if (tuple == NULL)
                               1053                 :                :     {
10221 vadim4o@yahoo.com        1054                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
 9867 bruce@momjian.us         1055                 :              0 :         return NULL;
                               1056                 :                :     }
                               1057                 :                : 
 3224 tgl@sss.pgh.pa.us        1058         [ -  + ]:CBC        1092 :     if (_SPI_current == NULL)
                               1059                 :                :     {
 3224 tgl@sss.pgh.pa.us        1060                 :UBC           0 :         SPI_result = SPI_ERROR_UNCONNECTED;
                               1061                 :              0 :         return NULL;
                               1062                 :                :     }
                               1063                 :                : 
 3224 tgl@sss.pgh.pa.us        1064                 :CBC        1092 :     oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);
                               1065                 :                : 
10221 vadim4o@yahoo.com        1066                 :           1092 :     ctuple = heap_copytuple(tuple);
                               1067                 :                : 
 3224 tgl@sss.pgh.pa.us        1068                 :           1092 :     MemoryContextSwitchTo(oldcxt);
                               1069                 :                : 
 9867 bruce@momjian.us         1070                 :           1092 :     return ctuple;
                               1071                 :                : }
                               1072                 :                : 
                               1073                 :                : HeapTupleHeader
 7828 tgl@sss.pgh.pa.us        1074                 :           3076 : SPI_returntuple(HeapTuple tuple, TupleDesc tupdesc)
                               1075                 :                : {
                               1076                 :                :     MemoryContext oldcxt;
                               1077                 :                :     HeapTupleHeader dtup;
                               1078                 :                : 
 8706                          1079   [ +  -  -  + ]:           3076 :     if (tuple == NULL || tupdesc == NULL)
                               1080                 :                :     {
 8706 tgl@sss.pgh.pa.us        1081                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
                               1082                 :              0 :         return NULL;
                               1083                 :                :     }
                               1084                 :                : 
 3224 tgl@sss.pgh.pa.us        1085         [ -  + ]:CBC        3076 :     if (_SPI_current == NULL)
                               1086                 :                :     {
 3224 tgl@sss.pgh.pa.us        1087                 :UBC           0 :         SPI_result = SPI_ERROR_UNCONNECTED;
                               1088                 :              0 :         return NULL;
                               1089                 :                :     }
                               1090                 :                : 
                               1091                 :                :     /* For RECORD results, make sure a typmod has been assigned */
 7828 tgl@sss.pgh.pa.us        1092         [ +  + ]:CBC        3076 :     if (tupdesc->tdtypeid == RECORDOID &&
                               1093         [ -  + ]:           3068 :         tupdesc->tdtypmod < 0)
 7828 tgl@sss.pgh.pa.us        1094                 :UBC           0 :         assign_record_type_typmod(tupdesc);
                               1095                 :                : 
 3224 tgl@sss.pgh.pa.us        1096                 :CBC        3076 :     oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);
                               1097                 :                : 
 4146                          1098                 :           3076 :     dtup = DatumGetHeapTupleHeader(heap_copy_tuple_as_datum(tuple, tupdesc));
                               1099                 :                : 
 3224                          1100                 :           3076 :     MemoryContextSwitchTo(oldcxt);
                               1101                 :                : 
 7828                          1102                 :           3076 :     return dtup;
                               1103                 :                : }
                               1104                 :                : 
                               1105                 :                : HeapTuple
10221 vadim4o@yahoo.com        1106                 :UBC           0 : SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum,
                               1107                 :                :                 Datum *Values, const char *Nulls)
                               1108                 :                : {
                               1109                 :                :     MemoryContext oldcxt;
                               1110                 :                :     HeapTuple   mtuple;
                               1111                 :                :     int         numberOfAttributes;
                               1112                 :                :     Datum      *v;
                               1113                 :                :     bool       *n;
                               1114                 :                :     int         i;
                               1115                 :                : 
 8026 tgl@sss.pgh.pa.us        1116   [ #  #  #  #  :              0 :     if (rel == NULL || tuple == NULL || natts < 0 || attnum == NULL || Values == NULL)
                                     #  #  #  #  #  
                                                 # ]
                               1117                 :                :     {
10221 vadim4o@yahoo.com        1118                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
 9867 bruce@momjian.us         1119                 :              0 :         return NULL;
                               1120                 :                :     }
                               1121                 :                : 
 3224 tgl@sss.pgh.pa.us        1122         [ #  # ]:              0 :     if (_SPI_current == NULL)
                               1123                 :                :     {
                               1124                 :              0 :         SPI_result = SPI_ERROR_UNCONNECTED;
                               1125                 :              0 :         return NULL;
                               1126                 :                :     }
                               1127                 :                : 
                               1128                 :              0 :     oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);
                               1129                 :                : 
10221 vadim4o@yahoo.com        1130                 :              0 :     SPI_result = 0;
                               1131                 :                : 
                               1132                 :              0 :     numberOfAttributes = rel->rd_att->natts;
                               1133                 :              0 :     v = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
 6152 tgl@sss.pgh.pa.us        1134                 :              0 :     n = (bool *) palloc(numberOfAttributes * sizeof(bool));
                               1135                 :                : 
                               1136                 :                :     /* fetch old values and nulls */
                               1137                 :              0 :     heap_deform_tuple(tuple, rel->rd_att, v, n);
                               1138                 :                : 
                               1139                 :                :     /* replace values and nulls */
10221 vadim4o@yahoo.com        1140         [ #  # ]:              0 :     for (i = 0; i < natts; i++)
                               1141                 :                :     {
                               1142   [ #  #  #  # ]:              0 :         if (attnum[i] <= 0 || attnum[i] > numberOfAttributes)
                               1143                 :                :             break;
                               1144                 :              0 :         v[attnum[i] - 1] = Values[i];
 1459 michael@paquier.xyz      1145   [ #  #  #  # ]:              0 :         n[attnum[i] - 1] = (Nulls && Nulls[i] == 'n');
                               1146                 :                :     }
                               1147                 :                : 
 9867 bruce@momjian.us         1148         [ #  # ]:              0 :     if (i == natts)             /* no errors in *attnum */
                               1149                 :                :     {
 6152 tgl@sss.pgh.pa.us        1150                 :              0 :         mtuple = heap_form_tuple(rel->rd_att, v, n);
                               1151                 :                : 
                               1152                 :                :         /*
                               1153                 :                :          * copy the identification info of the old tuple: t_ctid, t_self, and
                               1154                 :                :          * OID (if any)
                               1155                 :                :          */
 8405                          1156                 :              0 :         mtuple->t_data->t_ctid = tuple->t_data->t_ctid;
                               1157                 :              0 :         mtuple->t_self = tuple->t_self;
                               1158                 :              0 :         mtuple->t_tableOid = tuple->t_tableOid;
                               1159                 :                :     }
                               1160                 :                :     else
                               1161                 :                :     {
10221 vadim4o@yahoo.com        1162                 :              0 :         mtuple = NULL;
                               1163                 :              0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
                               1164                 :                :     }
                               1165                 :                : 
                               1166                 :              0 :     pfree(v);
                               1167                 :              0 :     pfree(n);
                               1168                 :                : 
 3224 tgl@sss.pgh.pa.us        1169                 :              0 :     MemoryContextSwitchTo(oldcxt);
                               1170                 :                : 
 9867 bruce@momjian.us         1171                 :              0 :     return mtuple;
                               1172                 :                : }
                               1173                 :                : 
                               1174                 :                : int
 8286 tgl@sss.pgh.pa.us        1175                 :CBC       11233 : SPI_fnumber(TupleDesc tupdesc, const char *fname)
                               1176                 :                : {
                               1177                 :                :     int         res;
                               1178                 :                :     const FormData_pg_attribute *sysatt;
                               1179                 :                : 
10226 bruce@momjian.us         1180         [ +  + ]:          60468 :     for (res = 0; res < tupdesc->natts; res++)
                               1181                 :                :     {
 2939 andres@anarazel.de       1182                 :          60461 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, res);
                               1183                 :                : 
                               1184         [ +  + ]:          60461 :         if (namestrcmp(&attr->attname, fname) == 0 &&
                               1185         [ +  - ]:          11226 :             !attr->attisdropped)
 9867 bruce@momjian.us         1186                 :          11226 :             return res + 1;
                               1187                 :                :     }
                               1188                 :                : 
 2482 andres@anarazel.de       1189                 :              7 :     sysatt = SystemAttributeByName(fname);
 8719 tgl@sss.pgh.pa.us        1190         [ -  + ]:              7 :     if (sysatt != NULL)
 8719 tgl@sss.pgh.pa.us        1191                 :UBC           0 :         return sysatt->attnum;
                               1192                 :                : 
                               1193                 :                :     /* SPI_ERROR_NOATTRIBUTE is different from all sys column numbers */
 9867 bruce@momjian.us         1194                 :CBC           7 :     return SPI_ERROR_NOATTRIBUTE;
                               1195                 :                : }
                               1196                 :                : 
                               1197                 :                : char *
10222 vadim4o@yahoo.com        1198                 :            486 : SPI_fname(TupleDesc tupdesc, int fnumber)
                               1199                 :                : {
                               1200                 :                :     const FormData_pg_attribute *att;
                               1201                 :                : 
                               1202                 :            486 :     SPI_result = 0;
                               1203                 :                : 
 8719 tgl@sss.pgh.pa.us        1204   [ +  -  +  -  :            486 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              -  + ]
                               1205                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1206                 :                :     {
10222 vadim4o@yahoo.com        1207                 :UBC           0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
 9867 bruce@momjian.us         1208                 :              0 :         return NULL;
                               1209                 :                :     }
                               1210                 :                : 
 8719 tgl@sss.pgh.pa.us        1211         [ +  - ]:CBC         486 :     if (fnumber > 0)
 2939 andres@anarazel.de       1212                 :            486 :         att = TupleDescAttr(tupdesc, fnumber - 1);
                               1213                 :                :     else
 2482 andres@anarazel.de       1214                 :UBC           0 :         att = SystemAttributeDefinition(fnumber);
                               1215                 :                : 
 8719 tgl@sss.pgh.pa.us        1216                 :CBC         486 :     return pstrdup(NameStr(att->attname));
                               1217                 :                : }
                               1218                 :                : 
                               1219                 :                : char *
10226 bruce@momjian.us         1220                 :           4872 : SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
                               1221                 :                : {
                               1222                 :                :     Datum       val;
                               1223                 :                :     bool        isnull;
                               1224                 :                :     Oid         typoid,
                               1225                 :                :                 foutoid;
                               1226                 :                :     bool        typisvarlena;
                               1227                 :                : 
                               1228                 :           4872 :     SPI_result = 0;
                               1229                 :                : 
 6169 tgl@sss.pgh.pa.us        1230   [ +  -  +  -  :           4872 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              -  + ]
                               1231                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1232                 :                :     {
10204 vadim4o@yahoo.com        1233                 :UBC           0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
 9867 bruce@momjian.us         1234                 :              0 :         return NULL;
                               1235                 :                :     }
                               1236                 :                : 
 4325 tgl@sss.pgh.pa.us        1237                 :CBC        4872 :     val = heap_getattr(tuple, fnumber, tupdesc, &isnull);
10226 bruce@momjian.us         1238         [ +  + ]:           4872 :     if (isnull)
 9867                          1239                 :             66 :         return NULL;
                               1240                 :                : 
 8719 tgl@sss.pgh.pa.us        1241         [ +  - ]:           4806 :     if (fnumber > 0)
 2939 andres@anarazel.de       1242                 :           4806 :         typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
                               1243                 :                :     else
 2482 andres@anarazel.de       1244                 :UBC           0 :         typoid = (SystemAttributeDefinition(fnumber))->atttypid;
                               1245                 :                : 
 7433 tgl@sss.pgh.pa.us        1246                 :CBC        4806 :     getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
                               1247                 :                : 
 4325                          1248                 :           4806 :     return OidOutputFunctionCall(foutoid, val);
                               1249                 :                : }
                               1250                 :                : 
                               1251                 :                : Datum
10054 bruce@momjian.us         1252                 :          28937 : SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
                               1253                 :                : {
10226                          1254                 :          28937 :     SPI_result = 0;
                               1255                 :                : 
 6169 tgl@sss.pgh.pa.us        1256   [ +  -  +  -  :          28937 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              -  + ]
                               1257                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1258                 :                :     {
10204 vadim4o@yahoo.com        1259                 :UBC           0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
 8719 tgl@sss.pgh.pa.us        1260                 :              0 :         *isnull = true;
   29 tgl@sss.pgh.pa.us        1261                 :UNC           0 :         return (Datum) 0;
                               1262                 :                :     }
                               1263                 :                : 
 8719 tgl@sss.pgh.pa.us        1264                 :CBC       28937 :     return heap_getattr(tuple, fnumber, tupdesc, isnull);
                               1265                 :                : }
                               1266                 :                : 
                               1267                 :                : char *
10226 bruce@momjian.us         1268                 :              4 : SPI_gettype(TupleDesc tupdesc, int fnumber)
                               1269                 :                : {
                               1270                 :                :     Oid         typoid;
                               1271                 :                :     HeapTuple   typeTuple;
                               1272                 :                :     char       *result;
                               1273                 :                : 
                               1274                 :              4 :     SPI_result = 0;
                               1275                 :                : 
 8719 tgl@sss.pgh.pa.us        1276   [ +  -  +  -  :              4 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              -  + ]
                               1277                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1278                 :                :     {
10226 bruce@momjian.us         1279                 :UBC           0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
 9867                          1280                 :              0 :         return NULL;
                               1281                 :                :     }
                               1282                 :                : 
 8719 tgl@sss.pgh.pa.us        1283         [ +  - ]:CBC           4 :     if (fnumber > 0)
 2939 andres@anarazel.de       1284                 :              4 :         typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
                               1285                 :                :     else
 2482 andres@anarazel.de       1286                 :UBC           0 :         typoid = (SystemAttributeDefinition(fnumber))->atttypid;
                               1287                 :                : 
 5683 rhaas@postgresql.org     1288                 :CBC           4 :     typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typoid));
                               1289                 :                : 
10226 bruce@momjian.us         1290         [ -  + ]:              4 :     if (!HeapTupleIsValid(typeTuple))
                               1291                 :                :     {
10226 bruce@momjian.us         1292                 :UBC           0 :         SPI_result = SPI_ERROR_TYPUNKNOWN;
 9867                          1293                 :              0 :         return NULL;
                               1294                 :                :     }
                               1295                 :                : 
 9060 tgl@sss.pgh.pa.us        1296                 :CBC           4 :     result = pstrdup(NameStr(((Form_pg_type) GETSTRUCT(typeTuple))->typname));
                               1297                 :              4 :     ReleaseSysCache(typeTuple);
                               1298                 :              4 :     return result;
                               1299                 :                : }
                               1300                 :                : 
                               1301                 :                : /*
                               1302                 :                :  * Get the data type OID for a column.
                               1303                 :                :  *
                               1304                 :                :  * There's nothing similar for typmod and typcollation.  The rare consumers
                               1305                 :                :  * thereof should inspect the TupleDesc directly.
                               1306                 :                :  */
                               1307                 :                : Oid
10226 bruce@momjian.us         1308                 :            602 : SPI_gettypeid(TupleDesc tupdesc, int fnumber)
                               1309                 :                : {
                               1310                 :            602 :     SPI_result = 0;
                               1311                 :                : 
 8719 tgl@sss.pgh.pa.us        1312   [ +  -  +  -  :            602 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              -  + ]
                               1313                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1314                 :                :     {
10226 bruce@momjian.us         1315                 :UBC           0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
 9867                          1316                 :              0 :         return InvalidOid;
                               1317                 :                :     }
                               1318                 :                : 
 8719 tgl@sss.pgh.pa.us        1319         [ +  - ]:CBC         602 :     if (fnumber > 0)
 2939 andres@anarazel.de       1320                 :            602 :         return TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
                               1321                 :                :     else
 2482 andres@anarazel.de       1322                 :UBC           0 :         return (SystemAttributeDefinition(fnumber))->atttypid;
                               1323                 :                : }
                               1324                 :                : 
                               1325                 :                : char *
10226 bruce@momjian.us         1326                 :CBC         183 : SPI_getrelname(Relation rel)
                               1327                 :                : {
 9435                          1328                 :            183 :     return pstrdup(RelationGetRelationName(rel));
                               1329                 :                : }
                               1330                 :                : 
                               1331                 :                : char *
 7466 neilc@samurai.com        1332                 :            141 : SPI_getnspname(Relation rel)
                               1333                 :                : {
 7266 bruce@momjian.us         1334                 :            141 :     return get_namespace_name(RelationGetNamespace(rel));
                               1335                 :                : }
                               1336                 :                : 
                               1337                 :                : void *
10054                          1338                 :             19 : SPI_palloc(Size size)
                               1339                 :                : {
 3224 tgl@sss.pgh.pa.us        1340         [ -  + ]:             19 :     if (_SPI_current == NULL)
 3224 tgl@sss.pgh.pa.us        1341         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_palloc called while not connected to SPI");
                               1342                 :                : 
 3224 tgl@sss.pgh.pa.us        1343                 :CBC          19 :     return MemoryContextAlloc(_SPI_current->savedcxt, size);
                               1344                 :                : }
                               1345                 :                : 
                               1346                 :                : void *
10054 bruce@momjian.us         1347                 :UBC           0 : SPI_repalloc(void *pointer, Size size)
                               1348                 :                : {
                               1349                 :                :     /* No longer need to worry which context chunk was in... */
 9201 tgl@sss.pgh.pa.us        1350                 :              0 :     return repalloc(pointer, size);
                               1351                 :                : }
                               1352                 :                : 
                               1353                 :                : void
10054 bruce@momjian.us         1354                 :              0 : SPI_pfree(void *pointer)
                               1355                 :                : {
                               1356                 :                :     /* No longer need to worry which context chunk was in... */
                               1357                 :              0 :     pfree(pointer);
10209 vadim4o@yahoo.com        1358                 :              0 : }
                               1359                 :                : 
                               1360                 :                : Datum
 3768 tgl@sss.pgh.pa.us        1361                 :CBC        3021 : SPI_datumTransfer(Datum value, bool typByVal, int typLen)
                               1362                 :                : {
                               1363                 :                :     MemoryContext oldcxt;
                               1364                 :                :     Datum       result;
                               1365                 :                : 
 3224                          1366         [ -  + ]:           3021 :     if (_SPI_current == NULL)
 3224 tgl@sss.pgh.pa.us        1367         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_datumTransfer called while not connected to SPI");
                               1368                 :                : 
 3224 tgl@sss.pgh.pa.us        1369                 :CBC        3021 :     oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);
                               1370                 :                : 
 3768                          1371                 :           3021 :     result = datumTransfer(value, typByVal, typLen);
                               1372                 :                : 
 3224                          1373                 :           3021 :     MemoryContextSwitchTo(oldcxt);
                               1374                 :                : 
 3768                          1375                 :           3021 :     return result;
                               1376                 :                : }
                               1377                 :                : 
                               1378                 :                : void
 9396 JanWieck@Yahoo.com       1379                 :UBC           0 : SPI_freetuple(HeapTuple tuple)
                               1380                 :                : {
                               1381                 :                :     /* No longer need to worry which context tuple was in... */
                               1382                 :              0 :     heap_freetuple(tuple);
                               1383                 :              0 : }
                               1384                 :                : 
                               1385                 :                : void
 8874 JanWieck@Yahoo.com       1386                 :CBC       89624 : SPI_freetuptable(SPITupleTable *tuptable)
                               1387                 :                : {
 4426 tgl@sss.pgh.pa.us        1388                 :          89624 :     bool        found = false;
                               1389                 :                : 
                               1390                 :                :     /* ignore call if NULL pointer */
                               1391         [ +  + ]:          89624 :     if (tuptable == NULL)
                               1392                 :          50514 :         return;
                               1393                 :                : 
                               1394                 :                :     /*
                               1395                 :                :      * Search only the topmost SPI context for a matching tuple table.
                               1396                 :                :      */
 3224                          1397         [ +  - ]:          39110 :     if (_SPI_current != NULL)
                               1398                 :                :     {
                               1399                 :                :         slist_mutable_iter siter;
                               1400                 :                : 
                               1401                 :                :         /* find tuptable in active list, then remove it */
 4426                          1402   [ +  -  +  -  :          39112 :         slist_foreach_modify(siter, &_SPI_current->tuptables)
                                              +  - ]
                               1403                 :                :         {
                               1404                 :                :             SPITupleTable *tt;
                               1405                 :                : 
                               1406                 :          39112 :             tt = slist_container(SPITupleTable, next, siter.cur);
                               1407         [ +  + ]:          39112 :             if (tt == tuptable)
                               1408                 :                :             {
                               1409                 :          39110 :                 slist_delete_current(&siter);
                               1410                 :          39110 :                 found = true;
                               1411                 :          39110 :                 break;
                               1412                 :                :             }
                               1413                 :                :         }
                               1414                 :                :     }
                               1415                 :                : 
                               1416                 :                :     /*
                               1417                 :                :      * Refuse the deletion if we didn't find it in the topmost SPI context.
                               1418                 :                :      * This is primarily a guard against double deletion, but might prevent
                               1419                 :                :      * other errors as well.  Since the worst consequence of not deleting a
                               1420                 :                :      * tuptable would be a transient memory leak, this is just a WARNING.
                               1421                 :                :      */
                               1422         [ -  + ]:          39110 :     if (!found)
                               1423                 :                :     {
 4426 tgl@sss.pgh.pa.us        1424         [ #  # ]:UBC           0 :         elog(WARNING, "attempt to delete invalid SPITupleTable %p", tuptable);
                               1425                 :              0 :         return;
                               1426                 :                :     }
                               1427                 :                : 
                               1428                 :                :     /* for safety, reset global variables that might point at tuptable */
 4426 tgl@sss.pgh.pa.us        1429         [ -  + ]:CBC       39110 :     if (tuptable == _SPI_current->tuptable)
 4426 tgl@sss.pgh.pa.us        1430                 :UBC           0 :         _SPI_current->tuptable = NULL;
 4426 tgl@sss.pgh.pa.us        1431         [ +  + ]:CBC       39110 :     if (tuptable == SPI_tuptable)
                               1432                 :          35245 :         SPI_tuptable = NULL;
                               1433                 :                : 
                               1434                 :                :     /* release all memory belonging to tuptable */
                               1435                 :          39110 :     MemoryContextDelete(tuptable->tuptabcxt);
                               1436                 :                : }
                               1437                 :                : 
                               1438                 :                : 
                               1439                 :                : /*
                               1440                 :                :  * SPI_cursor_open()
                               1441                 :                :  *
                               1442                 :                :  *  Open a prepared SPI plan as a portal
                               1443                 :                :  */
                               1444                 :                : Portal
 6750                          1445                 :            107 : SPI_cursor_open(const char *name, SPIPlanPtr plan,
                               1446                 :                :                 Datum *Values, const char *Nulls,
                               1447                 :                :                 bool read_only)
                               1448                 :                : {
                               1449                 :                :     Portal      portal;
                               1450                 :                :     ParamListInfo paramLI;
                               1451                 :                : 
                               1452                 :                :     /* build transient ParamListInfo in caller's context */
 5785                          1453                 :            107 :     paramLI = _SPI_convert_params(plan->nargs, plan->argtypes,
                               1454                 :                :                                   Values, Nulls);
                               1455                 :                : 
                               1456                 :            107 :     portal = SPI_cursor_open_internal(name, plan, paramLI, read_only);
                               1457                 :                : 
                               1458                 :                :     /* done with the transient ParamListInfo */
                               1459         [ +  + ]:            107 :     if (paramLI)
                               1460                 :              4 :         pfree(paramLI);
                               1461                 :                : 
                               1462                 :            107 :     return portal;
                               1463                 :                : }
                               1464                 :                : 
                               1465                 :                : 
                               1466                 :                : /*
                               1467                 :                :  * SPI_cursor_open_with_args()
                               1468                 :                :  *
                               1469                 :                :  * Parse and plan a query and open it as a portal.
                               1470                 :                :  */
                               1471                 :                : Portal
 6306 tgl@sss.pgh.pa.us        1472                 :UBC           0 : SPI_cursor_open_with_args(const char *name,
                               1473                 :                :                           const char *src,
                               1474                 :                :                           int nargs, Oid *argtypes,
                               1475                 :                :                           Datum *Values, const char *Nulls,
                               1476                 :                :                           bool read_only, int cursorOptions)
                               1477                 :                : {
                               1478                 :                :     Portal      result;
                               1479                 :                :     _SPI_plan   plan;
                               1480                 :                :     ParamListInfo paramLI;
                               1481                 :                : 
                               1482   [ #  #  #  # ]:              0 :     if (src == NULL || nargs < 0)
                               1483         [ #  # ]:              0 :         elog(ERROR, "SPI_cursor_open_with_args called with invalid arguments");
                               1484                 :                : 
                               1485   [ #  #  #  #  :              0 :     if (nargs > 0 && (argtypes == NULL || Values == NULL))
                                              #  # ]
                               1486         [ #  # ]:              0 :         elog(ERROR, "SPI_cursor_open_with_args called with missing parameters");
                               1487                 :                : 
                               1488                 :              0 :     SPI_result = _SPI_begin_call(true);
                               1489         [ #  # ]:              0 :     if (SPI_result < 0)
                               1490         [ #  # ]:              0 :         elog(ERROR, "SPI_cursor_open_with_args called while not connected");
                               1491                 :                : 
                               1492                 :              0 :     memset(&plan, 0, sizeof(_SPI_plan));
                               1493                 :              0 :     plan.magic = _SPI_PLAN_MAGIC;
 1706                          1494                 :              0 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 6306                          1495                 :              0 :     plan.cursor_options = cursorOptions;
                               1496                 :              0 :     plan.nargs = nargs;
                               1497                 :              0 :     plan.argtypes = argtypes;
 5785                          1498                 :              0 :     plan.parserSetup = NULL;
                               1499                 :              0 :     plan.parserSetupArg = NULL;
                               1500                 :                : 
                               1501                 :                :     /* build transient ParamListInfo in executor context */
 6306                          1502                 :              0 :     paramLI = _SPI_convert_params(nargs, argtypes,
                               1503                 :                :                                   Values, Nulls);
                               1504                 :                : 
 4628                          1505                 :              0 :     _SPI_prepare_plan(src, &plan);
                               1506                 :                : 
                               1507                 :                :     /* We needn't copy the plan; SPI_cursor_open_internal will do so */
                               1508                 :                : 
 5785                          1509                 :              0 :     result = SPI_cursor_open_internal(name, &plan, paramLI, read_only);
                               1510                 :                : 
                               1511                 :                :     /* And clean up */
 6306                          1512                 :              0 :     _SPI_end_call(true);
                               1513                 :                : 
                               1514                 :              0 :     return result;
                               1515                 :                : }
                               1516                 :                : 
                               1517                 :                : 
                               1518                 :                : /*
                               1519                 :                :  * SPI_cursor_open_with_paramlist()
                               1520                 :                :  *
                               1521                 :                :  *  Same as SPI_cursor_open except that parameters (if any) are passed
                               1522                 :                :  *  as a ParamListInfo, which supports dynamic parameter set determination
                               1523                 :                :  */
                               1524                 :                : Portal
 5785 tgl@sss.pgh.pa.us        1525                 :CBC        1312 : SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan,
                               1526                 :                :                                ParamListInfo params, bool read_only)
                               1527                 :                : {
                               1528                 :           1312 :     return SPI_cursor_open_internal(name, plan, params, read_only);
                               1529                 :                : }
                               1530                 :                : 
                               1531                 :                : /* Parse a query and open it as a cursor */
                               1532                 :                : Portal
 1684                          1533                 :           4707 : SPI_cursor_parse_open(const char *name,
                               1534                 :                :                       const char *src,
                               1535                 :                :                       const SPIParseOpenOptions *options)
                               1536                 :                : {
                               1537                 :                :     Portal      result;
                               1538                 :                :     _SPI_plan   plan;
                               1539                 :                : 
                               1540   [ +  -  -  + ]:           4707 :     if (src == NULL || options == NULL)
 1684 tgl@sss.pgh.pa.us        1541         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_cursor_parse_open called with invalid arguments");
                               1542                 :                : 
 1912 tgl@sss.pgh.pa.us        1543                 :CBC        4707 :     SPI_result = _SPI_begin_call(true);
                               1544         [ -  + ]:           4707 :     if (SPI_result < 0)
 1684 tgl@sss.pgh.pa.us        1545         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_cursor_parse_open called while not connected");
                               1546                 :                : 
 1912 tgl@sss.pgh.pa.us        1547                 :CBC        4707 :     memset(&plan, 0, sizeof(_SPI_plan));
                               1548                 :           4707 :     plan.magic = _SPI_PLAN_MAGIC;
 1706                          1549                 :           4707 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 1684                          1550                 :           4707 :     plan.cursor_options = options->cursorOptions;
                               1551         [ +  + ]:           4707 :     if (options->params)
                               1552                 :                :     {
                               1553                 :              6 :         plan.parserSetup = options->params->parserSetup;
                               1554                 :              6 :         plan.parserSetupArg = options->params->parserSetupArg;
                               1555                 :                :     }
                               1556                 :                : 
 1912                          1557                 :           4707 :     _SPI_prepare_plan(src, &plan);
                               1558                 :                : 
                               1559                 :                :     /* We needn't copy the plan; SPI_cursor_open_internal will do so */
                               1560                 :                : 
 1684                          1561                 :           4707 :     result = SPI_cursor_open_internal(name, &plan,
                               1562                 :           4707 :                                       options->params, options->read_only);
                               1563                 :                : 
                               1564                 :                :     /* And clean up */
 1912                          1565                 :           4707 :     _SPI_end_call(true);
                               1566                 :                : 
                               1567                 :           4707 :     return result;
                               1568                 :                : }
                               1569                 :                : 
                               1570                 :                : 
                               1571                 :                : /*
                               1572                 :                :  * SPI_cursor_open_internal()
                               1573                 :                :  *
                               1574                 :                :  *  Common code for SPI_cursor_open variants
                               1575                 :                :  */
                               1576                 :                : static Portal
 6306                          1577                 :           6126 : SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
                               1578                 :                :                          ParamListInfo paramLI, bool read_only)
                               1579                 :                : {
                               1580                 :                :     CachedPlanSource *plansource;
                               1581                 :                :     CachedPlan *cplan;
                               1582                 :                :     List       *stmt_list;
                               1583                 :                :     char       *query_string;
                               1584                 :                :     Snapshot    snapshot;
                               1585                 :                :     MemoryContext oldcontext;
                               1586                 :                :     Portal      portal;
                               1587                 :                :     SPICallbackArg spicallbackarg;
                               1588                 :                :     ErrorContextCallback spierrcontext;
                               1589                 :                : 
                               1590                 :                :     /*
                               1591                 :                :      * Check that the plan is something the Portal code will special-case as
                               1592                 :                :      * returning one tupleset.
                               1593                 :                :      */
 6750                          1594         [ -  + ]:           6126 :     if (!SPI_is_cursor_plan(plan))
                               1595                 :                :     {
                               1596                 :                :         /* try to give a good error message */
                               1597                 :                :         const char *cmdtag;
                               1598                 :                : 
 6750 tgl@sss.pgh.pa.us        1599         [ #  # ]:UBC           0 :         if (list_length(plan->plancache_list) != 1)
 6963                          1600         [ #  # ]:              0 :             ereport(ERROR,
                               1601                 :                :                     (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
                               1602                 :                :                      errmsg("cannot open multi-query plan as cursor")));
 6750                          1603                 :              0 :         plansource = (CachedPlanSource *) linitial(plan->plancache_list);
                               1604                 :                :         /* A SELECT that fails SPI_is_cursor_plan() must be SELECT INTO */
 1477                          1605         [ #  # ]:              0 :         if (plansource->commandTag == CMDTAG_SELECT)
                               1606                 :              0 :             cmdtag = "SELECT INTO";
                               1607                 :                :         else
                               1608                 :              0 :             cmdtag = GetCommandTagName(plansource->commandTag);
 6965                          1609         [ #  # ]:              0 :         ereport(ERROR,
                               1610                 :                :                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
                               1611                 :                :         /* translator: %s is name of a SQL command, eg INSERT */
                               1612                 :                :                  errmsg("cannot open %s query as cursor", cmdtag)));
                               1613                 :                :     }
                               1614                 :                : 
 6750 tgl@sss.pgh.pa.us        1615         [ -  + ]:CBC        6126 :     Assert(list_length(plan->plancache_list) == 1);
                               1616                 :           6126 :     plansource = (CachedPlanSource *) linitial(plan->plancache_list);
                               1617                 :                : 
                               1618                 :                :     /* Push the SPI stack */
 5104                          1619         [ -  + ]:           6126 :     if (_SPI_begin_call(true) < 0)
 6416 tgl@sss.pgh.pa.us        1620         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_cursor_open called while not connected");
                               1621                 :                : 
                               1622                 :                :     /* Reset SPI result (note we deliberately don't touch lastoid) */
 8874 JanWieck@Yahoo.com       1623                 :CBC        6126 :     SPI_processed = 0;
                               1624                 :           6126 :     SPI_tuptable = NULL;
                               1625                 :           6126 :     _SPI_current->processed = 0;
                               1626                 :           6126 :     _SPI_current->tuptable = NULL;
                               1627                 :                : 
                               1628                 :                :     /* Create the portal */
 8163 tgl@sss.pgh.pa.us        1629   [ +  +  -  + ]:           6126 :     if (name == NULL || name[0] == '\0')
                               1630                 :                :     {
                               1631                 :                :         /* Use a random nonconflicting name */
                               1632                 :           6102 :         portal = CreateNewPortal();
                               1633                 :                :     }
                               1634                 :                :     else
                               1635                 :                :     {
                               1636                 :                :         /* In this path, error if portal of same name already exists */
                               1637                 :             24 :         portal = CreatePortal(name, false, false);
                               1638                 :                :     }
                               1639                 :                : 
                               1640                 :                :     /* Copy the plan's query string into the portal */
 2821 peter_e@gmx.net          1641                 :           6126 :     query_string = MemoryContextStrdup(portal->portalContext,
                               1642                 :                :                                        plansource->query_string);
                               1643                 :                : 
                               1644                 :                :     /*
                               1645                 :                :      * Setup error traceback support for ereport(), in case GetCachedPlan
                               1646                 :                :      * throws an error.
                               1647                 :                :      */
 1706 tgl@sss.pgh.pa.us        1648                 :           6126 :     spicallbackarg.query = plansource->query_string;
                               1649                 :           6126 :     spicallbackarg.mode = plan->parse_mode;
 4602                          1650                 :           6126 :     spierrcontext.callback = _SPI_error_callback;
 1706                          1651                 :           6126 :     spierrcontext.arg = &spicallbackarg;
 4602                          1652                 :           6126 :     spierrcontext.previous = error_context_stack;
                               1653                 :           6126 :     error_context_stack = &spierrcontext;
                               1654                 :                : 
                               1655                 :                :     /*
                               1656                 :                :      * Note: for a saved plan, we mustn't have any failure occur between
                               1657                 :                :      * GetCachedPlan and PortalDefineQuery; that would result in leaking our
                               1658                 :                :      * plancache refcount.
                               1659                 :                :      */
                               1660                 :                : 
                               1661                 :                :     /* Replan if needed, and increment plan refcount for portal */
 1685                          1662                 :           6126 :     cplan = GetCachedPlan(plansource, paramLI, NULL, _SPI_current->queryEnv);
 5104                          1663                 :           6126 :     stmt_list = cplan->stmt_list;
                               1664                 :                : 
                               1665         [ +  + ]:           6126 :     if (!plan->saved)
                               1666                 :                :     {
                               1667                 :                :         /*
                               1668                 :                :          * We don't want the portal to depend on an unsaved CachedPlanSource,
                               1669                 :                :          * so must copy the plan into the portal's context.  An error here
                               1670                 :                :          * will result in leaking our refcount on the plan, but it doesn't
                               1671                 :                :          * matter because the plan is unsaved and hence transient anyway.
                               1672                 :                :          */
 2821 peter_e@gmx.net          1673                 :           4808 :         oldcontext = MemoryContextSwitchTo(portal->portalContext);
 5104 tgl@sss.pgh.pa.us        1674                 :           4808 :         stmt_list = copyObject(stmt_list);
 6750                          1675                 :           4808 :         MemoryContextSwitchTo(oldcontext);
 1685                          1676                 :           4808 :         ReleaseCachedPlan(cplan, NULL);
 6750                          1677                 :           4808 :         cplan = NULL;           /* portal shouldn't depend on cplan */
                               1678                 :                :     }
                               1679                 :                : 
                               1680                 :                :     /*
                               1681                 :                :      * Set up the portal.
                               1682                 :                :      */
 8163                          1683                 :           6126 :     PortalDefineQuery(portal,
                               1684                 :                :                       NULL,     /* no statement name */
                               1685                 :                :                       query_string,
                               1686                 :                :                       plansource->commandTag,
                               1687                 :                :                       stmt_list,
                               1688                 :                :                       cplan);
                               1689                 :                : 
                               1690                 :                :     /*
                               1691                 :                :      * Set up options for portal.  Default SCROLL type is chosen the same way
                               1692                 :                :      * as PerformCursorOpen does it.
                               1693                 :                :      */
 6718                          1694                 :           6126 :     portal->cursorOptions = plan->cursor_options;
                               1695         [ +  + ]:           6126 :     if (!(portal->cursorOptions & (CURSOR_OPT_SCROLL | CURSOR_OPT_NO_SCROLL)))
                               1696                 :                :     {
                               1697         [ +  - ]:            210 :         if (list_length(stmt_list) == 1 &&
 2999                          1698         [ +  - ]:            210 :             linitial_node(PlannedStmt, stmt_list)->commandType != CMD_UTILITY &&
 3071                          1699   [ +  +  +  + ]:            419 :             linitial_node(PlannedStmt, stmt_list)->rowMarks == NIL &&
                               1700                 :            209 :             ExecSupportsBackwardScan(linitial_node(PlannedStmt, stmt_list)->planTree))
 6718                          1701                 :            194 :             portal->cursorOptions |= CURSOR_OPT_SCROLL;
                               1702                 :                :         else
                               1703                 :             16 :             portal->cursorOptions |= CURSOR_OPT_NO_SCROLL;
                               1704                 :                :     }
                               1705                 :                : 
                               1706                 :                :     /*
                               1707                 :                :      * Disallow SCROLL with SELECT FOR UPDATE.  This is not redundant with the
                               1708                 :                :      * check in transformDeclareCursorStmt because the cursor options might
                               1709                 :                :      * not have come through there.
                               1710                 :                :      */
 6527                          1711         [ +  + ]:           6126 :     if (portal->cursorOptions & CURSOR_OPT_SCROLL)
                               1712                 :                :     {
                               1713         [ +  - ]:            207 :         if (list_length(stmt_list) == 1 &&
 2999                          1714         [ +  - ]:            207 :             linitial_node(PlannedStmt, stmt_list)->commandType != CMD_UTILITY &&
 3071                          1715         [ -  + ]:            207 :             linitial_node(PlannedStmt, stmt_list)->rowMarks != NIL)
 6527 tgl@sss.pgh.pa.us        1716         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1717                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1718                 :                :                      errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
                               1719                 :                :                      errdetail("Scrollable cursors must be READ ONLY.")));
                               1720                 :                :     }
                               1721                 :                : 
                               1722                 :                :     /* Make current query environment available to portal at execution time. */
 3077 kgrittn@postgresql.o     1723                 :CBC        6126 :     portal->queryEnv = _SPI_current->queryEnv;
                               1724                 :                : 
                               1725                 :                :     /*
                               1726                 :                :      * If told to be read-only, we'd better check for read-only queries. This
                               1727                 :                :      * can't be done earlier because we need to look at the finished, planned
                               1728                 :                :      * queries.  (In particular, we don't want to do it between GetCachedPlan
                               1729                 :                :      * and PortalDefineQuery, because throwing an error between those steps
                               1730                 :                :      * would result in leaking our plancache refcount.)
                               1731                 :                :      */
 2060 rhaas@postgresql.org     1732         [ +  + ]:           6126 :     if (read_only)
                               1733                 :                :     {
                               1734                 :                :         ListCell   *lc;
                               1735                 :                : 
 6748 tgl@sss.pgh.pa.us        1736   [ +  -  +  +  :            156 :         foreach(lc, stmt_list)
                                              +  + ]
                               1737                 :                :         {
 3071                          1738                 :             78 :             PlannedStmt *pstmt = lfirst_node(PlannedStmt, lc);
                               1739                 :                : 
 6748                          1740         [ -  + ]:             78 :             if (!CommandIsReadOnly(pstmt))
 2060 rhaas@postgresql.org     1741         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               1742                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1743                 :                :                 /* translator: %s is a SQL statement name */
                               1744                 :                :                          errmsg("%s is not allowed in a non-volatile function",
                               1745                 :                :                                 CreateCommandName((Node *) pstmt))));
                               1746                 :                :         }
                               1747                 :                :     }
                               1748                 :                : 
                               1749                 :                :     /* Set up the snapshot to use. */
 4667 tgl@sss.pgh.pa.us        1750         [ +  + ]:CBC        6126 :     if (read_only)
                               1751                 :             78 :         snapshot = GetActiveSnapshot();
                               1752                 :                :     else
                               1753                 :                :     {
                               1754                 :           6048 :         CommandCounterIncrement();
                               1755                 :           6048 :         snapshot = GetTransactionSnapshot();
                               1756                 :                :     }
                               1757                 :                : 
                               1758                 :                :     /*
                               1759                 :                :      * If the plan has parameters, copy them into the portal.  Note that this
                               1760                 :                :      * must be done after revalidating the plan, because in dynamic parameter
                               1761                 :                :      * cases the set of parameters could have changed during re-parsing.
                               1762                 :                :      */
 5785                          1763         [ +  + ]:           6126 :     if (paramLI)
                               1764                 :                :     {
 2821 peter_e@gmx.net          1765                 :            335 :         oldcontext = MemoryContextSwitchTo(portal->portalContext);
 5785 tgl@sss.pgh.pa.us        1766                 :            335 :         paramLI = copyParamList(paramLI);
                               1767                 :            335 :         MemoryContextSwitchTo(oldcontext);
                               1768                 :                :     }
                               1769                 :                : 
                               1770                 :                :     /*
                               1771                 :                :      * Start portal execution.
                               1772                 :                :      */
 4667                          1773                 :           6126 :     PortalStart(portal, paramLI, 0, snapshot);
                               1774                 :                : 
 6963                          1775         [ -  + ]:           6126 :     Assert(portal->strategy != PORTAL_MULTI_QUERY);
                               1776                 :                : 
                               1777                 :                :     /* Pop the error context stack */
 3063                          1778                 :           6126 :     error_context_stack = spierrcontext.previous;
                               1779                 :                : 
                               1780                 :                :     /* Pop the SPI stack */
 5104                          1781                 :           6126 :     _SPI_end_call(true);
                               1782                 :                : 
                               1783                 :                :     /* Return the created portal */
 8874 JanWieck@Yahoo.com       1784                 :           6126 :     return portal;
                               1785                 :                : }
                               1786                 :                : 
                               1787                 :                : 
                               1788                 :                : /*
                               1789                 :                :  * SPI_cursor_find()
                               1790                 :                :  *
                               1791                 :                :  *  Find the portal of an existing open cursor
                               1792                 :                :  */
                               1793                 :                : Portal
 8286 tgl@sss.pgh.pa.us        1794                 :            281 : SPI_cursor_find(const char *name)
                               1795                 :                : {
 8874 JanWieck@Yahoo.com       1796                 :            281 :     return GetPortalByName(name);
                               1797                 :                : }
                               1798                 :                : 
                               1799                 :                : 
                               1800                 :                : /*
                               1801                 :                :  * SPI_cursor_fetch()
                               1802                 :                :  *
                               1803                 :                :  *  Fetch rows in a cursor
                               1804                 :                :  */
                               1805                 :                : void
 6943 bruce@momjian.us         1806                 :          22003 : SPI_cursor_fetch(Portal portal, bool forward, long count)
                               1807                 :                : {
 6718 tgl@sss.pgh.pa.us        1808                 :          22003 :     _SPI_cursor_operation(portal,
                               1809                 :          22003 :                           forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
                               1810                 :                :                           CreateDestReceiver(DestSPI));
                               1811                 :                :     /* we know that the DestSPI receiver doesn't need a destroy call */
 8874 JanWieck@Yahoo.com       1812                 :          21999 : }
                               1813                 :                : 
                               1814                 :                : 
                               1815                 :                : /*
                               1816                 :                :  * SPI_cursor_move()
                               1817                 :                :  *
                               1818                 :                :  *  Move in a cursor
                               1819                 :                :  */
                               1820                 :                : void
 6943 bruce@momjian.us         1821                 :UBC           0 : SPI_cursor_move(Portal portal, bool forward, long count)
                               1822                 :                : {
 6718 tgl@sss.pgh.pa.us        1823                 :              0 :     _SPI_cursor_operation(portal,
                               1824                 :              0 :                           forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
                               1825                 :                :                           None_Receiver);
                               1826                 :              0 : }
                               1827                 :                : 
                               1828                 :                : 
                               1829                 :                : /*
                               1830                 :                :  * SPI_scroll_cursor_fetch()
                               1831                 :                :  *
                               1832                 :                :  *  Fetch rows in a scrollable cursor
                               1833                 :                :  */
                               1834                 :                : void
 6718 tgl@sss.pgh.pa.us        1835                 :CBC         151 : SPI_scroll_cursor_fetch(Portal portal, FetchDirection direction, long count)
                               1836                 :                : {
                               1837                 :            151 :     _SPI_cursor_operation(portal,
                               1838                 :                :                           direction, count,
                               1839                 :                :                           CreateDestReceiver(DestSPI));
                               1840                 :                :     /* we know that the DestSPI receiver doesn't need a destroy call */
                               1841                 :            148 : }
                               1842                 :                : 
                               1843                 :                : 
                               1844                 :                : /*
                               1845                 :                :  * SPI_scroll_cursor_move()
                               1846                 :                :  *
                               1847                 :                :  *  Move in a scrollable cursor
                               1848                 :                :  */
                               1849                 :                : void
                               1850                 :             21 : SPI_scroll_cursor_move(Portal portal, FetchDirection direction, long count)
                               1851                 :                : {
                               1852                 :             21 :     _SPI_cursor_operation(portal, direction, count, None_Receiver);
 8874 JanWieck@Yahoo.com       1853                 :             21 : }
                               1854                 :                : 
                               1855                 :                : 
                               1856                 :                : /*
                               1857                 :                :  * SPI_cursor_close()
                               1858                 :                :  *
                               1859                 :                :  *  Close a cursor
                               1860                 :                :  */
                               1861                 :                : void
                               1862                 :           6071 : SPI_cursor_close(Portal portal)
                               1863                 :                : {
 8737 tgl@sss.pgh.pa.us        1864         [ -  + ]:           6071 :     if (!PortalIsValid(portal))
 8874 JanWieck@Yahoo.com       1865         [ #  # ]:UBC           0 :         elog(ERROR, "invalid portal in SPI cursor operation");
                               1866                 :                : 
 8199 bruce@momjian.us         1867                 :CBC        6071 :     PortalDrop(portal, false);
 8874 JanWieck@Yahoo.com       1868                 :           6071 : }
                               1869                 :                : 
                               1870                 :                : /*
                               1871                 :                :  * Returns the Oid representing the type id for argument at argIndex. First
                               1872                 :                :  * parameter is at index zero.
                               1873                 :                :  */
                               1874                 :                : Oid
 6750 tgl@sss.pgh.pa.us        1875                 :UBC           0 : SPI_getargtypeid(SPIPlanPtr plan, int argIndex)
                               1876                 :                : {
                               1877   [ #  #  #  #  :              0 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
                                              #  # ]
                               1878         [ #  # ]:              0 :         argIndex < 0 || argIndex >= plan->nargs)
                               1879                 :                :     {
 7839                          1880                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
                               1881                 :              0 :         return InvalidOid;
                               1882                 :                :     }
 6750                          1883                 :              0 :     return plan->argtypes[argIndex];
                               1884                 :                : }
                               1885                 :                : 
                               1886                 :                : /*
                               1887                 :                :  * Returns the number of arguments for the prepared plan.
                               1888                 :                :  */
                               1889                 :                : int
                               1890                 :              0 : SPI_getargcount(SPIPlanPtr plan)
                               1891                 :                : {
                               1892   [ #  #  #  # ]:              0 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
                               1893                 :                :     {
 7839                          1894                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
                               1895                 :              0 :         return -1;
                               1896                 :                :     }
 6750                          1897                 :              0 :     return plan->nargs;
                               1898                 :                : }
                               1899                 :                : 
                               1900                 :                : /*
                               1901                 :                :  * Returns true if the plan contains exactly one command
                               1902                 :                :  * and that command returns tuples to the caller (eg, SELECT or
                               1903                 :                :  * INSERT ... RETURNING, but not SELECT ... INTO). In essence,
                               1904                 :                :  * the result indicates if the command can be used with SPI_cursor_open
                               1905                 :                :  *
                               1906                 :                :  * Parameters
                               1907                 :                :  *    plan: A plan previously prepared using SPI_prepare
                               1908                 :                :  */
                               1909                 :                : bool
 6750 tgl@sss.pgh.pa.us        1910                 :CBC        6126 : SPI_is_cursor_plan(SPIPlanPtr plan)
                               1911                 :                : {
                               1912                 :                :     CachedPlanSource *plansource;
                               1913                 :                : 
                               1914   [ +  -  -  + ]:           6126 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
                               1915                 :                :     {
 7839 tgl@sss.pgh.pa.us        1916                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
                               1917                 :              0 :         return false;
                               1918                 :                :     }
                               1919                 :                : 
 6750 tgl@sss.pgh.pa.us        1920         [ -  + ]:CBC        6126 :     if (list_length(plan->plancache_list) != 1)
                               1921                 :                :     {
 6416 tgl@sss.pgh.pa.us        1922                 :UBC           0 :         SPI_result = 0;
 6963                          1923                 :              0 :         return false;           /* not exactly 1 pre-rewrite command */
                               1924                 :                :     }
 6750 tgl@sss.pgh.pa.us        1925                 :CBC        6126 :     plansource = (CachedPlanSource *) linitial(plan->plancache_list);
                               1926                 :                : 
                               1927                 :                :     /*
                               1928                 :                :      * We used to force revalidation of the cached plan here, but that seems
                               1929                 :                :      * unnecessary: invalidation could mean a change in the rowtype of the
                               1930                 :                :      * tuples returned by a plan, but not whether it returns tuples at all.
                               1931                 :                :      */
 6416                          1932                 :           6126 :     SPI_result = 0;
                               1933                 :                : 
                               1934                 :                :     /* Does it return tuples? */
 6750                          1935         [ +  - ]:           6126 :     if (plansource->resultDesc)
                               1936                 :           6126 :         return true;
                               1937                 :                : 
 7839 tgl@sss.pgh.pa.us        1938                 :UBC           0 :     return false;
                               1939                 :                : }
                               1940                 :                : 
                               1941                 :                : /*
                               1942                 :                :  * SPI_plan_is_valid --- test whether a SPI plan is currently valid
                               1943                 :                :  * (that is, not marked as being in need of revalidation).
                               1944                 :                :  *
                               1945                 :                :  * See notes for CachedPlanIsValid before using this.
                               1946                 :                :  */
                               1947                 :                : bool
 6200 tgl@sss.pgh.pa.us        1948                 :CBC        1814 : SPI_plan_is_valid(SPIPlanPtr plan)
                               1949                 :                : {
                               1950                 :                :     ListCell   *lc;
                               1951                 :                : 
 5104                          1952         [ -  + ]:           1814 :     Assert(plan->magic == _SPI_PLAN_MAGIC);
                               1953                 :                : 
                               1954   [ +  -  +  +  :           3455 :     foreach(lc, plan->plancache_list)
                                              +  + ]
                               1955                 :                :     {
                               1956                 :           1814 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               1957                 :                : 
                               1958         [ +  + ]:           1814 :         if (!CachedPlanIsValid(plansource))
                               1959                 :            173 :             return false;
                               1960                 :                :     }
                               1961                 :           1641 :     return true;
                               1962                 :                : }
                               1963                 :                : 
                               1964                 :                : /*
                               1965                 :                :  * SPI_result_code_string --- convert any SPI return code to a string
                               1966                 :                :  *
                               1967                 :                :  * This is often useful in error messages.  Most callers will probably
                               1968                 :                :  * only pass negative (error-case) codes, but for generality we recognize
                               1969                 :                :  * the success codes too.
                               1970                 :                :  */
                               1971                 :                : const char *
 7707                          1972                 :             60 : SPI_result_code_string(int code)
                               1973                 :                : {
                               1974                 :                :     static char buf[64];
                               1975                 :                : 
                               1976   [ -  -  -  -  :             60 :     switch (code)
                                     -  -  +  -  -  
                                     -  -  -  -  -  
                                     -  +  +  -  +  
                                     -  -  -  -  -  
                                     -  -  -  -  -  
                                           -  -  - ]
                               1977                 :                :     {
 7707 tgl@sss.pgh.pa.us        1978                 :UBC           0 :         case SPI_ERROR_CONNECT:
                               1979                 :              0 :             return "SPI_ERROR_CONNECT";
                               1980                 :              0 :         case SPI_ERROR_COPY:
                               1981                 :              0 :             return "SPI_ERROR_COPY";
                               1982                 :              0 :         case SPI_ERROR_OPUNKNOWN:
                               1983                 :              0 :             return "SPI_ERROR_OPUNKNOWN";
                               1984                 :              0 :         case SPI_ERROR_UNCONNECTED:
                               1985                 :              0 :             return "SPI_ERROR_UNCONNECTED";
                               1986                 :              0 :         case SPI_ERROR_ARGUMENT:
                               1987                 :              0 :             return "SPI_ERROR_ARGUMENT";
                               1988                 :              0 :         case SPI_ERROR_PARAM:
                               1989                 :              0 :             return "SPI_ERROR_PARAM";
 7707 tgl@sss.pgh.pa.us        1990                 :CBC           3 :         case SPI_ERROR_TRANSACTION:
                               1991                 :              3 :             return "SPI_ERROR_TRANSACTION";
 7707 tgl@sss.pgh.pa.us        1992                 :UBC           0 :         case SPI_ERROR_NOATTRIBUTE:
                               1993                 :              0 :             return "SPI_ERROR_NOATTRIBUTE";
                               1994                 :              0 :         case SPI_ERROR_NOOUTFUNC:
                               1995                 :              0 :             return "SPI_ERROR_NOOUTFUNC";
                               1996                 :              0 :         case SPI_ERROR_TYPUNKNOWN:
                               1997                 :              0 :             return "SPI_ERROR_TYPUNKNOWN";
 3081 kgrittn@postgresql.o     1998                 :              0 :         case SPI_ERROR_REL_DUPLICATE:
                               1999                 :              0 :             return "SPI_ERROR_REL_DUPLICATE";
                               2000                 :              0 :         case SPI_ERROR_REL_NOT_FOUND:
                               2001                 :              0 :             return "SPI_ERROR_REL_NOT_FOUND";
 7707 tgl@sss.pgh.pa.us        2002                 :              0 :         case SPI_OK_CONNECT:
                               2003                 :              0 :             return "SPI_OK_CONNECT";
                               2004                 :              0 :         case SPI_OK_FINISH:
                               2005                 :              0 :             return "SPI_OK_FINISH";
                               2006                 :              0 :         case SPI_OK_FETCH:
                               2007                 :              0 :             return "SPI_OK_FETCH";
 7707 tgl@sss.pgh.pa.us        2008                 :CBC           1 :         case SPI_OK_UTILITY:
                               2009                 :              1 :             return "SPI_OK_UTILITY";
                               2010                 :             11 :         case SPI_OK_SELECT:
                               2011                 :             11 :             return "SPI_OK_SELECT";
 7707 tgl@sss.pgh.pa.us        2012                 :UBC           0 :         case SPI_OK_SELINTO:
                               2013                 :              0 :             return "SPI_OK_SELINTO";
 7707 tgl@sss.pgh.pa.us        2014                 :CBC          45 :         case SPI_OK_INSERT:
                               2015                 :             45 :             return "SPI_OK_INSERT";
 7707 tgl@sss.pgh.pa.us        2016                 :UBC           0 :         case SPI_OK_DELETE:
                               2017                 :              0 :             return "SPI_OK_DELETE";
                               2018                 :              0 :         case SPI_OK_UPDATE:
                               2019                 :              0 :             return "SPI_OK_UPDATE";
                               2020                 :              0 :         case SPI_OK_CURSOR:
                               2021                 :              0 :             return "SPI_OK_CURSOR";
 6950                          2022                 :              0 :         case SPI_OK_INSERT_RETURNING:
                               2023                 :              0 :             return "SPI_OK_INSERT_RETURNING";
                               2024                 :              0 :         case SPI_OK_DELETE_RETURNING:
                               2025                 :              0 :             return "SPI_OK_DELETE_RETURNING";
                               2026                 :              0 :         case SPI_OK_UPDATE_RETURNING:
                               2027                 :              0 :             return "SPI_OK_UPDATE_RETURNING";
 6072 heikki.linnakangas@i     2028                 :              0 :         case SPI_OK_REWRITTEN:
                               2029                 :              0 :             return "SPI_OK_REWRITTEN";
 3081 kgrittn@postgresql.o     2030                 :              0 :         case SPI_OK_REL_REGISTER:
                               2031                 :              0 :             return "SPI_OK_REL_REGISTER";
                               2032                 :              0 :         case SPI_OK_REL_UNREGISTER:
                               2033                 :              0 :             return "SPI_OK_REL_UNREGISTER";
  927 dean.a.rasheed@gmail     2034                 :              0 :         case SPI_OK_TD_REGISTER:
                               2035                 :              0 :             return "SPI_OK_TD_REGISTER";
                               2036                 :              0 :         case SPI_OK_MERGE:
                               2037                 :              0 :             return "SPI_OK_MERGE";
  538                          2038                 :              0 :         case SPI_OK_MERGE_RETURNING:
                               2039                 :              0 :             return "SPI_OK_MERGE_RETURNING";
                               2040                 :                :     }
                               2041                 :                :     /* Unrecognized code ... return something useful ... */
 7707 tgl@sss.pgh.pa.us        2042                 :              0 :     sprintf(buf, "Unrecognized SPI code %d", code);
                               2043                 :              0 :     return buf;
                               2044                 :                : }
                               2045                 :                : 
                               2046                 :                : /*
                               2047                 :                :  * SPI_plan_get_plan_sources --- get a SPI plan's underlying list of
                               2048                 :                :  * CachedPlanSources.
                               2049                 :                :  *
                               2050                 :                :  * CAUTION: there is no check on whether the CachedPlanSources are up-to-date.
                               2051                 :                :  *
                               2052                 :                :  * This is exported so that PL/pgSQL can use it (this beats letting PL/pgSQL
                               2053                 :                :  * look directly into the SPIPlan for itself).  It's not documented in
                               2054                 :                :  * spi.sgml because we'd just as soon not have too many places using this.
                               2055                 :                :  */
                               2056                 :                : List *
 4602 tgl@sss.pgh.pa.us        2057                 :CBC       30740 : SPI_plan_get_plan_sources(SPIPlanPtr plan)
                               2058                 :                : {
                               2059         [ -  + ]:          30740 :     Assert(plan->magic == _SPI_PLAN_MAGIC);
                               2060                 :          30740 :     return plan->plancache_list;
                               2061                 :                : }
                               2062                 :                : 
                               2063                 :                : /*
                               2064                 :                :  * SPI_plan_get_cached_plan --- get a SPI plan's generic CachedPlan,
                               2065                 :                :  * if the SPI plan contains exactly one CachedPlanSource.  If not,
                               2066                 :                :  * return NULL.
                               2067                 :                :  *
                               2068                 :                :  * The plan's refcount is incremented (and logged in CurrentResourceOwner,
                               2069                 :                :  * if it's a saved plan).  Caller is responsible for doing ReleaseCachedPlan.
                               2070                 :                :  *
                               2071                 :                :  * This is exported so that PL/pgSQL can use it (this beats letting PL/pgSQL
                               2072                 :                :  * look directly into the SPIPlan for itself).  It's not documented in
                               2073                 :                :  * spi.sgml because we'd just as soon not have too many places using this.
                               2074                 :                :  */
                               2075                 :                : CachedPlan *
                               2076                 :          14874 : SPI_plan_get_cached_plan(SPIPlanPtr plan)
                               2077                 :                : {
                               2078                 :                :     CachedPlanSource *plansource;
                               2079                 :                :     CachedPlan *cplan;
                               2080                 :                :     SPICallbackArg spicallbackarg;
                               2081                 :                :     ErrorContextCallback spierrcontext;
                               2082                 :                : 
                               2083         [ -  + ]:          14874 :     Assert(plan->magic == _SPI_PLAN_MAGIC);
                               2084                 :                : 
                               2085                 :                :     /* Can't support one-shot plans here */
                               2086         [ -  + ]:          14874 :     if (plan->oneshot)
 4602 tgl@sss.pgh.pa.us        2087                 :UBC           0 :         return NULL;
                               2088                 :                : 
                               2089                 :                :     /* Must have exactly one CachedPlanSource */
 4602 tgl@sss.pgh.pa.us        2090         [ -  + ]:CBC       14874 :     if (list_length(plan->plancache_list) != 1)
 4602 tgl@sss.pgh.pa.us        2091                 :UBC           0 :         return NULL;
 4602 tgl@sss.pgh.pa.us        2092                 :CBC       14874 :     plansource = (CachedPlanSource *) linitial(plan->plancache_list);
                               2093                 :                : 
                               2094                 :                :     /* Setup error traceback support for ereport() */
 1706                          2095                 :          14874 :     spicallbackarg.query = plansource->query_string;
                               2096                 :          14874 :     spicallbackarg.mode = plan->parse_mode;
 4602                          2097                 :          14874 :     spierrcontext.callback = _SPI_error_callback;
 1706                          2098                 :          14874 :     spierrcontext.arg = &spicallbackarg;
 4602                          2099                 :          14874 :     spierrcontext.previous = error_context_stack;
                               2100                 :          14874 :     error_context_stack = &spierrcontext;
                               2101                 :                : 
                               2102                 :                :     /* Get the generic plan for the query */
 1685                          2103                 :          14874 :     cplan = GetCachedPlan(plansource, NULL,
                               2104                 :          14874 :                           plan->saved ? CurrentResourceOwner : NULL,
 3081 kgrittn@postgresql.o     2105         [ +  + ]:          14874 :                           _SPI_current->queryEnv);
 4602 tgl@sss.pgh.pa.us        2106         [ -  + ]:          14855 :     Assert(cplan == plansource->gplan);
                               2107                 :                : 
                               2108                 :                :     /* Pop the error context stack */
                               2109                 :          14855 :     error_context_stack = spierrcontext.previous;
                               2110                 :                : 
                               2111                 :          14855 :     return cplan;
                               2112                 :                : }
                               2113                 :                : 
                               2114                 :                : 
                               2115                 :                : /* =================== private functions =================== */
                               2116                 :                : 
                               2117                 :                : /*
                               2118                 :                :  * spi_dest_startup
                               2119                 :                :  *      Initialize to receive tuples from Executor into SPITupleTable
                               2120                 :                :  *      of current SPI procedure
                               2121                 :                :  */
                               2122                 :                : void
 8157                          2123                 :          47894 : spi_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
                               2124                 :                : {
                               2125                 :                :     SPITupleTable *tuptable;
                               2126                 :                :     MemoryContext oldcxt;
                               2127                 :                :     MemoryContext tuptabcxt;
                               2128                 :                : 
 3224                          2129         [ -  + ]:          47894 :     if (_SPI_current == NULL)
 3224 tgl@sss.pgh.pa.us        2130         [ #  # ]:UBC           0 :         elog(ERROR, "spi_dest_startup called while not connected to SPI");
                               2131                 :                : 
 8264 tgl@sss.pgh.pa.us        2132         [ -  + ]:CBC       47894 :     if (_SPI_current->tuptable != NULL)
 8083 tgl@sss.pgh.pa.us        2133         [ #  # ]:UBC           0 :         elog(ERROR, "improper call to spi_dest_startup");
                               2134                 :                : 
                               2135                 :                :     /* We create the tuple table context as a child of procCxt */
                               2136                 :                : 
 8264 tgl@sss.pgh.pa.us        2137                 :CBC       47894 :     oldcxt = _SPI_procmem();    /* switch to procedure memory context */
                               2138                 :                : 
                               2139                 :          47894 :     tuptabcxt = AllocSetContextCreate(CurrentMemoryContext,
                               2140                 :                :                                       "SPI TupTable",
                               2141                 :                :                                       ALLOCSET_DEFAULT_SIZES);
                               2142                 :          47894 :     MemoryContextSwitchTo(tuptabcxt);
                               2143                 :                : 
                               2144                 :          47894 :     _SPI_current->tuptable = tuptable = (SPITupleTable *)
 4426                          2145                 :          47894 :         palloc0(sizeof(SPITupleTable));
 8264                          2146                 :          47894 :     tuptable->tuptabcxt = tuptabcxt;
 4426                          2147                 :          47894 :     tuptable->subid = GetCurrentSubTransactionId();
                               2148                 :                : 
                               2149                 :                :     /*
                               2150                 :                :      * The tuptable is now valid enough to be freed by AtEOSubXact_SPI, so put
                               2151                 :                :      * it onto the SPI context's tuptables list.  This will ensure it's not
                               2152                 :                :      * leaked even in the unlikely event the following few lines fail.
                               2153                 :                :      */
                               2154                 :          47894 :     slist_push_head(&_SPI_current->tuptables, &tuptable->next);
                               2155                 :                : 
                               2156                 :                :     /* set up initial allocations */
 2242                          2157                 :          47894 :     tuptable->alloced = 128;
 8264                          2158                 :          47894 :     tuptable->vals = (HeapTuple *) palloc(tuptable->alloced * sizeof(HeapTuple));
 2242                          2159                 :          47894 :     tuptable->numvals = 0;
 8264                          2160                 :          47894 :     tuptable->tupdesc = CreateTupleDescCopy(typeinfo);
                               2161                 :                : 
                               2162                 :          47894 :     MemoryContextSwitchTo(oldcxt);
                               2163                 :          47894 : }
                               2164                 :                : 
                               2165                 :                : /*
                               2166                 :                :  * spi_printtup
                               2167                 :                :  *      store tuple retrieved by Executor into SPITupleTable
                               2168                 :                :  *      of current SPI procedure
                               2169                 :                :  */
                               2170                 :                : bool
 7479                          2171                 :          59222 : spi_printtup(TupleTableSlot *slot, DestReceiver *self)
                               2172                 :                : {
                               2173                 :                :     SPITupleTable *tuptable;
                               2174                 :                :     MemoryContext oldcxt;
                               2175                 :                : 
 3224                          2176         [ -  + ]:          59222 :     if (_SPI_current == NULL)
 3224 tgl@sss.pgh.pa.us        2177         [ #  # ]:UBC           0 :         elog(ERROR, "spi_printtup called while not connected to SPI");
                               2178                 :                : 
10226 bruce@momjian.us         2179                 :CBC       59222 :     tuptable = _SPI_current->tuptable;
                               2180         [ -  + ]:          59222 :     if (tuptable == NULL)
 8083 tgl@sss.pgh.pa.us        2181         [ #  # ]:UBC           0 :         elog(ERROR, "improper call to spi_printtup");
                               2182                 :                : 
 8264 tgl@sss.pgh.pa.us        2183                 :CBC       59222 :     oldcxt = MemoryContextSwitchTo(tuptable->tuptabcxt);
                               2184                 :                : 
 2242                          2185         [ +  + ]:          59222 :     if (tuptable->numvals >= tuptable->alloced)
                               2186                 :                :     {
                               2187                 :                :         /* Double the size of the pointer array */
                               2188                 :              2 :         uint64      newalloced = tuptable->alloced * 2;
                               2189                 :                : 
 3463                          2190                 :              2 :         tuptable->vals = (HeapTuple *) repalloc_huge(tuptable->vals,
                               2191                 :                :                                                      newalloced * sizeof(HeapTuple));
 2242                          2192                 :              2 :         tuptable->alloced = newalloced;
                               2193                 :                :     }
                               2194                 :                : 
                               2195                 :          59222 :     tuptable->vals[tuptable->numvals] = ExecCopySlotHeapTuple(slot);
                               2196                 :          59222 :     (tuptable->numvals)++;
                               2197                 :                : 
10226 bruce@momjian.us         2198                 :          59222 :     MemoryContextSwitchTo(oldcxt);
                               2199                 :                : 
 3379 rhaas@postgresql.org     2200                 :          59222 :     return true;
                               2201                 :                : }
                               2202                 :                : 
                               2203                 :                : /*
                               2204                 :                :  * Static functions
                               2205                 :                :  */
                               2206                 :                : 
                               2207                 :                : /*
                               2208                 :                :  * Parse and analyze a querystring.
                               2209                 :                :  *
                               2210                 :                :  * At entry, plan->argtypes and plan->nargs (or alternatively plan->parserSetup
                               2211                 :                :  * and plan->parserSetupArg) must be valid, as must plan->parse_mode and
                               2212                 :                :  * plan->cursor_options.
                               2213                 :                :  *
                               2214                 :                :  * Results are stored into *plan (specifically, plan->plancache_list).
                               2215                 :                :  * Note that the result data is all in CurrentMemoryContext or child contexts
                               2216                 :                :  * thereof; in practice this means it is in the SPI executor context, and
                               2217                 :                :  * what we are creating is a "temporary" SPIPlan.  Cruft generated during
                               2218                 :                :  * parsing is also left in CurrentMemoryContext.
                               2219                 :                :  */
                               2220                 :                : static void
 4628 tgl@sss.pgh.pa.us        2221                 :          21919 : _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
                               2222                 :                : {
                               2223                 :                :     List       *raw_parsetree_list;
                               2224                 :                :     List       *plancache_list;
                               2225                 :                :     ListCell   *list_item;
                               2226                 :                :     SPICallbackArg spicallbackarg;
                               2227                 :                :     ErrorContextCallback spierrcontext;
                               2228                 :                : 
                               2229                 :                :     /*
                               2230                 :                :      * Setup error traceback support for ereport()
                               2231                 :                :      */
 1706                          2232                 :          21919 :     spicallbackarg.query = src;
                               2233                 :          21919 :     spicallbackarg.mode = plan->parse_mode;
 7839                          2234                 :          21919 :     spierrcontext.callback = _SPI_error_callback;
 1706                          2235                 :          21919 :     spierrcontext.arg = &spicallbackarg;
 7839                          2236                 :          21919 :     spierrcontext.previous = error_context_stack;
                               2237                 :          21919 :     error_context_stack = &spierrcontext;
                               2238                 :                : 
                               2239                 :                :     /*
                               2240                 :                :      * Parse the request string into a list of raw parse trees.
                               2241                 :                :      */
 1706                          2242                 :          21919 :     raw_parsetree_list = raw_parser(src, plan->parse_mode);
                               2243                 :                : 
                               2244                 :                :     /*
                               2245                 :                :      * Do parse analysis and rule rewrite for each raw parsetree, storing the
                               2246                 :                :      * results into unsaved plancache entries.
                               2247                 :                :      */
 6750                          2248                 :          21919 :     plancache_list = NIL;
                               2249                 :                : 
 8363                          2250   [ +  -  +  +  :          43789 :     foreach(list_item, raw_parsetree_list)
                                              +  + ]
                               2251                 :                :     {
 3071                          2252                 :          21919 :         RawStmt    *parsetree = lfirst_node(RawStmt, list_item);
                               2253                 :                :         List       *stmt_list;
                               2254                 :                :         CachedPlanSource *plansource;
                               2255                 :                : 
                               2256                 :                :         /*
                               2257                 :                :          * Create the CachedPlanSource before we do parse analysis, since it
                               2258                 :                :          * needs to see the unmodified raw parse tree.
                               2259                 :                :          */
 5104                          2260                 :          21919 :         plansource = CreateCachedPlan(parsetree,
                               2261                 :                :                                       src,
                               2262                 :                :                                       CreateCommandTag(parsetree->stmt));
                               2263                 :                : 
                               2264                 :                :         /*
                               2265                 :                :          * Parameter datatypes are driven by parserSetup hook if provided,
                               2266                 :                :          * otherwise we use the fixed parameter list.
                               2267                 :                :          */
 5785                          2268         [ +  + ]:          21919 :         if (plan->parserSetup != NULL)
                               2269                 :                :         {
                               2270         [ -  + ]:          14608 :             Assert(plan->nargs == 0);
 1282 peter@eisentraut.org     2271                 :          14608 :             stmt_list = pg_analyze_and_rewrite_withcb(parsetree,
                               2272                 :                :                                                       src,
                               2273                 :                :                                                       plan->parserSetup,
                               2274                 :                :                                                       plan->parserSetupArg,
 3081 kgrittn@postgresql.o     2275                 :          14608 :                                                       _SPI_current->queryEnv);
                               2276                 :                :         }
                               2277                 :                :         else
                               2278                 :                :         {
 1282 peter@eisentraut.org     2279                 :           7311 :             stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
                               2280                 :                :                                                            src,
 1213 tgl@sss.pgh.pa.us        2281                 :           7311 :                                                            plan->argtypes,
                               2282                 :                :                                                            plan->nargs,
                               2283                 :           7311 :                                                            _SPI_current->queryEnv);
                               2284                 :                :         }
                               2285                 :                : 
                               2286                 :                :         /* Finish filling in the CachedPlanSource */
 5104                          2287                 :          21870 :         CompleteCachedPlan(plansource,
                               2288                 :                :                            stmt_list,
                               2289                 :                :                            NULL,
                               2290                 :                :                            plan->argtypes,
                               2291                 :                :                            plan->nargs,
                               2292                 :                :                            plan->parserSetup,
                               2293                 :                :                            plan->parserSetupArg,
                               2294                 :                :                            plan->cursor_options,
                               2295                 :                :                            false);  /* not fixed result */
                               2296                 :                : 
 6750                          2297                 :          21870 :         plancache_list = lappend(plancache_list, plansource);
                               2298                 :                :     }
                               2299                 :                : 
                               2300                 :          21870 :     plan->plancache_list = plancache_list;
 4628                          2301                 :          21870 :     plan->oneshot = false;
                               2302                 :                : 
                               2303                 :                :     /*
                               2304                 :                :      * Pop the error context stack
                               2305                 :                :      */
                               2306                 :          21870 :     error_context_stack = spierrcontext.previous;
                               2307                 :          21870 : }
                               2308                 :                : 
                               2309                 :                : /*
                               2310                 :                :  * Parse, but don't analyze, a querystring.
                               2311                 :                :  *
                               2312                 :                :  * This is a stripped-down version of _SPI_prepare_plan that only does the
                               2313                 :                :  * initial raw parsing.  It creates "one shot" CachedPlanSources
                               2314                 :                :  * that still require parse analysis before execution is possible.
                               2315                 :                :  *
                               2316                 :                :  * The advantage of using the "one shot" form of CachedPlanSource is that
                               2317                 :                :  * we eliminate data copying and invalidation overhead.  Postponing parse
                               2318                 :                :  * analysis also prevents issues if some of the raw parsetrees are DDL
                               2319                 :                :  * commands that affect validity of later parsetrees.  Both of these
                               2320                 :                :  * attributes are good things for SPI_execute() and similar cases.
                               2321                 :                :  *
                               2322                 :                :  * Results are stored into *plan (specifically, plan->plancache_list).
                               2323                 :                :  * Note that the result data is all in CurrentMemoryContext or child contexts
                               2324                 :                :  * thereof; in practice this means it is in the SPI executor context, and
                               2325                 :                :  * what we are creating is a "temporary" SPIPlan.  Cruft generated during
                               2326                 :                :  * parsing is also left in CurrentMemoryContext.
                               2327                 :                :  */
                               2328                 :                : static void
                               2329                 :           8001 : _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
                               2330                 :                : {
                               2331                 :                :     List       *raw_parsetree_list;
                               2332                 :                :     List       *plancache_list;
                               2333                 :                :     ListCell   *list_item;
                               2334                 :                :     SPICallbackArg spicallbackarg;
                               2335                 :                :     ErrorContextCallback spierrcontext;
                               2336                 :                : 
                               2337                 :                :     /*
                               2338                 :                :      * Setup error traceback support for ereport()
                               2339                 :                :      */
 1706                          2340                 :           8001 :     spicallbackarg.query = src;
                               2341                 :           8001 :     spicallbackarg.mode = plan->parse_mode;
 4628                          2342                 :           8001 :     spierrcontext.callback = _SPI_error_callback;
 1706                          2343                 :           8001 :     spierrcontext.arg = &spicallbackarg;
 4628                          2344                 :           8001 :     spierrcontext.previous = error_context_stack;
                               2345                 :           8001 :     error_context_stack = &spierrcontext;
                               2346                 :                : 
                               2347                 :                :     /*
                               2348                 :                :      * Parse the request string into a list of raw parse trees.
                               2349                 :                :      */
 1706                          2350                 :           8001 :     raw_parsetree_list = raw_parser(src, plan->parse_mode);
                               2351                 :                : 
                               2352                 :                :     /*
                               2353                 :                :      * Construct plancache entries, but don't do parse analysis yet.
                               2354                 :                :      */
 4628                          2355                 :           7995 :     plancache_list = NIL;
                               2356                 :                : 
                               2357   [ +  -  +  +  :          15995 :     foreach(list_item, raw_parsetree_list)
                                              +  + ]
                               2358                 :                :     {
 3071                          2359                 :           8000 :         RawStmt    *parsetree = lfirst_node(RawStmt, list_item);
                               2360                 :                :         CachedPlanSource *plansource;
                               2361                 :                : 
 4628                          2362                 :           8000 :         plansource = CreateOneShotCachedPlan(parsetree,
                               2363                 :                :                                              src,
                               2364                 :                :                                              CreateCommandTag(parsetree->stmt));
                               2365                 :                : 
                               2366                 :           8000 :         plancache_list = lappend(plancache_list, plansource);
                               2367                 :                :     }
                               2368                 :                : 
                               2369                 :           7995 :     plan->plancache_list = plancache_list;
                               2370                 :           7995 :     plan->oneshot = true;
                               2371                 :                : 
                               2372                 :                :     /*
                               2373                 :                :      * Pop the error context stack
                               2374                 :                :      */
 7839                          2375                 :           7995 :     error_context_stack = spierrcontext.previous;
10235 vadim4o@yahoo.com        2376                 :           7995 : }
                               2377                 :                : 
                               2378                 :                : /*
                               2379                 :                :  * _SPI_execute_plan: execute the given plan with the given options
                               2380                 :                :  *
                               2381                 :                :  * options contains options accessible from outside SPI:
                               2382                 :                :  * params: parameter values to pass to query
                               2383                 :                :  * read_only: true for read-only execution (no CommandCounterIncrement)
                               2384                 :                :  * allow_nonatomic: true to allow nonatomic CALL/DO execution
                               2385                 :                :  * must_return_tuples: throw error if query doesn't return tuples
                               2386                 :                :  * tcount: execution tuple-count limit, or 0 for none
                               2387                 :                :  * dest: DestReceiver to receive output, or NULL for normal SPI output
                               2388                 :                :  * owner: ResourceOwner that will be used to hold refcount on plan;
                               2389                 :                :  *      if NULL, CurrentResourceOwner is used (ignored for non-saved plan)
                               2390                 :                :  *
                               2391                 :                :  * Additional, only-internally-accessible options:
                               2392                 :                :  * snapshot: query snapshot to use, or InvalidSnapshot for the normal
                               2393                 :                :  *      behavior of taking a new snapshot for each query.
                               2394                 :                :  * crosscheck_snapshot: for RI use, all others pass InvalidSnapshot
                               2395                 :                :  * fire_triggers: true to fire AFTER triggers at end of query (normal case);
                               2396                 :                :  *      false means any AFTER triggers are postponed to end of outer query
                               2397                 :                :  */
                               2398                 :                : static int
 1434 tgl@sss.pgh.pa.us        2399                 :          52157 : _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
                               2400                 :                :                   Snapshot snapshot, Snapshot crosscheck_snapshot,
                               2401                 :                :                   bool fire_triggers)
                               2402                 :                : {
 6326 alvherre@alvh.no-ip.     2403                 :          52157 :     int         my_res = 0;
 3465 tgl@sss.pgh.pa.us        2404                 :          52157 :     uint64      my_processed = 0;
 6326 alvherre@alvh.no-ip.     2405                 :          52157 :     SPITupleTable *my_tuptable = NULL;
                               2406                 :          52157 :     int         res = 0;
                               2407                 :                :     bool        allow_nonatomic;
 5304 tgl@sss.pgh.pa.us        2408                 :          52157 :     bool        pushed_active_snap = false;
 1434                          2409                 :          52157 :     ResourceOwner plan_owner = options->owner;
                               2410                 :                :     SPICallbackArg spicallbackarg;
                               2411                 :                :     ErrorContextCallback spierrcontext;
 6326 alvherre@alvh.no-ip.     2412                 :          52157 :     CachedPlan *cplan = NULL;
                               2413                 :                :     ListCell   *lc1;
                               2414                 :                : 
                               2415                 :                :     /*
                               2416                 :                :      * We allow nonatomic behavior only if options->allow_nonatomic is set
                               2417                 :                :      * *and* the SPI_OPT_NONATOMIC flag was given when connecting and we are
                               2418                 :                :      * not inside a subtransaction.  The latter two tests match whether
                               2419                 :                :      * _SPI_commit() would allow a commit; see there for more commentary.
                               2420                 :                :      */
  325 tgl@sss.pgh.pa.us        2421                 :         104373 :     allow_nonatomic = options->allow_nonatomic &&
                               2422   [ +  +  +  +  :          52157 :         !_SPI_current->atomic && !IsSubTransaction();
                                              +  + ]
                               2423                 :                : 
                               2424                 :                :     /*
                               2425                 :                :      * Setup error traceback support for ereport()
                               2426                 :                :      */
 1706                          2427                 :          52157 :     spicallbackarg.query = NULL;    /* we'll fill this below */
                               2428                 :          52157 :     spicallbackarg.mode = plan->parse_mode;
 6326 alvherre@alvh.no-ip.     2429                 :          52157 :     spierrcontext.callback = _SPI_error_callback;
 1706 tgl@sss.pgh.pa.us        2430                 :          52157 :     spierrcontext.arg = &spicallbackarg;
 6326 alvherre@alvh.no-ip.     2431                 :          52157 :     spierrcontext.previous = error_context_stack;
                               2432                 :          52157 :     error_context_stack = &spierrcontext;
                               2433                 :                : 
                               2434                 :                :     /*
                               2435                 :                :      * We support four distinct snapshot management behaviors:
                               2436                 :                :      *
                               2437                 :                :      * snapshot != InvalidSnapshot, read_only = true: use exactly the given
                               2438                 :                :      * snapshot.
                               2439                 :                :      *
                               2440                 :                :      * snapshot != InvalidSnapshot, read_only = false: use the given snapshot,
                               2441                 :                :      * modified by advancing its command ID before each querytree.
                               2442                 :                :      *
                               2443                 :                :      * snapshot == InvalidSnapshot, read_only = true: do nothing for queries
                               2444                 :                :      * that require no snapshot.  For those that do, ensure that a Portal
                               2445                 :                :      * snapshot exists; then use that, or use the entry-time ActiveSnapshot if
                               2446                 :                :      * that exists and is different.
                               2447                 :                :      *
                               2448                 :                :      * snapshot == InvalidSnapshot, read_only = false: do nothing for queries
                               2449                 :                :      * that require no snapshot.  For those that do, ensure that a Portal
                               2450                 :                :      * snapshot exists; then, in atomic execution (!allow_nonatomic) take a
                               2451                 :                :      * full new snapshot for each user command, and advance its command ID
                               2452                 :                :      * before each querytree within the command.  In allow_nonatomic mode we
                               2453                 :                :      * just use the Portal snapshot unmodified.
                               2454                 :                :      *
                               2455                 :                :      * In the first two cases, we can just push the snap onto the stack once
                               2456                 :                :      * for the whole plan list.
                               2457                 :                :      *
                               2458                 :                :      * Note that snapshot != InvalidSnapshot implies an atomic execution
                               2459                 :                :      * context.
                               2460                 :                :      */
 1569 tgl@sss.pgh.pa.us        2461         [ +  + ]:          52157 :     if (snapshot != InvalidSnapshot)
                               2462                 :                :     {
                               2463                 :                :         /* this intentionally tests the options field not the derived value */
 1434                          2464         [ -  + ]:            600 :         Assert(!options->allow_nonatomic);
                               2465         [ +  + ]:            600 :         if (options->read_only)
                               2466                 :                :         {
 5304                          2467                 :            584 :             PushActiveSnapshot(snapshot);
                               2468                 :            584 :             pushed_active_snap = true;
                               2469                 :                :         }
                               2470                 :                :         else
                               2471                 :                :         {
                               2472                 :                :             /* Make sure we have a private copy of the snapshot to modify */
                               2473                 :             16 :             PushCopiedSnapshot(snapshot);
                               2474                 :             16 :             pushed_active_snap = true;
                               2475                 :                :         }
                               2476                 :                :     }
                               2477                 :                : 
                               2478                 :                :     /*
                               2479                 :                :      * Ensure that we have a resource owner if plan is saved, and not if it
                               2480                 :                :      * isn't.
                               2481                 :                :      */
 1685                          2482         [ +  + ]:          52157 :     if (!plan->saved)
                               2483                 :           8653 :         plan_owner = NULL;
                               2484         [ +  + ]:          43504 :     else if (plan_owner == NULL)
                               2485                 :          43449 :         plan_owner = CurrentResourceOwner;
                               2486                 :                : 
                               2487                 :                :     /*
                               2488                 :                :      * We interpret must_return_tuples as "there must be at least one query,
                               2489                 :                :      * and all of them must return tuples".  This is a bit laxer than
                               2490                 :                :      * SPI_is_cursor_plan's check, but there seems no reason to enforce that
                               2491                 :                :      * there be only one query.
                               2492                 :                :      */
 1434                          2493   [ +  +  -  + ]:          52157 :     if (options->must_return_tuples && plan->plancache_list == NIL)
 1434 tgl@sss.pgh.pa.us        2494         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2495                 :                :                 (errcode(ERRCODE_SYNTAX_ERROR),
                               2496                 :                :                  errmsg("empty query does not return tuples")));
                               2497                 :                : 
 6326 alvherre@alvh.no-ip.     2498   [ +  -  +  +  :CBC      101460 :     foreach(lc1, plan->plancache_list)
                                              +  + ]
                               2499                 :                :     {
                               2500                 :          52161 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc1);
                               2501                 :                :         List       *stmt_list;
                               2502                 :                :         ListCell   *lc2;
                               2503                 :                : 
 1706 tgl@sss.pgh.pa.us        2504                 :          52161 :         spicallbackarg.query = plansource->query_string;
                               2505                 :                : 
                               2506                 :                :         /*
                               2507                 :                :          * If this is a one-shot plan, we still need to do parse analysis.
                               2508                 :                :          */
 4628                          2509         [ +  + ]:          52161 :         if (plan->oneshot)
                               2510                 :                :         {
 3157                          2511                 :           7999 :             RawStmt    *parsetree = plansource->raw_parse_tree;
 4628                          2512                 :           7999 :             const char *src = plansource->query_string;
                               2513                 :                :             List       *querytree_list;
                               2514                 :                : 
                               2515                 :                :             /*
                               2516                 :                :              * Parameter datatypes are driven by parserSetup hook if provided,
                               2517                 :                :              * otherwise we use the fixed parameter list.
                               2518                 :                :              */
 3951                          2519         [ -  + ]:           7999 :             if (parsetree == NULL)
 1067 drowley@postgresql.o     2520                 :UBC           0 :                 querytree_list = NIL;
 3951 tgl@sss.pgh.pa.us        2521         [ +  + ]:CBC        7999 :             else if (plan->parserSetup != NULL)
                               2522                 :                :             {
 4628                          2523         [ -  + ]:            285 :                 Assert(plan->nargs == 0);
 1067 drowley@postgresql.o     2524                 :            285 :                 querytree_list = pg_analyze_and_rewrite_withcb(parsetree,
                               2525                 :                :                                                                src,
                               2526                 :                :                                                                plan->parserSetup,
                               2527                 :                :                                                                plan->parserSetupArg,
                               2528                 :            285 :                                                                _SPI_current->queryEnv);
                               2529                 :                :             }
                               2530                 :                :             else
                               2531                 :                :             {
                               2532                 :           7714 :                 querytree_list = pg_analyze_and_rewrite_fixedparams(parsetree,
                               2533                 :                :                                                                     src,
                               2534                 :           7714 :                                                                     plan->argtypes,
                               2535                 :                :                                                                     plan->nargs,
                               2536                 :           7714 :                                                                     _SPI_current->queryEnv);
                               2537                 :                :             }
                               2538                 :                : 
                               2539                 :                :             /* Finish filling in the CachedPlanSource */
 4628 tgl@sss.pgh.pa.us        2540                 :           7992 :             CompleteCachedPlan(plansource,
                               2541                 :                :                                querytree_list,
                               2542                 :                :                                NULL,
                               2543                 :                :                                plan->argtypes,
                               2544                 :                :                                plan->nargs,
                               2545                 :                :                                plan->parserSetup,
                               2546                 :                :                                plan->parserSetupArg,
                               2547                 :                :                                plan->cursor_options,
                               2548                 :                :                                false);  /* not fixed result */
                               2549                 :                :         }
                               2550                 :                : 
                               2551                 :                :         /*
                               2552                 :                :          * If asked to, complain when query does not return tuples.
                               2553                 :                :          * (Replanning can't change this, so we can check it before that.
                               2554                 :                :          * However, we can't check it till after parse analysis, so in the
                               2555                 :                :          * case of a one-shot plan this is the earliest we could check.)
                               2556                 :                :          */
 1434                          2557   [ +  +  +  + ]:          52154 :         if (options->must_return_tuples && !plansource->resultDesc)
                               2558                 :                :         {
                               2559                 :                :             /* try to give a good error message */
                               2560                 :                :             const char *cmdtag;
                               2561                 :                : 
                               2562                 :                :             /* A SELECT without resultDesc must be SELECT INTO */
                               2563         [ +  - ]:              6 :             if (plansource->commandTag == CMDTAG_SELECT)
                               2564                 :              6 :                 cmdtag = "SELECT INTO";
                               2565                 :                :             else
 1434 tgl@sss.pgh.pa.us        2566                 :UBC           0 :                 cmdtag = GetCommandTagName(plansource->commandTag);
 1434 tgl@sss.pgh.pa.us        2567         [ +  - ]:CBC           6 :             ereport(ERROR,
                               2568                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                               2569                 :                :             /* translator: %s is name of a SQL command, eg INSERT */
                               2570                 :                :                      errmsg("%s query does not return tuples", cmdtag)));
                               2571                 :                :         }
                               2572                 :                : 
                               2573                 :                :         /*
                               2574                 :                :          * Replan if needed, and increment plan refcount.  If it's a saved
                               2575                 :                :          * plan, the refcount must be backed by the plan_owner.
                               2576                 :                :          */
                               2577                 :          52148 :         cplan = GetCachedPlan(plansource, options->params,
 1685                          2578                 :          52148 :                               plan_owner, _SPI_current->queryEnv);
                               2579                 :                : 
 5104                          2580                 :          52077 :         stmt_list = cplan->stmt_list;
                               2581                 :                : 
                               2582                 :                :         /*
                               2583                 :                :          * If we weren't given a specific snapshot to use, and the statement
                               2584                 :                :          * list requires a snapshot, set that up.
                               2585                 :                :          */
 1569                          2586   [ +  +  +  - ]:         103554 :         if (snapshot == InvalidSnapshot &&
                               2587         [ +  - ]:         102954 :             (list_length(stmt_list) > 1 ||
                               2588         [ +  + ]:         102954 :              (list_length(stmt_list) == 1 &&
                               2589                 :          51477 :               PlannedStmtRequiresSnapshot(linitial_node(PlannedStmt,
                               2590                 :                :                                                         stmt_list)))))
                               2591                 :                :         {
                               2592                 :                :             /*
                               2593                 :                :              * First, ensure there's a Portal-level snapshot.  This back-fills
                               2594                 :                :              * the snapshot stack in case the previous operation was a COMMIT
                               2595                 :                :              * or ROLLBACK inside a procedure or DO block.  (We can't put back
                               2596                 :                :              * the Portal snapshot any sooner, or we'd break cases like doing
                               2597                 :                :              * SET or LOCK just after COMMIT.)  It's enough to check once per
                               2598                 :                :              * statement list, since COMMIT/ROLLBACK/CALL/DO can't appear
                               2599                 :                :              * within a multi-statement list.
                               2600                 :                :              */
                               2601                 :          44911 :             EnsurePortalSnapshotExists();
                               2602                 :                : 
                               2603                 :                :             /*
                               2604                 :                :              * In the default non-read-only case, get a new per-statement-list
                               2605                 :                :              * snapshot, replacing any that we pushed in a previous cycle.
                               2606                 :                :              * Skip it when doing non-atomic execution, though (we rely
                               2607                 :                :              * entirely on the Portal snapshot in that case).
                               2608                 :                :              */
  456                          2609   [ +  +  +  + ]:          44911 :             if (!options->read_only && !allow_nonatomic)
                               2610                 :                :             {
 1569                          2611         [ +  + ]:          42347 :                 if (pushed_active_snap)
                               2612                 :              4 :                     PopActiveSnapshot();
                               2613                 :          42347 :                 PushActiveSnapshot(GetTransactionSnapshot());
                               2614                 :          42347 :                 pushed_active_snap = true;
                               2615                 :                :             }
                               2616                 :                :         }
                               2617                 :                : 
 6326 alvherre@alvh.no-ip.     2618   [ +  -  +  +  :         101380 :         foreach(lc2, stmt_list)
                                              +  + ]
                               2619                 :                :         {
 3071 tgl@sss.pgh.pa.us        2620                 :          52077 :             PlannedStmt *stmt = lfirst_node(PlannedStmt, lc2);
 3157                          2621                 :          52077 :             bool        canSetTag = stmt->canSetTag;
                               2622                 :                :             DestReceiver *dest;
                               2623                 :                : 
                               2624                 :                :             /*
                               2625                 :                :              * Reset output state.  (Note that if a non-SPI receiver is used,
                               2626                 :                :              * _SPI_current->processed will stay zero, and that's what we'll
                               2627                 :                :              * report to the caller.  It's the receiver's job to count tuples
                               2628                 :                :              * in that case.)
                               2629                 :                :              */
 6326 alvherre@alvh.no-ip.     2630                 :          52077 :             _SPI_current->processed = 0;
                               2631                 :          52077 :             _SPI_current->tuptable = NULL;
                               2632                 :                : 
                               2633                 :                :             /* Check for unsupported cases. */
 3157 tgl@sss.pgh.pa.us        2634         [ +  + ]:          52077 :             if (stmt->utilityStmt)
                               2635                 :                :             {
                               2636         [ +  + ]:          12624 :                 if (IsA(stmt->utilityStmt, CopyStmt))
                               2637                 :                :                 {
                               2638                 :              9 :                     CopyStmt   *cstmt = (CopyStmt *) stmt->utilityStmt;
                               2639                 :                : 
 6326 alvherre@alvh.no-ip.     2640         [ +  + ]:              9 :                     if (cstmt->filename == NULL)
                               2641                 :                :                     {
                               2642                 :              4 :                         my_res = SPI_ERROR_COPY;
 7663 tgl@sss.pgh.pa.us        2643                 :              9 :                         goto fail;
                               2644                 :                :                     }
                               2645                 :                :                 }
 3157                          2646         [ +  + ]:          12615 :                 else if (IsA(stmt->utilityStmt, TransactionStmt))
                               2647                 :                :                 {
 6326 alvherre@alvh.no-ip.     2648                 :              5 :                     my_res = SPI_ERROR_TRANSACTION;
                               2649                 :              5 :                     goto fail;
                               2650                 :                :                 }
                               2651                 :                :             }
                               2652                 :                : 
 1434 tgl@sss.pgh.pa.us        2653   [ +  +  -  + ]:          52068 :             if (options->read_only && !CommandIsReadOnly(stmt))
 6326 alvherre@alvh.no-ip.     2654         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2655                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               2656                 :                :                 /* translator: %s is a SQL statement name */
                               2657                 :                :                          errmsg("%s is not allowed in a non-volatile function",
                               2658                 :                :                                 CreateCommandName((Node *) stmt))));
                               2659                 :                : 
                               2660                 :                :             /*
                               2661                 :                :              * If not read-only mode, advance the command counter before each
                               2662                 :                :              * command and update the snapshot.  (But skip it if the snapshot
                               2663                 :                :              * isn't under our control.)
                               2664                 :                :              */
 1434 tgl@sss.pgh.pa.us        2665   [ +  +  +  + ]:CBC       52068 :             if (!options->read_only && pushed_active_snap)
                               2666                 :                :             {
 6326 alvherre@alvh.no-ip.     2667                 :          42359 :                 CommandCounterIncrement();
 5304 tgl@sss.pgh.pa.us        2668                 :          42359 :                 UpdateActiveSnapshotCommandId();
                               2669                 :                :             }
                               2670                 :                : 
                               2671                 :                :             /*
                               2672                 :                :              * Select appropriate tuple receiver.  Output from non-canSetTag
                               2673                 :                :              * subqueries always goes to the bit bucket.
                               2674                 :                :              */
 1912                          2675         [ -  + ]:          52068 :             if (!canSetTag)
 1912 tgl@sss.pgh.pa.us        2676                 :UBC           0 :                 dest = CreateDestReceiver(DestNone);
 1434 tgl@sss.pgh.pa.us        2677         [ +  + ]:CBC       52068 :             else if (options->dest)
                               2678                 :           1335 :                 dest = options->dest;
                               2679                 :                :             else
 1912                          2680                 :          50733 :                 dest = CreateDestReceiver(DestSPI);
                               2681                 :                : 
 3157                          2682         [ +  + ]:          52068 :             if (stmt->utilityStmt == NULL)
                               2683                 :                :             {
                               2684                 :                :                 QueryDesc  *qdesc;
                               2685                 :                :                 Snapshot    snap;
                               2686                 :                : 
 6326 alvherre@alvh.no-ip.     2687         [ +  - ]:          39453 :                 if (ActiveSnapshotSet())
                               2688                 :          39453 :                     snap = GetActiveSnapshot();
                               2689                 :                :                 else
 6326 alvherre@alvh.no-ip.     2690                 :UBC           0 :                     snap = InvalidSnapshot;
                               2691                 :                : 
 3157 tgl@sss.pgh.pa.us        2692                 :CBC       39453 :                 qdesc = CreateQueryDesc(stmt,
                               2693                 :                :                                         plansource->query_string,
                               2694                 :                :                                         snap, crosscheck_snapshot,
                               2695                 :                :                                         dest,
 1434                          2696                 :          39453 :                                         options->params,
                               2697                 :          39453 :                                         _SPI_current->queryEnv,
                               2698                 :                :                                         0);
  107 amitlan@postgresql.o     2699         [ +  - ]:          39453 :                 res = _SPI_pquery(qdesc, fire_triggers,
                               2700                 :                :                                   canSetTag ? options->tcount : 0);
 6326 alvherre@alvh.no-ip.     2701                 :          36757 :                 FreeQueryDesc(qdesc);
                               2702                 :                :             }
                               2703                 :                :             else
                               2704                 :                :             {
                               2705                 :                :                 ProcessUtilityContext context;
                               2706                 :                :                 QueryCompletion qc;
                               2707                 :                : 
                               2708                 :                :                 /*
                               2709                 :                :                  * If we're not allowing nonatomic operations, tell
                               2710                 :                :                  * ProcessUtility this is an atomic execution context.
                               2711                 :                :                  */
  456 tgl@sss.pgh.pa.us        2712         [ +  + ]:          12615 :                 if (allow_nonatomic)
 2723 peter_e@gmx.net          2713                 :             51 :                     context = PROCESS_UTILITY_QUERY_NONATOMIC;
                               2714                 :                :                 else
  456 tgl@sss.pgh.pa.us        2715                 :          12564 :                     context = PROCESS_UTILITY_QUERY;
                               2716                 :                : 
 2014 alvherre@alvh.no-ip.     2717                 :          12615 :                 InitializeQueryCompletion(&qc);
 6326                          2718                 :          12615 :                 ProcessUtility(stmt,
                               2719                 :                :                                plansource->query_string,
                               2720                 :                :                                true,    /* protect plancache's node tree */
                               2721                 :                :                                context,
 1434 tgl@sss.pgh.pa.us        2722                 :          12615 :                                options->params,
 3081 kgrittn@postgresql.o     2723                 :          12615 :                                _SPI_current->queryEnv,
                               2724                 :                :                                dest,
                               2725                 :                :                                &qc);
                               2726                 :                : 
                               2727                 :                :                 /* Update "processed" if stmt returned tuples */
 6326 alvherre@alvh.no-ip.     2728         [ +  + ]:          12546 :                 if (_SPI_current->tuptable)
 2242 tgl@sss.pgh.pa.us        2729                 :            937 :                     _SPI_current->processed = _SPI_current->tuptable->numvals;
                               2730                 :                : 
 4721 heikki.linnakangas@i     2731                 :          12546 :                 res = SPI_OK_UTILITY;
                               2732                 :                : 
                               2733                 :                :                 /*
                               2734                 :                :                  * Some utility statements return a row count, even though the
                               2735                 :                :                  * tuples are not returned to the caller.
                               2736                 :                :                  */
 3157 tgl@sss.pgh.pa.us        2737         [ +  + ]:          12546 :                 if (IsA(stmt->utilityStmt, CreateTableAsStmt))
                               2738                 :                :                 {
                               2739                 :             25 :                     CreateTableAsStmt *ctastmt = (CreateTableAsStmt *) stmt->utilityStmt;
                               2740                 :                : 
 2014 alvherre@alvh.no-ip.     2741         [ +  + ]:             25 :                     if (qc.commandTag == CMDTAG_SELECT)
                               2742                 :             22 :                         _SPI_current->processed = qc.nprocessed;
                               2743                 :                :                     else
                               2744                 :                :                     {
                               2745                 :                :                         /*
                               2746                 :                :                          * Must be an IF NOT EXISTS that did nothing, or a
                               2747                 :                :                          * CREATE ... WITH NO DATA.
                               2748                 :                :                          */
 3313 tgl@sss.pgh.pa.us        2749   [ +  -  -  + ]:              3 :                         Assert(ctastmt->if_not_exists ||
                               2750                 :                :                                ctastmt->into->skipData);
 3435                          2751                 :              3 :                         _SPI_current->processed = 0;
                               2752                 :                :                     }
                               2753                 :                : 
                               2754                 :                :                     /*
                               2755                 :                :                      * For historical reasons, if CREATE TABLE AS was spelled
                               2756                 :                :                      * as SELECT INTO, return a special return code.
                               2757                 :                :                      */
                               2758         [ -  + ]:             25 :                     if (ctastmt->is_select_into)
 4919 tgl@sss.pgh.pa.us        2759                 :UBC           0 :                         res = SPI_OK_SELINTO;
                               2760                 :                :                 }
 3157 tgl@sss.pgh.pa.us        2761         [ +  + ]:CBC       12521 :                 else if (IsA(stmt->utilityStmt, CopyStmt))
                               2762                 :                :                 {
 2014 alvherre@alvh.no-ip.     2763         [ -  + ]:              5 :                     Assert(qc.commandTag == CMDTAG_COPY);
                               2764                 :              5 :                     _SPI_current->processed = qc.nprocessed;
                               2765                 :                :                 }
                               2766                 :                :             }
                               2767                 :                : 
                               2768                 :                :             /*
                               2769                 :                :              * The last canSetTag query sets the status values returned to the
                               2770                 :                :              * caller.  Be careful to free any tuptables not returned, to
                               2771                 :                :              * avoid intra-transaction memory leak.
                               2772                 :                :              */
 6326                          2773         [ +  - ]:          49303 :             if (canSetTag)
                               2774                 :                :             {
                               2775                 :          49303 :                 my_processed = _SPI_current->processed;
                               2776                 :          49303 :                 SPI_freetuptable(my_tuptable);
                               2777                 :          49303 :                 my_tuptable = _SPI_current->tuptable;
                               2778                 :          49303 :                 my_res = res;
                               2779                 :                :             }
                               2780                 :                :             else
                               2781                 :                :             {
 6326 alvherre@alvh.no-ip.     2782                 :UBC           0 :                 SPI_freetuptable(_SPI_current->tuptable);
                               2783                 :              0 :                 _SPI_current->tuptable = NULL;
                               2784                 :                :             }
                               2785                 :                : 
                               2786                 :                :             /*
                               2787                 :                :              * We don't issue a destroy call to the receiver.  The SPI and
                               2788                 :                :              * None receivers would ignore it anyway, while if the caller
                               2789                 :                :              * supplied a receiver, it's not our job to destroy it.
                               2790                 :                :              */
                               2791                 :                : 
 6326 alvherre@alvh.no-ip.     2792         [ -  + ]:CBC       49303 :             if (res < 0)
                               2793                 :                :             {
 6326 alvherre@alvh.no-ip.     2794                 :UBC           0 :                 my_res = res;
                               2795                 :              0 :                 goto fail;
                               2796                 :                :             }
                               2797                 :                :         }
                               2798                 :                : 
                               2799                 :                :         /* Done with this plan, so release refcount */
 1685 tgl@sss.pgh.pa.us        2800                 :CBC       49303 :         ReleaseCachedPlan(cplan, plan_owner);
 6326 alvherre@alvh.no-ip.     2801                 :          49303 :         cplan = NULL;
                               2802                 :                : 
                               2803                 :                :         /*
                               2804                 :                :          * If not read-only mode, advance the command counter after the last
                               2805                 :                :          * command.  This ensures that its effects are visible, in case it was
                               2806                 :                :          * DDL that would affect the next CachedPlanSource.
                               2807                 :                :          */
 1434 tgl@sss.pgh.pa.us        2808         [ +  + ]:          49303 :         if (!options->read_only)
 6326 alvherre@alvh.no-ip.     2809                 :          46233 :             CommandCounterIncrement();
                               2810                 :                :     }
                               2811                 :                : 
                               2812                 :          49308 : fail:
                               2813                 :                : 
                               2814                 :                :     /* Pop the snapshot off the stack if we pushed one */
 5304 tgl@sss.pgh.pa.us        2815         [ +  + ]:          49308 :     if (pushed_active_snap)
                               2816                 :          40213 :         PopActiveSnapshot();
                               2817                 :                : 
                               2818                 :                :     /* We no longer need the cached plan refcount, if any */
 6326 alvherre@alvh.no-ip.     2819         [ +  + ]:          49308 :     if (cplan)
 1685 tgl@sss.pgh.pa.us        2820                 :              9 :         ReleaseCachedPlan(cplan, plan_owner);
                               2821                 :                : 
                               2822                 :                :     /*
                               2823                 :                :      * Pop the error context stack
                               2824                 :                :      */
 6326 alvherre@alvh.no-ip.     2825                 :          49308 :     error_context_stack = spierrcontext.previous;
                               2826                 :                : 
                               2827                 :                :     /* Save results for caller */
 7280 tgl@sss.pgh.pa.us        2828                 :          49308 :     SPI_processed = my_processed;
                               2829                 :          49308 :     SPI_tuptable = my_tuptable;
                               2830                 :                : 
                               2831                 :                :     /* tuptable now is caller's responsibility, not SPI's */
 6847                          2832                 :          49308 :     _SPI_current->tuptable = NULL;
                               2833                 :                : 
                               2834                 :                :     /*
                               2835                 :                :      * If none of the queries had canSetTag, return SPI_OK_REWRITTEN. Prior to
                               2836                 :                :      * 8.4, we used return the last query's result code, but not its auxiliary
                               2837                 :                :      * results, but that's confusing.
                               2838                 :                :      */
 6829                          2839         [ -  + ]:          49308 :     if (my_res == 0)
 6072 heikki.linnakangas@i     2840                 :UBC           0 :         my_res = SPI_OK_REWRITTEN;
                               2841                 :                : 
 6963 tgl@sss.pgh.pa.us        2842                 :CBC       49308 :     return my_res;
                               2843                 :                : }
                               2844                 :                : 
                               2845                 :                : /*
                               2846                 :                :  * Convert arrays of query parameters to form wanted by planner and executor
                               2847                 :                :  */
                               2848                 :                : static ParamListInfo
 6367                          2849                 :           6283 : _SPI_convert_params(int nargs, Oid *argtypes,
                               2850                 :                :                     Datum *Values, const char *Nulls)
                               2851                 :                : {
                               2852                 :                :     ParamListInfo paramLI;
                               2853                 :                : 
                               2854         [ +  + ]:           6283 :     if (nargs > 0)
                               2855                 :                :     {
 2368 peter@eisentraut.org     2856                 :           5509 :         paramLI = makeParamList(nargs);
                               2857                 :                : 
                               2858         [ +  + ]:          14164 :         for (int i = 0; i < nargs; i++)
                               2859                 :                :         {
 6367 tgl@sss.pgh.pa.us        2860                 :           8655 :             ParamExternData *prm = &paramLI->params[i];
                               2861                 :                : 
                               2862                 :           8655 :             prm->value = Values[i];
                               2863   [ +  +  -  + ]:           8655 :             prm->isnull = (Nulls && Nulls[i] == 'n');
 5104                          2864                 :           8655 :             prm->pflags = PARAM_FLAG_CONST;
 6367                          2865                 :           8655 :             prm->ptype = argtypes[i];
                               2866                 :                :         }
                               2867                 :                :     }
                               2868                 :                :     else
                               2869                 :            774 :         paramLI = NULL;
                               2870                 :           6283 :     return paramLI;
                               2871                 :                : }
                               2872                 :                : 
                               2873                 :                : static int
  107 amitlan@postgresql.o     2874                 :          39453 : _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount)
                               2875                 :                : {
10208 vadim4o@yahoo.com        2876                 :          39453 :     int         operation = queryDesc->operation;
                               2877                 :                :     int         eflags;
                               2878                 :                :     int         res;
                               2879                 :                : 
10226 bruce@momjian.us         2880   [ +  +  +  +  :          39453 :     switch (operation)
                                              +  - ]
                               2881                 :                :     {
10225                          2882                 :          25621 :         case CMD_SELECT:
 1912 tgl@sss.pgh.pa.us        2883         [ -  + ]:          25621 :             if (queryDesc->dest->mydest == DestNone)
                               2884                 :                :             {
                               2885                 :                :                 /* Don't return SPI_OK_SELECT if we're discarding result */
 7280 tgl@sss.pgh.pa.us        2886                 :UBC           0 :                 res = SPI_OK_UTILITY;
                               2887                 :                :             }
                               2888                 :                :             else
 6965 tgl@sss.pgh.pa.us        2889                 :CBC       25621 :                 res = SPI_OK_SELECT;
10225 bruce@momjian.us         2890                 :          25621 :             break;
                               2891                 :           8767 :         case CMD_INSERT:
 5810 tgl@sss.pgh.pa.us        2892         [ +  + ]:           8767 :             if (queryDesc->plannedstmt->hasReturning)
 6950                          2893                 :            473 :                 res = SPI_OK_INSERT_RETURNING;
                               2894                 :                :             else
                               2895                 :           8294 :                 res = SPI_OK_INSERT;
10225 bruce@momjian.us         2896                 :           8767 :             break;
                               2897                 :           4220 :         case CMD_DELETE:
 5810 tgl@sss.pgh.pa.us        2898         [ -  + ]:           4220 :             if (queryDesc->plannedstmt->hasReturning)
 6950 tgl@sss.pgh.pa.us        2899                 :UBC           0 :                 res = SPI_OK_DELETE_RETURNING;
                               2900                 :                :             else
 6950 tgl@sss.pgh.pa.us        2901                 :CBC        4220 :                 res = SPI_OK_DELETE;
10225 bruce@momjian.us         2902                 :           4220 :             break;
                               2903                 :            812 :         case CMD_UPDATE:
 5810 tgl@sss.pgh.pa.us        2904         [ +  + ]:            812 :             if (queryDesc->plannedstmt->hasReturning)
 6950                          2905                 :              5 :                 res = SPI_OK_UPDATE_RETURNING;
                               2906                 :                :             else
                               2907                 :            807 :                 res = SPI_OK_UPDATE;
10225 bruce@momjian.us         2908                 :            812 :             break;
 1258 alvherre@alvh.no-ip.     2909                 :             33 :         case CMD_MERGE:
  538 dean.a.rasheed@gmail     2910         [ +  + ]:             33 :             if (queryDesc->plannedstmt->hasReturning)
                               2911                 :              9 :                 res = SPI_OK_MERGE_RETURNING;
                               2912                 :                :             else
                               2913                 :             24 :                 res = SPI_OK_MERGE;
 1258 alvherre@alvh.no-ip.     2914                 :             33 :             break;
10225 bruce@momjian.us         2915                 :UBC           0 :         default:
 9867                          2916                 :              0 :             return SPI_ERROR_OPUNKNOWN;
                               2917                 :                :     }
                               2918                 :                : 
                               2919                 :                : #ifdef SPI_EXECUTOR_STATS
                               2920                 :                :     if (ShowExecutorStats)
                               2921                 :                :         ResetUsage();
                               2922                 :                : #endif
                               2923                 :                : 
                               2924                 :                :     /* Select execution options */
 6597 tgl@sss.pgh.pa.us        2925         [ +  + ]:CBC       39453 :     if (fire_triggers)
 5305                          2926                 :          35572 :         eflags = 0;             /* default run-to-completion flags */
                               2927                 :                :     else
                               2928                 :           3881 :         eflags = EXEC_FLAG_SKIP_TRIGGERS;
                               2929                 :                : 
  107 amitlan@postgresql.o     2930                 :          39453 :     ExecutorStart(queryDesc, eflags);
                               2931                 :                : 
  271 tgl@sss.pgh.pa.us        2932                 :          39453 :     ExecutorRun(queryDesc, ForwardScanDirection, tcount);
                               2933                 :                : 
 8311                          2934                 :          36759 :     _SPI_current->processed = queryDesc->estate->es_processed;
                               2935                 :                : 
 5810                          2936   [ +  +  +  + ]:          36759 :     if ((res == SPI_OK_SELECT || queryDesc->plannedstmt->hasReturning) &&
 6950                          2937         [ +  + ]:          23432 :         queryDesc->dest->mydest == DestSPI)
                               2938                 :                :     {
10208 vadim4o@yahoo.com        2939         [ -  + ]:          22128 :         if (_SPI_checktuples())
 8083 tgl@sss.pgh.pa.us        2940         [ #  # ]:UBC           0 :             elog(ERROR, "consistency check on SPI tuple count failed");
                               2941                 :                :     }
                               2942                 :                : 
 5305 tgl@sss.pgh.pa.us        2943                 :CBC       36759 :     ExecutorFinish(queryDesc);
 7470                          2944                 :          36757 :     ExecutorEnd(queryDesc);
                               2945                 :                :     /* FreeQueryDesc is done by the caller */
                               2946                 :                : 
                               2947                 :                : #ifdef SPI_EXECUTOR_STATS
                               2948                 :                :     if (ShowExecutorStats)
                               2949                 :                :         ShowUsage("SPI EXECUTOR STATS");
                               2950                 :                : #endif
                               2951                 :                : 
 8301                          2952                 :          36757 :     return res;
                               2953                 :                : }
                               2954                 :                : 
                               2955                 :                : /*
                               2956                 :                :  * _SPI_error_callback
                               2957                 :                :  *
                               2958                 :                :  * Add context information when a query invoked via SPI fails
                               2959                 :                :  */
                               2960                 :                : static void
 7839                          2961                 :           3258 : _SPI_error_callback(void *arg)
                               2962                 :                : {
 1706                          2963                 :           3258 :     SPICallbackArg *carg = (SPICallbackArg *) arg;
                               2964                 :           3258 :     const char *query = carg->query;
                               2965                 :                :     int         syntaxerrposition;
                               2966                 :                : 
 2826                          2967         [ -  + ]:           3258 :     if (query == NULL)          /* in case arg wasn't set yet */
 2826 tgl@sss.pgh.pa.us        2968                 :UBC           0 :         return;
                               2969                 :                : 
                               2970                 :                :     /*
                               2971                 :                :      * If there is a syntax error position, convert to internal syntax error;
                               2972                 :                :      * otherwise treat the query as an item of context stack
                               2973                 :                :      */
 7839 tgl@sss.pgh.pa.us        2974                 :CBC        3258 :     syntaxerrposition = geterrposition();
                               2975         [ +  + ]:           3258 :     if (syntaxerrposition > 0)
                               2976                 :                :     {
                               2977                 :             53 :         errposition(0);
                               2978                 :             53 :         internalerrposition(syntaxerrposition);
                               2979                 :             53 :         internalerrquery(query);
                               2980                 :                :     }
                               2981                 :                :     else
                               2982                 :                :     {
                               2983                 :                :         /* Use the parse mode to decide how to describe the query */
 1706                          2984      [ +  +  + ]:           3205 :         switch (carg->mode)
                               2985                 :                :         {
                               2986                 :             41 :             case RAW_PARSE_PLPGSQL_EXPR:
  366 peter@eisentraut.org     2987                 :             41 :                 errcontext("PL/pgSQL expression \"%s\"", query);
 1706 tgl@sss.pgh.pa.us        2988                 :             41 :                 break;
                               2989                 :              8 :             case RAW_PARSE_PLPGSQL_ASSIGN1:
                               2990                 :                :             case RAW_PARSE_PLPGSQL_ASSIGN2:
                               2991                 :                :             case RAW_PARSE_PLPGSQL_ASSIGN3:
                               2992                 :              8 :                 errcontext("PL/pgSQL assignment \"%s\"", query);
                               2993                 :              8 :                 break;
                               2994                 :           3156 :             default:
                               2995                 :           3156 :                 errcontext("SQL statement \"%s\"", query);
                               2996                 :           3156 :                 break;
                               2997                 :                :         }
                               2998                 :                :     }
                               2999                 :                : }
                               3000                 :                : 
                               3001                 :                : /*
                               3002                 :                :  * _SPI_cursor_operation()
                               3003                 :                :  *
                               3004                 :                :  *  Do a FETCH or MOVE in a cursor
                               3005                 :                :  */
                               3006                 :                : static void
 6718                          3007                 :          22175 : _SPI_cursor_operation(Portal portal, FetchDirection direction, long count,
                               3008                 :                :                       DestReceiver *dest)
                               3009                 :                : {
                               3010                 :                :     uint64      nfetched;
                               3011                 :                : 
                               3012                 :                :     /* Check that the portal is valid */
 8874 JanWieck@Yahoo.com       3013         [ -  + ]:          22175 :     if (!PortalIsValid(portal))
 8874 JanWieck@Yahoo.com       3014         [ #  # ]:UBC           0 :         elog(ERROR, "invalid portal in SPI cursor operation");
                               3015                 :                : 
                               3016                 :                :     /* Push the SPI stack */
 8019 tgl@sss.pgh.pa.us        3017         [ -  + ]:CBC       22175 :     if (_SPI_begin_call(true) < 0)
 8019 tgl@sss.pgh.pa.us        3018         [ #  # ]:UBC           0 :         elog(ERROR, "SPI cursor operation called while not connected");
                               3019                 :                : 
                               3020                 :                :     /* Reset the SPI result (note we deliberately don't touch lastoid) */
 8874 JanWieck@Yahoo.com       3021                 :CBC       22175 :     SPI_processed = 0;
                               3022                 :          22175 :     SPI_tuptable = NULL;
                               3023                 :          22175 :     _SPI_current->processed = 0;
                               3024                 :          22175 :     _SPI_current->tuptable = NULL;
                               3025                 :                : 
                               3026                 :                :     /* Run the cursor */
 8065 tgl@sss.pgh.pa.us        3027                 :          22175 :     nfetched = PortalRunFetch(portal,
                               3028                 :                :                               direction,
                               3029                 :                :                               count,
                               3030                 :                :                               dest);
                               3031                 :                : 
                               3032                 :                :     /*
                               3033                 :                :      * Think not to combine this store with the preceding function call. If
                               3034                 :                :      * the portal contains calls to functions that use SPI, then _SPI_stack is
                               3035                 :                :      * likely to move around while the portal runs.  When control returns,
                               3036                 :                :      * _SPI_current will point to the correct stack entry... but the pointer
                               3037                 :                :      * may be different than it was beforehand. So we must be sure to re-fetch
                               3038                 :                :      * the pointer after the function call completes.
                               3039                 :                :      */
                               3040                 :          22168 :     _SPI_current->processed = nfetched;
                               3041                 :                : 
 7247 alvherre@alvh.no-ip.     3042   [ +  +  -  + ]:          22168 :     if (dest->mydest == DestSPI && _SPI_checktuples())
 8083 tgl@sss.pgh.pa.us        3043         [ #  # ]:UBC           0 :         elog(ERROR, "consistency check on SPI tuple count failed");
                               3044                 :                : 
                               3045                 :                :     /* Put the result into place for access by caller */
 8874 JanWieck@Yahoo.com       3046                 :CBC       22168 :     SPI_processed = _SPI_current->processed;
 8717 bruce@momjian.us         3047                 :          22168 :     SPI_tuptable = _SPI_current->tuptable;
                               3048                 :                : 
                               3049                 :                :     /* tuptable now is caller's responsibility, not SPI's */
 6847 tgl@sss.pgh.pa.us        3050                 :          22168 :     _SPI_current->tuptable = NULL;
                               3051                 :                : 
                               3052                 :                :     /* Pop the SPI stack */
 8874 JanWieck@Yahoo.com       3053                 :          22168 :     _SPI_end_call(true);
                               3054                 :          22168 : }
                               3055                 :                : 
                               3056                 :                : 
                               3057                 :                : static MemoryContext
 7633 neilc@samurai.com        3058                 :         102383 : _SPI_execmem(void)
                               3059                 :                : {
 9201 tgl@sss.pgh.pa.us        3060                 :         102383 :     return MemoryContextSwitchTo(_SPI_current->execCxt);
                               3061                 :                : }
                               3062                 :                : 
                               3063                 :                : static MemoryContext
 7633 neilc@samurai.com        3064                 :         147366 : _SPI_procmem(void)
                               3065                 :                : {
 9201 tgl@sss.pgh.pa.us        3066                 :         147366 :     return MemoryContextSwitchTo(_SPI_current->procCxt);
                               3067                 :                : }
                               3068                 :                : 
                               3069                 :                : /*
                               3070                 :                :  * _SPI_begin_call: begin a SPI operation within a connected procedure
                               3071                 :                :  *
                               3072                 :                :  * use_exec is true if we intend to make use of the procedure's execCxt
                               3073                 :                :  * during this SPI operation.  We'll switch into that context, and arrange
                               3074                 :                :  * for it to be cleaned up at _SPI_end_call or if an error occurs.
                               3075                 :                :  */
                               3076                 :                : static int
 2892                          3077                 :         152851 : _SPI_begin_call(bool use_exec)
                               3078                 :                : {
 3224                          3079         [ -  + ]:         152851 :     if (_SPI_current == NULL)
 9867 bruce@momjian.us         3080                 :UBC           0 :         return SPI_ERROR_UNCONNECTED;
                               3081                 :                : 
 2892 tgl@sss.pgh.pa.us        3082         [ +  + ]:CBC      152851 :     if (use_exec)
                               3083                 :                :     {
                               3084                 :                :         /* remember when the Executor operation started */
                               3085                 :         102383 :         _SPI_current->execSubid = GetCurrentSubTransactionId();
                               3086                 :                :         /* switch to the Executor memory context */
10226 bruce@momjian.us         3087                 :         102383 :         _SPI_execmem();
                               3088                 :                :     }
                               3089                 :                : 
 9867                          3090                 :         152851 :     return 0;
                               3091                 :                : }
                               3092                 :                : 
                               3093                 :                : /*
                               3094                 :                :  * _SPI_end_call: end a SPI operation within a connected procedure
                               3095                 :                :  *
                               3096                 :                :  * use_exec must be the same as in the previous _SPI_begin_call
                               3097                 :                :  *
                               3098                 :                :  * Note: this currently has no failure return cases, so callers don't check
                               3099                 :                :  */
                               3100                 :                : static int
 2892 tgl@sss.pgh.pa.us        3101                 :          99829 : _SPI_end_call(bool use_exec)
                               3102                 :                : {
                               3103         [ +  + ]:          99829 :     if (use_exec)
                               3104                 :                :     {
                               3105                 :                :         /* switch to the procedure memory context */
10226 bruce@momjian.us         3106                 :          99472 :         _SPI_procmem();
                               3107                 :                :         /* mark Executor context no longer in use */
 2892 tgl@sss.pgh.pa.us        3108                 :          99472 :         _SPI_current->execSubid = InvalidSubTransactionId;
                               3109                 :                :         /* and free Executor memory */
  661 nathan@postgresql.or     3110                 :          99472 :         MemoryContextReset(_SPI_current->execCxt);
                               3111                 :                :     }
                               3112                 :                : 
 9867 bruce@momjian.us         3113                 :          99829 :     return 0;
                               3114                 :                : }
                               3115                 :                : 
                               3116                 :                : static bool
 8801 tgl@sss.pgh.pa.us        3117                 :          44275 : _SPI_checktuples(void)
                               3118                 :                : {
 3465                          3119                 :          44275 :     uint64      processed = _SPI_current->processed;
10225 bruce@momjian.us         3120                 :          44275 :     SPITupleTable *tuptable = _SPI_current->tuptable;
                               3121                 :          44275 :     bool        failed = false;
                               3122                 :                : 
 8069                          3123         [ -  + ]:          44275 :     if (tuptable == NULL)       /* spi_dest_startup was not called */
 8264 tgl@sss.pgh.pa.us        3124                 :UBC           0 :         failed = true;
 2242 tgl@sss.pgh.pa.us        3125         [ -  + ]:CBC       44275 :     else if (processed != tuptable->numvals)
 8264 tgl@sss.pgh.pa.us        3126                 :UBC           0 :         failed = true;
                               3127                 :                : 
 9867 bruce@momjian.us         3128                 :CBC       44275 :     return failed;
                               3129                 :                : }
                               3130                 :                : 
                               3131                 :                : /*
                               3132                 :                :  * Convert a "temporary" SPIPlan into an "unsaved" plan.
                               3133                 :                :  *
                               3134                 :                :  * The passed _SPI_plan struct is on the stack, and all its subsidiary data
                               3135                 :                :  * is in or under the current SPI executor context.  Copy the plan into the
                               3136                 :                :  * SPI procedure context so it will survive _SPI_end_call().  To minimize
                               3137                 :                :  * data copying, this destructively modifies the input plan, by taking the
                               3138                 :                :  * plancache entries away from it and reparenting them to the new SPIPlan.
                               3139                 :                :  */
                               3140                 :                : static SPIPlanPtr
 5104 tgl@sss.pgh.pa.us        3141                 :          17163 : _SPI_make_plan_non_temp(SPIPlanPtr plan)
                               3142                 :                : {
                               3143                 :                :     SPIPlanPtr  newplan;
                               3144                 :          17163 :     MemoryContext parentcxt = _SPI_current->procCxt;
                               3145                 :                :     MemoryContext plancxt;
                               3146                 :                :     MemoryContext oldcxt;
                               3147                 :                :     ListCell   *lc;
                               3148                 :                : 
                               3149                 :                :     /* Assert the input is a temporary SPIPlan */
                               3150         [ -  + ]:          17163 :     Assert(plan->magic == _SPI_PLAN_MAGIC);
                               3151         [ -  + ]:          17163 :     Assert(plan->plancxt == NULL);
                               3152                 :                :     /* One-shot plans can't be saved */
 4628                          3153         [ -  + ]:          17163 :     Assert(!plan->oneshot);
                               3154                 :                : 
                               3155                 :                :     /*
                               3156                 :                :      * Create a memory context for the plan, underneath the procedure context.
                               3157                 :                :      * We don't expect the plan to be very large.
                               3158                 :                :      */
 8874 JanWieck@Yahoo.com       3159                 :          17163 :     plancxt = AllocSetContextCreate(parentcxt,
                               3160                 :                :                                     "SPI Plan",
                               3161                 :                :                                     ALLOCSET_SMALL_SIZES);
                               3162                 :          17163 :     oldcxt = MemoryContextSwitchTo(plancxt);
                               3163                 :                : 
                               3164                 :                :     /* Copy the _SPI_plan struct and subsidiary data into the new context */
 2723 peter_e@gmx.net          3165                 :          17163 :     newplan = (SPIPlanPtr) palloc0(sizeof(_SPI_plan));
 6750 tgl@sss.pgh.pa.us        3166                 :          17163 :     newplan->magic = _SPI_PLAN_MAGIC;
 8874 JanWieck@Yahoo.com       3167                 :          17163 :     newplan->plancxt = plancxt;
 1706 tgl@sss.pgh.pa.us        3168                 :          17163 :     newplan->parse_mode = plan->parse_mode;
 6718                          3169                 :          17163 :     newplan->cursor_options = plan->cursor_options;
10226 bruce@momjian.us         3170                 :          17163 :     newplan->nargs = plan->nargs;
                               3171         [ +  + ]:          17163 :     if (plan->nargs > 0)
                               3172                 :                :     {
                               3173                 :           1835 :         newplan->argtypes = (Oid *) palloc(plan->nargs * sizeof(Oid));
                               3174                 :           1835 :         memcpy(newplan->argtypes, plan->argtypes, plan->nargs * sizeof(Oid));
                               3175                 :                :     }
                               3176                 :                :     else
                               3177                 :          15328 :         newplan->argtypes = NULL;
 5785 tgl@sss.pgh.pa.us        3178                 :          17163 :     newplan->parserSetup = plan->parserSetup;
                               3179                 :          17163 :     newplan->parserSetupArg = plan->parserSetupArg;
                               3180                 :                : 
                               3181                 :                :     /*
                               3182                 :                :      * Reparent all the CachedPlanSources into the procedure context.  In
                               3183                 :                :      * theory this could fail partway through due to the pallocs, but we don't
                               3184                 :                :      * care too much since both the procedure context and the executor context
                               3185                 :                :      * would go away on error.
                               3186                 :                :      */
 6750                          3187   [ +  -  +  +  :          34326 :     foreach(lc, plan->plancache_list)
                                              +  + ]
                               3188                 :                :     {
                               3189                 :          17163 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               3190                 :                : 
 5104                          3191                 :          17163 :         CachedPlanSetParentContext(plansource, parentcxt);
                               3192                 :                : 
                               3193                 :                :         /* Build new list, with list cells in plancxt */
                               3194                 :          17163 :         newplan->plancache_list = lappend(newplan->plancache_list, plansource);
                               3195                 :                :     }
                               3196                 :                : 
 6750                          3197                 :          17163 :     MemoryContextSwitchTo(oldcxt);
                               3198                 :                : 
                               3199                 :                :     /* For safety, unlink the CachedPlanSources from the temporary plan */
 5104                          3200                 :          17163 :     plan->plancache_list = NIL;
                               3201                 :                : 
 6750                          3202                 :          17163 :     return newplan;
                               3203                 :                : }
                               3204                 :                : 
                               3205                 :                : /*
                               3206                 :                :  * Make a "saved" copy of the given plan.
                               3207                 :                :  */
                               3208                 :                : static SPIPlanPtr
 6750 tgl@sss.pgh.pa.us        3209                 :UBC           0 : _SPI_save_plan(SPIPlanPtr plan)
                               3210                 :                : {
                               3211                 :                :     SPIPlanPtr  newplan;
                               3212                 :                :     MemoryContext plancxt;
                               3213                 :                :     MemoryContext oldcxt;
                               3214                 :                :     ListCell   *lc;
                               3215                 :                : 
                               3216                 :                :     /* One-shot plans can't be saved */
 4628                          3217         [ #  # ]:              0 :     Assert(!plan->oneshot);
                               3218                 :                : 
                               3219                 :                :     /*
                               3220                 :                :      * Create a memory context for the plan.  We don't expect the plan to be
                               3221                 :                :      * very large, so use smaller-than-default alloc parameters.  It's a
                               3222                 :                :      * transient context until we finish copying everything.
                               3223                 :                :      */
 5104                          3224                 :              0 :     plancxt = AllocSetContextCreate(CurrentMemoryContext,
                               3225                 :                :                                     "SPI Plan",
                               3226                 :                :                                     ALLOCSET_SMALL_SIZES);
 6750                          3227                 :              0 :     oldcxt = MemoryContextSwitchTo(plancxt);
                               3228                 :                : 
                               3229                 :                :     /* Copy the SPI plan into its own context */
 2723 peter_e@gmx.net          3230                 :              0 :     newplan = (SPIPlanPtr) palloc0(sizeof(_SPI_plan));
 6750 tgl@sss.pgh.pa.us        3231                 :              0 :     newplan->magic = _SPI_PLAN_MAGIC;
                               3232                 :              0 :     newplan->plancxt = plancxt;
 1706                          3233                 :              0 :     newplan->parse_mode = plan->parse_mode;
 6718                          3234                 :              0 :     newplan->cursor_options = plan->cursor_options;
 6750                          3235                 :              0 :     newplan->nargs = plan->nargs;
                               3236         [ #  # ]:              0 :     if (plan->nargs > 0)
                               3237                 :                :     {
                               3238                 :              0 :         newplan->argtypes = (Oid *) palloc(plan->nargs * sizeof(Oid));
                               3239                 :              0 :         memcpy(newplan->argtypes, plan->argtypes, plan->nargs * sizeof(Oid));
                               3240                 :                :     }
                               3241                 :                :     else
                               3242                 :              0 :         newplan->argtypes = NULL;
 5785                          3243                 :              0 :     newplan->parserSetup = plan->parserSetup;
                               3244                 :              0 :     newplan->parserSetupArg = plan->parserSetupArg;
                               3245                 :                : 
                               3246                 :                :     /* Copy all the plancache entries */
 6750                          3247   [ #  #  #  #  :              0 :     foreach(lc, plan->plancache_list)
                                              #  # ]
                               3248                 :                :     {
                               3249                 :              0 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               3250                 :                :         CachedPlanSource *newsource;
                               3251                 :                : 
 5104                          3252                 :              0 :         newsource = CopyCachedPlan(plansource);
 6750                          3253                 :              0 :         newplan->plancache_list = lappend(newplan->plancache_list, newsource);
                               3254                 :                :     }
                               3255                 :                : 
 8874 JanWieck@Yahoo.com       3256                 :              0 :     MemoryContextSwitchTo(oldcxt);
                               3257                 :                : 
                               3258                 :                :     /*
                               3259                 :                :      * Mark it saved, reparent it under CacheMemoryContext, and mark all the
                               3260                 :                :      * component CachedPlanSources as saved.  This sequence cannot fail
                               3261                 :                :      * partway through, so there's no risk of long-term memory leakage.
                               3262                 :                :      */
 5104 tgl@sss.pgh.pa.us        3263                 :              0 :     newplan->saved = true;
                               3264                 :              0 :     MemoryContextSetParent(newplan->plancxt, CacheMemoryContext);
                               3265                 :                : 
                               3266   [ #  #  #  #  :              0 :     foreach(lc, newplan->plancache_list)
                                              #  # ]
                               3267                 :                :     {
                               3268                 :              0 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               3269                 :                : 
                               3270                 :              0 :         SaveCachedPlan(plansource);
                               3271                 :                :     }
                               3272                 :                : 
 9867 bruce@momjian.us         3273                 :              0 :     return newplan;
                               3274                 :                : }
                               3275                 :                : 
                               3276                 :                : /*
                               3277                 :                :  * Internal lookup of ephemeral named relation by name.
                               3278                 :                :  */
                               3279                 :                : static EphemeralNamedRelation
 3081 kgrittn@postgresql.o     3280                 :CBC         357 : _SPI_find_ENR_by_name(const char *name)
                               3281                 :                : {
                               3282                 :                :     /* internal static function; any error is bug in SPI itself */
                               3283         [ -  + ]:            357 :     Assert(name != NULL);
                               3284                 :                : 
                               3285                 :                :     /* fast exit if no tuplestores have been added */
                               3286         [ +  + ]:            357 :     if (_SPI_current->queryEnv == NULL)
                               3287                 :            282 :         return NULL;
                               3288                 :                : 
                               3289                 :             75 :     return get_ENR(_SPI_current->queryEnv, name);
                               3290                 :                : }
                               3291                 :                : 
                               3292                 :                : /*
                               3293                 :                :  * Register an ephemeral named relation for use by the planner and executor on
                               3294                 :                :  * subsequent calls using this SPI connection.
                               3295                 :                :  */
                               3296                 :                : int
                               3297                 :            357 : SPI_register_relation(EphemeralNamedRelation enr)
                               3298                 :                : {
                               3299                 :                :     EphemeralNamedRelation match;
                               3300                 :                :     int         res;
                               3301                 :                : 
                               3302   [ +  -  -  + ]:            357 :     if (enr == NULL || enr->md.name == NULL)
 3081 kgrittn@postgresql.o     3303                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                               3304                 :                : 
 2999 tgl@sss.pgh.pa.us        3305                 :CBC         357 :     res = _SPI_begin_call(false);   /* keep current memory context */
 3081 kgrittn@postgresql.o     3306         [ -  + ]:            357 :     if (res < 0)
 3081 kgrittn@postgresql.o     3307                 :UBC           0 :         return res;
                               3308                 :                : 
 3081 kgrittn@postgresql.o     3309                 :CBC         357 :     match = _SPI_find_ENR_by_name(enr->md.name);
                               3310         [ -  + ]:            357 :     if (match)
 3081 kgrittn@postgresql.o     3311                 :UBC           0 :         res = SPI_ERROR_REL_DUPLICATE;
                               3312                 :                :     else
                               3313                 :                :     {
 3081 kgrittn@postgresql.o     3314         [ +  + ]:CBC         357 :         if (_SPI_current->queryEnv == NULL)
                               3315                 :            282 :             _SPI_current->queryEnv = create_queryEnv();
                               3316                 :                : 
                               3317                 :            357 :         register_ENR(_SPI_current->queryEnv, enr);
                               3318                 :            357 :         res = SPI_OK_REL_REGISTER;
                               3319                 :                :     }
                               3320                 :                : 
                               3321                 :            357 :     _SPI_end_call(false);
                               3322                 :                : 
                               3323                 :            357 :     return res;
                               3324                 :                : }
                               3325                 :                : 
                               3326                 :                : /*
                               3327                 :                :  * Unregister an ephemeral named relation by name.  This will probably be a
                               3328                 :                :  * rarely used function, since SPI_finish will clear it automatically.
                               3329                 :                :  */
                               3330                 :                : int
 3081 kgrittn@postgresql.o     3331                 :UBC           0 : SPI_unregister_relation(const char *name)
                               3332                 :                : {
                               3333                 :                :     EphemeralNamedRelation match;
                               3334                 :                :     int         res;
                               3335                 :                : 
                               3336         [ #  # ]:              0 :     if (name == NULL)
                               3337                 :              0 :         return SPI_ERROR_ARGUMENT;
                               3338                 :                : 
 2999 tgl@sss.pgh.pa.us        3339                 :              0 :     res = _SPI_begin_call(false);   /* keep current memory context */
 3081 kgrittn@postgresql.o     3340         [ #  # ]:              0 :     if (res < 0)
                               3341                 :              0 :         return res;
                               3342                 :                : 
                               3343                 :              0 :     match = _SPI_find_ENR_by_name(name);
                               3344         [ #  # ]:              0 :     if (match)
                               3345                 :                :     {
                               3346                 :              0 :         unregister_ENR(_SPI_current->queryEnv, match->md.name);
                               3347                 :              0 :         res = SPI_OK_REL_UNREGISTER;
                               3348                 :                :     }
                               3349                 :                :     else
                               3350                 :              0 :         res = SPI_ERROR_REL_NOT_FOUND;
                               3351                 :                : 
                               3352                 :              0 :     _SPI_end_call(false);
                               3353                 :                : 
                               3354                 :              0 :     return res;
                               3355                 :                : }
                               3356                 :                : 
                               3357                 :                : /*
                               3358                 :                :  * Register the transient relations from 'tdata' using this SPI connection.
                               3359                 :                :  * This should be called by PL implementations' trigger handlers after
                               3360                 :                :  * connecting, in order to make transition tables visible to any queries run
                               3361                 :                :  * in this connection.
                               3362                 :                :  */
                               3363                 :                : int
 3077 kgrittn@postgresql.o     3364                 :CBC        7739 : SPI_register_trigger_data(TriggerData *tdata)
                               3365                 :                : {
                               3366         [ -  + ]:           7739 :     if (tdata == NULL)
 3077 kgrittn@postgresql.o     3367                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                               3368                 :                : 
 3077 kgrittn@postgresql.o     3369         [ +  + ]:CBC        7739 :     if (tdata->tg_newtable)
                               3370                 :                :     {
                               3371                 :                :         EphemeralNamedRelation enr =
  841 tgl@sss.pgh.pa.us        3372                 :            201 :             palloc(sizeof(EphemeralNamedRelationData));
                               3373                 :                :         int         rc;
                               3374                 :                : 
 3077 kgrittn@postgresql.o     3375                 :            201 :         enr->md.name = tdata->tg_trigger->tgnewtable;
                               3376                 :            201 :         enr->md.reliddesc = tdata->tg_relation->rd_id;
                               3377                 :            201 :         enr->md.tupdesc = NULL;
                               3378                 :            201 :         enr->md.enrtype = ENR_NAMED_TUPLESTORE;
                               3379                 :            201 :         enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_newtable);
                               3380                 :            201 :         enr->reldata = tdata->tg_newtable;
                               3381                 :            201 :         rc = SPI_register_relation(enr);
                               3382         [ -  + ]:            201 :         if (rc != SPI_OK_REL_REGISTER)
 3077 kgrittn@postgresql.o     3383                 :UBC           0 :             return rc;
                               3384                 :                :     }
                               3385                 :                : 
 3077 kgrittn@postgresql.o     3386         [ +  + ]:CBC        7739 :     if (tdata->tg_oldtable)
                               3387                 :                :     {
                               3388                 :                :         EphemeralNamedRelation enr =
  841 tgl@sss.pgh.pa.us        3389                 :            156 :             palloc(sizeof(EphemeralNamedRelationData));
                               3390                 :                :         int         rc;
                               3391                 :                : 
 3077 kgrittn@postgresql.o     3392                 :            156 :         enr->md.name = tdata->tg_trigger->tgoldtable;
                               3393                 :            156 :         enr->md.reliddesc = tdata->tg_relation->rd_id;
                               3394                 :            156 :         enr->md.tupdesc = NULL;
                               3395                 :            156 :         enr->md.enrtype = ENR_NAMED_TUPLESTORE;
                               3396                 :            156 :         enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_oldtable);
                               3397                 :            156 :         enr->reldata = tdata->tg_oldtable;
                               3398                 :            156 :         rc = SPI_register_relation(enr);
                               3399         [ -  + ]:            156 :         if (rc != SPI_OK_REL_REGISTER)
 3077 kgrittn@postgresql.o     3400                 :UBC           0 :             return rc;
                               3401                 :                :     }
                               3402                 :                : 
 3077 kgrittn@postgresql.o     3403                 :CBC        7739 :     return SPI_OK_TD_REGISTER;
                               3404                 :                : }
        

Generated by: LCOV version 2.4-beta