Client-server communication for FETCH

From: Tim Fors <tim4stheenchanter(at)gmail(dot)com>
To: pgsql-performance(at)lists(dot)postgresql(dot)org
Subject: Client-server communication for FETCH
Date: 2025-12-03 18:02:40
Message-ID: CAOjiQ0C7NEV9ZBFxbM6KgXnzquaUY3MrN1x8vz_tv7uXac0UwQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hello community,

I have a simple C program using libpq V18 to FETCH one row at a time (see
below). Does the server send multiple rows to the client where they are
cached for satisfying the FETCH statements, or does it just send one row at
a time since that's all that each FETCH statement is asking for? If it's
sending multiple rows at a time, is there some way that I can observe this
e.g. some kind of trace info that would show it? Or where in the libpq
source would I look to see how the client is retrieving rows from the
server?

I have done extensive searching to try and find the definitive answer to
this. The searches indicate that libpq supports the concept of a
client-side cursor, where it has a cache of rows sent by the server and
uses that cache to perform each FETCH, but I'd like to be able to verify
whether this is true or not.

Thanks,

Tim

res = PQexec(conn, "DECLARE cur CURSOR FOR select * from table1");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("DECLARE CURSOR failed: %s", PQerrorMessage(conn));
PQclear(res);
exit(1);
}
PQclear(res);

res = PQexec(conn, "FETCH in cur");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
printf("FETCH failed: %s", PQerrorMessage(conn));
PQclear(res);
exit(1);
}
PQclear(res);

res = PQexec(conn, "FETCH in cur");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
printf("FETCH failed: %s", PQerrorMessage(conn));
PQclear(res);
exit(1);
}

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2025-12-03 18:41:48 Re: Client-server communication for FETCH
Previous Message Jim Jones 2025-12-03 13:44:16 Re: proposal: schema variables