📄 spi.c
字号:
/*------------------------------------------------------------------------- * * spi.c * Server Programming Interface * * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.107 2003/10/01 21:30:52 tgl Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include "access/printtup.h"#include "catalog/heap.h"#include "executor/spi_priv.h"#include "tcop/tcopprot.h"#include "utils/lsyscache.h"uint32 SPI_processed = 0;Oid SPI_lastoid = InvalidOid;SPITupleTable *SPI_tuptable = NULL;int SPI_result;static _SPI_connection *_SPI_stack = NULL;static _SPI_connection *_SPI_current = NULL;static int _SPI_connected = -1;static int _SPI_curid = -1;static int _SPI_execute(const char *src, int tcount, _SPI_plan *plan);static int _SPI_pquery(QueryDesc *queryDesc, bool runit, bool useCurrentSnapshot, int tcount);static int _SPI_execute_plan(_SPI_plan *plan, Datum *Values, const char *Nulls, bool useCurrentSnapshot, int tcount);static void _SPI_cursor_operation(Portal portal, bool forward, int count, DestReceiver *dest);static _SPI_plan *_SPI_copy_plan(_SPI_plan *plan, int location);static int _SPI_begin_call(bool execmem);static int _SPI_end_call(bool procmem);static MemoryContext _SPI_execmem(void);static MemoryContext _SPI_procmem(void);static bool _SPI_checktuples(void);/* =================== interface functions =================== */intSPI_connect(void){ _SPI_connection *new_SPI_stack; /* * When procedure called by Executor _SPI_curid expected to be equal * to _SPI_connected */ if (_SPI_curid != _SPI_connected) return SPI_ERROR_CONNECT; if (_SPI_stack == NULL) { if (_SPI_connected != -1) elog(ERROR, "SPI stack corrupted"); new_SPI_stack = (_SPI_connection *) malloc(sizeof(_SPI_connection)); } else { if (_SPI_connected < 0) elog(ERROR, "SPI stack corrupted"); new_SPI_stack = (_SPI_connection *) realloc(_SPI_stack, (_SPI_connected + 2) * sizeof(_SPI_connection)); } if (new_SPI_stack == NULL) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); /* * We' returning to procedure where _SPI_curid == _SPI_connected - 1 */ _SPI_stack = new_SPI_stack; _SPI_connected++; _SPI_current = &(_SPI_stack[_SPI_connected]); _SPI_current->processed = 0; _SPI_current->tuptable = NULL; /* * Create memory contexts for this procedure * * XXX it would be better to use PortalContext as the parent context, but * we may not be inside a portal (consider deferred-trigger * execution). */ _SPI_current->procCxt = AllocSetContextCreate(TopTransactionContext, "SPI Proc", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); _SPI_current->execCxt = AllocSetContextCreate(TopTransactionContext, "SPI Exec", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); /* ... and switch to procedure's context */ _SPI_current->savedcxt = MemoryContextSwitchTo(_SPI_current->procCxt); return SPI_OK_CONNECT;}intSPI_finish(void){ int res; res = _SPI_begin_call(false); /* live in procedure memory */ if (res < 0) return res; /* Restore memory context as it was before procedure call */ MemoryContextSwitchTo(_SPI_current->savedcxt); /* Release memory used in procedure call */ MemoryContextDelete(_SPI_current->execCxt); MemoryContextDelete(_SPI_current->procCxt); /* * Reset result variables, especially SPI_tuptable which is probably * pointing at a just-deleted tuptable */ SPI_processed = 0; SPI_lastoid = InvalidOid; SPI_tuptable = NULL; /* * After _SPI_begin_call _SPI_connected == _SPI_curid. Now we are * closing connection to SPI and returning to upper Executor and so * _SPI_connected must be equal to _SPI_curid. */ _SPI_connected--; _SPI_curid--; if (_SPI_connected == -1) { free(_SPI_stack); _SPI_stack = NULL; _SPI_current = NULL; } else { _SPI_connection *new_SPI_stack; new_SPI_stack = (_SPI_connection *) realloc(_SPI_stack, (_SPI_connected + 1) * sizeof(_SPI_connection)); /* This could only fail with a pretty stupid malloc package ... */ if (new_SPI_stack == NULL) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); _SPI_stack = new_SPI_stack; _SPI_current = &(_SPI_stack[_SPI_connected]); } return SPI_OK_FINISH;}/* * Clean up SPI state at transaction commit or abort (we don't care which). */voidAtEOXact_SPI(void){ /* * Note that memory contexts belonging to SPI stack entries will be * freed automatically, so we can ignore them here. We just need to * restore our static variables to initial state. */ if (_SPI_stack != NULL) /* there was abort */ free(_SPI_stack); _SPI_current = _SPI_stack = NULL; _SPI_connected = _SPI_curid = -1; SPI_processed = 0; SPI_lastoid = InvalidOid; SPI_tuptable = NULL;}voidSPI_push(void){ _SPI_curid++;}voidSPI_pop(void){ _SPI_curid--;}intSPI_exec(const char *src, int tcount){ int res; if (src == NULL || tcount < 0) return SPI_ERROR_ARGUMENT; res = _SPI_begin_call(true); if (res < 0) return res; res = _SPI_execute(src, tcount, NULL); _SPI_end_call(true); return res;}intSPI_execp(void *plan, Datum *Values, const char *Nulls, int tcount){ int res; if (plan == NULL || tcount < 0) return SPI_ERROR_ARGUMENT; if (((_SPI_plan *) plan)->nargs > 0 && Values == NULL) return SPI_ERROR_PARAM; res = _SPI_begin_call(true); if (res < 0) return res; res = _SPI_execute_plan((_SPI_plan *) plan, Values, Nulls, false, tcount); _SPI_end_call(true); return res;}/* * SPI_execp_current -- identical to SPI_execp, except that we expose the * Executor option to use a current snapshot instead of the normal * QuerySnapshot. This is currently not documented in spi.sgml because * it is only intended for use by RI triggers. */intSPI_execp_current(void *plan, Datum *Values, const char *Nulls, bool useCurrentSnapshot, int tcount){ int res; if (plan == NULL || tcount < 0) return SPI_ERROR_ARGUMENT; if (((_SPI_plan *) plan)->nargs > 0 && Values == NULL) return SPI_ERROR_PARAM; res = _SPI_begin_call(true); if (res < 0) return res; res = _SPI_execute_plan((_SPI_plan *) plan, Values, Nulls, useCurrentSnapshot, tcount); _SPI_end_call(true); return res;}void *SPI_prepare(const char *src, int nargs, Oid *argtypes){ _SPI_plan *plan; if (src == NULL || nargs < 0 || (nargs > 0 && argtypes == NULL)) { SPI_result = SPI_ERROR_ARGUMENT; return NULL; } SPI_result = _SPI_begin_call(true); if (SPI_result < 0) return NULL; plan = (_SPI_plan *) palloc(sizeof(_SPI_plan)); /* Executor context */ plan->argtypes = argtypes; plan->nargs = nargs; SPI_result = _SPI_execute(src, 0, plan); if (SPI_result >= 0) /* copy plan to procedure context */ plan = _SPI_copy_plan(plan, _SPI_CPLAN_PROCXT); else plan = NULL; _SPI_end_call(true); return (void *) plan;}void *SPI_saveplan(void *plan){ _SPI_plan *newplan; if (plan == NULL) { SPI_result = SPI_ERROR_ARGUMENT; return NULL; } SPI_result = _SPI_begin_call(false); /* don't change context */ if (SPI_result < 0) return NULL; newplan = _SPI_copy_plan((_SPI_plan *) plan, _SPI_CPLAN_TOPCXT); _SPI_curid--; SPI_result = 0; return (void *) newplan;}intSPI_freeplan(void *plan){ _SPI_plan *spiplan = (_SPI_plan *) plan; if (plan == NULL) return SPI_ERROR_ARGUMENT; MemoryContextDelete(spiplan->plancxt); return 0;}HeapTupleSPI_copytuple(HeapTuple tuple){ MemoryContext oldcxt = NULL; HeapTuple ctuple; if (tuple == NULL) { SPI_result = SPI_ERROR_ARGUMENT; return NULL; } if (_SPI_curid + 1 == _SPI_connected) /* connected */ { if (_SPI_current != &(_SPI_stack[_SPI_curid + 1])) elog(ERROR, "SPI stack corrupted"); oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt); } ctuple = heap_copytuple(tuple); if (oldcxt) MemoryContextSwitchTo(oldcxt); return ctuple;}TupleDescSPI_copytupledesc(TupleDesc tupdesc){ MemoryContext oldcxt = NULL; TupleDesc ctupdesc; if (tupdesc == NULL) { SPI_result = SPI_ERROR_ARGUMENT; return NULL; } if (_SPI_curid + 1 == _SPI_connected) /* connected */ { if (_SPI_current != &(_SPI_stack[_SPI_curid + 1])) elog(ERROR, "SPI stack corrupted"); oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt); } ctupdesc = CreateTupleDescCopy(tupdesc); if (oldcxt) MemoryContextSwitchTo(oldcxt); return ctupdesc;}TupleTableSlot *SPI_copytupleintoslot(HeapTuple tuple, TupleDesc tupdesc){ MemoryContext oldcxt = NULL; TupleTableSlot *cslot; HeapTuple ctuple; TupleDesc ctupdesc; if (tuple == NULL || tupdesc == NULL) { SPI_result = SPI_ERROR_ARGUMENT; return NULL; } if (_SPI_curid + 1 == _SPI_connected) /* connected */ { if (_SPI_current != &(_SPI_stack[_SPI_curid + 1])) elog(ERROR, "SPI stack corrupted"); oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt); } ctuple = heap_copytuple(tuple); ctupdesc = CreateTupleDescCopy(tupdesc); cslot = MakeTupleTableSlot(); ExecSetSlotDescriptor(cslot, ctupdesc, true); cslot = ExecStoreTuple(ctuple, cslot, InvalidBuffer, true); if (oldcxt) MemoryContextSwitchTo(oldcxt); return cslot;}HeapTupleSPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum, Datum *Values, const char *Nulls){ MemoryContext oldcxt = NULL; HeapTuple mtuple; int numberOfAttributes; Datum *v; char *n; bool isnull; int i; if (rel == NULL || tuple == NULL || natts < 0 || attnum == NULL || Values == NULL) { SPI_result = SPI_ERROR_ARGUMENT; return NULL; } if (_SPI_curid + 1 == _SPI_connected) /* connected */ { if (_SPI_current != &(_SPI_stack[_SPI_curid + 1])) elog(ERROR, "SPI stack corrupted"); oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt); } SPI_result = 0; numberOfAttributes = rel->rd_att->natts; v = (Datum *) palloc(numberOfAttributes * sizeof(Datum)); n = (char *) palloc(numberOfAttributes * sizeof(char)); /* fetch old values and nulls */ for (i = 0; i < numberOfAttributes; i++) { v[i] = heap_getattr(tuple, i + 1, rel->rd_att, &isnull); n[i] = (isnull) ? 'n' : ' '; } /* replace values and nulls */ for (i = 0; i < natts; i++) { if (attnum[i] <= 0 || attnum[i] > numberOfAttributes) break; v[attnum[i] - 1] = Values[i]; n[attnum[i] - 1] = (Nulls && Nulls[i] == 'n') ? 'n' : ' '; } if (i == natts) /* no errors in *attnum */ { mtuple = heap_formtuple(rel->rd_att, v, n); /* * copy the identification info of the old tuple: t_ctid, t_self, * and OID (if any) */ mtuple->t_data->t_ctid = tuple->t_data->t_ctid; mtuple->t_self = tuple->t_self; mtuple->t_tableOid = tuple->t_tableOid;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -