Skip to content

Commit ea98924

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 59592ef commit ea98924

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
@@ -1763,7 +1763,8 @@ spi_printtup(TupleTableSlot *slot, DestReceiver *self)
17631763

17641764
if (tuptable->free == 0)
17651765
{
1766-
tuptable->free = 256;
1766+
/* Double the size of the pointer array */
1767+
tuptable->free = tuptable->alloced;
17671768
tuptable->alloced += tuptable->free;
17681769
tuptable->vals = (HeapTuple *) repalloc(tuptable->vals,
17691770
tuptable->alloced * sizeof(HeapTuple));

0 commit comments

Comments
 (0)