LCOV - differential code coverage report
Current view: top level - src/backend/executor - spi.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: d36b728949bf4e37ada1cd23e0f2aaa94f609a70 vs 52e118fe2f7e3381bdaa479816a7f72eda2ae517 Lines: 73.4 % 1257 923 5 329 22 7 894 5 8
Current Date: 2026-06-29 16:15:13 +0200 Functions: 83.3 % 84 70 2 12 1 5 64 1
Baseline: lcov-20260630-baseline Branches: 55.5 % 805 447 358 8 439
Baseline Date: 2026-06-29 13:01:57 +0200 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 58.3 % 12 7 5 7
(360..) days: 73.6 % 1245 916 329 22 894
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 83.1 % 83 69 2 12 1 4 64
Branch coverage date bins:
(360..) days: 55.5 % 805 447 358 8 439

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * spi.c
                                  4                 :                :  *              Server Programming Interface
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, 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/tuplestore.h"
                                 36                 :                : #include "utils/typcache.h"
                                 37                 :                : 
                                 38                 :                : 
                                 39                 :                : /*
                                 40                 :                :  * These global variables are part of the API for various SPI functions
                                 41                 :                :  * (a horrible API choice, but it's too late now).  To reduce the risk of
                                 42                 :                :  * interference between different SPI callers, we save and restore them
                                 43                 :                :  * when entering/exiting a SPI nesting level.
                                 44                 :                :  */
                                 45                 :                : uint64      SPI_processed = 0;
                                 46                 :                : SPITupleTable *SPI_tuptable = NULL;
                                 47                 :                : int         SPI_result = 0;
                                 48                 :                : 
                                 49                 :                : static _SPI_connection *_SPI_stack = NULL;
                                 50                 :                : static _SPI_connection *_SPI_current = NULL;
                                 51                 :                : static int  _SPI_stack_depth = 0;   /* allocated size of _SPI_stack */
                                 52                 :                : static int  _SPI_connected = -1;    /* current stack index */
                                 53                 :                : 
                                 54                 :                : typedef struct SPICallbackArg
                                 55                 :                : {
                                 56                 :                :     const char *query;
                                 57                 :                :     RawParseMode mode;
                                 58                 :                : } SPICallbackArg;
                                 59                 :                : 
                                 60                 :                : static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
                                 61                 :                :                                        ParamListInfo paramLI, bool read_only);
                                 62                 :                : 
                                 63                 :                : static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan);
                                 64                 :                : 
                                 65                 :                : static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan);
                                 66                 :                : 
                                 67                 :                : static int  _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
                                 68                 :                :                               Snapshot snapshot, Snapshot crosscheck_snapshot,
                                 69                 :                :                               bool fire_triggers);
                                 70                 :                : 
                                 71                 :                : static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes,
                                 72                 :                :                                          const Datum *Values, const char *Nulls);
                                 73                 :                : 
                                 74                 :                : static int  _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount);
                                 75                 :                : 
                                 76                 :                : static void _SPI_error_callback(void *arg);
                                 77                 :                : 
                                 78                 :                : static void _SPI_cursor_operation(Portal portal,
                                 79                 :                :                                   FetchDirection direction, long count,
                                 80                 :                :                                   DestReceiver *dest);
                                 81                 :                : 
                                 82                 :                : static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan);
                                 83                 :                : static SPIPlanPtr _SPI_save_plan(SPIPlanPtr plan);
                                 84                 :                : 
                                 85                 :                : static int  _SPI_begin_call(bool use_exec);
                                 86                 :                : static int  _SPI_end_call(bool use_exec);
                                 87                 :                : static MemoryContext _SPI_execmem(void);
                                 88                 :                : static MemoryContext _SPI_procmem(void);
                                 89                 :                : static bool _SPI_checktuples(void);
                                 90                 :                : 
                                 91                 :                : 
                                 92                 :                : /* =================== interface functions =================== */
                                 93                 :                : 
                                 94                 :                : int
 9160 tgl@sss.pgh.pa.us          95                 :CBC        9109 : SPI_connect(void)
                                 96                 :                : {
 3081 peter_e@gmx.net            97                 :           9109 :     return SPI_connect_ext(0);
                                 98                 :                : }
                                 99                 :                : 
                                100                 :                : int
                                101                 :          67740 : SPI_connect_ext(int options)
                                102                 :                : {
                                103                 :                :     int         newdepth;
                                104                 :                : 
                                105                 :                :     /* Enlarge stack if necessary */
10523 bruce@momjian.us          106         [ +  + ]:          67740 :     if (_SPI_stack == NULL)
                                107                 :                :     {
 8034 tgl@sss.pgh.pa.us         108   [ +  -  -  + ]:           1071 :         if (_SPI_connected != -1 || _SPI_stack_depth != 0)
 8380 tgl@sss.pgh.pa.us         109         [ #  # ]:UBC           0 :             elog(ERROR, "SPI stack corrupted");
 8034 tgl@sss.pgh.pa.us         110                 :CBC        1071 :         newdepth = 16;
                                111                 :           1071 :         _SPI_stack = (_SPI_connection *)
 3081 peter_e@gmx.net           112                 :           1071 :             MemoryContextAlloc(TopMemoryContext,
                                113                 :                :                                newdepth * sizeof(_SPI_connection));
 8034 tgl@sss.pgh.pa.us         114                 :           1071 :         _SPI_stack_depth = newdepth;
                                115                 :                :     }
                                116                 :                :     else
                                117                 :                :     {
                                118   [ +  -  -  + ]:          66669 :         if (_SPI_stack_depth <= 0 || _SPI_stack_depth <= _SPI_connected)
 8380 tgl@sss.pgh.pa.us         119         [ #  # ]:UBC           0 :             elog(ERROR, "SPI stack corrupted");
 8034 tgl@sss.pgh.pa.us         120         [ +  + ]:CBC       66669 :         if (_SPI_stack_depth == _SPI_connected + 1)
                                121                 :                :         {
                                122                 :             13 :             newdepth = _SPI_stack_depth * 2;
                                123                 :             13 :             _SPI_stack = (_SPI_connection *)
                                124                 :             13 :                 repalloc(_SPI_stack,
                                125                 :                :                          newdepth * sizeof(_SPI_connection));
                                126                 :             13 :             _SPI_stack_depth = newdepth;
                                127                 :                :         }
                                128                 :                :     }
                                129                 :                : 
                                130                 :                :     /* Enter new stack level */
10523 bruce@momjian.us          131                 :          67740 :     _SPI_connected++;
 8034 tgl@sss.pgh.pa.us         132   [ +  -  -  + ]:          67740 :     Assert(_SPI_connected >= 0 && _SPI_connected < _SPI_stack_depth);
                                133                 :                : 
10523 bruce@momjian.us          134                 :          67740 :     _SPI_current = &(_SPI_stack[_SPI_connected]);
                                135                 :          67740 :     _SPI_current->processed = 0;
                                136                 :          67740 :     _SPI_current->tuptable = NULL;
 3189 tgl@sss.pgh.pa.us         137                 :          67740 :     _SPI_current->execSubid = InvalidSubTransactionId;
 4723                           138                 :          67740 :     slist_init(&_SPI_current->tuptables);
 3296                           139                 :          67740 :     _SPI_current->procCxt = NULL;    /* in case we fail to create 'em */
 7957                           140                 :          67740 :     _SPI_current->execCxt = NULL;
                                141                 :          67740 :     _SPI_current->connectSubid = GetCurrentSubTransactionId();
 3378 kgrittn@postgresql.o      142                 :          67740 :     _SPI_current->queryEnv = NULL;
 3081 peter_e@gmx.net           143                 :          67740 :     _SPI_current->atomic = (options & SPI_OPT_NONATOMIC ? false : true);
                                144                 :          67740 :     _SPI_current->internal_xact = false;
 2853 tgl@sss.pgh.pa.us         145                 :          67740 :     _SPI_current->outer_processed = SPI_processed;
                                146                 :          67740 :     _SPI_current->outer_tuptable = SPI_tuptable;
                                147                 :          67740 :     _SPI_current->outer_result = SPI_result;
                                148                 :                : 
                                149                 :                :     /*
                                150                 :                :      * Create memory contexts for this procedure
                                151                 :                :      *
                                152                 :                :      * In atomic contexts (the normal case), we use TopTransactionContext,
                                153                 :                :      * otherwise PortalContext, so that it lives across transaction
                                154                 :                :      * boundaries.
                                155                 :                :      *
                                156                 :                :      * XXX It could be better to use PortalContext as the parent context in
                                157                 :                :      * all cases, but we may not be inside a portal (consider deferred-trigger
                                158                 :                :      * execution).  Perhaps CurTransactionContext could be an option?  For now
                                159                 :                :      * it doesn't matter because we clean up explicitly in AtEOSubXact_SPI();
                                160                 :                :      * but see also AtEOXact_SPI().
                                161                 :                :      */
 3081 peter_e@gmx.net           162         [ +  + ]:          67740 :     _SPI_current->procCxt = AllocSetContextCreate(_SPI_current->atomic ? TopTransactionContext : PortalContext,
                                163                 :                :                                                   "SPI Proc",
                                164                 :                :                                                   ALLOCSET_DEFAULT_SIZES);
                                165         [ +  + ]:          67740 :     _SPI_current->execCxt = AllocSetContextCreate(_SPI_current->atomic ? TopTransactionContext : _SPI_current->procCxt,
                                166                 :                :                                                   "SPI Exec",
                                167                 :                :                                                   ALLOCSET_DEFAULT_SIZES);
                                168                 :                :     /* ... and switch to procedure's context */
 9498 tgl@sss.pgh.pa.us         169                 :          67740 :     _SPI_current->savedcxt = MemoryContextSwitchTo(_SPI_current->procCxt);
                                170                 :                : 
                                171                 :                :     /*
                                172                 :                :      * Reset API global variables so that current caller cannot accidentally
                                173                 :                :      * depend on state of an outer caller.
                                174                 :                :      */
 2853                           175                 :          67740 :     SPI_processed = 0;
                                176                 :          67740 :     SPI_tuptable = NULL;
                                177                 :          67740 :     SPI_result = 0;
                                178                 :                : 
10164 bruce@momjian.us          179                 :          67740 :     return SPI_OK_CONNECT;
                                180                 :                : }
                                181                 :                : 
                                182                 :                : int
 9160 tgl@sss.pgh.pa.us         183                 :          66168 : SPI_finish(void)
                                184                 :                : {
                                185                 :                :     int         res;
                                186                 :                : 
 3189                           187                 :          66168 :     res = _SPI_begin_call(false);   /* just check we're connected */
10523 bruce@momjian.us          188         [ -  + ]:          66168 :     if (res < 0)
10164 bruce@momjian.us          189                 :UBC           0 :         return res;
                                190                 :                : 
                                191                 :                :     /* Restore memory context as it was before procedure call */
10523 bruce@momjian.us          192                 :CBC       66168 :     MemoryContextSwitchTo(_SPI_current->savedcxt);
                                193                 :                : 
                                194                 :                :     /* Release memory used in procedure call (including tuptables) */
 9498 tgl@sss.pgh.pa.us         195                 :          66168 :     MemoryContextDelete(_SPI_current->execCxt);
 7957                           196                 :          66168 :     _SPI_current->execCxt = NULL;
 9498                           197                 :          66168 :     MemoryContextDelete(_SPI_current->procCxt);
 7957                           198                 :          66168 :     _SPI_current->procCxt = NULL;
                                199                 :                : 
                                200                 :                :     /*
                                201                 :                :      * Restore outer API variables, especially SPI_tuptable which is probably
                                202                 :                :      * pointing at a just-deleted tuptable
                                203                 :                :      */
 2853                           204                 :          66168 :     SPI_processed = _SPI_current->outer_processed;
                                205                 :          66168 :     SPI_tuptable = _SPI_current->outer_tuptable;
                                206                 :          66168 :     SPI_result = _SPI_current->outer_result;
                                207                 :                : 
                                208                 :                :     /* Exit stack level */
10523 bruce@momjian.us          209                 :          66168 :     _SPI_connected--;
 3521 tgl@sss.pgh.pa.us         210         [ +  + ]:          66168 :     if (_SPI_connected < 0)
 9498                           211                 :          55748 :         _SPI_current = NULL;
                                212                 :                :     else
10523 bruce@momjian.us          213                 :          10420 :         _SPI_current = &(_SPI_stack[_SPI_connected]);
                                214                 :                : 
10164                           215                 :          66168 :     return SPI_OK_FINISH;
                                216                 :                : }
                                217                 :                : 
                                218                 :                : /*
                                219                 :                :  * SPI_start_transaction is a no-op, kept for backwards compatibility.
                                220                 :                :  * SPI callers are *always* inside a transaction.
                                221                 :                :  */
                                222                 :                : void
 3081 peter_e@gmx.net           223                 :UBC           0 : SPI_start_transaction(void)
                                224                 :                : {
                                225                 :              0 : }
                                226                 :                : 
                                227                 :                : static void
 2655 peter@eisentraut.org      228                 :CBC        2144 : _SPI_commit(bool chain)
                                229                 :                : {
 3081 peter_e@gmx.net           230                 :           2144 :     MemoryContext oldcontext = CurrentMemoryContext;
                                231                 :                :     SavedTransactionCharacteristics savetc;
                                232                 :                : 
                                233                 :                :     /*
                                234                 :                :      * Complain if we are in a context that doesn't permit transaction
                                235                 :                :      * termination.  (Note: here and _SPI_rollback should be the only places
                                236                 :                :      * that throw ERRCODE_INVALID_TRANSACTION_TERMINATION, so that callers can
                                237                 :                :      * test for that with security that they know what happened.)
                                238                 :                :      */
                                239         [ +  + ]:           2144 :     if (_SPI_current->atomic)
                                240         [ +  - ]:             16 :         ereport(ERROR,
                                241                 :                :                 (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
                                242                 :                :                  errmsg("invalid transaction termination")));
                                243                 :                : 
                                244                 :                :     /*
                                245                 :                :      * This restriction is required by PLs implemented on top of SPI.  They
                                246                 :                :      * use subtransactions to establish exception blocks that are supposed to
                                247                 :                :      * be rolled back together if there is an error.  Terminating the
                                248                 :                :      * top-level transaction in such a block violates that idea.  A future PL
                                249                 :                :      * implementation might have different ideas about this, in which case
                                250                 :                :      * this restriction would have to be refined or the check possibly be
                                251                 :                :      * moved out of SPI into the PLs.  Note however that the code below relies
                                252                 :                :      * on not being within a subtransaction.
                                253                 :                :      */
                                254         [ +  + ]:           2128 :     if (IsSubTransaction())
                                255         [ +  - ]:              3 :         ereport(ERROR,
                                256                 :                :                 (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
                                257                 :                :                  errmsg("cannot commit while a subtransaction is active")));
                                258                 :                : 
 1583 tgl@sss.pgh.pa.us         259         [ +  + ]:           2125 :     if (chain)
                                260                 :              2 :         SaveTransactionCharacteristics(&savetc);
                                261                 :                : 
                                262                 :                :     /* Catch any error occurring during the COMMIT */
                                263         [ +  + ]:           2125 :     PG_TRY();
                                264                 :                :     {
                                265                 :                :         /* Protect current SPI stack entry against deletion */
                                266                 :           2125 :         _SPI_current->internal_xact = true;
                                267                 :                : 
                                268                 :                :         /*
                                269                 :                :          * Hold any pinned portals that any PLs might be using.  We have to do
                                270                 :                :          * this before changing transaction state, since this will run
                                271                 :                :          * user-defined code that might throw an error.
                                272                 :                :          */
                                273                 :           2125 :         HoldPinnedPortals();
                                274                 :                : 
                                275                 :                :         /* Release snapshots associated with portals */
                                276                 :           2124 :         ForgetPortalSnapshots();
                                277                 :                : 
                                278                 :                :         /* Do the deed */
                                279                 :           2124 :         CommitTransactionCommand();
                                280                 :                : 
                                281                 :                :         /* Immediately start a new transaction */
 2655 peter@eisentraut.org      282                 :           2117 :         StartTransactionCommand();
 1583 tgl@sss.pgh.pa.us         283         [ +  + ]:           2117 :         if (chain)
                                284                 :              2 :             RestoreTransactionCharacteristics(&savetc);
                                285                 :                : 
                                286                 :           2117 :         MemoryContextSwitchTo(oldcontext);
                                287                 :                : 
                                288                 :           2117 :         _SPI_current->internal_xact = false;
                                289                 :                :     }
                                290                 :              8 :     PG_CATCH();
                                291                 :                :     {
                                292                 :                :         ErrorData  *edata;
                                293                 :                : 
                                294                 :                :         /* Save error info in caller's context */
                                295                 :              8 :         MemoryContextSwitchTo(oldcontext);
                                296                 :              8 :         edata = CopyErrorData();
                                297                 :              8 :         FlushErrorState();
                                298                 :                : 
                                299                 :                :         /*
                                300                 :                :          * Abort the failed transaction.  If this fails too, we'll just
                                301                 :                :          * propagate the error out ... there's not that much we can do.
                                302                 :                :          */
                                303                 :              8 :         AbortCurrentTransaction();
                                304                 :                : 
                                305                 :                :         /* ... and start a new one */
                                306                 :              8 :         StartTransactionCommand();
                                307         [ -  + ]:              8 :         if (chain)
 1583 tgl@sss.pgh.pa.us         308                 :UBC           0 :             RestoreTransactionCharacteristics(&savetc);
                                309                 :                : 
 1583 tgl@sss.pgh.pa.us         310                 :CBC           8 :         MemoryContextSwitchTo(oldcontext);
                                311                 :                : 
                                312                 :              8 :         _SPI_current->internal_xact = false;
                                313                 :                : 
                                314                 :                :         /* Now that we've cleaned up the transaction, re-throw the error */
                                315                 :              8 :         ReThrowError(edata);
                                316                 :                :     }
                                317         [ -  + ]:           2117 :     PG_END_TRY();
 3081 peter_e@gmx.net           318                 :           2117 : }
                                319                 :                : 
                                320                 :                : void
 2655 peter@eisentraut.org      321                 :           2142 : SPI_commit(void)
                                322                 :                : {
                                323                 :           2142 :     _SPI_commit(false);
                                324                 :           2115 : }
                                325                 :                : 
                                326                 :                : void
                                327                 :              2 : SPI_commit_and_chain(void)
                                328                 :                : {
                                329                 :              2 :     _SPI_commit(true);
                                330                 :              2 : }
                                331                 :                : 
                                332                 :                : static void
                                333                 :             96 : _SPI_rollback(bool chain)
                                334                 :                : {
 3081 peter_e@gmx.net           335                 :             96 :     MemoryContext oldcontext = CurrentMemoryContext;
                                336                 :                :     SavedTransactionCharacteristics savetc;
                                337                 :                : 
                                338                 :                :     /* see comments in _SPI_commit() */
                                339         [ -  + ]:             96 :     if (_SPI_current->atomic)
 3081 peter_e@gmx.net           340         [ #  # ]:UBC           0 :         ereport(ERROR,
                                341                 :                :                 (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
                                342                 :                :                  errmsg("invalid transaction termination")));
                                343                 :                : 
                                344                 :                :     /* see comments in _SPI_commit() */
 3081 peter_e@gmx.net           345         [ +  + ]:CBC          96 :     if (IsSubTransaction())
                                346         [ +  - ]:              2 :         ereport(ERROR,
                                347                 :                :                 (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
                                348                 :                :                  errmsg("cannot roll back while a subtransaction is active")));
                                349                 :                : 
 1583 tgl@sss.pgh.pa.us         350         [ +  + ]:             94 :     if (chain)
                                351                 :              2 :         SaveTransactionCharacteristics(&savetc);
                                352                 :                : 
                                353                 :                :     /* Catch any error occurring during the ROLLBACK */
                                354         [ +  + ]:             94 :     PG_TRY();
                                355                 :                :     {
                                356                 :                :         /* Protect current SPI stack entry against deletion */
                                357                 :             94 :         _SPI_current->internal_xact = true;
                                358                 :                : 
                                359                 :                :         /*
                                360                 :                :          * Hold any pinned portals that any PLs might be using.  We have to do
                                361                 :                :          * this before changing transaction state, since this will run
                                362                 :                :          * user-defined code that might throw an error, and in any case
                                363                 :                :          * couldn't be run in an already-aborted transaction.
                                364                 :                :          */
                                365                 :             94 :         HoldPinnedPortals();
                                366                 :                : 
                                367                 :                :         /* Release snapshots associated with portals */
                                368                 :             92 :         ForgetPortalSnapshots();
                                369                 :                : 
                                370                 :                :         /* Do the deed */
                                371                 :             92 :         AbortCurrentTransaction();
                                372                 :                : 
                                373                 :                :         /* Immediately start a new transaction */
 2655 peter@eisentraut.org      374                 :             92 :         StartTransactionCommand();
 1583 tgl@sss.pgh.pa.us         375         [ +  + ]:             92 :         if (chain)
                                376                 :              2 :             RestoreTransactionCharacteristics(&savetc);
                                377                 :                : 
                                378                 :             92 :         MemoryContextSwitchTo(oldcontext);
                                379                 :                : 
                                380                 :             92 :         _SPI_current->internal_xact = false;
                                381                 :                :     }
                                382                 :              2 :     PG_CATCH();
                                383                 :                :     {
                                384                 :                :         ErrorData  *edata;
                                385                 :                : 
                                386                 :                :         /* Save error info in caller's context */
                                387                 :              2 :         MemoryContextSwitchTo(oldcontext);
                                388                 :              2 :         edata = CopyErrorData();
                                389                 :              2 :         FlushErrorState();
                                390                 :                : 
                                391                 :                :         /*
                                392                 :                :          * Try again to abort the failed transaction.  If this fails too,
                                393                 :                :          * we'll just propagate the error out ... there's not that much we can
                                394                 :                :          * do.
                                395                 :                :          */
                                396                 :              2 :         AbortCurrentTransaction();
                                397                 :                : 
                                398                 :                :         /* ... and start a new one */
                                399                 :              2 :         StartTransactionCommand();
                                400         [ -  + ]:              2 :         if (chain)
 1583 tgl@sss.pgh.pa.us         401                 :UBC           0 :             RestoreTransactionCharacteristics(&savetc);
                                402                 :                : 
 1583 tgl@sss.pgh.pa.us         403                 :CBC           2 :         MemoryContextSwitchTo(oldcontext);
                                404                 :                : 
                                405                 :              2 :         _SPI_current->internal_xact = false;
                                406                 :                : 
                                407                 :                :         /* Now that we've cleaned up the transaction, re-throw the error */
                                408                 :              2 :         ReThrowError(edata);
                                409                 :                :     }
                                410         [ -  + ]:             92 :     PG_END_TRY();
 3081 peter_e@gmx.net           411                 :             92 : }
                                412                 :                : 
                                413                 :                : void
 2655 peter@eisentraut.org      414                 :             94 : SPI_rollback(void)
                                415                 :                : {
                                416                 :             94 :     _SPI_rollback(false);
                                417                 :             90 : }
                                418                 :                : 
                                419                 :                : void
                                420                 :              2 : SPI_rollback_and_chain(void)
                                421                 :                : {
                                422                 :              2 :     _SPI_rollback(true);
                                423                 :              2 : }
                                424                 :                : 
                                425                 :                : /*
                                426                 :                :  * Clean up SPI state at transaction commit or abort.
                                427                 :                :  */
                                428                 :                : void
 8246 mail@joeconway.com        429                 :         426532 : AtEOXact_SPI(bool isCommit)
                                430                 :                : {
 1583 tgl@sss.pgh.pa.us         431                 :         426532 :     bool        found = false;
                                432                 :                : 
                                433                 :                :     /*
                                434                 :                :      * Pop stack entries, stopping if we find one marked internal_xact (that
                                435                 :                :      * one belongs to the caller of SPI_commit or SPI_rollback).
                                436                 :                :      */
                                437         [ +  + ]:         427966 :     while (_SPI_connected >= 0)
                                438                 :                :     {
                                439                 :           3653 :         _SPI_connection *connection = &(_SPI_stack[_SPI_connected]);
                                440                 :                : 
                                441         [ +  + ]:           3653 :         if (connection->internal_xact)
                                442                 :           2219 :             break;
                                443                 :                : 
                                444                 :           1434 :         found = true;
                                445                 :                : 
                                446                 :                :         /*
                                447                 :                :          * We need not release the procedure's memory contexts explicitly, as
                                448                 :                :          * they'll go away automatically when their parent context does; see
                                449                 :                :          * notes in SPI_connect_ext.
                                450                 :                :          */
                                451                 :                : 
                                452                 :                :         /*
                                453                 :                :          * Restore outer global variables and pop the stack entry.  Unlike
                                454                 :                :          * SPI_finish(), we don't risk switching to memory contexts that might
                                455                 :                :          * be already gone.
                                456                 :                :          */
                                457                 :           1434 :         SPI_processed = connection->outer_processed;
                                458                 :           1434 :         SPI_tuptable = connection->outer_tuptable;
                                459                 :           1434 :         SPI_result = connection->outer_result;
                                460                 :                : 
                                461                 :           1434 :         _SPI_connected--;
                                462         [ +  + ]:           1434 :         if (_SPI_connected < 0)
                                463                 :           1401 :             _SPI_current = NULL;
                                464                 :                :         else
                                465                 :             33 :             _SPI_current = &(_SPI_stack[_SPI_connected]);
                                466                 :                :     }
                                467                 :                : 
                                468                 :                :     /* We should only find entries to pop during an ABORT. */
                                469   [ +  +  -  + ]:         426532 :     if (found && isCommit)
 8034 tgl@sss.pgh.pa.us         470         [ #  # ]:UBC           0 :         ereport(WARNING,
                                471                 :                :                 (errcode(ERRCODE_WARNING),
                                472                 :                :                  errmsg("transaction left non-empty SPI stack"),
                                473                 :                :                  errhint("Check for missing \"SPI_finish\" calls.")));
 9498 tgl@sss.pgh.pa.us         474                 :CBC      426532 : }
                                475                 :                : 
                                476                 :                : /*
                                477                 :                :  * Clean up SPI state at subtransaction commit or abort.
                                478                 :                :  *
                                479                 :                :  * During commit, there shouldn't be any unclosed entries remaining from
                                480                 :                :  * the current subtransaction; we emit a warning if any are found.
                                481                 :                :  */
                                482                 :                : void
 7957                           483                 :          12682 : AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
                                484                 :                : {
 7975 bruce@momjian.us          485                 :          12682 :     bool        found = false;
                                486                 :                : 
 8034 tgl@sss.pgh.pa.us         487         [ +  + ]:          12820 :     while (_SPI_connected >= 0)
                                488                 :                :     {
                                489                 :          10197 :         _SPI_connection *connection = &(_SPI_stack[_SPI_connected]);
                                490                 :                : 
 7957                           491         [ +  + ]:          10197 :         if (connection->connectSubid != mySubid)
 8034                           492                 :          10059 :             break;              /* couldn't be any underneath it either */
                                493                 :                : 
 3081 peter_e@gmx.net           494         [ -  + ]:            138 :         if (connection->internal_xact)
 3081 peter_e@gmx.net           495                 :UBC           0 :             break;
                                496                 :                : 
 8034 tgl@sss.pgh.pa.us         497                 :CBC         138 :         found = true;
                                498                 :                : 
                                499                 :                :         /*
                                500                 :                :          * Release procedure memory explicitly (see note in SPI_connect)
                                501                 :                :          */
 7957                           502         [ +  - ]:            138 :         if (connection->execCxt)
                                503                 :                :         {
                                504                 :            138 :             MemoryContextDelete(connection->execCxt);
                                505                 :            138 :             connection->execCxt = NULL;
                                506                 :                :         }
                                507         [ +  - ]:            138 :         if (connection->procCxt)
                                508                 :                :         {
                                509                 :            138 :             MemoryContextDelete(connection->procCxt);
                                510                 :            138 :             connection->procCxt = NULL;
                                511                 :                :         }
                                512                 :                : 
                                513                 :                :         /*
                                514                 :                :          * Restore outer global variables and pop the stack entry.  Unlike
                                515                 :                :          * SPI_finish(), we don't risk switching to memory contexts that might
                                516                 :                :          * be already gone.
                                517                 :                :          */
 2853                           518                 :            138 :         SPI_processed = connection->outer_processed;
                                519                 :            138 :         SPI_tuptable = connection->outer_tuptable;
                                520                 :            138 :         SPI_result = connection->outer_result;
                                521                 :                : 
 8034                           522                 :            138 :         _SPI_connected--;
 3521                           523         [ +  + ]:            138 :         if (_SPI_connected < 0)
 8034                           524                 :             48 :             _SPI_current = NULL;
                                525                 :                :         else
                                526                 :             90 :             _SPI_current = &(_SPI_stack[_SPI_connected]);
                                527                 :                :     }
                                528                 :                : 
                                529   [ +  +  -  + ]:          12682 :     if (found && isCommit)
 8034 tgl@sss.pgh.pa.us         530         [ #  # ]:UBC           0 :         ereport(WARNING,
                                531                 :                :                 (errcode(ERRCODE_WARNING),
                                532                 :                :                  errmsg("subtransaction left non-empty SPI stack"),
                                533                 :                :                  errhint("Check for missing \"SPI_finish\" calls.")));
                                534                 :                : 
                                535                 :                :     /*
                                536                 :                :      * If we are aborting a subtransaction and there is an open SPI context
                                537                 :                :      * surrounding the subxact, clean up to prevent memory leakage.
                                538                 :                :      */
 7161 tgl@sss.pgh.pa.us         539   [ +  +  +  + ]:CBC       12682 :     if (_SPI_current && !isCommit)
                                540                 :                :     {
                                541                 :                :         slist_mutable_iter siter;
                                542                 :                : 
                                543                 :                :         /*
                                544                 :                :          * Throw away executor state if current executor operation was started
                                545                 :                :          * within current subxact (essentially, force a _SPI_end_call(true)).
                                546                 :                :          */
 3189                           547         [ +  + ]:           3666 :         if (_SPI_current->execSubid >= mySubid)
                                548                 :                :         {
                                549                 :           3142 :             _SPI_current->execSubid = InvalidSubTransactionId;
  958 nathan@postgresql.or      550                 :           3142 :             MemoryContextReset(_SPI_current->execCxt);
                                551                 :                :         }
                                552                 :                : 
                                553                 :                :         /* throw away any tuple tables created within current subxact */
 4723 tgl@sss.pgh.pa.us         554   [ +  +  +  +  :           9228 :         slist_foreach_modify(siter, &_SPI_current->tuptables)
                                              +  + ]
                                555                 :                :         {
                                556                 :                :             SPITupleTable *tuptable;
                                557                 :                : 
                                558                 :           5562 :             tuptable = slist_container(SPITupleTable, next, siter.cur);
                                559         [ +  + ]:           5562 :             if (tuptable->subid >= mySubid)
                                560                 :                :             {
                                561                 :                :                 /*
                                562                 :                :                  * If we used SPI_freetuptable() here, its internal search of
                                563                 :                :                  * the tuptables list would make this operation O(N^2).
                                564                 :                :                  * Instead, just free the tuptable manually.  This should
                                565                 :                :                  * match what SPI_freetuptable() does.
                                566                 :                :                  */
                                567                 :           2986 :                 slist_delete_current(&siter);
                                568         [ +  + ]:           2986 :                 if (tuptable == _SPI_current->tuptable)
                                569                 :           2982 :                     _SPI_current->tuptable = NULL;
                                570         [ +  + ]:           2986 :                 if (tuptable == SPI_tuptable)
                                571                 :              4 :                     SPI_tuptable = NULL;
                                572                 :           2986 :                 MemoryContextDelete(tuptable->tuptabcxt);
                                573                 :                :             }
                                574                 :                :         }
                                575                 :                :     }
 8034                           576                 :          12682 : }
                                577                 :                : 
                                578                 :                : /*
                                579                 :                :  * Are we executing inside a procedure (that is, a nonatomic SPI context)?
                                580                 :                :  */
                                581                 :                : bool
 2822                           582                 :         420496 : SPI_inside_nonatomic_context(void)
                                583                 :                : {
                                584         [ +  + ]:         420496 :     if (_SPI_current == NULL)
                                585                 :         418277 :         return false;           /* not in any SPI context at all */
                                586                 :                :     /* these tests must match _SPI_commit's opinion of what's atomic: */
                                587         [ -  + ]:           2219 :     if (_SPI_current->atomic)
 2822 tgl@sss.pgh.pa.us         588                 :UBC           0 :         return false;           /* it's atomic (ie function not procedure) */
  622 tgl@sss.pgh.pa.us         589         [ -  + ]:CBC        2219 :     if (IsSubTransaction())
  622 tgl@sss.pgh.pa.us         590                 :UBC           0 :         return false;           /* if within subtransaction, it's atomic */
 2822 tgl@sss.pgh.pa.us         591                 :CBC        2219 :     return true;
                                592                 :                : }
                                593                 :                : 
                                594                 :                : 
                                595                 :                : /* Parse, plan, and execute a query string */
                                596                 :                : int
 7729 neilc@samurai.com         597                 :            854 : SPI_execute(const char *src, bool read_only, long tcount)
                                598                 :                : {
                                599                 :                :     _SPI_plan   plan;
                                600                 :                :     SPIExecuteOptions options;
                                601                 :                :     int         res;
                                602                 :                : 
10523 bruce@momjian.us          603   [ +  -  -  + ]:            854 :     if (src == NULL || tcount < 0)
10164 bruce@momjian.us          604                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                605                 :                : 
10523 bruce@momjian.us          606                 :CBC         854 :     res = _SPI_begin_call(true);
                                607         [ -  + ]:            854 :     if (res < 0)
10164 bruce@momjian.us          608                 :UBC           0 :         return res;
                                609                 :                : 
 7047 tgl@sss.pgh.pa.us         610                 :CBC         854 :     memset(&plan, 0, sizeof(_SPI_plan));
                                611                 :            854 :     plan.magic = _SPI_PLAN_MAGIC;
 2003                           612                 :            854 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 3385 rhaas@postgresql.org      613                 :            854 :     plan.cursor_options = CURSOR_OPT_PARALLEL_OK;
                                614                 :                : 
 4925 tgl@sss.pgh.pa.us         615                 :            854 :     _SPI_prepare_oneshot_plan(src, &plan);
                                616                 :                : 
 1731                           617                 :            848 :     memset(&options, 0, sizeof(options));
                                618                 :            848 :     options.read_only = read_only;
                                619                 :            848 :     options.tcount = tcount;
                                620                 :                : 
                                621                 :            848 :     res = _SPI_execute_plan(&plan, &options,
                                622                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                623                 :                :                             true);
                                624                 :                : 
10523 bruce@momjian.us          625                 :            822 :     _SPI_end_call(true);
10164                           626                 :            822 :     return res;
                                627                 :                : }
                                628                 :                : 
                                629                 :                : /* Obsolete version of SPI_execute */
                                630                 :                : int
 7729 neilc@samurai.com         631                 :            325 : SPI_exec(const char *src, long tcount)
                                632                 :                : {
 7960 tgl@sss.pgh.pa.us         633                 :            325 :     return SPI_execute(src, false, tcount);
                                634                 :                : }
                                635                 :                : 
                                636                 :                : /* Parse, plan, and execute a query string, with extensible options */
                                637                 :                : int
 1981                           638                 :          11320 : SPI_execute_extended(const char *src,
                                639                 :                :                      const SPIExecuteOptions *options)
                                640                 :                : {
                                641                 :                :     int         res;
                                642                 :                :     _SPI_plan   plan;
                                643                 :                : 
                                644   [ +  -  -  + ]:          11320 :     if (src == NULL || options == NULL)
 1981 tgl@sss.pgh.pa.us         645                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                646                 :                : 
 1981 tgl@sss.pgh.pa.us         647                 :CBC       11320 :     res = _SPI_begin_call(true);
                                648         [ -  + ]:          11320 :     if (res < 0)
 1981 tgl@sss.pgh.pa.us         649                 :UBC           0 :         return res;
                                650                 :                : 
 1981 tgl@sss.pgh.pa.us         651                 :CBC       11320 :     memset(&plan, 0, sizeof(_SPI_plan));
                                652                 :          11320 :     plan.magic = _SPI_PLAN_MAGIC;
                                653                 :          11320 :     plan.parse_mode = RAW_PARSE_DEFAULT;
                                654                 :          11320 :     plan.cursor_options = CURSOR_OPT_PARALLEL_OK;
                                655         [ +  + ]:          11320 :     if (options->params)
                                656                 :                :     {
                                657                 :            289 :         plan.parserSetup = options->params->parserSetup;
                                658                 :            289 :         plan.parserSetupArg = options->params->parserSetupArg;
                                659                 :                :     }
                                660                 :                : 
                                661                 :          11320 :     _SPI_prepare_oneshot_plan(src, &plan);
                                662                 :                : 
 1731                           663                 :          11320 :     res = _SPI_execute_plan(&plan, options,
                                664                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                665                 :                :                             true);
                                666                 :                : 
 1981                           667                 :          11161 :     _SPI_end_call(true);
                                668                 :          11161 :     return res;
                                669                 :                : }
                                670                 :                : 
                                671                 :                : /* Execute a previously prepared plan */
                                672                 :                : int
  242 peter@eisentraut.org      673                 :GNC        2665 : SPI_execute_plan(SPIPlanPtr plan, const Datum *Values, const char *Nulls,
                                674                 :                :                  bool read_only, long tcount)
                                675                 :                : {
                                676                 :                :     SPIExecuteOptions options;
                                677                 :                :     int         res;
                                678                 :                : 
 7047 tgl@sss.pgh.pa.us         679   [ +  -  +  -  :CBC        2665 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
                                              -  + ]
10164 bruce@momjian.us          680                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                681                 :                : 
 7047 tgl@sss.pgh.pa.us         682   [ +  +  -  + ]:CBC        2665 :     if (plan->nargs > 0 && Values == NULL)
10164 bruce@momjian.us          683                 :UBC           0 :         return SPI_ERROR_PARAM;
                                684                 :                : 
10523 bruce@momjian.us          685                 :CBC        2665 :     res = _SPI_begin_call(true);
                                686         [ -  + ]:           2665 :     if (res < 0)
10164 bruce@momjian.us          687                 :UBC           0 :         return res;
                                688                 :                : 
 1731 tgl@sss.pgh.pa.us         689                 :CBC        2665 :     memset(&options, 0, sizeof(options));
                                690                 :           2665 :     options.params = _SPI_convert_params(plan->nargs, plan->argtypes,
                                691                 :                :                                          Values, Nulls);
                                692                 :           2665 :     options.read_only = read_only;
                                693                 :           2665 :     options.tcount = tcount;
                                694                 :                : 
                                695                 :           2665 :     res = _SPI_execute_plan(plan, &options,
                                696                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                697                 :                :                             true);
                                698                 :                : 
 8314                           699                 :           2664 :     _SPI_end_call(true);
                                700                 :           2664 :     return res;
                                701                 :                : }
                                702                 :                : 
                                703                 :                : /* Obsolete version of SPI_execute_plan */
                                704                 :                : int
 7047                           705                 :             27 : SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls, long tcount)
                                706                 :                : {
 7960                           707                 :             27 :     return SPI_execute_plan(plan, Values, Nulls, false, tcount);
                                708                 :                : }
                                709                 :                : 
                                710                 :                : /* Execute a previously prepared plan */
                                711                 :                : int
 1982                           712                 :           1714 : SPI_execute_plan_extended(SPIPlanPtr plan,
                                713                 :                :                           const SPIExecuteOptions *options)
                                714                 :                : {
                                715                 :                :     int         res;
                                716                 :                : 
                                717   [ +  -  +  -  :           1714 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || options == NULL)
                                              -  + ]
 6082 tgl@sss.pgh.pa.us         718                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                719                 :                : 
 6082 tgl@sss.pgh.pa.us         720                 :CBC        1714 :     res = _SPI_begin_call(true);
                                721         [ -  + ]:           1714 :     if (res < 0)
 6082 tgl@sss.pgh.pa.us         722                 :UBC           0 :         return res;
                                723                 :                : 
 1731 tgl@sss.pgh.pa.us         724                 :CBC        1714 :     res = _SPI_execute_plan(plan, options,
                                725                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                726                 :                :                             true);
                                727                 :                : 
 2209                           728                 :           1708 :     _SPI_end_call(true);
                                729                 :           1708 :     return res;
                                730                 :                : }
                                731                 :                : 
                                732                 :                : /* Execute a previously prepared plan */
                                733                 :                : int
 1982                           734                 :          48602 : SPI_execute_plan_with_paramlist(SPIPlanPtr plan, ParamListInfo params,
                                735                 :                :                                 bool read_only, long tcount)
                                736                 :                : {
                                737                 :                :     SPIExecuteOptions options;
                                738                 :                :     int         res;
                                739                 :                : 
 2209                           740   [ +  -  +  -  :          48602 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
                                              -  + ]
 2209 tgl@sss.pgh.pa.us         741                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                742                 :                : 
 2209 tgl@sss.pgh.pa.us         743                 :CBC       48602 :     res = _SPI_begin_call(true);
                                744         [ -  + ]:          48602 :     if (res < 0)
 2209 tgl@sss.pgh.pa.us         745                 :UBC           0 :         return res;
                                746                 :                : 
 1731 tgl@sss.pgh.pa.us         747                 :CBC       48602 :     memset(&options, 0, sizeof(options));
                                748                 :          48602 :     options.params = params;
                                749                 :          48602 :     options.read_only = read_only;
                                750                 :          48602 :     options.tcount = tcount;
                                751                 :                : 
                                752                 :          48602 :     res = _SPI_execute_plan(plan, &options,
                                753                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                754                 :                :                             true);
                                755                 :                : 
 6082                           756                 :          45569 :     _SPI_end_call(true);
                                757                 :          45569 :     return res;
                                758                 :                : }
                                759                 :                : 
                                760                 :                : /*
                                761                 :                :  * SPI_execute_snapshot -- identical to SPI_execute_plan, except that we allow
                                762                 :                :  * the caller to specify exactly which snapshots to use, which will be
                                763                 :                :  * registered here.  Also, the caller may specify that AFTER triggers should be
                                764                 :                :  * queued as part of the outer query rather than being fired immediately at the
                                765                 :                :  * end of the command.
                                766                 :                :  *
                                767                 :                :  * This is currently not documented in spi.sgml because it is only intended
                                768                 :                :  * for use by RI triggers.
                                769                 :                :  *
                                770                 :                :  * Passing snapshot == InvalidSnapshot will select the normal behavior of
                                771                 :                :  * fetching a new snapshot for each query.
                                772                 :                :  */
                                773                 :                : int
 7047                           774                 :           3301 : SPI_execute_snapshot(SPIPlanPtr plan,
                                775                 :                :                      const Datum *Values, const char *Nulls,
                                776                 :                :                      Snapshot snapshot, Snapshot crosscheck_snapshot,
                                777                 :                :                      bool read_only, bool fire_triggers, long tcount)
                                778                 :                : {
                                779                 :                :     SPIExecuteOptions options;
                                780                 :                :     int         res;
                                781                 :                : 
                                782   [ +  -  +  -  :           3301 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
                                              -  + ]
 8314 tgl@sss.pgh.pa.us         783                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                784                 :                : 
 7047 tgl@sss.pgh.pa.us         785   [ +  +  -  + ]:CBC        3301 :     if (plan->nargs > 0 && Values == NULL)
 8314 tgl@sss.pgh.pa.us         786                 :UBC           0 :         return SPI_ERROR_PARAM;
                                787                 :                : 
 8314 tgl@sss.pgh.pa.us         788                 :CBC        3301 :     res = _SPI_begin_call(true);
                                789         [ -  + ]:           3301 :     if (res < 0)
 8314 tgl@sss.pgh.pa.us         790                 :UBC           0 :         return res;
                                791                 :                : 
 1731 tgl@sss.pgh.pa.us         792                 :CBC        3301 :     memset(&options, 0, sizeof(options));
                                793                 :           3301 :     options.params = _SPI_convert_params(plan->nargs, plan->argtypes,
                                794                 :                :                                          Values, Nulls);
                                795                 :           3301 :     options.read_only = read_only;
                                796                 :           3301 :     options.tcount = tcount;
                                797                 :                : 
                                798                 :           3301 :     res = _SPI_execute_plan(plan, &options,
                                799                 :                :                             snapshot, crosscheck_snapshot,
                                800                 :                :                             fire_triggers);
                                801                 :                : 
10523 bruce@momjian.us          802                 :           3291 :     _SPI_end_call(true);
10164                           803                 :           3291 :     return res;
                                804                 :                : }
                                805                 :                : 
                                806                 :                : /*
                                807                 :                :  * SPI_execute_with_args -- plan and execute a query with supplied arguments
                                808                 :                :  *
                                809                 :                :  * This is functionally equivalent to SPI_prepare followed by
                                810                 :                :  * SPI_execute_plan.
                                811                 :                :  */
                                812                 :                : int
 6664 tgl@sss.pgh.pa.us         813                 :GBC           7 : SPI_execute_with_args(const char *src,
                                814                 :                :                       int nargs, Oid *argtypes,
                                815                 :                :                       const Datum *Values, const char *Nulls,
                                816                 :                :                       bool read_only, long tcount)
                                817                 :                : {
                                818                 :                :     int         res;
                                819                 :                :     _SPI_plan   plan;
                                820                 :                :     ParamListInfo paramLI;
                                821                 :                :     SPIExecuteOptions options;
                                822                 :                : 
                                823   [ +  -  +  -  :              7 :     if (src == NULL || nargs < 0 || tcount < 0)
                                              -  + ]
 6664 tgl@sss.pgh.pa.us         824                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                825                 :                : 
 6664 tgl@sss.pgh.pa.us         826   [ +  -  +  -  :GBC           7 :     if (nargs > 0 && (argtypes == NULL || Values == NULL))
                                              -  + ]
 6664 tgl@sss.pgh.pa.us         827                 :UBC           0 :         return SPI_ERROR_PARAM;
                                828                 :                : 
 6664 tgl@sss.pgh.pa.us         829                 :GBC           7 :     res = _SPI_begin_call(true);
                                830         [ -  + ]:              7 :     if (res < 0)
 6664 tgl@sss.pgh.pa.us         831                 :UBC           0 :         return res;
                                832                 :                : 
 6664 tgl@sss.pgh.pa.us         833                 :GBC           7 :     memset(&plan, 0, sizeof(_SPI_plan));
                                834                 :              7 :     plan.magic = _SPI_PLAN_MAGIC;
 2003                           835                 :              7 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 3385 rhaas@postgresql.org      836                 :              7 :     plan.cursor_options = CURSOR_OPT_PARALLEL_OK;
 6664 tgl@sss.pgh.pa.us         837                 :              7 :     plan.nargs = nargs;
                                838                 :              7 :     plan.argtypes = argtypes;
 6082                           839                 :              7 :     plan.parserSetup = NULL;
                                840                 :              7 :     plan.parserSetupArg = NULL;
                                841                 :                : 
 6664                           842                 :              7 :     paramLI = _SPI_convert_params(nargs, argtypes,
                                843                 :                :                                   Values, Nulls);
                                844                 :                : 
 4925                           845                 :              7 :     _SPI_prepare_oneshot_plan(src, &plan);
                                846                 :                : 
 1731                           847                 :              7 :     memset(&options, 0, sizeof(options));
                                848                 :              7 :     options.params = paramLI;
                                849                 :              7 :     options.read_only = read_only;
                                850                 :              7 :     options.tcount = tcount;
                                851                 :                : 
                                852                 :              7 :     res = _SPI_execute_plan(&plan, &options,
                                853                 :                :                             InvalidSnapshot, InvalidSnapshot,
                                854                 :                :                             true);
                                855                 :                : 
 2209                           856                 :              7 :     _SPI_end_call(true);
                                857                 :              7 :     return res;
                                858                 :                : }
                                859                 :                : 
                                860                 :                : SPIPlanPtr
 8583 tgl@sss.pgh.pa.us         861                 :CBC        2441 : SPI_prepare(const char *src, int nargs, Oid *argtypes)
                                862                 :                : {
 7015                           863                 :           2441 :     return SPI_prepare_cursor(src, nargs, argtypes, 0);
                                864                 :                : }
                                865                 :                : 
                                866                 :                : SPIPlanPtr
                                867                 :           2441 : SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes,
                                868                 :                :                    int cursorOptions)
                                869                 :                : {
                                870                 :                :     _SPI_plan   plan;
                                871                 :                :     SPIPlanPtr  result;
                                872                 :                : 
10504 vadim4o@yahoo.com         873   [ +  -  +  -  :           2441 :     if (src == NULL || nargs < 0 || (nargs > 0 && argtypes == NULL))
                                        +  +  -  + ]
                                874                 :                :     {
10523 bruce@momjian.us          875                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
10164                           876                 :              0 :         return NULL;
                                877                 :                :     }
                                878                 :                : 
10523 bruce@momjian.us          879                 :CBC        2441 :     SPI_result = _SPI_begin_call(true);
                                880         [ -  + ]:           2441 :     if (SPI_result < 0)
10164 bruce@momjian.us          881                 :UBC           0 :         return NULL;
                                882                 :                : 
 7047 tgl@sss.pgh.pa.us         883                 :CBC        2441 :     memset(&plan, 0, sizeof(_SPI_plan));
                                884                 :           2441 :     plan.magic = _SPI_PLAN_MAGIC;
 2003                           885                 :           2441 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 7015                           886                 :           2441 :     plan.cursor_options = cursorOptions;
 8136                           887                 :           2441 :     plan.nargs = nargs;
                                888                 :           2441 :     plan.argtypes = argtypes;
 6082                           889                 :           2441 :     plan.parserSetup = NULL;
                                890                 :           2441 :     plan.parserSetupArg = NULL;
                                891                 :                : 
 4925                           892                 :           2441 :     _SPI_prepare_plan(src, &plan);
                                893                 :                : 
                                894                 :                :     /* copy plan to procedure context */
 5401                           895                 :           2440 :     result = _SPI_make_plan_non_temp(&plan);
                                896                 :                : 
 6082                           897                 :           2440 :     _SPI_end_call(true);
                                898                 :                : 
                                899                 :           2440 :     return result;
                                900                 :                : }
                                901                 :                : 
                                902                 :                : SPIPlanPtr
 2003                           903                 :          17933 : SPI_prepare_extended(const char *src,
                                904                 :                :                      const SPIPrepareOptions *options)
                                905                 :                : {
                                906                 :                :     _SPI_plan   plan;
                                907                 :                :     SPIPlanPtr  result;
                                908                 :                : 
                                909   [ +  -  -  + ]:          17933 :     if (src == NULL || options == NULL)
                                910                 :                :     {
 2003 tgl@sss.pgh.pa.us         911                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
                                912                 :              0 :         return NULL;
                                913                 :                :     }
                                914                 :                : 
 2003 tgl@sss.pgh.pa.us         915                 :CBC       17933 :     SPI_result = _SPI_begin_call(true);
                                916         [ -  + ]:          17933 :     if (SPI_result < 0)
 2003 tgl@sss.pgh.pa.us         917                 :UBC           0 :         return NULL;
                                918                 :                : 
 2003 tgl@sss.pgh.pa.us         919                 :CBC       17933 :     memset(&plan, 0, sizeof(_SPI_plan));
                                920                 :          17933 :     plan.magic = _SPI_PLAN_MAGIC;
                                921                 :          17933 :     plan.parse_mode = options->parseMode;
                                922                 :          17933 :     plan.cursor_options = options->cursorOptions;
                                923                 :          17933 :     plan.nargs = 0;
                                924                 :          17933 :     plan.argtypes = NULL;
                                925                 :          17933 :     plan.parserSetup = options->parserSetup;
                                926                 :          17933 :     plan.parserSetupArg = options->parserSetupArg;
                                927                 :                : 
                                928                 :          17933 :     _SPI_prepare_plan(src, &plan);
                                929                 :                : 
                                930                 :                :     /* copy plan to procedure context */
                                931                 :          17872 :     result = _SPI_make_plan_non_temp(&plan);
                                932                 :                : 
                                933                 :          17872 :     _SPI_end_call(true);
                                934                 :                : 
                                935                 :          17872 :     return result;
                                936                 :                : }
                                937                 :                : 
                                938                 :                : SPIPlanPtr
 6082 tgl@sss.pgh.pa.us         939                 :UBC           0 : SPI_prepare_params(const char *src,
                                940                 :                :                    ParserSetupHook parserSetup,
                                941                 :                :                    void *parserSetupArg,
                                942                 :                :                    int cursorOptions)
                                943                 :                : {
                                944                 :                :     _SPI_plan   plan;
                                945                 :                :     SPIPlanPtr  result;
                                946                 :                : 
                                947         [ #  # ]:              0 :     if (src == NULL)
                                948                 :                :     {
                                949                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
                                950                 :              0 :         return NULL;
                                951                 :                :     }
                                952                 :                : 
                                953                 :              0 :     SPI_result = _SPI_begin_call(true);
                                954         [ #  # ]:              0 :     if (SPI_result < 0)
                                955                 :              0 :         return NULL;
                                956                 :                : 
                                957                 :              0 :     memset(&plan, 0, sizeof(_SPI_plan));
                                958                 :              0 :     plan.magic = _SPI_PLAN_MAGIC;
 2003                           959                 :              0 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 6082                           960                 :              0 :     plan.cursor_options = cursorOptions;
                                961                 :              0 :     plan.nargs = 0;
                                962                 :              0 :     plan.argtypes = NULL;
                                963                 :              0 :     plan.parserSetup = parserSetup;
                                964                 :              0 :     plan.parserSetupArg = parserSetupArg;
                                965                 :                : 
 4925                           966                 :              0 :     _SPI_prepare_plan(src, &plan);
                                967                 :                : 
                                968                 :                :     /* copy plan to procedure context */
 5401                           969                 :              0 :     result = _SPI_make_plan_non_temp(&plan);
                                970                 :                : 
10523 bruce@momjian.us          971                 :              0 :     _SPI_end_call(true);
                                972                 :                : 
 7047 tgl@sss.pgh.pa.us         973                 :              0 :     return result;
                                974                 :                : }
                                975                 :                : 
                                976                 :                : int
 5401 tgl@sss.pgh.pa.us         977                 :CBC       19245 : SPI_keepplan(SPIPlanPtr plan)
                                978                 :                : {
                                979                 :                :     ListCell   *lc;
                                980                 :                : 
 4925                           981   [ +  -  +  - ]:          19245 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
                                982   [ +  -  -  + ]:          19245 :         plan->saved || plan->oneshot)
 5401 tgl@sss.pgh.pa.us         983                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                                984                 :                : 
                                985                 :                :     /*
                                986                 :                :      * Mark it saved, reparent it under CacheMemoryContext, and mark all the
                                987                 :                :      * component CachedPlanSources as saved.  This sequence cannot fail
                                988                 :                :      * partway through, so there's no risk of long-term memory leakage.
                                989                 :                :      */
 5401 tgl@sss.pgh.pa.us         990                 :CBC       19245 :     plan->saved = true;
                                991                 :          19245 :     MemoryContextSetParent(plan->plancxt, CacheMemoryContext);
                                992                 :                : 
                                993   [ +  -  +  +  :          38490 :     foreach(lc, plan->plancache_list)
                                              +  + ]
                                994                 :                :     {
                                995                 :          19245 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                                996                 :                : 
                                997                 :          19245 :         SaveCachedPlan(plansource);
                                998                 :                :     }
                                999                 :                : 
                               1000                 :          19245 :     return 0;
                               1001                 :                : }
                               1002                 :                : 
                               1003                 :                : SPIPlanPtr
 7047 tgl@sss.pgh.pa.us        1004                 :UBC           0 : SPI_saveplan(SPIPlanPtr plan)
                               1005                 :                : {
                               1006                 :                :     SPIPlanPtr  newplan;
                               1007                 :                : 
 5401                          1008   [ #  #  #  # ]:              0 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
                               1009                 :                :     {
10523 bruce@momjian.us         1010                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
10164                          1011                 :              0 :         return NULL;
                               1012                 :                :     }
                               1013                 :                : 
 3296 tgl@sss.pgh.pa.us        1014                 :              0 :     SPI_result = _SPI_begin_call(false);    /* don't change context */
10523 bruce@momjian.us         1015         [ #  # ]:              0 :     if (SPI_result < 0)
10164                          1016                 :              0 :         return NULL;
                               1017                 :                : 
 7047 tgl@sss.pgh.pa.us        1018                 :              0 :     newplan = _SPI_save_plan(plan);
                               1019                 :                : 
 5401                          1020                 :              0 :     SPI_result = _SPI_end_call(false);
                               1021                 :                : 
 7047                          1022                 :              0 :     return newplan;
                               1023                 :                : }
                               1024                 :                : 
                               1025                 :                : int
 7047 tgl@sss.pgh.pa.us        1026                 :CBC        4419 : SPI_freeplan(SPIPlanPtr plan)
                               1027                 :                : {
                               1028                 :                :     ListCell   *lc;
                               1029                 :                : 
                               1030   [ +  -  -  + ]:           4419 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
 9171 JanWieck@Yahoo.com       1031                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                               1032                 :                : 
                               1033                 :                :     /* Release the plancache entries */
 5401 tgl@sss.pgh.pa.us        1034   [ +  -  +  +  :CBC        8838 :     foreach(lc, plan->plancache_list)
                                              +  + ]
                               1035                 :                :     {
                               1036                 :           4419 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               1037                 :                : 
                               1038                 :           4419 :         DropCachedPlan(plansource);
                               1039                 :                :     }
                               1040                 :                : 
                               1041                 :                :     /* Now get rid of the _SPI_plan and subsidiary data in its plancxt */
 7047                          1042                 :           4419 :     MemoryContextDelete(plan->plancxt);
                               1043                 :                : 
 9171 JanWieck@Yahoo.com       1044                 :           4419 :     return 0;
                               1045                 :                : }
                               1046                 :                : 
                               1047                 :                : HeapTuple
10518 vadim4o@yahoo.com        1048                 :           1376 : SPI_copytuple(HeapTuple tuple)
                               1049                 :                : {
                               1050                 :                :     MemoryContext oldcxt;
                               1051                 :                :     HeapTuple   ctuple;
                               1052                 :                : 
                               1053         [ -  + ]:           1376 :     if (tuple == NULL)
                               1054                 :                :     {
10518 vadim4o@yahoo.com        1055                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
10164 bruce@momjian.us         1056                 :              0 :         return NULL;
                               1057                 :                :     }
                               1058                 :                : 
 3521 tgl@sss.pgh.pa.us        1059         [ -  + ]:CBC        1376 :     if (_SPI_current == NULL)
                               1060                 :                :     {
 3521 tgl@sss.pgh.pa.us        1061                 :UBC           0 :         SPI_result = SPI_ERROR_UNCONNECTED;
                               1062                 :              0 :         return NULL;
                               1063                 :                :     }
                               1064                 :                : 
 3521 tgl@sss.pgh.pa.us        1065                 :CBC        1376 :     oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);
                               1066                 :                : 
10518 vadim4o@yahoo.com        1067                 :           1376 :     ctuple = heap_copytuple(tuple);
                               1068                 :                : 
 3521 tgl@sss.pgh.pa.us        1069                 :           1376 :     MemoryContextSwitchTo(oldcxt);
                               1070                 :                : 
10164 bruce@momjian.us         1071                 :           1376 :     return ctuple;
                               1072                 :                : }
                               1073                 :                : 
                               1074                 :                : HeapTupleHeader
 8125 tgl@sss.pgh.pa.us        1075                 :           3270 : SPI_returntuple(HeapTuple tuple, TupleDesc tupdesc)
                               1076                 :                : {
                               1077                 :                :     MemoryContext oldcxt;
                               1078                 :                :     HeapTupleHeader dtup;
                               1079                 :                : 
 9003                          1080   [ +  -  -  + ]:           3270 :     if (tuple == NULL || tupdesc == NULL)
                               1081                 :                :     {
 9003 tgl@sss.pgh.pa.us        1082                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
                               1083                 :              0 :         return NULL;
                               1084                 :                :     }
                               1085                 :                : 
 3521 tgl@sss.pgh.pa.us        1086         [ -  + ]:CBC        3270 :     if (_SPI_current == NULL)
                               1087                 :                :     {
 3521 tgl@sss.pgh.pa.us        1088                 :UBC           0 :         SPI_result = SPI_ERROR_UNCONNECTED;
                               1089                 :              0 :         return NULL;
                               1090                 :                :     }
                               1091                 :                : 
                               1092                 :                :     /* For RECORD results, make sure a typmod has been assigned */
 8125 tgl@sss.pgh.pa.us        1093         [ +  + ]:CBC        3270 :     if (tupdesc->tdtypeid == RECORDOID &&
                               1094         [ -  + ]:           3261 :         tupdesc->tdtypmod < 0)
 8125 tgl@sss.pgh.pa.us        1095                 :UBC           0 :         assign_record_type_typmod(tupdesc);
                               1096                 :                : 
 3521 tgl@sss.pgh.pa.us        1097                 :CBC        3270 :     oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);
                               1098                 :                : 
 4443                          1099                 :           3270 :     dtup = DatumGetHeapTupleHeader(heap_copy_tuple_as_datum(tuple, tupdesc));
                               1100                 :                : 
 3521                          1101                 :           3270 :     MemoryContextSwitchTo(oldcxt);
                               1102                 :                : 
 8125                          1103                 :           3270 :     return dtup;
                               1104                 :                : }
                               1105                 :                : 
                               1106                 :                : HeapTuple
10518 vadim4o@yahoo.com        1107                 :UBC           0 : SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum,
                               1108                 :                :                 Datum *Values, const char *Nulls)
                               1109                 :                : {
                               1110                 :                :     MemoryContext oldcxt;
                               1111                 :                :     HeapTuple   mtuple;
                               1112                 :                :     int         numberOfAttributes;
                               1113                 :                :     Datum      *v;
                               1114                 :                :     bool       *n;
                               1115                 :                :     int         i;
                               1116                 :                : 
 8323 tgl@sss.pgh.pa.us        1117   [ #  #  #  #  :              0 :     if (rel == NULL || tuple == NULL || natts < 0 || attnum == NULL || Values == NULL)
                                     #  #  #  #  #  
                                                 # ]
                               1118                 :                :     {
10518 vadim4o@yahoo.com        1119                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
10164 bruce@momjian.us         1120                 :              0 :         return NULL;
                               1121                 :                :     }
                               1122                 :                : 
 3521 tgl@sss.pgh.pa.us        1123         [ #  # ]:              0 :     if (_SPI_current == NULL)
                               1124                 :                :     {
                               1125                 :              0 :         SPI_result = SPI_ERROR_UNCONNECTED;
                               1126                 :              0 :         return NULL;
                               1127                 :                :     }
                               1128                 :                : 
                               1129                 :              0 :     oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);
                               1130                 :                : 
10518 vadim4o@yahoo.com        1131                 :              0 :     SPI_result = 0;
                               1132                 :                : 
                               1133                 :              0 :     numberOfAttributes = rel->rd_att->natts;
  202 michael@paquier.xyz      1134                 :UNC           0 :     v = palloc_array(Datum, numberOfAttributes);
                               1135                 :              0 :     n = palloc_array(bool, numberOfAttributes);
                               1136                 :                : 
                               1137                 :                :     /* fetch old values and nulls */
 6449 tgl@sss.pgh.pa.us        1138                 :UBC           0 :     heap_deform_tuple(tuple, rel->rd_att, v, n);
                               1139                 :                : 
                               1140                 :                :     /* replace values and nulls */
10518 vadim4o@yahoo.com        1141         [ #  # ]:              0 :     for (i = 0; i < natts; i++)
                               1142                 :                :     {
                               1143   [ #  #  #  # ]:              0 :         if (attnum[i] <= 0 || attnum[i] > numberOfAttributes)
                               1144                 :                :             break;
                               1145                 :              0 :         v[attnum[i] - 1] = Values[i];
 1756 michael@paquier.xyz      1146   [ #  #  #  # ]:              0 :         n[attnum[i] - 1] = (Nulls && Nulls[i] == 'n');
                               1147                 :                :     }
                               1148                 :                : 
10164 bruce@momjian.us         1149         [ #  # ]:              0 :     if (i == natts)             /* no errors in *attnum */
                               1150                 :                :     {
 6449 tgl@sss.pgh.pa.us        1151                 :              0 :         mtuple = heap_form_tuple(rel->rd_att, v, n);
                               1152                 :                : 
                               1153                 :                :         /*
                               1154                 :                :          * copy the identification info of the old tuple: t_ctid, t_self, and
                               1155                 :                :          * OID (if any)
                               1156                 :                :          */
 8702                          1157                 :              0 :         mtuple->t_data->t_ctid = tuple->t_data->t_ctid;
                               1158                 :              0 :         mtuple->t_self = tuple->t_self;
                               1159                 :              0 :         mtuple->t_tableOid = tuple->t_tableOid;
                               1160                 :                :     }
                               1161                 :                :     else
                               1162                 :                :     {
10518 vadim4o@yahoo.com        1163                 :              0 :         mtuple = NULL;
                               1164                 :              0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
                               1165                 :                :     }
                               1166                 :                : 
                               1167                 :              0 :     pfree(v);
                               1168                 :              0 :     pfree(n);
                               1169                 :                : 
 3521 tgl@sss.pgh.pa.us        1170                 :              0 :     MemoryContextSwitchTo(oldcxt);
                               1171                 :                : 
10164 bruce@momjian.us         1172                 :              0 :     return mtuple;
                               1173                 :                : }
                               1174                 :                : 
                               1175                 :                : int
 8583 tgl@sss.pgh.pa.us        1176                 :CBC       12998 : SPI_fnumber(TupleDesc tupdesc, const char *fname)
                               1177                 :                : {
                               1178                 :                :     int         res;
                               1179                 :                :     const FormData_pg_attribute *sysatt;
                               1180                 :                : 
10523 bruce@momjian.us         1181         [ +  + ]:          70223 :     for (res = 0; res < tupdesc->natts; res++)
                               1182                 :                :     {
 3236 andres@anarazel.de       1183                 :          70216 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, res);
                               1184                 :                : 
                               1185         [ +  + ]:          70216 :         if (namestrcmp(&attr->attname, fname) == 0 &&
                               1186         [ +  - ]:          12991 :             !attr->attisdropped)
10164 bruce@momjian.us         1187                 :          12991 :             return res + 1;
                               1188                 :                :     }
                               1189                 :                : 
 2779 andres@anarazel.de       1190                 :              7 :     sysatt = SystemAttributeByName(fname);
 9016 tgl@sss.pgh.pa.us        1191         [ -  + ]:              7 :     if (sysatt != NULL)
 9016 tgl@sss.pgh.pa.us        1192                 :UBC           0 :         return sysatt->attnum;
                               1193                 :                : 
                               1194                 :                :     /* SPI_ERROR_NOATTRIBUTE is different from all sys column numbers */
10164 bruce@momjian.us         1195                 :CBC           7 :     return SPI_ERROR_NOATTRIBUTE;
                               1196                 :                : }
                               1197                 :                : 
                               1198                 :                : char *
10519 vadim4o@yahoo.com        1199                 :            644 : SPI_fname(TupleDesc tupdesc, int fnumber)
                               1200                 :                : {
                               1201                 :                :     const FormData_pg_attribute *att;
                               1202                 :                : 
                               1203                 :            644 :     SPI_result = 0;
                               1204                 :                : 
 9016 tgl@sss.pgh.pa.us        1205   [ +  -  +  -  :            644 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              -  + ]
                               1206                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1207                 :                :     {
10519 vadim4o@yahoo.com        1208                 :UBC           0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
10164 bruce@momjian.us         1209                 :              0 :         return NULL;
                               1210                 :                :     }
                               1211                 :                : 
 9016 tgl@sss.pgh.pa.us        1212         [ +  - ]:CBC         644 :     if (fnumber > 0)
 3236 andres@anarazel.de       1213                 :            644 :         att = TupleDescAttr(tupdesc, fnumber - 1);
                               1214                 :                :     else
 2779 andres@anarazel.de       1215                 :UBC           0 :         att = SystemAttributeDefinition(fnumber);
                               1216                 :                : 
 9016 tgl@sss.pgh.pa.us        1217                 :CBC         644 :     return pstrdup(NameStr(att->attname));
                               1218                 :                : }
                               1219                 :                : 
                               1220                 :                : char *
10523 bruce@momjian.us         1221                 :           5565 : SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
                               1222                 :                : {
                               1223                 :                :     Datum       val;
                               1224                 :                :     bool        isnull;
                               1225                 :                :     Oid         typoid,
                               1226                 :                :                 foutoid;
                               1227                 :                :     bool        typisvarlena;
                               1228                 :                : 
                               1229                 :           5565 :     SPI_result = 0;
                               1230                 :                : 
 6466 tgl@sss.pgh.pa.us        1231   [ +  -  +  -  :           5565 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              -  + ]
                               1232                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1233                 :                :     {
10501 vadim4o@yahoo.com        1234                 :UBC           0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
10164 bruce@momjian.us         1235                 :              0 :         return NULL;
                               1236                 :                :     }
                               1237                 :                : 
 4622 tgl@sss.pgh.pa.us        1238                 :CBC        5565 :     val = heap_getattr(tuple, fnumber, tupdesc, &isnull);
10523 bruce@momjian.us         1239         [ +  + ]:           5565 :     if (isnull)
10164                          1240                 :             66 :         return NULL;
                               1241                 :                : 
 9016 tgl@sss.pgh.pa.us        1242         [ +  - ]:           5499 :     if (fnumber > 0)
 3236 andres@anarazel.de       1243                 :           5499 :         typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
                               1244                 :                :     else
 2779 andres@anarazel.de       1245                 :UBC           0 :         typoid = (SystemAttributeDefinition(fnumber))->atttypid;
                               1246                 :                : 
 7730 tgl@sss.pgh.pa.us        1247                 :CBC        5499 :     getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
                               1248                 :                : 
 4622                          1249                 :           5499 :     return OidOutputFunctionCall(foutoid, val);
                               1250                 :                : }
                               1251                 :                : 
                               1252                 :                : Datum
10351 bruce@momjian.us         1253                 :          33782 : SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
                               1254                 :                : {
10523                          1255                 :          33782 :     SPI_result = 0;
                               1256                 :                : 
 6466 tgl@sss.pgh.pa.us        1257   [ +  -  +  -  :          33782 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              -  + ]
                               1258                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1259                 :                :     {
10501 vadim4o@yahoo.com        1260                 :UBC           0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
 9016 tgl@sss.pgh.pa.us        1261                 :              0 :         *isnull = true;
  326 tgl@sss.pgh.pa.us        1262                 :UNC           0 :         return (Datum) 0;
                               1263                 :                :     }
                               1264                 :                : 
 9016 tgl@sss.pgh.pa.us        1265                 :CBC       33782 :     return heap_getattr(tuple, fnumber, tupdesc, isnull);
                               1266                 :                : }
                               1267                 :                : 
                               1268                 :                : char *
10523 bruce@momjian.us         1269                 :UBC           0 : SPI_gettype(TupleDesc tupdesc, int fnumber)
                               1270                 :                : {
                               1271                 :                :     Oid         typoid;
                               1272                 :                :     HeapTuple   typeTuple;
                               1273                 :                :     char       *result;
                               1274                 :                : 
                               1275                 :              0 :     SPI_result = 0;
                               1276                 :                : 
 9016 tgl@sss.pgh.pa.us        1277   [ #  #  #  #  :              0 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              #  # ]
                               1278                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1279                 :                :     {
10523 bruce@momjian.us         1280                 :              0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
10164                          1281                 :              0 :         return NULL;
                               1282                 :                :     }
                               1283                 :                : 
 9016 tgl@sss.pgh.pa.us        1284         [ #  # ]:              0 :     if (fnumber > 0)
 3236 andres@anarazel.de       1285                 :              0 :         typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
                               1286                 :                :     else
 2779                          1287                 :              0 :         typoid = (SystemAttributeDefinition(fnumber))->atttypid;
                               1288                 :                : 
 5980 rhaas@postgresql.org     1289                 :              0 :     typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typoid));
                               1290                 :                : 
10523 bruce@momjian.us         1291         [ #  # ]:              0 :     if (!HeapTupleIsValid(typeTuple))
                               1292                 :                :     {
                               1293                 :              0 :         SPI_result = SPI_ERROR_TYPUNKNOWN;
10164                          1294                 :              0 :         return NULL;
                               1295                 :                :     }
                               1296                 :                : 
 9357 tgl@sss.pgh.pa.us        1297                 :              0 :     result = pstrdup(NameStr(((Form_pg_type) GETSTRUCT(typeTuple))->typname));
                               1298                 :              0 :     ReleaseSysCache(typeTuple);
                               1299                 :              0 :     return result;
                               1300                 :                : }
                               1301                 :                : 
                               1302                 :                : /*
                               1303                 :                :  * Get the data type OID for a column.
                               1304                 :                :  *
                               1305                 :                :  * There's nothing similar for typmod and typcollation.  The rare consumers
                               1306                 :                :  * thereof should inspect the TupleDesc directly.
                               1307                 :                :  */
                               1308                 :                : Oid
10523 bruce@momjian.us         1309                 :CBC         822 : SPI_gettypeid(TupleDesc tupdesc, int fnumber)
                               1310                 :                : {
                               1311                 :            822 :     SPI_result = 0;
                               1312                 :                : 
 9016 tgl@sss.pgh.pa.us        1313   [ +  -  +  -  :            822 :     if (fnumber > tupdesc->natts || fnumber == 0 ||
                                              -  + ]
                               1314                 :                :         fnumber <= FirstLowInvalidHeapAttributeNumber)
                               1315                 :                :     {
10523 bruce@momjian.us         1316                 :UBC           0 :         SPI_result = SPI_ERROR_NOATTRIBUTE;
10164                          1317                 :              0 :         return InvalidOid;
                               1318                 :                :     }
                               1319                 :                : 
 9016 tgl@sss.pgh.pa.us        1320         [ +  - ]:CBC         822 :     if (fnumber > 0)
 3236 andres@anarazel.de       1321                 :            822 :         return TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
                               1322                 :                :     else
 2779 andres@anarazel.de       1323                 :UBC           0 :         return (SystemAttributeDefinition(fnumber))->atttypid;
                               1324                 :                : }
                               1325                 :                : 
                               1326                 :                : char *
10523 bruce@momjian.us         1327                 :CBC         183 : SPI_getrelname(Relation rel)
                               1328                 :                : {
 9732                          1329                 :            183 :     return pstrdup(RelationGetRelationName(rel));
                               1330                 :                : }
                               1331                 :                : 
                               1332                 :                : char *
 7763 neilc@samurai.com        1333                 :            141 : SPI_getnspname(Relation rel)
                               1334                 :                : {
 7563 bruce@momjian.us         1335                 :            141 :     return get_namespace_name(RelationGetNamespace(rel));
                               1336                 :                : }
                               1337                 :                : 
                               1338                 :                : void *
10351                          1339                 :             24 : SPI_palloc(Size size)
                               1340                 :                : {
 3521 tgl@sss.pgh.pa.us        1341         [ -  + ]:             24 :     if (_SPI_current == NULL)
 3521 tgl@sss.pgh.pa.us        1342         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_palloc called while not connected to SPI");
                               1343                 :                : 
 3521 tgl@sss.pgh.pa.us        1344                 :CBC          24 :     return MemoryContextAlloc(_SPI_current->savedcxt, size);
                               1345                 :                : }
                               1346                 :                : 
                               1347                 :                : void *
10351 bruce@momjian.us         1348                 :UBC           0 : SPI_repalloc(void *pointer, Size size)
                               1349                 :                : {
                               1350                 :                :     /* No longer need to worry which context chunk was in... */
 9498 tgl@sss.pgh.pa.us        1351                 :              0 :     return repalloc(pointer, size);
                               1352                 :                : }
                               1353                 :                : 
                               1354                 :                : void
10351 bruce@momjian.us         1355                 :              0 : SPI_pfree(void *pointer)
                               1356                 :                : {
                               1357                 :                :     /* No longer need to worry which context chunk was in... */
                               1358                 :              0 :     pfree(pointer);
10506 vadim4o@yahoo.com        1359                 :              0 : }
                               1360                 :                : 
                               1361                 :                : Datum
 4065 tgl@sss.pgh.pa.us        1362                 :CBC        3706 : SPI_datumTransfer(Datum value, bool typByVal, int typLen)
                               1363                 :                : {
                               1364                 :                :     MemoryContext oldcxt;
                               1365                 :                :     Datum       result;
                               1366                 :                : 
 3521                          1367         [ -  + ]:           3706 :     if (_SPI_current == NULL)
 3521 tgl@sss.pgh.pa.us        1368         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_datumTransfer called while not connected to SPI");
                               1369                 :                : 
 3521 tgl@sss.pgh.pa.us        1370                 :CBC        3706 :     oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);
                               1371                 :                : 
 4065                          1372                 :           3706 :     result = datumTransfer(value, typByVal, typLen);
                               1373                 :                : 
 3521                          1374                 :           3706 :     MemoryContextSwitchTo(oldcxt);
                               1375                 :                : 
 4065                          1376                 :           3706 :     return result;
                               1377                 :                : }
                               1378                 :                : 
                               1379                 :                : void
 9693 JanWieck@Yahoo.com       1380                 :UBC           0 : SPI_freetuple(HeapTuple tuple)
                               1381                 :                : {
                               1382                 :                :     /* No longer need to worry which context tuple was in... */
                               1383                 :              0 :     heap_freetuple(tuple);
                               1384                 :              0 : }
                               1385                 :                : 
                               1386                 :                : void
 9171 JanWieck@Yahoo.com       1387                 :CBC      120924 : SPI_freetuptable(SPITupleTable *tuptable)
                               1388                 :                : {
 4723 tgl@sss.pgh.pa.us        1389                 :         120924 :     bool        found = false;
                               1390                 :                : 
                               1391                 :                :     /* ignore call if NULL pointer */
                               1392         [ +  + ]:         120924 :     if (tuptable == NULL)
                               1393                 :          69196 :         return;
                               1394                 :                : 
                               1395                 :                :     /*
                               1396                 :                :      * Search only the topmost SPI context for a matching tuple table.
                               1397                 :                :      */
 3521                          1398         [ +  - ]:          51728 :     if (_SPI_current != NULL)
                               1399                 :                :     {
                               1400                 :                :         slist_mutable_iter siter;
                               1401                 :                : 
                               1402                 :                :         /* find tuptable in active list, then remove it */
 4723                          1403   [ +  -  +  -  :          51730 :         slist_foreach_modify(siter, &_SPI_current->tuptables)
                                              +  - ]
                               1404                 :                :         {
                               1405                 :                :             SPITupleTable *tt;
                               1406                 :                : 
                               1407                 :          51730 :             tt = slist_container(SPITupleTable, next, siter.cur);
                               1408         [ +  + ]:          51730 :             if (tt == tuptable)
                               1409                 :                :             {
                               1410                 :          51728 :                 slist_delete_current(&siter);
                               1411                 :          51728 :                 found = true;
                               1412                 :          51728 :                 break;
                               1413                 :                :             }
                               1414                 :                :         }
                               1415                 :                :     }
                               1416                 :                : 
                               1417                 :                :     /*
                               1418                 :                :      * Refuse the deletion if we didn't find it in the topmost SPI context.
                               1419                 :                :      * This is primarily a guard against double deletion, but might prevent
                               1420                 :                :      * other errors as well.  Since the worst consequence of not deleting a
                               1421                 :                :      * tuptable would be a transient memory leak, this is just a WARNING.
                               1422                 :                :      */
                               1423         [ -  + ]:          51728 :     if (!found)
                               1424                 :                :     {
 4723 tgl@sss.pgh.pa.us        1425         [ #  # ]:UBC           0 :         elog(WARNING, "attempt to delete invalid SPITupleTable %p", tuptable);
                               1426                 :              0 :         return;
                               1427                 :                :     }
                               1428                 :                : 
                               1429                 :                :     /* for safety, reset global variables that might point at tuptable */
 4723 tgl@sss.pgh.pa.us        1430         [ -  + ]:CBC       51728 :     if (tuptable == _SPI_current->tuptable)
 4723 tgl@sss.pgh.pa.us        1431                 :UBC           0 :         _SPI_current->tuptable = NULL;
 4723 tgl@sss.pgh.pa.us        1432         [ +  + ]:CBC       51728 :     if (tuptable == SPI_tuptable)
                               1433                 :          46459 :         SPI_tuptable = NULL;
                               1434                 :                : 
                               1435                 :                :     /* release all memory belonging to tuptable */
                               1436                 :          51728 :     MemoryContextDelete(tuptable->tuptabcxt);
                               1437                 :                : }
                               1438                 :                : 
                               1439                 :                : 
                               1440                 :                : /*
                               1441                 :                :  * SPI_cursor_open()
                               1442                 :                :  *
                               1443                 :                :  *  Open a prepared SPI plan as a portal
                               1444                 :                :  */
                               1445                 :                : Portal
 7047                          1446                 :            133 : SPI_cursor_open(const char *name, SPIPlanPtr plan,
                               1447                 :                :                 const Datum *Values, const char *Nulls,
                               1448                 :                :                 bool read_only)
                               1449                 :                : {
                               1450                 :                :     Portal      portal;
                               1451                 :                :     ParamListInfo paramLI;
                               1452                 :                : 
                               1453                 :                :     /* build transient ParamListInfo in caller's context */
 6082                          1454                 :            133 :     paramLI = _SPI_convert_params(plan->nargs, plan->argtypes,
                               1455                 :                :                                   Values, Nulls);
                               1456                 :                : 
                               1457                 :            133 :     portal = SPI_cursor_open_internal(name, plan, paramLI, read_only);
                               1458                 :                : 
                               1459                 :                :     /* done with the transient ParamListInfo */
                               1460         [ +  + ]:            133 :     if (paramLI)
                               1461                 :              4 :         pfree(paramLI);
                               1462                 :                : 
                               1463                 :            133 :     return portal;
                               1464                 :                : }
                               1465                 :                : 
                               1466                 :                : 
                               1467                 :                : /*
                               1468                 :                :  * SPI_cursor_open_with_args()
                               1469                 :                :  *
                               1470                 :                :  * Parse and plan a query and open it as a portal.
                               1471                 :                :  */
                               1472                 :                : Portal
 6603 tgl@sss.pgh.pa.us        1473                 :UBC           0 : SPI_cursor_open_with_args(const char *name,
                               1474                 :                :                           const char *src,
                               1475                 :                :                           int nargs, Oid *argtypes,
                               1476                 :                :                           Datum *Values, const char *Nulls,
                               1477                 :                :                           bool read_only, int cursorOptions)
                               1478                 :                : {
                               1479                 :                :     Portal      result;
                               1480                 :                :     _SPI_plan   plan;
                               1481                 :                :     ParamListInfo paramLI;
                               1482                 :                : 
                               1483   [ #  #  #  # ]:              0 :     if (src == NULL || nargs < 0)
                               1484         [ #  # ]:              0 :         elog(ERROR, "SPI_cursor_open_with_args called with invalid arguments");
                               1485                 :                : 
                               1486   [ #  #  #  #  :              0 :     if (nargs > 0 && (argtypes == NULL || Values == NULL))
                                              #  # ]
                               1487         [ #  # ]:              0 :         elog(ERROR, "SPI_cursor_open_with_args called with missing parameters");
                               1488                 :                : 
                               1489                 :              0 :     SPI_result = _SPI_begin_call(true);
                               1490         [ #  # ]:              0 :     if (SPI_result < 0)
                               1491         [ #  # ]:              0 :         elog(ERROR, "SPI_cursor_open_with_args called while not connected");
                               1492                 :                : 
                               1493                 :              0 :     memset(&plan, 0, sizeof(_SPI_plan));
                               1494                 :              0 :     plan.magic = _SPI_PLAN_MAGIC;
 2003                          1495                 :              0 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 6603                          1496                 :              0 :     plan.cursor_options = cursorOptions;
                               1497                 :              0 :     plan.nargs = nargs;
                               1498                 :              0 :     plan.argtypes = argtypes;
 6082                          1499                 :              0 :     plan.parserSetup = NULL;
                               1500                 :              0 :     plan.parserSetupArg = NULL;
                               1501                 :                : 
                               1502                 :                :     /* build transient ParamListInfo in executor context */
 6603                          1503                 :              0 :     paramLI = _SPI_convert_params(nargs, argtypes,
                               1504                 :                :                                   Values, Nulls);
                               1505                 :                : 
 4925                          1506                 :              0 :     _SPI_prepare_plan(src, &plan);
                               1507                 :                : 
                               1508                 :                :     /* We needn't copy the plan; SPI_cursor_open_internal will do so */
                               1509                 :                : 
 6082                          1510                 :              0 :     result = SPI_cursor_open_internal(name, &plan, paramLI, read_only);
                               1511                 :                : 
                               1512                 :                :     /* And clean up */
 6603                          1513                 :              0 :     _SPI_end_call(true);
                               1514                 :                : 
                               1515                 :              0 :     return result;
                               1516                 :                : }
                               1517                 :                : 
                               1518                 :                : 
                               1519                 :                : /*
                               1520                 :                :  * SPI_cursor_open_with_paramlist()
                               1521                 :                :  *
                               1522                 :                :  *  Same as SPI_cursor_open except that parameters (if any) are passed
                               1523                 :                :  *  as a ParamListInfo, which supports dynamic parameter set determination
                               1524                 :                :  */
                               1525                 :                : Portal
 6082 tgl@sss.pgh.pa.us        1526                 :CBC        1780 : SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan,
                               1527                 :                :                                ParamListInfo params, bool read_only)
                               1528                 :                : {
                               1529                 :           1780 :     return SPI_cursor_open_internal(name, plan, params, read_only);
                               1530                 :                : }
                               1531                 :                : 
                               1532                 :                : /* Parse a query and open it as a cursor */
                               1533                 :                : Portal
 1981                          1534                 :           6401 : SPI_cursor_parse_open(const char *name,
                               1535                 :                :                       const char *src,
                               1536                 :                :                       const SPIParseOpenOptions *options)
                               1537                 :                : {
                               1538                 :                :     Portal      result;
                               1539                 :                :     _SPI_plan   plan;
                               1540                 :                : 
                               1541   [ +  -  -  + ]:           6401 :     if (src == NULL || options == NULL)
 1981 tgl@sss.pgh.pa.us        1542         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_cursor_parse_open called with invalid arguments");
                               1543                 :                : 
 2209 tgl@sss.pgh.pa.us        1544                 :CBC        6401 :     SPI_result = _SPI_begin_call(true);
                               1545         [ -  + ]:           6401 :     if (SPI_result < 0)
 1981 tgl@sss.pgh.pa.us        1546         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_cursor_parse_open called while not connected");
                               1547                 :                : 
 2209 tgl@sss.pgh.pa.us        1548                 :CBC        6401 :     memset(&plan, 0, sizeof(_SPI_plan));
                               1549                 :           6401 :     plan.magic = _SPI_PLAN_MAGIC;
 2003                          1550                 :           6401 :     plan.parse_mode = RAW_PARSE_DEFAULT;
 1981                          1551                 :           6401 :     plan.cursor_options = options->cursorOptions;
                               1552         [ +  + ]:           6401 :     if (options->params)
                               1553                 :                :     {
                               1554                 :              8 :         plan.parserSetup = options->params->parserSetup;
                               1555                 :              8 :         plan.parserSetupArg = options->params->parserSetupArg;
                               1556                 :                :     }
                               1557                 :                : 
 2209                          1558                 :           6401 :     _SPI_prepare_plan(src, &plan);
                               1559                 :                : 
                               1560                 :                :     /* We needn't copy the plan; SPI_cursor_open_internal will do so */
                               1561                 :                : 
 1981                          1562                 :           6401 :     result = SPI_cursor_open_internal(name, &plan,
                               1563                 :           6401 :                                       options->params, options->read_only);
                               1564                 :                : 
                               1565                 :                :     /* And clean up */
 2209                          1566                 :           6401 :     _SPI_end_call(true);
                               1567                 :                : 
                               1568                 :           6401 :     return result;
                               1569                 :                : }
                               1570                 :                : 
                               1571                 :                : 
                               1572                 :                : /*
                               1573                 :                :  * SPI_cursor_open_internal()
                               1574                 :                :  *
                               1575                 :                :  *  Common code for SPI_cursor_open variants
                               1576                 :                :  */
                               1577                 :                : static Portal
 6603                          1578                 :           8314 : SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
                               1579                 :                :                          ParamListInfo paramLI, bool read_only)
                               1580                 :                : {
                               1581                 :                :     CachedPlanSource *plansource;
                               1582                 :                :     CachedPlan *cplan;
                               1583                 :                :     List       *stmt_list;
                               1584                 :                :     char       *query_string;
                               1585                 :                :     Snapshot    snapshot;
                               1586                 :                :     MemoryContext oldcontext;
                               1587                 :                :     Portal      portal;
                               1588                 :                :     SPICallbackArg spicallbackarg;
                               1589                 :                :     ErrorContextCallback spierrcontext;
                               1590                 :                : 
                               1591                 :                :     /*
                               1592                 :                :      * Check that the plan is something the Portal code will special-case as
                               1593                 :                :      * returning one tupleset.
                               1594                 :                :      */
 7047                          1595         [ -  + ]:           8314 :     if (!SPI_is_cursor_plan(plan))
                               1596                 :                :     {
                               1597                 :                :         /* try to give a good error message */
                               1598                 :                :         const char *cmdtag;
                               1599                 :                : 
 7047 tgl@sss.pgh.pa.us        1600         [ #  # ]:UBC           0 :         if (list_length(plan->plancache_list) != 1)
 7260                          1601         [ #  # ]:              0 :             ereport(ERROR,
                               1602                 :                :                     (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
                               1603                 :                :                      errmsg("cannot open multi-query plan as cursor")));
 7047                          1604                 :              0 :         plansource = (CachedPlanSource *) linitial(plan->plancache_list);
                               1605                 :                :         /* A SELECT that fails SPI_is_cursor_plan() must be SELECT INTO */
 1774                          1606         [ #  # ]:              0 :         if (plansource->commandTag == CMDTAG_SELECT)
                               1607                 :              0 :             cmdtag = "SELECT INTO";
                               1608                 :                :         else
                               1609                 :              0 :             cmdtag = GetCommandTagName(plansource->commandTag);
 7262                          1610         [ #  # ]:              0 :         ereport(ERROR,
                               1611                 :                :                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
                               1612                 :                :         /* translator: %s is name of a SQL command, eg INSERT */
                               1613                 :                :                  errmsg("cannot open %s query as cursor", cmdtag)));
                               1614                 :                :     }
                               1615                 :                : 
 7047 tgl@sss.pgh.pa.us        1616         [ -  + ]:CBC        8314 :     Assert(list_length(plan->plancache_list) == 1);
                               1617                 :           8314 :     plansource = (CachedPlanSource *) linitial(plan->plancache_list);
                               1618                 :                : 
                               1619                 :                :     /* Push the SPI stack */
 5401                          1620         [ -  + ]:           8314 :     if (_SPI_begin_call(true) < 0)
 6713 tgl@sss.pgh.pa.us        1621         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_cursor_open called while not connected");
                               1622                 :                : 
                               1623                 :                :     /* Reset SPI result (note we deliberately don't touch lastoid) */
 9171 JanWieck@Yahoo.com       1624                 :CBC        8314 :     SPI_processed = 0;
                               1625                 :           8314 :     SPI_tuptable = NULL;
                               1626                 :           8314 :     _SPI_current->processed = 0;
                               1627                 :           8314 :     _SPI_current->tuptable = NULL;
                               1628                 :                : 
                               1629                 :                :     /* Create the portal */
 8460 tgl@sss.pgh.pa.us        1630   [ +  +  -  + ]:           8314 :     if (name == NULL || name[0] == '\0')
                               1631                 :                :     {
                               1632                 :                :         /* Use a random nonconflicting name */
                               1633                 :           8282 :         portal = CreateNewPortal();
                               1634                 :                :     }
                               1635                 :                :     else
                               1636                 :                :     {
                               1637                 :                :         /* In this path, error if portal of same name already exists */
                               1638                 :             32 :         portal = CreatePortal(name, false, false);
                               1639                 :                :     }
                               1640                 :                : 
                               1641                 :                :     /* Copy the plan's query string into the portal */
 3118 peter_e@gmx.net          1642                 :           8314 :     query_string = MemoryContextStrdup(portal->portalContext,
                               1643                 :                :                                        plansource->query_string);
                               1644                 :                : 
                               1645                 :                :     /*
                               1646                 :                :      * Setup error traceback support for ereport(), in case GetCachedPlan
                               1647                 :                :      * throws an error.
                               1648                 :                :      */
 2003 tgl@sss.pgh.pa.us        1649                 :           8314 :     spicallbackarg.query = plansource->query_string;
                               1650                 :           8314 :     spicallbackarg.mode = plan->parse_mode;
 4899                          1651                 :           8314 :     spierrcontext.callback = _SPI_error_callback;
 2003                          1652                 :           8314 :     spierrcontext.arg = &spicallbackarg;
 4899                          1653                 :           8314 :     spierrcontext.previous = error_context_stack;
                               1654                 :           8314 :     error_context_stack = &spierrcontext;
                               1655                 :                : 
                               1656                 :                :     /*
                               1657                 :                :      * Note: for a saved plan, we mustn't have any failure occur between
                               1658                 :                :      * GetCachedPlan and PortalDefineQuery; that would result in leaking our
                               1659                 :                :      * plancache refcount.
                               1660                 :                :      */
                               1661                 :                : 
                               1662                 :                :     /* Replan if needed, and increment plan refcount for portal */
 1982                          1663                 :           8314 :     cplan = GetCachedPlan(plansource, paramLI, NULL, _SPI_current->queryEnv);
 5401                          1664                 :           8314 :     stmt_list = cplan->stmt_list;
                               1665                 :                : 
                               1666         [ +  + ]:           8314 :     if (!plan->saved)
                               1667                 :                :     {
                               1668                 :                :         /*
                               1669                 :                :          * We don't want the portal to depend on an unsaved CachedPlanSource,
                               1670                 :                :          * so must copy the plan into the portal's context.  An error here
                               1671                 :                :          * will result in leaking our refcount on the plan, but it doesn't
                               1672                 :                :          * matter because the plan is unsaved and hence transient anyway.
                               1673                 :                :          */
 3118 peter_e@gmx.net          1674                 :           6528 :         oldcontext = MemoryContextSwitchTo(portal->portalContext);
 5401 tgl@sss.pgh.pa.us        1675                 :           6528 :         stmt_list = copyObject(stmt_list);
 7047                          1676                 :           6528 :         MemoryContextSwitchTo(oldcontext);
 1982                          1677                 :           6528 :         ReleaseCachedPlan(cplan, NULL);
 7047                          1678                 :           6528 :         cplan = NULL;           /* portal shouldn't depend on cplan */
                               1679                 :                :     }
                               1680                 :                : 
                               1681                 :                :     /*
                               1682                 :                :      * Set up the portal.
                               1683                 :                :      */
 8460                          1684                 :           8314 :     PortalDefineQuery(portal,
                               1685                 :                :                       NULL,     /* no statement name */
                               1686                 :                :                       query_string,
                               1687                 :                :                       plansource->commandTag,
                               1688                 :                :                       stmt_list,
                               1689                 :                :                       cplan);
                               1690                 :                : 
                               1691                 :                :     /*
                               1692                 :                :      * Set up options for portal.  Default SCROLL type is chosen the same way
                               1693                 :                :      * as PerformCursorOpen does it.
                               1694                 :                :      */
 7015                          1695                 :           8314 :     portal->cursorOptions = plan->cursor_options;
                               1696         [ +  + ]:           8314 :     if (!(portal->cursorOptions & (CURSOR_OPT_SCROLL | CURSOR_OPT_NO_SCROLL)))
                               1697                 :                :     {
                               1698         [ +  - ]:            270 :         if (list_length(stmt_list) == 1 &&
 3296                          1699         [ +  - ]:            270 :             linitial_node(PlannedStmt, stmt_list)->commandType != CMD_UTILITY &&
 3368                          1700   [ +  +  +  + ]:            539 :             linitial_node(PlannedStmt, stmt_list)->rowMarks == NIL &&
                               1701                 :            269 :             ExecSupportsBackwardScan(linitial_node(PlannedStmt, stmt_list)->planTree))
 7015                          1702                 :            252 :             portal->cursorOptions |= CURSOR_OPT_SCROLL;
                               1703                 :                :         else
                               1704                 :             18 :             portal->cursorOptions |= CURSOR_OPT_NO_SCROLL;
                               1705                 :                :     }
                               1706                 :                : 
                               1707                 :                :     /*
                               1708                 :                :      * Disallow SCROLL with SELECT FOR UPDATE.  This is not redundant with the
                               1709                 :                :      * check in transformDeclareCursorStmt because the cursor options might
                               1710                 :                :      * not have come through there.
                               1711                 :                :      */
 6824                          1712         [ +  + ]:           8314 :     if (portal->cursorOptions & CURSOR_OPT_SCROLL)
                               1713                 :                :     {
                               1714         [ +  - ]:            269 :         if (list_length(stmt_list) == 1 &&
 3296                          1715         [ +  - ]:            269 :             linitial_node(PlannedStmt, stmt_list)->commandType != CMD_UTILITY &&
 3368                          1716         [ -  + ]:            269 :             linitial_node(PlannedStmt, stmt_list)->rowMarks != NIL)
 6824 tgl@sss.pgh.pa.us        1717         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1718                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1719                 :                :                      errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
                               1720                 :                :                      errdetail("Scrollable cursors must be READ ONLY.")));
                               1721                 :                :     }
                               1722                 :                : 
                               1723                 :                :     /* Make current query environment available to portal at execution time. */
 3374 kgrittn@postgresql.o     1724                 :CBC        8314 :     portal->queryEnv = _SPI_current->queryEnv;
                               1725                 :                : 
                               1726                 :                :     /*
                               1727                 :                :      * If told to be read-only, we'd better check for read-only queries. This
                               1728                 :                :      * can't be done earlier because we need to look at the finished, planned
                               1729                 :                :      * queries.  (In particular, we don't want to do it between GetCachedPlan
                               1730                 :                :      * and PortalDefineQuery, because throwing an error between those steps
                               1731                 :                :      * would result in leaking our plancache refcount.)
                               1732                 :                :      */
 2357 rhaas@postgresql.org     1733         [ +  + ]:           8314 :     if (read_only)
                               1734                 :                :     {
                               1735                 :                :         ListCell   *lc;
                               1736                 :                : 
 7045 tgl@sss.pgh.pa.us        1737   [ +  -  +  +  :            208 :         foreach(lc, stmt_list)
                                              +  + ]
                               1738                 :                :         {
 3368                          1739                 :            104 :             PlannedStmt *pstmt = lfirst_node(PlannedStmt, lc);
                               1740                 :                : 
 7045                          1741         [ -  + ]:            104 :             if (!CommandIsReadOnly(pstmt))
 2357 rhaas@postgresql.org     1742         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               1743                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1744                 :                :                 /* translator: %s is a SQL statement name */
                               1745                 :                :                          errmsg("%s is not allowed in a non-volatile function",
                               1746                 :                :                                 CreateCommandName((Node *) pstmt))));
                               1747                 :                :         }
                               1748                 :                :     }
                               1749                 :                : 
                               1750                 :                :     /* Set up the snapshot to use. */
 4964 tgl@sss.pgh.pa.us        1751         [ +  + ]:CBC        8314 :     if (read_only)
                               1752                 :            104 :         snapshot = GetActiveSnapshot();
                               1753                 :                :     else
                               1754                 :                :     {
                               1755                 :           8210 :         CommandCounterIncrement();
                               1756                 :           8210 :         snapshot = GetTransactionSnapshot();
                               1757                 :                :     }
                               1758                 :                : 
                               1759                 :                :     /*
                               1760                 :                :      * If the plan has parameters, copy them into the portal.  Note that this
                               1761                 :                :      * must be done after revalidating the plan, because in dynamic parameter
                               1762                 :                :      * cases the set of parameters could have changed during re-parsing.
                               1763                 :                :      */
 6082                          1764         [ +  + ]:           8314 :     if (paramLI)
                               1765                 :                :     {
 3118 peter_e@gmx.net          1766                 :            447 :         oldcontext = MemoryContextSwitchTo(portal->portalContext);
 6082 tgl@sss.pgh.pa.us        1767                 :            447 :         paramLI = copyParamList(paramLI);
                               1768                 :            447 :         MemoryContextSwitchTo(oldcontext);
                               1769                 :                :     }
                               1770                 :                : 
                               1771                 :                :     /*
                               1772                 :                :      * Start portal execution.
                               1773                 :                :      */
 4964                          1774                 :           8314 :     PortalStart(portal, paramLI, 0, snapshot);
                               1775                 :                : 
 7260                          1776         [ -  + ]:           8314 :     Assert(portal->strategy != PORTAL_MULTI_QUERY);
                               1777                 :                : 
                               1778                 :                :     /* Pop the error context stack */
 3360                          1779                 :           8314 :     error_context_stack = spierrcontext.previous;
                               1780                 :                : 
                               1781                 :                :     /* Pop the SPI stack */
 5401                          1782                 :           8314 :     _SPI_end_call(true);
                               1783                 :                : 
                               1784                 :                :     /* Return the created portal */
 9171 JanWieck@Yahoo.com       1785                 :           8314 :     return portal;
                               1786                 :                : }
                               1787                 :                : 
                               1788                 :                : 
                               1789                 :                : /*
                               1790                 :                :  * SPI_cursor_find()
                               1791                 :                :  *
                               1792                 :                :  *  Find the portal of an existing open cursor
                               1793                 :                :  */
                               1794                 :                : Portal
 8583 tgl@sss.pgh.pa.us        1795                 :            362 : SPI_cursor_find(const char *name)
                               1796                 :                : {
 9171 JanWieck@Yahoo.com       1797                 :            362 :     return GetPortalByName(name);
                               1798                 :                : }
                               1799                 :                : 
                               1800                 :                : 
                               1801                 :                : /*
                               1802                 :                :  * SPI_cursor_fetch()
                               1803                 :                :  *
                               1804                 :                :  *  Fetch rows in a cursor
                               1805                 :                :  */
                               1806                 :                : void
 7240 bruce@momjian.us         1807                 :          29654 : SPI_cursor_fetch(Portal portal, bool forward, long count)
                               1808                 :                : {
 7015 tgl@sss.pgh.pa.us        1809                 :          29654 :     _SPI_cursor_operation(portal,
                               1810                 :          29654 :                           forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
                               1811                 :                :                           CreateDestReceiver(DestSPI));
                               1812                 :                :     /* we know that the DestSPI receiver doesn't need a destroy call */
 9171 JanWieck@Yahoo.com       1813                 :          29649 : }
                               1814                 :                : 
                               1815                 :                : 
                               1816                 :                : /*
                               1817                 :                :  * SPI_cursor_move()
                               1818                 :                :  *
                               1819                 :                :  *  Move in a cursor
                               1820                 :                :  */
                               1821                 :                : void
 7240 bruce@momjian.us         1822                 :UBC           0 : SPI_cursor_move(Portal portal, bool forward, long count)
                               1823                 :                : {
 7015 tgl@sss.pgh.pa.us        1824                 :              0 :     _SPI_cursor_operation(portal,
                               1825                 :              0 :                           forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
                               1826                 :                :                           None_Receiver);
                               1827                 :              0 : }
                               1828                 :                : 
                               1829                 :                : 
                               1830                 :                : /*
                               1831                 :                :  * SPI_scroll_cursor_fetch()
                               1832                 :                :  *
                               1833                 :                :  *  Fetch rows in a scrollable cursor
                               1834                 :                :  */
                               1835                 :                : void
 7015 tgl@sss.pgh.pa.us        1836                 :CBC         201 : SPI_scroll_cursor_fetch(Portal portal, FetchDirection direction, long count)
                               1837                 :                : {
                               1838                 :            201 :     _SPI_cursor_operation(portal,
                               1839                 :                :                           direction, count,
                               1840                 :                :                           CreateDestReceiver(DestSPI));
                               1841                 :                :     /* we know that the DestSPI receiver doesn't need a destroy call */
                               1842                 :            197 : }
                               1843                 :                : 
                               1844                 :                : 
                               1845                 :                : /*
                               1846                 :                :  * SPI_scroll_cursor_move()
                               1847                 :                :  *
                               1848                 :                :  *  Move in a scrollable cursor
                               1849                 :                :  */
                               1850                 :                : void
                               1851                 :             28 : SPI_scroll_cursor_move(Portal portal, FetchDirection direction, long count)
                               1852                 :                : {
                               1853                 :             28 :     _SPI_cursor_operation(portal, direction, count, None_Receiver);
 9171 JanWieck@Yahoo.com       1854                 :             28 : }
                               1855                 :                : 
                               1856                 :                : 
                               1857                 :                : /*
                               1858                 :                :  * SPI_cursor_close()
                               1859                 :                :  *
                               1860                 :                :  *  Close a cursor
                               1861                 :                :  */
                               1862                 :                : void
                               1863                 :           8245 : SPI_cursor_close(Portal portal)
                               1864                 :                : {
 9034 tgl@sss.pgh.pa.us        1865         [ -  + ]:           8245 :     if (!PortalIsValid(portal))
 9171 JanWieck@Yahoo.com       1866         [ #  # ]:UBC           0 :         elog(ERROR, "invalid portal in SPI cursor operation");
                               1867                 :                : 
 8496 bruce@momjian.us         1868                 :CBC        8245 :     PortalDrop(portal, false);
 9171 JanWieck@Yahoo.com       1869                 :           8245 : }
                               1870                 :                : 
                               1871                 :                : /*
                               1872                 :                :  * Returns the Oid representing the type id for argument at argIndex. First
                               1873                 :                :  * parameter is at index zero.
                               1874                 :                :  */
                               1875                 :                : Oid
 7047 tgl@sss.pgh.pa.us        1876                 :UBC           0 : SPI_getargtypeid(SPIPlanPtr plan, int argIndex)
                               1877                 :                : {
                               1878   [ #  #  #  #  :              0 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
                                              #  # ]
                               1879         [ #  # ]:              0 :         argIndex < 0 || argIndex >= plan->nargs)
                               1880                 :                :     {
 8136                          1881                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
                               1882                 :              0 :         return InvalidOid;
                               1883                 :                :     }
 7047                          1884                 :              0 :     return plan->argtypes[argIndex];
                               1885                 :                : }
                               1886                 :                : 
                               1887                 :                : /*
                               1888                 :                :  * Returns the number of arguments for the prepared plan.
                               1889                 :                :  */
                               1890                 :                : int
                               1891                 :              0 : SPI_getargcount(SPIPlanPtr plan)
                               1892                 :                : {
                               1893   [ #  #  #  # ]:              0 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
                               1894                 :                :     {
 8136                          1895                 :              0 :         SPI_result = SPI_ERROR_ARGUMENT;
                               1896                 :              0 :         return -1;
                               1897                 :                :     }
 7047                          1898                 :              0 :     return plan->nargs;
                               1899                 :                : }
                               1900                 :                : 
                               1901                 :                : /*
                               1902                 :                :  * Returns true if the plan contains exactly one command
                               1903                 :                :  * and that command returns tuples to the caller (eg, SELECT or
                               1904                 :                :  * INSERT ... RETURNING, but not SELECT ... INTO). In essence,
                               1905                 :                :  * the result indicates if the command can be used with SPI_cursor_open
                               1906                 :                :  *
                               1907                 :                :  * Parameters
                               1908                 :                :  *    plan: A plan previously prepared using SPI_prepare
                               1909                 :                :  */
                               1910                 :                : bool
 7047 tgl@sss.pgh.pa.us        1911                 :CBC        8314 : SPI_is_cursor_plan(SPIPlanPtr plan)
                               1912                 :                : {
                               1913                 :                :     CachedPlanSource *plansource;
                               1914                 :                : 
                               1915   [ +  -  -  + ]:           8314 :     if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
                               1916                 :                :     {
 8136 tgl@sss.pgh.pa.us        1917                 :UBC           0 :         SPI_result = SPI_ERROR_ARGUMENT;
                               1918                 :              0 :         return false;
                               1919                 :                :     }
                               1920                 :                : 
 7047 tgl@sss.pgh.pa.us        1921         [ -  + ]:CBC        8314 :     if (list_length(plan->plancache_list) != 1)
                               1922                 :                :     {
 6713 tgl@sss.pgh.pa.us        1923                 :UBC           0 :         SPI_result = 0;
 7260                          1924                 :              0 :         return false;           /* not exactly 1 pre-rewrite command */
                               1925                 :                :     }
 7047 tgl@sss.pgh.pa.us        1926                 :CBC        8314 :     plansource = (CachedPlanSource *) linitial(plan->plancache_list);
                               1927                 :                : 
                               1928                 :                :     /*
                               1929                 :                :      * We used to force revalidation of the cached plan here, but that seems
                               1930                 :                :      * unnecessary: invalidation could mean a change in the rowtype of the
                               1931                 :                :      * tuples returned by a plan, but not whether it returns tuples at all.
                               1932                 :                :      */
 6713                          1933                 :           8314 :     SPI_result = 0;
                               1934                 :                : 
                               1935                 :                :     /* Does it return tuples? */
 7047                          1936         [ +  - ]:           8314 :     if (plansource->resultDesc)
                               1937                 :           8314 :         return true;
                               1938                 :                : 
 8136 tgl@sss.pgh.pa.us        1939                 :UBC           0 :     return false;
                               1940                 :                : }
                               1941                 :                : 
                               1942                 :                : /*
                               1943                 :                :  * SPI_plan_is_valid --- test whether a SPI plan is currently valid
                               1944                 :                :  * (that is, not marked as being in need of revalidation).
                               1945                 :                :  *
                               1946                 :                :  * See notes for CachedPlanIsValid before using this.
                               1947                 :                :  */
                               1948                 :                : bool
 6497 tgl@sss.pgh.pa.us        1949                 :CBC        1455 : SPI_plan_is_valid(SPIPlanPtr plan)
                               1950                 :                : {
                               1951                 :                :     ListCell   *lc;
                               1952                 :                : 
 5401                          1953         [ -  + ]:           1455 :     Assert(plan->magic == _SPI_PLAN_MAGIC);
                               1954                 :                : 
                               1955   [ +  -  +  +  :           2781 :     foreach(lc, plan->plancache_list)
                                              +  + ]
                               1956                 :                :     {
                               1957                 :           1455 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               1958                 :                : 
                               1959         [ +  + ]:           1455 :         if (!CachedPlanIsValid(plansource))
                               1960                 :            129 :             return false;
                               1961                 :                :     }
                               1962                 :           1326 :     return true;
                               1963                 :                : }
                               1964                 :                : 
                               1965                 :                : /*
                               1966                 :                :  * SPI_result_code_string --- convert any SPI return code to a string
                               1967                 :                :  *
                               1968                 :                :  * This is often useful in error messages.  Most callers will probably
                               1969                 :                :  * only pass negative (error-case) codes, but for generality we recognize
                               1970                 :                :  * the success codes too.
                               1971                 :                :  */
                               1972                 :                : const char *
 8004                          1973                 :             60 : SPI_result_code_string(int code)
                               1974                 :                : {
                               1975                 :                :     static char buf[64];
                               1976                 :                : 
                               1977   [ -  -  -  -  :             60 :     switch (code)
                                     -  -  +  -  -  
                                     -  -  -  -  -  
                                     -  +  +  -  +  
                                     -  -  -  -  -  
                                     -  -  -  -  -  
                                           -  -  - ]
                               1978                 :                :     {
 8004 tgl@sss.pgh.pa.us        1979                 :UBC           0 :         case SPI_ERROR_CONNECT:
                               1980                 :              0 :             return "SPI_ERROR_CONNECT";
                               1981                 :              0 :         case SPI_ERROR_COPY:
                               1982                 :              0 :             return "SPI_ERROR_COPY";
                               1983                 :              0 :         case SPI_ERROR_OPUNKNOWN:
                               1984                 :              0 :             return "SPI_ERROR_OPUNKNOWN";
                               1985                 :              0 :         case SPI_ERROR_UNCONNECTED:
                               1986                 :              0 :             return "SPI_ERROR_UNCONNECTED";
                               1987                 :              0 :         case SPI_ERROR_ARGUMENT:
                               1988                 :              0 :             return "SPI_ERROR_ARGUMENT";
                               1989                 :              0 :         case SPI_ERROR_PARAM:
                               1990                 :              0 :             return "SPI_ERROR_PARAM";
 8004 tgl@sss.pgh.pa.us        1991                 :CBC           3 :         case SPI_ERROR_TRANSACTION:
                               1992                 :              3 :             return "SPI_ERROR_TRANSACTION";
 8004 tgl@sss.pgh.pa.us        1993                 :UBC           0 :         case SPI_ERROR_NOATTRIBUTE:
                               1994                 :              0 :             return "SPI_ERROR_NOATTRIBUTE";
                               1995                 :              0 :         case SPI_ERROR_NOOUTFUNC:
                               1996                 :              0 :             return "SPI_ERROR_NOOUTFUNC";
                               1997                 :              0 :         case SPI_ERROR_TYPUNKNOWN:
                               1998                 :              0 :             return "SPI_ERROR_TYPUNKNOWN";
 3378 kgrittn@postgresql.o     1999                 :              0 :         case SPI_ERROR_REL_DUPLICATE:
                               2000                 :              0 :             return "SPI_ERROR_REL_DUPLICATE";
                               2001                 :              0 :         case SPI_ERROR_REL_NOT_FOUND:
                               2002                 :              0 :             return "SPI_ERROR_REL_NOT_FOUND";
 8004 tgl@sss.pgh.pa.us        2003                 :              0 :         case SPI_OK_CONNECT:
                               2004                 :              0 :             return "SPI_OK_CONNECT";
                               2005                 :              0 :         case SPI_OK_FINISH:
                               2006                 :              0 :             return "SPI_OK_FINISH";
                               2007                 :              0 :         case SPI_OK_FETCH:
                               2008                 :              0 :             return "SPI_OK_FETCH";
 8004 tgl@sss.pgh.pa.us        2009                 :CBC           1 :         case SPI_OK_UTILITY:
                               2010                 :              1 :             return "SPI_OK_UTILITY";
                               2011                 :             11 :         case SPI_OK_SELECT:
                               2012                 :             11 :             return "SPI_OK_SELECT";
 8004 tgl@sss.pgh.pa.us        2013                 :UBC           0 :         case SPI_OK_SELINTO:
                               2014                 :              0 :             return "SPI_OK_SELINTO";
 8004 tgl@sss.pgh.pa.us        2015                 :CBC          45 :         case SPI_OK_INSERT:
                               2016                 :             45 :             return "SPI_OK_INSERT";
 8004 tgl@sss.pgh.pa.us        2017                 :UBC           0 :         case SPI_OK_DELETE:
                               2018                 :              0 :             return "SPI_OK_DELETE";
                               2019                 :              0 :         case SPI_OK_UPDATE:
                               2020                 :              0 :             return "SPI_OK_UPDATE";
                               2021                 :              0 :         case SPI_OK_CURSOR:
                               2022                 :              0 :             return "SPI_OK_CURSOR";
 7247                          2023                 :              0 :         case SPI_OK_INSERT_RETURNING:
                               2024                 :              0 :             return "SPI_OK_INSERT_RETURNING";
                               2025                 :              0 :         case SPI_OK_DELETE_RETURNING:
                               2026                 :              0 :             return "SPI_OK_DELETE_RETURNING";
                               2027                 :              0 :         case SPI_OK_UPDATE_RETURNING:
                               2028                 :              0 :             return "SPI_OK_UPDATE_RETURNING";
 6369 heikki.linnakangas@i     2029                 :              0 :         case SPI_OK_REWRITTEN:
                               2030                 :              0 :             return "SPI_OK_REWRITTEN";
 3378 kgrittn@postgresql.o     2031                 :              0 :         case SPI_OK_REL_REGISTER:
                               2032                 :              0 :             return "SPI_OK_REL_REGISTER";
                               2033                 :              0 :         case SPI_OK_REL_UNREGISTER:
                               2034                 :              0 :             return "SPI_OK_REL_UNREGISTER";
 1224 dean.a.rasheed@gmail     2035                 :              0 :         case SPI_OK_TD_REGISTER:
                               2036                 :              0 :             return "SPI_OK_TD_REGISTER";
                               2037                 :              0 :         case SPI_OK_MERGE:
                               2038                 :              0 :             return "SPI_OK_MERGE";
  835                          2039                 :              0 :         case SPI_OK_MERGE_RETURNING:
                               2040                 :              0 :             return "SPI_OK_MERGE_RETURNING";
                               2041                 :                :     }
                               2042                 :                :     /* Unrecognized code ... return something useful ... */
 8004 tgl@sss.pgh.pa.us        2043                 :              0 :     sprintf(buf, "Unrecognized SPI code %d", code);
                               2044                 :              0 :     return buf;
                               2045                 :                : }
                               2046                 :                : 
                               2047                 :                : /*
                               2048                 :                :  * SPI_plan_get_plan_sources --- get a SPI plan's underlying list of
                               2049                 :                :  * CachedPlanSources.
                               2050                 :                :  *
                               2051                 :                :  * CAUTION: there is no check on whether the CachedPlanSources are up-to-date.
                               2052                 :                :  *
                               2053                 :                :  * This is exported so that PL/pgSQL can use it (this beats letting PL/pgSQL
                               2054                 :                :  * look directly into the SPIPlan for itself).  It's not documented in
                               2055                 :                :  * spi.sgml because we'd just as soon not have too many places using this.
                               2056                 :                :  */
                               2057                 :                : List *
 4899 tgl@sss.pgh.pa.us        2058                 :CBC       38890 : SPI_plan_get_plan_sources(SPIPlanPtr plan)
                               2059                 :                : {
                               2060         [ -  + ]:          38890 :     Assert(plan->magic == _SPI_PLAN_MAGIC);
                               2061                 :          38890 :     return plan->plancache_list;
                               2062                 :                : }
                               2063                 :                : 
                               2064                 :                : /*
                               2065                 :                :  * SPI_plan_get_cached_plan --- get a SPI plan's generic CachedPlan,
                               2066                 :                :  * if the SPI plan contains exactly one CachedPlanSource.  If not,
                               2067                 :                :  * return NULL.
                               2068                 :                :  *
                               2069                 :                :  * The plan's refcount is incremented (and logged in CurrentResourceOwner,
                               2070                 :                :  * if it's a saved plan).  Caller is responsible for doing ReleaseCachedPlan.
                               2071                 :                :  *
                               2072                 :                :  * This is exported so that PL/pgSQL can use it (this beats letting PL/pgSQL
                               2073                 :                :  * look directly into the SPIPlan for itself).  It's not documented in
                               2074                 :                :  * spi.sgml because we'd just as soon not have too many places using this.
                               2075                 :                :  */
                               2076                 :                : CachedPlan *
                               2077                 :          19459 : SPI_plan_get_cached_plan(SPIPlanPtr plan)
                               2078                 :                : {
                               2079                 :                :     CachedPlanSource *plansource;
                               2080                 :                :     CachedPlan *cplan;
                               2081                 :                :     SPICallbackArg spicallbackarg;
                               2082                 :                :     ErrorContextCallback spierrcontext;
                               2083                 :                : 
                               2084         [ -  + ]:          19459 :     Assert(plan->magic == _SPI_PLAN_MAGIC);
                               2085                 :                : 
                               2086                 :                :     /* Can't support one-shot plans here */
                               2087         [ -  + ]:          19459 :     if (plan->oneshot)
 4899 tgl@sss.pgh.pa.us        2088                 :UBC           0 :         return NULL;
                               2089                 :                : 
                               2090                 :                :     /* Must have exactly one CachedPlanSource */
 4899 tgl@sss.pgh.pa.us        2091         [ -  + ]:CBC       19459 :     if (list_length(plan->plancache_list) != 1)
 4899 tgl@sss.pgh.pa.us        2092                 :UBC           0 :         return NULL;
 4899 tgl@sss.pgh.pa.us        2093                 :CBC       19459 :     plansource = (CachedPlanSource *) linitial(plan->plancache_list);
                               2094                 :                : 
                               2095                 :                :     /* Setup error traceback support for ereport() */
 2003                          2096                 :          19459 :     spicallbackarg.query = plansource->query_string;
                               2097                 :          19459 :     spicallbackarg.mode = plan->parse_mode;
 4899                          2098                 :          19459 :     spierrcontext.callback = _SPI_error_callback;
 2003                          2099                 :          19459 :     spierrcontext.arg = &spicallbackarg;
 4899                          2100                 :          19459 :     spierrcontext.previous = error_context_stack;
                               2101                 :          19459 :     error_context_stack = &spierrcontext;
                               2102                 :                : 
                               2103                 :                :     /* Get the generic plan for the query */
 1982                          2104                 :          19459 :     cplan = GetCachedPlan(plansource, NULL,
                               2105                 :          19459 :                           plan->saved ? CurrentResourceOwner : NULL,
 3378 kgrittn@postgresql.o     2106         [ +  + ]:          19459 :                           _SPI_current->queryEnv);
 4899 tgl@sss.pgh.pa.us        2107         [ -  + ]:          19430 :     Assert(cplan == plansource->gplan);
                               2108                 :                : 
                               2109                 :                :     /* Pop the error context stack */
                               2110                 :          19430 :     error_context_stack = spierrcontext.previous;
                               2111                 :                : 
                               2112                 :          19430 :     return cplan;
                               2113                 :                : }
                               2114                 :                : 
                               2115                 :                : 
                               2116                 :                : /* =================== private functions =================== */
                               2117                 :                : 
                               2118                 :                : /*
                               2119                 :                :  * spi_dest_startup
                               2120                 :                :  *      Initialize to receive tuples from Executor into SPITupleTable
                               2121                 :                :  *      of current SPI procedure
                               2122                 :                :  */
                               2123                 :                : void
 8454                          2124                 :          60586 : spi_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
                               2125                 :                : {
                               2126                 :                :     SPITupleTable *tuptable;
                               2127                 :                :     MemoryContext oldcxt;
                               2128                 :                :     MemoryContext tuptabcxt;
                               2129                 :                : 
 3521                          2130         [ -  + ]:          60586 :     if (_SPI_current == NULL)
 3521 tgl@sss.pgh.pa.us        2131         [ #  # ]:UBC           0 :         elog(ERROR, "spi_dest_startup called while not connected to SPI");
                               2132                 :                : 
 8561 tgl@sss.pgh.pa.us        2133         [ -  + ]:CBC       60586 :     if (_SPI_current->tuptable != NULL)
 8380 tgl@sss.pgh.pa.us        2134         [ #  # ]:UBC           0 :         elog(ERROR, "improper call to spi_dest_startup");
                               2135                 :                : 
                               2136                 :                :     /* We create the tuple table context as a child of procCxt */
                               2137                 :                : 
 8561 tgl@sss.pgh.pa.us        2138                 :CBC       60586 :     oldcxt = _SPI_procmem();    /* switch to procedure memory context */
                               2139                 :                : 
                               2140                 :          60586 :     tuptabcxt = AllocSetContextCreate(CurrentMemoryContext,
                               2141                 :                :                                       "SPI TupTable",
                               2142                 :                :                                       ALLOCSET_DEFAULT_SIZES);
                               2143                 :          60586 :     MemoryContextSwitchTo(tuptabcxt);
                               2144                 :                : 
  202 michael@paquier.xyz      2145                 :GNC       60586 :     _SPI_current->tuptable = tuptable = palloc0_object(SPITupleTable);
 8561 tgl@sss.pgh.pa.us        2146                 :CBC       60586 :     tuptable->tuptabcxt = tuptabcxt;
 4723                          2147                 :          60586 :     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                 :          60586 :     slist_push_head(&_SPI_current->tuptables, &tuptable->next);
                               2155                 :                : 
                               2156                 :                :     /* set up initial allocations */
 2539                          2157                 :          60586 :     tuptable->alloced = 128;
  202 michael@paquier.xyz      2158                 :GNC       60586 :     tuptable->vals = palloc_array(HeapTuple, tuptable->alloced);
 2539 tgl@sss.pgh.pa.us        2159                 :CBC       60586 :     tuptable->numvals = 0;
 8561                          2160                 :          60586 :     tuptable->tupdesc = CreateTupleDescCopy(typeinfo);
                               2161                 :                : 
                               2162                 :          60586 :     MemoryContextSwitchTo(oldcxt);
                               2163                 :          60586 : }
                               2164                 :                : 
                               2165                 :                : /*
                               2166                 :                :  * spi_printtup
                               2167                 :                :  *      store tuple retrieved by Executor into SPITupleTable
                               2168                 :                :  *      of current SPI procedure
                               2169                 :                :  */
                               2170                 :                : bool
 7776                          2171                 :          73714 : spi_printtup(TupleTableSlot *slot, DestReceiver *self)
                               2172                 :                : {
                               2173                 :                :     SPITupleTable *tuptable;
                               2174                 :                :     MemoryContext oldcxt;
                               2175                 :                : 
 3521                          2176         [ -  + ]:          73714 :     if (_SPI_current == NULL)
 3521 tgl@sss.pgh.pa.us        2177         [ #  # ]:UBC           0 :         elog(ERROR, "spi_printtup called while not connected to SPI");
                               2178                 :                : 
10523 bruce@momjian.us         2179                 :CBC       73714 :     tuptable = _SPI_current->tuptable;
                               2180         [ -  + ]:          73714 :     if (tuptable == NULL)
 8380 tgl@sss.pgh.pa.us        2181         [ #  # ]:UBC           0 :         elog(ERROR, "improper call to spi_printtup");
                               2182                 :                : 
 8561 tgl@sss.pgh.pa.us        2183                 :CBC       73714 :     oldcxt = MemoryContextSwitchTo(tuptable->tuptabcxt);
                               2184                 :                : 
 2539                          2185         [ -  + ]:          73714 :     if (tuptable->numvals >= tuptable->alloced)
                               2186                 :                :     {
                               2187                 :                :         /* Double the size of the pointer array */
 2539 tgl@sss.pgh.pa.us        2188                 :UBC           0 :         uint64      newalloced = tuptable->alloced * 2;
                               2189                 :                : 
 3760                          2190                 :              0 :         tuptable->vals = (HeapTuple *) repalloc_huge(tuptable->vals,
                               2191                 :                :                                                      newalloced * sizeof(HeapTuple));
 2539                          2192                 :              0 :         tuptable->alloced = newalloced;
                               2193                 :                :     }
                               2194                 :                : 
 2539 tgl@sss.pgh.pa.us        2195                 :CBC       73714 :     tuptable->vals[tuptable->numvals] = ExecCopySlotHeapTuple(slot);
                               2196                 :          73714 :     (tuptable->numvals)++;
                               2197                 :                : 
10523 bruce@momjian.us         2198                 :          73714 :     MemoryContextSwitchTo(oldcxt);
                               2199                 :                : 
 3676 rhaas@postgresql.org     2200                 :          73714 :     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
 4925 tgl@sss.pgh.pa.us        2221                 :          26775 : _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                 :                :      */
 2003                          2232                 :          26775 :     spicallbackarg.query = src;
                               2233                 :          26775 :     spicallbackarg.mode = plan->parse_mode;
 8136                          2234                 :          26775 :     spierrcontext.callback = _SPI_error_callback;
 2003                          2235                 :          26775 :     spierrcontext.arg = &spicallbackarg;
 8136                          2236                 :          26775 :     spierrcontext.previous = error_context_stack;
                               2237                 :          26775 :     error_context_stack = &spierrcontext;
                               2238                 :                : 
                               2239                 :                :     /*
                               2240                 :                :      * Parse the request string into a list of raw parse trees.
                               2241                 :                :      */
 2003                          2242                 :          26775 :     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                 :                :      */
 7047                          2248                 :          26775 :     plancache_list = NIL;
                               2249                 :                : 
 8660                          2250   [ +  -  +  +  :          53488 :     foreach(list_item, raw_parsetree_list)
                                              +  + ]
                               2251                 :                :     {
 3368                          2252                 :          26775 :         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                 :                :          */
 5401                          2260                 :          26775 :         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                 :                :          */
 6082                          2268         [ +  + ]:          26775 :         if (plan->parserSetup != NULL)
                               2269                 :                :         {
                               2270         [ -  + ]:          17941 :             Assert(plan->nargs == 0);
 1579 peter@eisentraut.org     2271                 :          17941 :             stmt_list = pg_analyze_and_rewrite_withcb(parsetree,
                               2272                 :                :                                                       src,
                               2273                 :                :                                                       plan->parserSetup,
                               2274                 :                :                                                       plan->parserSetupArg,
 3378 kgrittn@postgresql.o     2275                 :          17941 :                                                       _SPI_current->queryEnv);
                               2276                 :                :         }
                               2277                 :                :         else
                               2278                 :                :         {
 1579 peter@eisentraut.org     2279                 :           8834 :             stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
                               2280                 :                :                                                            src,
 1510 tgl@sss.pgh.pa.us        2281                 :           8834 :                                                            plan->argtypes,
                               2282                 :                :                                                            plan->nargs,
                               2283                 :           8834 :                                                            _SPI_current->queryEnv);
                               2284                 :                :         }
                               2285                 :                : 
                               2286                 :                :         /* Finish filling in the CachedPlanSource */
 5401                          2287                 :          26713 :         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                 :                : 
 7047                          2297                 :          26713 :         plancache_list = lappend(plancache_list, plansource);
                               2298                 :                :     }
                               2299                 :                : 
                               2300                 :          26713 :     plan->plancache_list = plancache_list;
 4925                          2301                 :          26713 :     plan->oneshot = false;
                               2302                 :                : 
                               2303                 :                :     /*
                               2304                 :                :      * Pop the error context stack
                               2305                 :                :      */
                               2306                 :          26713 :     error_context_stack = spierrcontext.previous;
                               2307                 :          26713 : }
                               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                 :          12181 : _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                 :                :      */
 2003                          2340                 :          12181 :     spicallbackarg.query = src;
                               2341                 :          12181 :     spicallbackarg.mode = plan->parse_mode;
 4925                          2342                 :          12181 :     spierrcontext.callback = _SPI_error_callback;
 2003                          2343                 :          12181 :     spierrcontext.arg = &spicallbackarg;
 4925                          2344                 :          12181 :     spierrcontext.previous = error_context_stack;
                               2345                 :          12181 :     error_context_stack = &spierrcontext;
                               2346                 :                : 
                               2347                 :                :     /*
                               2348                 :                :      * Parse the request string into a list of raw parse trees.
                               2349                 :                :      */
 2003                          2350                 :          12181 :     raw_parsetree_list = raw_parser(src, plan->parse_mode);
                               2351                 :                : 
                               2352                 :                :     /*
                               2353                 :                :      * Construct plancache entries, but don't do parse analysis yet.
                               2354                 :                :      */
 4925                          2355                 :          12175 :     plancache_list = NIL;
                               2356                 :                : 
                               2357   [ +  -  +  +  :          24355 :     foreach(list_item, raw_parsetree_list)
                                              +  + ]
                               2358                 :                :     {
 3368                          2359                 :          12180 :         RawStmt    *parsetree = lfirst_node(RawStmt, list_item);
                               2360                 :                :         CachedPlanSource *plansource;
                               2361                 :                : 
 4925                          2362                 :          12180 :         plansource = CreateOneShotCachedPlan(parsetree,
                               2363                 :                :                                              src,
                               2364                 :                :                                              CreateCommandTag(parsetree->stmt));
                               2365                 :                : 
                               2366                 :          12180 :         plancache_list = lappend(plancache_list, plansource);
                               2367                 :                :     }
                               2368                 :                : 
                               2369                 :          12175 :     plan->plancache_list = plancache_list;
                               2370                 :          12175 :     plan->oneshot = true;
                               2371                 :                : 
                               2372                 :                :     /*
                               2373                 :                :      * Pop the error context stack
                               2374                 :                :      */
 8136                          2375                 :          12175 :     error_context_stack = spierrcontext.previous;
10532 vadim4o@yahoo.com        2376                 :          12175 : }
                               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
 1731 tgl@sss.pgh.pa.us        2399                 :          68457 : _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
                               2400                 :                :                   Snapshot snapshot, Snapshot crosscheck_snapshot,
                               2401                 :                :                   bool fire_triggers)
                               2402                 :                : {
 6623 alvherre@alvh.no-ip.     2403                 :          68457 :     int         my_res = 0;
 3762 tgl@sss.pgh.pa.us        2404                 :          68457 :     uint64      my_processed = 0;
 6623 alvherre@alvh.no-ip.     2405                 :          68457 :     SPITupleTable *my_tuptable = NULL;
                               2406                 :          68457 :     int         res = 0;
                               2407                 :                :     bool        allow_nonatomic;
 5601 tgl@sss.pgh.pa.us        2408                 :          68457 :     bool        pushed_active_snap = false;
 1731                          2409                 :          68457 :     ResourceOwner plan_owner = options->owner;
                               2410                 :                :     SPICallbackArg spicallbackarg;
                               2411                 :                :     ErrorContextCallback spierrcontext;
 6623 alvherre@alvh.no-ip.     2412                 :          68457 :     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                 :                :      */
  622 tgl@sss.pgh.pa.us        2421                 :         136973 :     allow_nonatomic = options->allow_nonatomic &&
                               2422   [ +  +  +  +  :          68457 :         !_SPI_current->atomic && !IsSubTransaction();
                                              +  + ]
                               2423                 :                : 
                               2424                 :                :     /*
                               2425                 :                :      * Setup error traceback support for ereport()
                               2426                 :                :      */
 2003                          2427                 :          68457 :     spicallbackarg.query = NULL;    /* we'll fill this below */
                               2428                 :          68457 :     spicallbackarg.mode = plan->parse_mode;
 6623 alvherre@alvh.no-ip.     2429                 :          68457 :     spierrcontext.callback = _SPI_error_callback;
 2003 tgl@sss.pgh.pa.us        2430                 :          68457 :     spierrcontext.arg = &spicallbackarg;
 6623 alvherre@alvh.no-ip.     2431                 :          68457 :     spierrcontext.previous = error_context_stack;
                               2432                 :          68457 :     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                 :                :      */
 1866 tgl@sss.pgh.pa.us        2461         [ +  + ]:          68457 :     if (snapshot != InvalidSnapshot)
                               2462                 :                :     {
                               2463                 :                :         /* this intentionally tests the options field not the derived value */
 1731                          2464         [ -  + ]:            862 :         Assert(!options->allow_nonatomic);
                               2465         [ +  + ]:            862 :         if (options->read_only)
                               2466                 :                :         {
 5601                          2467                 :            826 :             PushActiveSnapshot(snapshot);
                               2468                 :            826 :             pushed_active_snap = true;
                               2469                 :                :         }
                               2470                 :                :         else
                               2471                 :                :         {
                               2472                 :                :             /* Make sure we have a private copy of the snapshot to modify */
                               2473                 :             36 :             PushCopiedSnapshot(snapshot);
                               2474                 :             36 :             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                 :                :      */
 1982                          2482         [ +  + ]:          68457 :     if (!plan->saved)
                               2483                 :          13124 :         plan_owner = NULL;
                               2484         [ +  + ]:          55333 :     else if (plan_owner == NULL)
                               2485                 :          55278 :         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                 :                :      */
 1731                          2493   [ +  +  -  + ]:          68457 :     if (options->must_return_tuples && plan->plancache_list == NIL)
 1731 tgl@sss.pgh.pa.us        2494         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2495                 :                :                 (errcode(ERRCODE_SYNTAX_ERROR),
                               2496                 :                :                  errmsg("empty query does not return tuples")));
                               2497                 :                : 
 6623 alvherre@alvh.no-ip.     2498   [ +  -  +  +  :CBC      133674 :     foreach(lc1, plan->plancache_list)
                                              +  + ]
                               2499                 :                :     {
                               2500                 :          68461 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc1);
                               2501                 :                :         List       *stmt_list;
                               2502                 :                :         ListCell   *lc2;
                               2503                 :                : 
 2003 tgl@sss.pgh.pa.us        2504                 :          68461 :         spicallbackarg.query = plansource->query_string;
                               2505                 :                : 
                               2506                 :                :         /*
                               2507                 :                :          * If this is a one-shot plan, we still need to do parse analysis.
                               2508                 :                :          */
 4925                          2509         [ +  + ]:          68461 :         if (plan->oneshot)
                               2510                 :                :         {
 3454                          2511                 :          12179 :             RawStmt    *parsetree = plansource->raw_parse_tree;
 4925                          2512                 :          12179 :             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                 :                :              */
 4248                          2519         [ -  + ]:          12179 :             if (parsetree == NULL)
 1364 drowley@postgresql.o     2520                 :UBC           0 :                 querytree_list = NIL;
 4248 tgl@sss.pgh.pa.us        2521         [ +  + ]:CBC       12179 :             else if (plan->parserSetup != NULL)
                               2522                 :                :             {
 4925                          2523         [ -  + ]:            289 :                 Assert(plan->nargs == 0);
 1364 drowley@postgresql.o     2524                 :            289 :                 querytree_list = pg_analyze_and_rewrite_withcb(parsetree,
                               2525                 :                :                                                                src,
                               2526                 :                :                                                                plan->parserSetup,
                               2527                 :                :                                                                plan->parserSetupArg,
                               2528                 :            289 :                                                                _SPI_current->queryEnv);
                               2529                 :                :             }
                               2530                 :                :             else
                               2531                 :                :             {
                               2532                 :          11890 :                 querytree_list = pg_analyze_and_rewrite_fixedparams(parsetree,
                               2533                 :                :                                                                     src,
                               2534                 :          11890 :                                                                     plan->argtypes,
                               2535                 :                :                                                                     plan->nargs,
                               2536                 :          11890 :                                                                     _SPI_current->queryEnv);
                               2537                 :                :             }
                               2538                 :                : 
                               2539                 :                :             /* Finish filling in the CachedPlanSource */
 4925 tgl@sss.pgh.pa.us        2540                 :          12172 :             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                 :                :          */
 1731                          2557   [ +  +  +  + ]:          68454 :         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         [ +  - ]:              8 :             if (plansource->commandTag == CMDTAG_SELECT)
                               2564                 :              8 :                 cmdtag = "SELECT INTO";
                               2565                 :                :             else
 1731 tgl@sss.pgh.pa.us        2566                 :UBC           0 :                 cmdtag = GetCommandTagName(plansource->commandTag);
 1731 tgl@sss.pgh.pa.us        2567         [ +  - ]:CBC           8 :             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                 :          68446 :         cplan = GetCachedPlan(plansource, options->params,
 1982                          2578                 :          68446 :                               plan_owner, _SPI_current->queryEnv);
                               2579                 :                : 
 5401                          2580                 :          68355 :         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                 :                :          */
 1866                          2586   [ +  +  +  - ]:         135848 :         if (snapshot == InvalidSnapshot &&
                               2587         [ +  - ]:         134986 :             (list_length(stmt_list) > 1 ||
                               2588         [ +  + ]:         134986 :              (list_length(stmt_list) == 1 &&
                               2589                 :          67493 :               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                 :          58831 :             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                 :                :              */
  753                          2609   [ +  +  +  + ]:          58831 :             if (!options->read_only && !allow_nonatomic)
                               2610                 :                :             {
 1866                          2611         [ +  + ]:          55861 :                 if (pushed_active_snap)
                               2612                 :              4 :                     PopActiveSnapshot();
                               2613                 :          55861 :                 PushActiveSnapshot(GetTransactionSnapshot());
                               2614                 :          55861 :                 pushed_active_snap = true;
                               2615                 :                :             }
                               2616                 :                :         }
                               2617                 :                : 
 6623 alvherre@alvh.no-ip.     2618   [ +  -  +  +  :         133572 :         foreach(lc2, stmt_list)
                                              +  + ]
                               2619                 :                :         {
 3368 tgl@sss.pgh.pa.us        2620                 :          68355 :             PlannedStmt *stmt = lfirst_node(PlannedStmt, lc2);
 3454                          2621                 :          68355 :             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                 :                :              */
 6623 alvherre@alvh.no-ip.     2630                 :          68355 :             _SPI_current->processed = 0;
                               2631                 :          68355 :             _SPI_current->tuptable = NULL;
                               2632                 :                : 
                               2633                 :                :             /* Check for unsupported cases. */
 3454 tgl@sss.pgh.pa.us        2634         [ +  + ]:          68355 :             if (stmt->utilityStmt)
                               2635                 :                :             {
                               2636         [ +  + ]:          17442 :                 if (IsA(stmt->utilityStmt, CopyStmt))
                               2637                 :                :                 {
                               2638                 :              9 :                     CopyStmt   *cstmt = (CopyStmt *) stmt->utilityStmt;
                               2639                 :                : 
 6623 alvherre@alvh.no-ip.     2640         [ +  + ]:              9 :                     if (cstmt->filename == NULL)
                               2641                 :                :                     {
                               2642                 :              4 :                         my_res = SPI_ERROR_COPY;
 7960 tgl@sss.pgh.pa.us        2643                 :              9 :                         goto fail;
                               2644                 :                :                     }
                               2645                 :                :                 }
 3454                          2646         [ +  + ]:          17433 :                 else if (IsA(stmt->utilityStmt, TransactionStmt))
                               2647                 :                :                 {
 6623 alvherre@alvh.no-ip.     2648                 :              5 :                     my_res = SPI_ERROR_TRANSACTION;
                               2649                 :              5 :                     goto fail;
                               2650                 :                :                 }
                               2651                 :                :             }
                               2652                 :                : 
 1731 tgl@sss.pgh.pa.us        2653   [ +  +  -  + ]:          68346 :             if (options->read_only && !CommandIsReadOnly(stmt))
 6623 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                 :                :              */
 1731 tgl@sss.pgh.pa.us        2665   [ +  +  +  + ]:CBC       68346 :             if (!options->read_only && pushed_active_snap)
                               2666                 :                :             {
 6623 alvherre@alvh.no-ip.     2667                 :          55893 :                 CommandCounterIncrement();
 5601 tgl@sss.pgh.pa.us        2668                 :          55893 :                 UpdateActiveSnapshotCommandId();
                               2669                 :                :             }
                               2670                 :                : 
                               2671                 :                :             /*
                               2672                 :                :              * Select appropriate tuple receiver.  Output from non-canSetTag
                               2673                 :                :              * subqueries always goes to the bit bucket.
                               2674                 :                :              */
 2209                          2675         [ -  + ]:          68346 :             if (!canSetTag)
 2209 tgl@sss.pgh.pa.us        2676                 :UBC           0 :                 dest = CreateDestReceiver(DestNone);
 1731 tgl@sss.pgh.pa.us        2677         [ +  + ]:CBC       68346 :             else if (options->dest)
                               2678                 :           1819 :                 dest = options->dest;
                               2679                 :                :             else
 2209                          2680                 :          66527 :                 dest = CreateDestReceiver(DestSPI);
                               2681                 :                : 
 3454                          2682         [ +  + ]:          68346 :             if (stmt->utilityStmt == NULL)
                               2683                 :                :             {
                               2684                 :                :                 QueryDesc  *qdesc;
                               2685                 :                :                 Snapshot    snap;
                               2686                 :                : 
 6623 alvherre@alvh.no-ip.     2687         [ +  - ]:          50913 :                 if (ActiveSnapshotSet())
                               2688                 :          50913 :                     snap = GetActiveSnapshot();
                               2689                 :                :                 else
 6623 alvherre@alvh.no-ip.     2690                 :UBC           0 :                     snap = InvalidSnapshot;
                               2691                 :                : 
 3454 tgl@sss.pgh.pa.us        2692                 :CBC       50913 :                 qdesc = CreateQueryDesc(stmt,
                               2693                 :                :                                         plansource->query_string,
                               2694                 :                :                                         snap, crosscheck_snapshot,
                               2695                 :                :                                         dest,
 1731                          2696                 :          50913 :                                         options->params,
                               2697                 :          50913 :                                         _SPI_current->queryEnv,
                               2698                 :                :                                         0);
  404 amitlan@postgresql.o     2699         [ +  - ]:          50913 :                 res = _SPI_pquery(qdesc, fire_triggers,
                               2700                 :                :                                   canSetTag ? options->tcount : 0);
 6623 alvherre@alvh.no-ip.     2701                 :          47885 :                 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                 :                :                  */
  753 tgl@sss.pgh.pa.us        2712         [ +  + ]:          17433 :                 if (allow_nonatomic)
 3020 peter_e@gmx.net          2713                 :             51 :                     context = PROCESS_UTILITY_QUERY_NONATOMIC;
                               2714                 :                :                 else
  753 tgl@sss.pgh.pa.us        2715                 :          17382 :                     context = PROCESS_UTILITY_QUERY;
                               2716                 :                : 
 2311 alvherre@alvh.no-ip.     2717                 :          17433 :                 InitializeQueryCompletion(&qc);
 6623                          2718                 :          17433 :                 ProcessUtility(stmt,
                               2719                 :                :                                plansource->query_string,
                               2720                 :                :                                true,    /* protect plancache's node tree */
                               2721                 :                :                                context,
 1731 tgl@sss.pgh.pa.us        2722                 :          17433 :                                options->params,
 3378 kgrittn@postgresql.o     2723                 :          17433 :                                _SPI_current->queryEnv,
                               2724                 :                :                                dest,
                               2725                 :                :                                &qc);
                               2726                 :                : 
                               2727                 :                :                 /* Update "processed" if stmt returned tuples */
 6623 alvherre@alvh.no-ip.     2728         [ +  + ]:          17332 :                 if (_SPI_current->tuptable)
 2539 tgl@sss.pgh.pa.us        2729                 :           1226 :                     _SPI_current->processed = _SPI_current->tuptable->numvals;
                               2730                 :                : 
 5018 heikki.linnakangas@i     2731                 :          17332 :                 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                 :                :                  */
 3454 tgl@sss.pgh.pa.us        2737         [ +  + ]:          17332 :                 if (IsA(stmt->utilityStmt, CreateTableAsStmt))
                               2738                 :                :                 {
                               2739                 :             33 :                     CreateTableAsStmt *ctastmt = (CreateTableAsStmt *) stmt->utilityStmt;
                               2740                 :                : 
 2311 alvherre@alvh.no-ip.     2741         [ +  + ]:             33 :                     if (qc.commandTag == CMDTAG_SELECT)
                               2742                 :             29 :                         _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                 :                :                          */
 3610 tgl@sss.pgh.pa.us        2749   [ +  -  -  + ]:              4 :                         Assert(ctastmt->if_not_exists ||
                               2750                 :                :                                ctastmt->into->skipData);
 3732                          2751                 :              4 :                         _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         [ -  + ]:             33 :                     if (ctastmt->is_select_into)
 5216 tgl@sss.pgh.pa.us        2759                 :UBC           0 :                         res = SPI_OK_SELINTO;
                               2760                 :                :                 }
 3454 tgl@sss.pgh.pa.us        2761         [ +  + ]:CBC       17299 :                 else if (IsA(stmt->utilityStmt, CopyStmt))
                               2762                 :                :                 {
 2311 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                 :                :              */
 6623                          2773         [ +  - ]:          65217 :             if (canSetTag)
                               2774                 :                :             {
                               2775                 :          65217 :                 my_processed = _SPI_current->processed;
                               2776                 :          65217 :                 SPI_freetuptable(my_tuptable);
                               2777                 :          65217 :                 my_tuptable = _SPI_current->tuptable;
                               2778                 :          65217 :                 my_res = res;
                               2779                 :                :             }
                               2780                 :                :             else
                               2781                 :                :             {
 6623 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                 :                : 
 6623 alvherre@alvh.no-ip.     2792         [ -  + ]:CBC       65217 :             if (res < 0)
                               2793                 :                :             {
 6623 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 */
 1982 tgl@sss.pgh.pa.us        2800                 :CBC       65217 :         ReleaseCachedPlan(cplan, plan_owner);
 6623 alvherre@alvh.no-ip.     2801                 :          65217 :         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                 :                :          */
 1731 tgl@sss.pgh.pa.us        2808         [ +  + ]:          65217 :         if (!options->read_only)
 6623 alvherre@alvh.no-ip.     2809                 :          61510 :             CommandCounterIncrement();
                               2810                 :                :     }
                               2811                 :                : 
                               2812                 :          65222 : fail:
                               2813                 :                : 
                               2814                 :                :     /* Pop the snapshot off the stack if we pushed one */
 5601 tgl@sss.pgh.pa.us        2815         [ +  + ]:          65222 :     if (pushed_active_snap)
                               2816                 :          53653 :         PopActiveSnapshot();
                               2817                 :                : 
                               2818                 :                :     /* We no longer need the cached plan refcount, if any */
 6623 alvherre@alvh.no-ip.     2819         [ +  + ]:          65222 :     if (cplan)
 1982 tgl@sss.pgh.pa.us        2820                 :              9 :         ReleaseCachedPlan(cplan, plan_owner);
                               2821                 :                : 
                               2822                 :                :     /*
                               2823                 :                :      * Pop the error context stack
                               2824                 :                :      */
 6623 alvherre@alvh.no-ip.     2825                 :          65222 :     error_context_stack = spierrcontext.previous;
                               2826                 :                : 
                               2827                 :                :     /* Save results for caller */
 7577 tgl@sss.pgh.pa.us        2828                 :          65222 :     SPI_processed = my_processed;
                               2829                 :          65222 :     SPI_tuptable = my_tuptable;
                               2830                 :                : 
                               2831                 :                :     /* tuptable now is caller's responsibility, not SPI's */
 7144                          2832                 :          65222 :     _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                 :                :      */
 7126                          2839         [ -  + ]:          65222 :     if (my_res == 0)
 6369 heikki.linnakangas@i     2840                 :UBC           0 :         my_res = SPI_OK_REWRITTEN;
                               2841                 :                : 
 7260 tgl@sss.pgh.pa.us        2842                 :CBC       65222 :     return my_res;
                               2843                 :                : }
                               2844                 :                : 
                               2845                 :                : /*
                               2846                 :                :  * Convert arrays of query parameters to form wanted by planner and executor
                               2847                 :                :  */
                               2848                 :                : static ParamListInfo
 6664                          2849                 :           6106 : _SPI_convert_params(int nargs, Oid *argtypes,
                               2850                 :                :                     const Datum *Values, const char *Nulls)
                               2851                 :                : {
                               2852                 :                :     ParamListInfo paramLI;
                               2853                 :                : 
                               2854         [ +  + ]:           6106 :     if (nargs > 0)
                               2855                 :                :     {
 2665 peter@eisentraut.org     2856                 :           5064 :         paramLI = makeParamList(nargs);
                               2857                 :                : 
                               2858         [ +  + ]:          13853 :         for (int i = 0; i < nargs; i++)
                               2859                 :                :         {
 6664 tgl@sss.pgh.pa.us        2860                 :           8789 :             ParamExternData *prm = &paramLI->params[i];
                               2861                 :                : 
                               2862                 :           8789 :             prm->value = Values[i];
                               2863   [ +  +  +  + ]:           8789 :             prm->isnull = (Nulls && Nulls[i] == 'n');
 5401                          2864                 :           8789 :             prm->pflags = PARAM_FLAG_CONST;
 6664                          2865                 :           8789 :             prm->ptype = argtypes[i];
                               2866                 :                :         }
                               2867                 :                :     }
                               2868                 :                :     else
                               2869                 :           1042 :         paramLI = NULL;
                               2870                 :           6106 :     return paramLI;
                               2871                 :                : }
                               2872                 :                : 
                               2873                 :                : static int
  404 amitlan@postgresql.o     2874                 :          50913 : _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount)
                               2875                 :                : {
10505 vadim4o@yahoo.com        2876                 :          50913 :     int         operation = queryDesc->operation;
                               2877                 :                :     int         eflags;
                               2878                 :                :     int         res;
                               2879                 :                : 
10523 bruce@momjian.us         2880   [ +  +  +  +  :          50913 :     switch (operation)
                                              +  - ]
                               2881                 :                :     {
10522                          2882                 :          30192 :         case CMD_SELECT:
 2209 tgl@sss.pgh.pa.us        2883         [ -  + ]:          30192 :             if (queryDesc->dest->mydest == DestNone)
                               2884                 :                :             {
                               2885                 :                :                 /* Don't return SPI_OK_SELECT if we're discarding result */
 7577 tgl@sss.pgh.pa.us        2886                 :UBC           0 :                 res = SPI_OK_UTILITY;
                               2887                 :                :             }
                               2888                 :                :             else
 7262 tgl@sss.pgh.pa.us        2889                 :CBC       30192 :                 res = SPI_OK_SELECT;
10522 bruce@momjian.us         2890                 :          30192 :             break;
                               2891                 :          13953 :         case CMD_INSERT:
 6107 tgl@sss.pgh.pa.us        2892         [ +  + ]:          13953 :             if (queryDesc->plannedstmt->hasReturning)
 7247                          2893                 :           1070 :                 res = SPI_OK_INSERT_RETURNING;
                               2894                 :                :             else
                               2895                 :          12883 :                 res = SPI_OK_INSERT;
10522 bruce@momjian.us         2896                 :          13953 :             break;
                               2897                 :           5629 :         case CMD_DELETE:
 6107 tgl@sss.pgh.pa.us        2898         [ -  + ]:           5629 :             if (queryDesc->plannedstmt->hasReturning)
 7247 tgl@sss.pgh.pa.us        2899                 :UBC           0 :                 res = SPI_OK_DELETE_RETURNING;
                               2900                 :                :             else
 7247 tgl@sss.pgh.pa.us        2901                 :CBC        5629 :                 res = SPI_OK_DELETE;
10522 bruce@momjian.us         2902                 :           5629 :             break;
                               2903                 :           1095 :         case CMD_UPDATE:
 6107 tgl@sss.pgh.pa.us        2904         [ +  + ]:           1095 :             if (queryDesc->plannedstmt->hasReturning)
 7247                          2905                 :             10 :                 res = SPI_OK_UPDATE_RETURNING;
                               2906                 :                :             else
                               2907                 :           1085 :                 res = SPI_OK_UPDATE;
10522 bruce@momjian.us         2908                 :           1095 :             break;
 1555 alvherre@alvh.no-ip.     2909                 :             44 :         case CMD_MERGE:
  835 dean.a.rasheed@gmail     2910         [ +  + ]:             44 :             if (queryDesc->plannedstmt->hasReturning)
                               2911                 :             12 :                 res = SPI_OK_MERGE_RETURNING;
                               2912                 :                :             else
                               2913                 :             32 :                 res = SPI_OK_MERGE;
 1555 alvherre@alvh.no-ip.     2914                 :             44 :             break;
10522 bruce@momjian.us         2915                 :UBC           0 :         default:
10164                          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 */
 6894 tgl@sss.pgh.pa.us        2925         [ +  + ]:CBC       50913 :     if (fire_triggers)
 5602                          2926                 :          47612 :         eflags = 0;             /* default run-to-completion flags */
                               2927                 :                :     else
                               2928                 :           3301 :         eflags = EXEC_FLAG_SKIP_TRIGGERS;
                               2929                 :                : 
  404 amitlan@postgresql.o     2930                 :          50913 :     ExecutorStart(queryDesc, eflags);
                               2931                 :                : 
  568 tgl@sss.pgh.pa.us        2932                 :          50913 :     ExecutorRun(queryDesc, ForwardScanDirection, tcount);
                               2933                 :                : 
 8608                          2934                 :          47891 :     _SPI_current->processed = queryDesc->estate->es_processed;
                               2935                 :                : 
 6107                          2936   [ +  +  +  + ]:          47891 :     if ((res == SPI_OK_SELECT || queryDesc->plannedstmt->hasReturning) &&
 7247                          2937         [ +  + ]:          28288 :         queryDesc->dest->mydest == DestSPI)
                               2938                 :                :     {
10505 vadim4o@yahoo.com        2939         [ -  + ]:          26510 :         if (_SPI_checktuples())
 8380 tgl@sss.pgh.pa.us        2940         [ #  # ]:UBC           0 :             elog(ERROR, "consistency check on SPI tuple count failed");
                               2941                 :                :     }
                               2942                 :                : 
 5602 tgl@sss.pgh.pa.us        2943                 :CBC       47891 :     ExecutorFinish(queryDesc);
 7767                          2944                 :          47885 :     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                 :                : 
 8598                          2952                 :          47885 :     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
 8136                          2961                 :           3783 : _SPI_error_callback(void *arg)
                               2962                 :                : {
 2003                          2963                 :           3783 :     SPICallbackArg *carg = (SPICallbackArg *) arg;
                               2964                 :           3783 :     const char *query = carg->query;
                               2965                 :                :     int         syntaxerrposition;
                               2966                 :                : 
 3123                          2967         [ -  + ]:           3783 :     if (query == NULL)          /* in case arg wasn't set yet */
 3123 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                 :                :      */
 8136 tgl@sss.pgh.pa.us        2974                 :CBC        3783 :     syntaxerrposition = geterrposition();
                               2975         [ +  + ]:           3783 :     if (syntaxerrposition > 0)
                               2976                 :                :     {
                               2977                 :             59 :         errposition(0);
                               2978                 :             59 :         internalerrposition(syntaxerrposition);
                               2979                 :             59 :         internalerrquery(query);
                               2980                 :                :     }
                               2981                 :                :     else
                               2982                 :                :     {
                               2983                 :                :         /* Use the parse mode to decide how to describe the query */
 2003                          2984      [ +  +  + ]:           3724 :         switch (carg->mode)
                               2985                 :                :         {
                               2986                 :             50 :             case RAW_PARSE_PLPGSQL_EXPR:
  663 peter@eisentraut.org     2987                 :             50 :                 errcontext("PL/pgSQL expression \"%s\"", query);
 2003 tgl@sss.pgh.pa.us        2988                 :             50 :                 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                 :           3666 :             default:
                               2995                 :           3666 :                 errcontext("SQL statement \"%s\"", query);
                               2996                 :           3666 :                 break;
                               2997                 :                :         }
                               2998                 :                :     }
                               2999                 :                : }
                               3000                 :                : 
                               3001                 :                : /*
                               3002                 :                :  * _SPI_cursor_operation()
                               3003                 :                :  *
                               3004                 :                :  *  Do a FETCH or MOVE in a cursor
                               3005                 :                :  */
                               3006                 :                : static void
 7015                          3007                 :          29883 : _SPI_cursor_operation(Portal portal, FetchDirection direction, long count,
                               3008                 :                :                       DestReceiver *dest)
                               3009                 :                : {
                               3010                 :                :     uint64      nfetched;
                               3011                 :                : 
                               3012                 :                :     /* Check that the portal is valid */
 9171 JanWieck@Yahoo.com       3013         [ -  + ]:          29883 :     if (!PortalIsValid(portal))
 9171 JanWieck@Yahoo.com       3014         [ #  # ]:UBC           0 :         elog(ERROR, "invalid portal in SPI cursor operation");
                               3015                 :                : 
                               3016                 :                :     /* Push the SPI stack */
 8316 tgl@sss.pgh.pa.us        3017         [ -  + ]:CBC       29883 :     if (_SPI_begin_call(true) < 0)
 8316 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) */
 9171 JanWieck@Yahoo.com       3021                 :CBC       29883 :     SPI_processed = 0;
                               3022                 :          29883 :     SPI_tuptable = NULL;
                               3023                 :          29883 :     _SPI_current->processed = 0;
                               3024                 :          29883 :     _SPI_current->tuptable = NULL;
                               3025                 :                : 
                               3026                 :                :     /* Run the cursor */
 8362 tgl@sss.pgh.pa.us        3027                 :          29883 :     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                 :          29874 :     _SPI_current->processed = nfetched;
                               3041                 :                : 
 7544 alvherre@alvh.no-ip.     3042   [ +  +  -  + ]:          29874 :     if (dest->mydest == DestSPI && _SPI_checktuples())
 8380 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 */
 9171 JanWieck@Yahoo.com       3046                 :CBC       29874 :     SPI_processed = _SPI_current->processed;
 9014 bruce@momjian.us         3047                 :          29874 :     SPI_tuptable = _SPI_current->tuptable;
                               3048                 :                : 
                               3049                 :                :     /* tuptable now is caller's responsibility, not SPI's */
 7144 tgl@sss.pgh.pa.us        3050                 :          29874 :     _SPI_current->tuptable = NULL;
                               3051                 :                : 
                               3052                 :                :     /* Pop the SPI stack */
 9171 JanWieck@Yahoo.com       3053                 :          29874 :     _SPI_end_call(true);
                               3054                 :          29874 : }
                               3055                 :                : 
                               3056                 :                : 
                               3057                 :                : static MemoryContext
 7930 neilc@samurai.com        3058                 :         133435 : _SPI_execmem(void)
                               3059                 :                : {
 9498 tgl@sss.pgh.pa.us        3060                 :         133435 :     return MemoryContextSwitchTo(_SPI_current->execCxt);
                               3061                 :                : }
                               3062                 :                : 
                               3063                 :                : static MemoryContext
 7930 neilc@samurai.com        3064                 :         190709 : _SPI_procmem(void)
                               3065                 :                : {
 9498 tgl@sss.pgh.pa.us        3066                 :         190709 :     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
 3189                          3077                 :         200153 : _SPI_begin_call(bool use_exec)
                               3078                 :                : {
 3521                          3079         [ -  + ]:         200153 :     if (_SPI_current == NULL)
10164 bruce@momjian.us         3080                 :UBC           0 :         return SPI_ERROR_UNCONNECTED;
                               3081                 :                : 
 3189 tgl@sss.pgh.pa.us        3082         [ +  + ]:CBC      200153 :     if (use_exec)
                               3083                 :                :     {
                               3084                 :                :         /* remember when the Executor operation started */
                               3085                 :         133435 :         _SPI_current->execSubid = GetCurrentSubTransactionId();
                               3086                 :                :         /* switch to the Executor memory context */
10523 bruce@momjian.us         3087                 :         133435 :         _SPI_execmem();
                               3088                 :                :     }
                               3089                 :                : 
10164                          3090                 :         200153 :     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
 3189 tgl@sss.pgh.pa.us        3101                 :         130673 : _SPI_end_call(bool use_exec)
                               3102                 :                : {
                               3103         [ +  + ]:         130673 :     if (use_exec)
                               3104                 :                :     {
                               3105                 :                :         /* switch to the procedure memory context */
10523 bruce@momjian.us         3106                 :         130123 :         _SPI_procmem();
                               3107                 :                :         /* mark Executor context no longer in use */
 3189 tgl@sss.pgh.pa.us        3108                 :         130123 :         _SPI_current->execSubid = InvalidSubTransactionId;
                               3109                 :                :         /* and free Executor memory */
  958 nathan@postgresql.or     3110                 :         130123 :         MemoryContextReset(_SPI_current->execCxt);
                               3111                 :                :     }
                               3112                 :                : 
10164 bruce@momjian.us         3113                 :         130673 :     return 0;
                               3114                 :                : }
                               3115                 :                : 
                               3116                 :                : static bool
 9098 tgl@sss.pgh.pa.us        3117                 :          56356 : _SPI_checktuples(void)
                               3118                 :                : {
 3762                          3119                 :          56356 :     uint64      processed = _SPI_current->processed;
10522 bruce@momjian.us         3120                 :          56356 :     SPITupleTable *tuptable = _SPI_current->tuptable;
                               3121                 :          56356 :     bool        failed = false;
                               3122                 :                : 
 8366                          3123         [ -  + ]:          56356 :     if (tuptable == NULL)       /* spi_dest_startup was not called */
 8561 tgl@sss.pgh.pa.us        3124                 :UBC           0 :         failed = true;
 2539 tgl@sss.pgh.pa.us        3125         [ -  + ]:CBC       56356 :     else if (processed != tuptable->numvals)
 8561 tgl@sss.pgh.pa.us        3126                 :UBC           0 :         failed = true;
                               3127                 :                : 
10164 bruce@momjian.us         3128                 :CBC       56356 :     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
 5401 tgl@sss.pgh.pa.us        3141                 :          20312 : _SPI_make_plan_non_temp(SPIPlanPtr plan)
                               3142                 :                : {
                               3143                 :                :     SPIPlanPtr  newplan;
                               3144                 :          20312 :     MemoryContext parentcxt = _SPI_current->procCxt;
                               3145                 :                :     MemoryContext plancxt;
                               3146                 :                :     MemoryContext oldcxt;
                               3147                 :                :     ListCell   *lc;
                               3148                 :                : 
                               3149                 :                :     /* Assert the input is a temporary SPIPlan */
                               3150         [ -  + ]:          20312 :     Assert(plan->magic == _SPI_PLAN_MAGIC);
                               3151         [ -  + ]:          20312 :     Assert(plan->plancxt == NULL);
                               3152                 :                :     /* One-shot plans can't be saved */
 4925                          3153         [ -  + ]:          20312 :     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                 :                :      */
 9171 JanWieck@Yahoo.com       3159                 :          20312 :     plancxt = AllocSetContextCreate(parentcxt,
                               3160                 :                :                                     "SPI Plan",
                               3161                 :                :                                     ALLOCSET_SMALL_SIZES);
                               3162                 :          20312 :     oldcxt = MemoryContextSwitchTo(plancxt);
                               3163                 :                : 
                               3164                 :                :     /* Copy the _SPI_plan struct and subsidiary data into the new context */
  202 michael@paquier.xyz      3165                 :GNC       20312 :     newplan = palloc0_object(_SPI_plan);
 7047 tgl@sss.pgh.pa.us        3166                 :CBC       20312 :     newplan->magic = _SPI_PLAN_MAGIC;
 9171 JanWieck@Yahoo.com       3167                 :          20312 :     newplan->plancxt = plancxt;
 2003 tgl@sss.pgh.pa.us        3168                 :          20312 :     newplan->parse_mode = plan->parse_mode;
 7015                          3169                 :          20312 :     newplan->cursor_options = plan->cursor_options;
10523 bruce@momjian.us         3170                 :          20312 :     newplan->nargs = plan->nargs;
                               3171         [ +  + ]:          20312 :     if (plan->nargs > 0)
                               3172                 :                :     {
  202 michael@paquier.xyz      3173                 :GNC        1398 :         newplan->argtypes = palloc_array(Oid, plan->nargs);
10523 bruce@momjian.us         3174                 :CBC        1398 :         memcpy(newplan->argtypes, plan->argtypes, plan->nargs * sizeof(Oid));
                               3175                 :                :     }
                               3176                 :                :     else
                               3177                 :          18914 :         newplan->argtypes = NULL;
 6082 tgl@sss.pgh.pa.us        3178                 :          20312 :     newplan->parserSetup = plan->parserSetup;
                               3179                 :          20312 :     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                 :                :      */
 7047                          3187   [ +  -  +  +  :          40624 :     foreach(lc, plan->plancache_list)
                                              +  + ]
                               3188                 :                :     {
                               3189                 :          20312 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               3190                 :                : 
 5401                          3191                 :          20312 :         CachedPlanSetParentContext(plansource, parentcxt);
                               3192                 :                : 
                               3193                 :                :         /* Build new list, with list cells in plancxt */
                               3194                 :          20312 :         newplan->plancache_list = lappend(newplan->plancache_list, plansource);
                               3195                 :                :     }
                               3196                 :                : 
 7047                          3197                 :          20312 :     MemoryContextSwitchTo(oldcxt);
                               3198                 :                : 
                               3199                 :                :     /* For safety, unlink the CachedPlanSources from the temporary plan */
 5401                          3200                 :          20312 :     plan->plancache_list = NIL;
                               3201                 :                : 
 7047                          3202                 :          20312 :     return newplan;
                               3203                 :                : }
                               3204                 :                : 
                               3205                 :                : /*
                               3206                 :                :  * Make a "saved" copy of the given plan.
                               3207                 :                :  */
                               3208                 :                : static SPIPlanPtr
 7047 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 */
 4925                          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                 :                :      */
 5401                          3224                 :              0 :     plancxt = AllocSetContextCreate(CurrentMemoryContext,
                               3225                 :                :                                     "SPI Plan",
                               3226                 :                :                                     ALLOCSET_SMALL_SIZES);
 7047                          3227                 :              0 :     oldcxt = MemoryContextSwitchTo(plancxt);
                               3228                 :                : 
                               3229                 :                :     /* Copy the SPI plan into its own context */
  202 michael@paquier.xyz      3230                 :UNC           0 :     newplan = palloc0_object(_SPI_plan);
 7047 tgl@sss.pgh.pa.us        3231                 :UBC           0 :     newplan->magic = _SPI_PLAN_MAGIC;
                               3232                 :              0 :     newplan->plancxt = plancxt;
 2003                          3233                 :              0 :     newplan->parse_mode = plan->parse_mode;
 7015                          3234                 :              0 :     newplan->cursor_options = plan->cursor_options;
 7047                          3235                 :              0 :     newplan->nargs = plan->nargs;
                               3236         [ #  # ]:              0 :     if (plan->nargs > 0)
                               3237                 :                :     {
  202 michael@paquier.xyz      3238                 :UNC           0 :         newplan->argtypes = palloc_array(Oid, plan->nargs);
 7047 tgl@sss.pgh.pa.us        3239                 :UBC           0 :         memcpy(newplan->argtypes, plan->argtypes, plan->nargs * sizeof(Oid));
                               3240                 :                :     }
                               3241                 :                :     else
                               3242                 :              0 :         newplan->argtypes = NULL;
 6082                          3243                 :              0 :     newplan->parserSetup = plan->parserSetup;
                               3244                 :              0 :     newplan->parserSetupArg = plan->parserSetupArg;
                               3245                 :                : 
                               3246                 :                :     /* Copy all the plancache entries */
 7047                          3247   [ #  #  #  #  :              0 :     foreach(lc, plan->plancache_list)
                                              #  # ]
                               3248                 :                :     {
                               3249                 :              0 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               3250                 :                :         CachedPlanSource *newsource;
                               3251                 :                : 
 5401                          3252                 :              0 :         newsource = CopyCachedPlan(plansource);
 7047                          3253                 :              0 :         newplan->plancache_list = lappend(newplan->plancache_list, newsource);
                               3254                 :                :     }
                               3255                 :                : 
 9171 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                 :                :      */
 5401 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                 :                : 
10164 bruce@momjian.us         3273                 :              0 :     return newplan;
                               3274                 :                : }
                               3275                 :                : 
                               3276                 :                : /*
                               3277                 :                :  * Internal lookup of ephemeral named relation by name.
                               3278                 :                :  */
                               3279                 :                : static EphemeralNamedRelation
 3378 kgrittn@postgresql.o     3280                 :CBC         550 : _SPI_find_ENR_by_name(const char *name)
                               3281                 :                : {
                               3282                 :                :     /* internal static function; any error is bug in SPI itself */
                               3283         [ -  + ]:            550 :     Assert(name != NULL);
                               3284                 :                : 
                               3285                 :                :     /* fast exit if no tuplestores have been added */
                               3286         [ +  + ]:            550 :     if (_SPI_current->queryEnv == NULL)
                               3287                 :            435 :         return NULL;
                               3288                 :                : 
                               3289                 :            115 :     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                 :            550 : SPI_register_relation(EphemeralNamedRelation enr)
                               3298                 :                : {
                               3299                 :                :     EphemeralNamedRelation match;
                               3300                 :                :     int         res;
                               3301                 :                : 
                               3302   [ +  -  -  + ]:            550 :     if (enr == NULL || enr->md.name == NULL)
 3378 kgrittn@postgresql.o     3303                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                               3304                 :                : 
 3296 tgl@sss.pgh.pa.us        3305                 :CBC         550 :     res = _SPI_begin_call(false);   /* keep current memory context */
 3378 kgrittn@postgresql.o     3306         [ -  + ]:            550 :     if (res < 0)
 3378 kgrittn@postgresql.o     3307                 :UBC           0 :         return res;
                               3308                 :                : 
 3378 kgrittn@postgresql.o     3309                 :CBC         550 :     match = _SPI_find_ENR_by_name(enr->md.name);
                               3310         [ -  + ]:            550 :     if (match)
 3378 kgrittn@postgresql.o     3311                 :UBC           0 :         res = SPI_ERROR_REL_DUPLICATE;
                               3312                 :                :     else
                               3313                 :                :     {
 3378 kgrittn@postgresql.o     3314         [ +  + ]:CBC         550 :         if (_SPI_current->queryEnv == NULL)
                               3315                 :            435 :             _SPI_current->queryEnv = create_queryEnv();
                               3316                 :                : 
                               3317                 :            550 :         register_ENR(_SPI_current->queryEnv, enr);
                               3318                 :            550 :         res = SPI_OK_REL_REGISTER;
                               3319                 :                :     }
                               3320                 :                : 
                               3321                 :            550 :     _SPI_end_call(false);
                               3322                 :                : 
                               3323                 :            550 :     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
 3378 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                 :                : 
 3296 tgl@sss.pgh.pa.us        3339                 :              0 :     res = _SPI_begin_call(false);   /* keep current memory context */
 3378 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
 3374 kgrittn@postgresql.o     3364                 :CBC       10493 : SPI_register_trigger_data(TriggerData *tdata)
                               3365                 :                : {
                               3366         [ -  + ]:          10493 :     if (tdata == NULL)
 3374 kgrittn@postgresql.o     3367                 :UBC           0 :         return SPI_ERROR_ARGUMENT;
                               3368                 :                : 
 3374 kgrittn@postgresql.o     3369         [ +  + ]:CBC       10493 :     if (tdata->tg_newtable)
                               3370                 :                :     {
                               3371                 :                :         EphemeralNamedRelation enr =
  202 michael@paquier.xyz      3372                 :GNC         319 :             palloc_object(EphemeralNamedRelationData);
                               3373                 :                :         int         rc;
                               3374                 :                : 
 3374 kgrittn@postgresql.o     3375                 :CBC         319 :         enr->md.name = tdata->tg_trigger->tgnewtable;
                               3376                 :            319 :         enr->md.reliddesc = tdata->tg_relation->rd_id;
                               3377                 :            319 :         enr->md.tupdesc = NULL;
                               3378                 :            319 :         enr->md.enrtype = ENR_NAMED_TUPLESTORE;
                               3379                 :            319 :         enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_newtable);
                               3380                 :            319 :         enr->reldata = tdata->tg_newtable;
                               3381                 :            319 :         rc = SPI_register_relation(enr);
                               3382         [ -  + ]:            319 :         if (rc != SPI_OK_REL_REGISTER)
 3374 kgrittn@postgresql.o     3383                 :UBC           0 :             return rc;
                               3384                 :                :     }
                               3385                 :                : 
 3374 kgrittn@postgresql.o     3386         [ +  + ]:CBC       10493 :     if (tdata->tg_oldtable)
                               3387                 :                :     {
                               3388                 :                :         EphemeralNamedRelation enr =
  202 michael@paquier.xyz      3389                 :GNC         231 :             palloc_object(EphemeralNamedRelationData);
                               3390                 :                :         int         rc;
                               3391                 :                : 
 3374 kgrittn@postgresql.o     3392                 :CBC         231 :         enr->md.name = tdata->tg_trigger->tgoldtable;
                               3393                 :            231 :         enr->md.reliddesc = tdata->tg_relation->rd_id;
                               3394                 :            231 :         enr->md.tupdesc = NULL;
                               3395                 :            231 :         enr->md.enrtype = ENR_NAMED_TUPLESTORE;
                               3396                 :            231 :         enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_oldtable);
                               3397                 :            231 :         enr->reldata = tdata->tg_oldtable;
                               3398                 :            231 :         rc = SPI_register_relation(enr);
                               3399         [ -  + ]:            231 :         if (rc != SPI_OK_REL_REGISTER)
 3374 kgrittn@postgresql.o     3400                 :UBC           0 :             return rc;
                               3401                 :                :     }
                               3402                 :                : 
 3374 kgrittn@postgresql.o     3403                 :CBC       10493 :     return SPI_OK_TD_REGISTER;
                               3404                 :                : }
        

Generated by: LCOV version 2.0-1