Re: libpq, PQexecPrepared, data size sent to FE vs. FETCH_COUNT

Lists: pgsql-hackers
From: Alex Goncharov <alex-goncharov(at)comcast(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Alex Goncharov <alex-goncharov(at)comcast(dot)net>
Subject: libpq, PQexecPrepared, data size sent to FE vs. FETCH_COUNT
Date: 2010-05-24 16:25:18
Message-ID: E1OGaSs-000DrQ-AH@daland.home
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I have some libpq-using application code, in which fetching the data
follows this logic (after a statement has been prepared):

----------------------------------------

PQexecPrepared(pg_result, pg_conn, pg_statement_name, input_param_cnt,
param_values, param_lengths, param_formats, result_format);

PQntuples(&rows_in_result, pg_result);

/* The application provides storage so that I can pass a certain number of rows
* (rows_to_pass_up) to the caller, and I repeat the following loop until
* many rows_to_pass_up cover all the rows_in_result (pg_row_num_base keeps the track
* of where I am in the process. */

for (int row_idx = 0; row_idx < rows_to_pass_up; ++row_idx) {
const int pg_row_number = row_idx + pg_row_num_base;

for (int pg_column_number = 0; pg_column_number < result_column_cnt_ ++pg_column_number) {
PQgetvalue(&value, pg_result, pg_row_number, pg_column_number);
PQgetlength(&length, pg_result, pg_row_number, pg_column_number);
}
}

----------------------------------------

My question is: am I doing the right thing from the "data size being
passed from BE to FE" perspective?

The code in `bin/psql' relies on the value of the FETCH_COUNT
parameter to build an appropriate

fetch forward FETCH_COUNT from _psql_cursor

command.

No equivalent of FETCH_COUNT is available at the libpq level, so I
assume that the interface I am using is smart enough not to send
gigabytes of data to FE.

Is that right? Is the logic I am using safe and good?

Where does the result set (GBs of data) reside after I call
PQexecPrepared? On BE, I hope?

Thanks,

-- Alex -- alex-goncharov(at)comcast(dot)net --


From: Alex Goncharov <alex-goncharov(at)comcast(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: libpq, PQexecPrepared, data size sent to FE vs. FETCH_COUNT
Date: 2010-05-25 11:35:34
Message-ID: E1OGsQ2-000FHM-FG@daland.home
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

,--- I/Alex (Mon, 24 May 2010 12:25:18 -0400) ----*
| No equivalent of FETCH_COUNT is available at the libpq level, so I
| assume that the interface I am using is smart enough not to send
| gigabytes of data to FE.
|
| Where does the result set (GBs of data) reside after I call
| PQexecPrepared? On BE, I hope?

Sorry for asking again...

No sarcasm meant: is there no straightforward answer here? Or nobody
is certain? Or a wrong list?

Thanks,

-- Alex -- alex-goncharov(at)comcast(dot)net --


From: Abhijit Menon-Sen <ams(at)toroid(dot)org>
To: Alex Goncharov <alex-goncharov(at)comcast(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: libpq, PQexecPrepared, data size sent to FE vs. FETCH_COUNT
Date: 2010-05-25 11:56:18
Message-ID: 20100525115618.GA929@toroid.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

At 2010-05-25 07:35:34 -0400, alex-goncharov(at)comcast(dot)net wrote:
>
> | Where does the result set (GBs of data) reside after I call
> | PQexecPrepared? On BE, I hope?

Unless you explicitly declare and fetch from an SQL-level cursor, your
many GBs of data are going to be transmitted to libpq, which will eat
lots of memory. (The wire protocol does have something like cursors,
but libpq does not use them, it retrieves the entire result set.)

-- ams


From: Yeb Havinga <yebhavinga(at)gmail(dot)com>
To: Alex Goncharov <alex-goncharov(at)comcast(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: libpq, PQexecPrepared, data size sent to FE vs. FETCH_COUNT
Date: 2010-05-25 12:08:51
Message-ID: 4BFBBDD3.5070509@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alex Goncharov wrote:
> ,--- I/Alex (Mon, 24 May 2010 12:25:18 -0400) ----*
> | No equivalent of FETCH_COUNT is available at the libpq level, so I
> | assume that the interface I am using is smart enough not to send
> | gigabytes of data to FE.
> |
> | Where does the result set (GBs of data) reside after I call
> | PQexecPrepared? On BE, I hope?
>
> Sorry for asking again...
>
> No sarcasm meant: is there no straightforward answer here? Or nobody
> is certain? Or a wrong list?
>
The straighforward answer is that the libpq frontend c-library does not
support something like the JDBC client's setFetchSize.

The GBs of data are gathered at the site of the libpq client (pgresult
object gathered/allocated while consuming result input from backend).

regards,
Yeb Havinga


From: Alex Goncharov <alex-goncharov(at)comcast(dot)net>
To: Abhijit Menon-Sen <ams(at)toroid(dot)org>, Yeb Havinga <yebhavinga(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: libpq, PQexecPrepared, data size sent to FE vs. FETCH_COUNT
Date: 2010-05-25 13:17:06
Message-ID: E1OGu0I-000FOv-7b@daland.home
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

,--- Abhijit Menon-Sen (Tue, 25 May 2010 17:26:18 +0530) ----*
| Unless you explicitly declare and fetch from an SQL-level cursor, your
| many GBs of data are going to be transmitted to libpq, which will eat
| lots of memory. (The wire protocol does have something like cursors,
| but libpq does not use them, it retrieves the entire result set.)
,--- Yeb Havinga (Tue, 25 May 2010 14:08:51 +0200) ----*
| The GBs of data are gathered at the site of the libpq client (pgresult
| object gathered/allocated while consuming result input from backend).
`------------------------------------------------------*

Thank you very much!

-- Alex -- alex-goncharov(at)comcast(dot)net --


From: Andrew Chernow <ac(at)esilo(dot)com>
To: Alex Goncharov <alex-goncharov(at)comcast(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: libpq, PQexecPrepared, data size sent to FE vs. FETCH_COUNT
Date: 2010-05-25 13:20:55
Message-ID: 4BFBCEB7.7000209@esilo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 05/25/2010 07:35 AM, Alex Goncharov wrote:
> ,--- I/Alex (Mon, 24 May 2010 12:25:18 -0400) ----*
> | No equivalent of FETCH_COUNT is available at the libpq level, so I
> | assume that the interface I am using is smart enough not to send
> | gigabytes of data to FE.
> |
> | Where does the result set (GBs of data) reside after I call
> | PQexecPrepared? On BE, I hope?
>
> Sorry for asking again...
>
> No sarcasm meant: is there no straightforward answer here? Or nobody
> is certain? Or a wrong list?
>

Issue multiple queries and make use of LIMIT/OFFSET. You'll have to go
manual on this one.

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Alex Goncharov <alex-goncharov(at)comcast(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: libpq, PQexecPrepared, data size sent to FE vs. FETCH_COUNT
Date: 2010-05-25 14:47:42
Message-ID: 4BFBE30E.6070305@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alex Goncharov wrote:
> ,--- I/Alex (Mon, 24 May 2010 12:25:18 -0400) ----*
> | No equivalent of FETCH_COUNT is available at the libpq level, so I
> | assume that the interface I am using is smart enough not to send
> | gigabytes of data to FE.
> |
> | Where does the result set (GBs of data) reside after I call
> | PQexecPrepared? On BE, I hope?
>
> Sorry for asking again...
>
> No sarcasm meant: is there no straightforward answer here? Or nobody
> is certain? Or a wrong list?
>
>
>

You have been given the answer. Please re-read the replies, e.g. the one
from Abhijit Menon-Sen.

The data is saved on the client side before the call returns. If that
uses too much memory, use a cursor.

cheers

andrew


From: Giles Lean <giles(dot)lean(at)pobox(dot)com>
To: Abhijit Menon-Sen <ams(at)toroid(dot)org>
Cc: Alex Goncharov <alex-goncharov(at)comcast(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: libpq, PQexecPrepared, data size sent to FE vs. FETCH_COUNT
Date: 2010-05-26 13:34:14
Message-ID: 20100526133414.5554.qmail@sapphire.netherstone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Abhijit Menon-Sen <ams(at)toroid(dot)org> wrote:

> Unless you explicitly declare and fetch from an SQL-level cursor, your
> many GBs of data are going to be transmitted to libpq, which will eat
> lots of memory. (The wire protocol does have something like cursors,
> but libpq does not use them, it retrieves the entire result set.)

Sounds like a project. Anyone got any suggestions about
semantics and function names? (Assuming that this can be done
without causing more problems on the backend; I'd rather one
frontend client get messed up than mess up the server if
someone makes a query like that.)

I'm not exactly volunteering to work on something like this
(my TODO list is a trifle long) but I'm working on a native Go
language interface for PostgreSQL presently (influced by but
not an exact clone of libpq) so it's perhaps something I could
do if I get free time in future.

Giles