Skip to content

Commit 7c57449

Browse files
committed
Avoid O(N^2) behavior when enlarging SPI tuple table in spi_printtup().
For no obvious reason, spi_printtup() was coded to enlarge the tuple pointer table by just 256 slots at a time, rather than doubling the size at each reallocation, as is our usual habit. For very large SPI results, this makes for O(N^2) time spent in repalloc(), which of course soon comes to dominate the runtime. Use the standard doubling approach instead. This is a longstanding performance bug, so back-patch to all active branches. Neil Conway
1 parent e41718f commit 7c57449

File tree

1 file changed

+2
-1
lines changed
  • src/backend/executor

1 file changed

+2
-1
lines changed

src/backend/executor/spi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,8 @@ spi_printtup(TupleTableSlot *slot, DestReceiver *self)
16221622

16231623
if (tuptable->free == 0)
16241624
{
1625-
tuptable->free = 256;
1625+
/* Double the size of the pointer array */
1626+
tuptable->free = tuptable->alloced;
16261627
tuptable->alloced += tuptable->free;
16271628
tuptable->vals = (HeapTuple *) repalloc(tuptable->vals,
16281629
tuptable->alloced * sizeof(HeapTuple));

0 commit comments

Comments
 (0)