Skip to content

Commit fe939d9

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 22b9ce7 commit fe939d9

File tree

1 file changed

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

1 file changed

+2
-1
lines changed

src/backend/executor/spi.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,8 @@ spi_printtup(TupleTableSlot *slot, DestReceiver *self)
17691769

17701770
if (tuptable->free == 0)
17711771
{
1772-
tuptable->free = 256;
1772+
/* Double the size of the pointer array */
1773+
tuptable->free = tuptable->alloced;
17731774
tuptable->alloced += tuptable->free;
17741775
tuptable->vals = (HeapTuple *) repalloc(tuptable->vals,
17751776
tuptable->alloced * sizeof(HeapTuple));

0 commit comments

Comments
 (0)