SPI_cursor_fetch Memory Issue

From: Wu Ivy <ivywuyzl(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: SPI_cursor_fetch Memory Issue
Date: 2018-08-21 23:33:41
Message-ID: CAH405oB_2vX7KbEOvQKt7==-aiaVb=nP66E7+oeTyioaN9eGKg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,

My goal is to get data from the Postgres table into a C-function to be
processed. Since the table can be very large, I only want to process one
chunk of data per time to keep the memory stable.

To achieve this, I use SPI cursor(
https://www.postgresql.org/docs/current/static/spi-spi-cursor-fetch.html)
to fetch row chunks from the table. Following is the pseudocode of my
program:

//Prepare the cursor

SPI_connect();

SPIPlanPtr SPIplan = SPI_prepare_cursor(command, 0, NULL, 0);

Portal cursor= SPI_cursor_open(NULL, SPIplan, NULL, NULL, true);

// Fetch 500 rows per time from cursor

SPI_cursor_fetch(cursor, true, 500);

int row_returned= SPI_processed;

while(row_returned != 0){

SPITupleTable *tuptable = SPI_tuptable;

// …

// Processing data: write data to a local file…

// …

SPI_freetuptable(tuptable);

// fetch next 500 rows

SPI_cursor_fetch(cursor, true, 500);

row_returned= SPI_processed;

}

SPI_cursor_close(cursor);

SPI_finish();

From my understanding, cursor points at the entire result rows on heap.
After fetching 500 rows, SPI_tuptable saves information of the row set.
When read from SPI_tuptable, memory increases.

After finishing process one chunk, I freed it by calling SPI_freetuptable(
) before next fetch. I expected the memory to be constant through out the
program, However, the actual memory kept increasing when I run the program.
Can anyone tell me why is it happening?

Thanks in advance!

Best,

Ivy

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2018-08-22 06:42:52 Re: Linker errors while creating a PostgreSQL C extension function.
Previous Message TalGloz 2018-08-21 18:31:54 Re: Linker errors while creating a PostgreSQL C extension function.