Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f59f3c8

Browse files
committed
Add a SPI_copytupledesc function that parallels SPI_copytuple --- ie,
it copies the tupdesc into upper-executor memory. This is necessary for returning tuple descriptors without leaking all of lower exec memory.
1 parent ba4d223 commit f59f3c8

File tree

3 files changed

+165
-18
lines changed

3 files changed

+165
-18
lines changed

doc/src/sgml/spi.sgml

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,121 @@ TBD
12291229
<!-- *********************************************** -->
12301230
<!-- *********************************************** -->
12311231

1232+
<REFENTRY ID="SPI-SPICOPYTUPLEDESC">
1233+
<REFMETA>
1234+
<REFENTRYTITLE>SPI_copytupledesc</REFENTRYTITLE>
1235+
<REFMISCINFO>SPI - Tuple Descriptor Copy</REFMISCINFO>
1236+
</REFMETA>
1237+
<REFNAMEDIV>
1238+
<REFNAME>SPI_copytupledesc
1239+
</REFNAME>
1240+
<REFPURPOSE>
1241+
Makes copy of tuple descriptor in upper Executor context
1242+
</REFPURPOSE>
1243+
<INDEXTERM ID="IX-SPI-SPICOPYTUPLEDESC-1"><PRIMARY>SPI</PRIMARY><SECONDARY>copying tuple descriptors</SECONDARY></INDEXTERM>
1244+
<INDEXTERM ID="IX-SPI-SPICOPYTUPLEDESC-2"><PRIMARY>SPI_copytupledesc</PRIMARY></INDEXTERM>
1245+
</REFNAMEDIV>
1246+
<REFSYNOPSISDIV>
1247+
<REFSYNOPSISDIVINFO>
1248+
<DATE>2001-08-02</DATE>
1249+
</REFSYNOPSISDIVINFO>
1250+
<SYNOPSIS>
1251+
SPI_copytupledesc(<REPLACEABLE CLASS="PARAMETER">tupdesc</REPLACEABLE>)
1252+
</SYNOPSIS>
1253+
1254+
<REFSECT2 ID="R2-SPI-SPICOPYTUPLEDESC-1">
1255+
<REFSECT2INFO>
1256+
<DATE>2001-08-02</DATE>
1257+
</REFSECT2INFO>
1258+
<TITLE>Inputs
1259+
</TITLE>
1260+
<VARIABLELIST>
1261+
<VARLISTENTRY>
1262+
<TERM>
1263+
TupleDesc <REPLACEABLE CLASS="PARAMETER">tupdesc</REPLACEABLE>
1264+
</TERM>
1265+
<LISTITEM>
1266+
<PARA>
1267+
Input tuple descriptor to be copied
1268+
</PARA>
1269+
</LISTITEM>
1270+
</VARLISTENTRY>
1271+
</VARIABLELIST>
1272+
</REFSECT2>
1273+
1274+
<REFSECT2 ID="R2-SPI-SPICOPYTUPLEDESC-2">
1275+
<REFSECT2INFO>
1276+
<DATE>2001-08-02</DATE>
1277+
</REFSECT2INFO>
1278+
<TITLE>Outputs
1279+
</TITLE>
1280+
<VARIABLELIST>
1281+
<VARLISTENTRY>
1282+
<TERM>
1283+
TupleDesc
1284+
</TERM>
1285+
<LISTITEM>
1286+
<PARA>
1287+
Copied tuple descriptor
1288+
<SimpleList>
1289+
<Member>
1290+
<ReturnValue>non-NULL</ReturnValue>
1291+
if <REPLACEABLE CLASS="PARAMETER">tupdesc</REPLACEABLE>
1292+
is not NULL and the copy was successful
1293+
</Member>
1294+
<Member>
1295+
<ReturnValue>NULL</ReturnValue>
1296+
only if <REPLACEABLE CLASS="PARAMETER">tupdesc</REPLACEABLE>
1297+
is NULL
1298+
</Member>
1299+
</SimpleList>
1300+
</para>
1301+
</LISTITEM>
1302+
</VARLISTENTRY>
1303+
</VARIABLELIST>
1304+
</REFSECT2>
1305+
</REFSYNOPSISDIV>
1306+
1307+
<REFSECT1 ID="R1-SPI-SPICOPYTUPLEDESC-1">
1308+
<REFSECT1INFO>
1309+
<DATE>2001-08-02</DATE>
1310+
</REFSECT1INFO>
1311+
<TITLE>Description
1312+
</TITLE>
1313+
<PARA>
1314+
<FUNCTION>SPI_copytupledesc</FUNCTION>
1315+
makes a copy of tupdesc in upper Executor context. See the section on Memory Management.
1316+
</PARA>
1317+
</REFSECT1>
1318+
<REFSECT1 ID="R1-SPI-SPICOPYTUPLEDESC-2">
1319+
<TITLE>Usage
1320+
</TITLE>
1321+
<Para>
1322+
TBD
1323+
</PARA>
1324+
</REFSECT1>
1325+
<!--
1326+
<REFSECT1 ID="R1-SPI-SPICOPYTUPLEDESC-3">
1327+
<TITLE>Algorithm
1328+
</TITLE>
1329+
<PARA>
1330+
</PARA>
1331+
</REFSECT1>
1332+
-->
1333+
<!--
1334+
<REFSECT1 ID="R1-SPI-SPICOPYTUPLEDESC-4">
1335+
<TITLE>Structures
1336+
</TITLE>
1337+
<PARA>None
1338+
</PARA>
1339+
</REFSECT1>
1340+
-->
1341+
</REFENTRY>
1342+
1343+
<!-- *********************************************** -->
1344+
<!-- *********************************************** -->
1345+
<!-- *********************************************** -->
1346+
12321347
<REFENTRY ID="SPI-SPIMODIFYTUPLE">
12331348
<REFMETA>
12341349
<REFENTRYTITLE>SPI_modifytuple</REFENTRYTITLE>
@@ -2647,10 +2762,13 @@ made in this context.
26472762

26482763
<Para>
26492764

2650-
After <Function>SPI_connect</Function> is called current context is the procedure's one. All
2651-
allocations made via <Function>palloc</Function>/<Function>repalloc</Function> or by SPI utility functions (except
2652-
for <Function>SPI_copytuple</Function>, <Function>SPI_modifytuple</Function>,
2653-
<Function>SPI_palloc</Function> and <Function>SPI_repalloc</Function>) are
2765+
After <Function>SPI_connect</Function> is called current context is the
2766+
procedure's one. All allocations made via
2767+
<Function>palloc</Function>/<Function>repalloc</Function> or by SPI utility
2768+
functions (except for <Function>SPI_copytuple</Function>,
2769+
<Function>SPI_copytupledesc</Function>,
2770+
<Function>SPI_modifytuple</Function>,
2771+
<Function>SPI_palloc</Function> and <Function>SPI_repalloc</Function>) are
26542772
made in this context.
26552773
</Para>
26562774

src/backend/executor/spi.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.56 2001/08/02 16:05:23 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.57 2001/08/02 18:08:43 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -333,6 +333,33 @@ SPI_copytuple(HeapTuple tuple)
333333
return ctuple;
334334
}
335335

336+
TupleDesc
337+
SPI_copytupledesc(TupleDesc tupdesc)
338+
{
339+
MemoryContext oldcxt = NULL;
340+
TupleDesc ctupdesc;
341+
342+
if (tupdesc == NULL)
343+
{
344+
SPI_result = SPI_ERROR_ARGUMENT;
345+
return NULL;
346+
}
347+
348+
if (_SPI_curid + 1 == _SPI_connected) /* connected */
349+
{
350+
if (_SPI_current != &(_SPI_stack[_SPI_curid + 1]))
351+
elog(FATAL, "SPI: stack corrupted");
352+
oldcxt = MemoryContextSwitchTo(_SPI_current->savedcxt);
353+
}
354+
355+
ctupdesc = CreateTupleDescCopy(tupdesc);
356+
357+
if (oldcxt)
358+
MemoryContextSwitchTo(oldcxt);
359+
360+
return ctupdesc;
361+
}
362+
336363
HeapTuple
337364
SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum,
338365
Datum *Values, char *Nulls)
@@ -1232,7 +1259,7 @@ _SPI_end_call(bool procmem)
12321259
}
12331260

12341261
static bool
1235-
_SPI_checktuples()
1262+
_SPI_checktuples(void)
12361263
{
12371264
uint32 processed = _SPI_current->processed;
12381265
SPITupleTable *tuptable = _SPI_current->tuptable;
@@ -1244,8 +1271,8 @@ _SPI_checktuples()
12441271
failed = true;
12451272
}
12461273
else
1247-
/* some tuples were processed */
12481274
{
1275+
/* some tuples were processed */
12491276
if (tuptable == NULL) /* spi_printtup was not called */
12501277
failed = true;
12511278
else if (processed != (tuptable->alloced - tuptable->free))

src/include/executor/spi.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*
33
* spi.h
44
*
5+
* $Id: spi.h,v 1.28 2001/08/02 18:08:43 tgl Exp $
56
*
67
*-------------------------------------------------------------------------
78
*/
@@ -48,17 +49,17 @@ typedef struct
4849
HeapTuple *vals; /* tuples */
4950
} SPITupleTable;
5051

51-
#define SPI_ERROR_CONNECT -1
52-
#define SPI_ERROR_COPY -2
53-
#define SPI_ERROR_OPUNKNOWN -3
54-
#define SPI_ERROR_UNCONNECTED -4
55-
#define SPI_ERROR_CURSOR -5
56-
#define SPI_ERROR_ARGUMENT -6
57-
#define SPI_ERROR_PARAM -7
58-
#define SPI_ERROR_TRANSACTION -8
59-
#define SPI_ERROR_NOATTRIBUTE -9
60-
#define SPI_ERROR_NOOUTFUNC -10
61-
#define SPI_ERROR_TYPUNKNOWN -11
52+
#define SPI_ERROR_CONNECT (-1)
53+
#define SPI_ERROR_COPY (-2)
54+
#define SPI_ERROR_OPUNKNOWN (-3)
55+
#define SPI_ERROR_UNCONNECTED (-4)
56+
#define SPI_ERROR_CURSOR (-5)
57+
#define SPI_ERROR_ARGUMENT (-6)
58+
#define SPI_ERROR_PARAM (-7)
59+
#define SPI_ERROR_TRANSACTION (-8)
60+
#define SPI_ERROR_NOATTRIBUTE (-9)
61+
#define SPI_ERROR_NOOUTFUNC (-10)
62+
#define SPI_ERROR_TYPUNKNOWN (-11)
6263

6364
#define SPI_OK_CONNECT 1
6465
#define SPI_OK_FINISH 2
@@ -87,6 +88,7 @@ extern void *SPI_saveplan(void *plan);
8788
extern int SPI_freeplan(void *plan);
8889

8990
extern HeapTuple SPI_copytuple(HeapTuple tuple);
91+
extern TupleDesc SPI_copytupledesc(TupleDesc tupdesc);
9092
extern HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts,
9193
int *attnum, Datum *Values, char *Nulls);
9294
extern int SPI_fnumber(TupleDesc tupdesc, char *fname);

0 commit comments

Comments
 (0)