Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbc8393c

Browse files
committed
Further adjust SPITupleTable to provide a public row-count field.
Now that commitfec0778 drew a clear line between public and privatefields in SPITupleTable, it seems pretty silly that the count of validtuples isn't on the public side of that line. The reason why not wasthat there wasn't such a count. For reasons lost in the mists of time,spi.c preferred to keep a count of remaining free entries in the array.But that seems pretty pointless: it's unlike the way we handle similarcode everywhere else, and it involves extra subtractions that surelyoutweigh having to do a comparison rather than test-for-zero to checkfor array-full.Hence, rearrange so that this code does the expansible array logicthe same as everywhere else, with a count of valid entries alongsidethe allocated array length. And document the count as public.I looked for core-code callers where it would make sense to startrelying on tuptable->numvals rather than the separate SPI_processedvariable. Right now there don't seem to be places where it'd bea win to do so without more code restructuring than I care toundertake today. In principle, though, having SPITupleTables befully self-contained should be helpful down the line.Discussion:https://postgr.es/m/16852.1563395722@sss.pgh.pa.us
1 parent7d24f6a commitbc8393c

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

‎doc/src/sgml/spi.sgml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,23 @@ typedef struct SPITupleTable
323323
/* Public members */
324324
TupleDesc tupdesc; /* tuple descriptor */
325325
HeapTuple *vals; /* array of tuples */
326+
uint64 numvals; /* number of valid tuples */
326327

327328
/* Private members, not intended for external callers */
329+
uint64 alloced; /* allocated length of vals array */
328330
MemoryContext tuptabcxt; /* memory context of result table */
329-
uint64 alloced; /* # of alloced vals */
330-
uint64 free; /* # of free vals */
331331
slist_node next; /* link for internal bookkeeping */
332332
SubTransactionId subid; /* subxact in which tuptable was created */
333333
} SPITupleTable;
334334
</programlisting>
335-
<structfield>vals</structfield> and <structfield>tupdesc</structfield> can
336-
be used by SPI callers, the remaining fields are internal.
337-
<structfield>vals</structfield> is an array of pointers to rows. (The number
338-
of valid entries is given by <varname>SPI_processed</varname>.)
335+
<structfield>tupdesc</structfield>,
336+
<structfield>vals</structfield>, and
337+
<structfield>numvals</structfield>
338+
can be used by SPI callers; the remaining fields are internal.
339+
<structfield>vals</structfield> is an array of pointers to rows.
340+
The number of rows is given by <structfield>numvals</structfield>
341+
(for somewhat historical reasons, this count is also returned
342+
in <varname>SPI_processed</varname>).
339343
<structfield>tupdesc</structfield> is a row descriptor which you can pass to
340344
SPI functions dealing with rows.
341345
</para>
@@ -4631,12 +4635,12 @@ execq(PG_FUNCTION_ARGS)
46314635
*/
46324636
if (ret &gt; 0 &amp;&amp; SPI_tuptable != NULL)
46334637
{
4634-
TupleDesc tupdesc = SPI_tuptable-&gt;tupdesc;
46354638
SPITupleTable *tuptable = SPI_tuptable;
4639+
TupleDesc tupdesc = tuptable-&gt;tupdesc;
46364640
char buf[8192];
46374641
uint64 j;
46384642

4639-
for (j = 0; j &lt;proc; j++)
4643+
for (j = 0; j &lt;tuptable-&gt;numvals; j++)
46404644
{
46414645
HeapTuple tuple = tuptable-&gt;vals[j];
46424646
int i;

‎src/backend/executor/spi.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,8 +1872,9 @@ spi_dest_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
18721872
slist_push_head(&_SPI_current->tuptables,&tuptable->next);
18731873

18741874
/* set up initial allocations */
1875-
tuptable->alloced=tuptable->free=128;
1875+
tuptable->alloced=128;
18761876
tuptable->vals= (HeapTuple*)palloc(tuptable->alloced*sizeof(HeapTuple));
1877+
tuptable->numvals=0;
18771878
tuptable->tupdesc=CreateTupleDescCopy(typeinfo);
18781879

18791880
MemoryContextSwitchTo(oldcxt);
@@ -1899,18 +1900,18 @@ spi_printtup(TupleTableSlot *slot, DestReceiver *self)
18991900

19001901
oldcxt=MemoryContextSwitchTo(tuptable->tuptabcxt);
19011902

1902-
if (tuptable->free==0)
1903+
if (tuptable->numvals >=tuptable->alloced)
19031904
{
19041905
/* Double the size of the pointer array */
1905-
tuptable->free=tuptable->alloced;
1906-
tuptable->alloced+=tuptable->free;
1906+
uint64newalloced=tuptable->alloced*2;
1907+
19071908
tuptable->vals= (HeapTuple*)repalloc_huge(tuptable->vals,
1908-
tuptable->alloced*sizeof(HeapTuple));
1909+
newalloced*sizeof(HeapTuple));
1910+
tuptable->alloced=newalloced;
19091911
}
19101912

1911-
tuptable->vals[tuptable->alloced-tuptable->free]=
1912-
ExecCopySlotHeapTuple(slot);
1913-
(tuptable->free)--;
1913+
tuptable->vals[tuptable->numvals]=ExecCopySlotHeapTuple(slot);
1914+
(tuptable->numvals)++;
19141915

19151916
MemoryContextSwitchTo(oldcxt);
19161917

@@ -2324,8 +2325,7 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
23242325

23252326
/* Update "processed" if stmt returned tuples */
23262327
if (_SPI_current->tuptable)
2327-
_SPI_current->processed=_SPI_current->tuptable->alloced-
2328-
_SPI_current->tuptable->free;
2328+
_SPI_current->processed=_SPI_current->tuptable->numvals;
23292329

23302330
res=SPI_OK_UTILITY;
23312331

@@ -2694,7 +2694,7 @@ _SPI_checktuples(void)
26942694

26952695
if (tuptable==NULL)/* spi_dest_startup was not called */
26962696
failed= true;
2697-
elseif (processed!=(tuptable->alloced-tuptable->free))
2697+
elseif (processed!=tuptable->numvals)
26982698
failed= true;
26992699

27002700
returnfailed;

‎src/include/executor/spi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ typedef struct SPITupleTable
2424
/* Public members */
2525
TupleDesctupdesc;/* tuple descriptor */
2626
HeapTuple*vals;/* array of tuples */
27+
uint64numvals;/* number of valid tuples */
2728

2829
/* Private members, not intended for external callers */
30+
uint64alloced;/* allocated length of vals array */
2931
MemoryContexttuptabcxt;/* memory context of result table */
30-
uint64alloced;/* # of alloced vals */
31-
uint64free;/* # of free vals */
3232
slist_nodenext;/* link for internal bookkeeping */
3333
SubTransactionIdsubid;/* subxact in which tuptable was created */
3434
}SPITupleTable;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp