Memory allocation in spi_printtup() - Mailing list pgsql-hackers

From Neil Conway
Subject Memory allocation in spi_printtup()
Date
Msg-id CAOW5sYZ-xqso3a++xG0sVVikMCt0g3Y_aOgoL5Np_9j7QcdB4A@mail.gmail.com
Whole thread Raw
Responses Re: Memory allocation in spi_printtup()  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
spi_printtup() has the following code (spi.c:1798):

        if (tuptable->free == 0)
        {
                tuptable->free = 256;
                tuptable->alloced += tuptable->free;
                tuptable->vals = (HeapTuple *) repalloc(tuptable->vals,

   tuptable->alloced * sizeof(HeapTuple));
        }

i.e., it grows the size of the tuptable by a fixed amount when it runs
out of space. That seems odd; doubling the size of the table would be
more standard. Does anyone know if there is a rationale for this
behavior?

Attached is a one-liner to double the size of the table when space is
exhausted. Constructing a simple test case in which a large result is
materialized via SPI_execute() (e.g., by passing two large queries to
crosstab() from contrib/tablefunc), this change reduces the time
required to exceed the palloc size limit from ~300 seconds to ~93
seconds on my laptop.

Of course, using SPI_execute() rather than cursors for queries with
large result sets is not a great idea, but there is demonstrably code
that does this (e.g., contrib/tablefunc -- I'll send a patch for that
shortly).

Neil

Attachment

pgsql-hackers by date:

Previous
From: Jim Nasby
Date:
Subject: Error message with plpgsql CONTINUE
Next
From: Heikki Linnakangas
Date:
Subject: Re: Configure checks and optimizations/crc32 tests