Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vQrnV-001SwI-2T for pgsql-performance@arkaria.postgresql.org; Wed, 03 Dec 2025 18:41:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vQrnU-00FRoS-2h for pgsql-performance@arkaria.postgresql.org; Wed, 03 Dec 2025 18:41:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vQrnU-00FRoJ-1i for pgsql-performance@lists.postgresql.org; Wed, 03 Dec 2025 18:41:52 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vQrnS-002xy0-17 for pgsql-performance@lists.postgresql.org; Wed, 03 Dec 2025 18:41:52 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 5B3Ifmn6689687; Wed, 3 Dec 2025 13:41:48 -0500 From: Tom Lane To: Tim Fors cc: pgsql-performance@lists.postgresql.org Subject: Re: Client-server communication for FETCH In-reply-to: References: Comments: In-reply-to Tim Fors message dated "Wed, 03 Dec 2025 13:02:40 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <689685.1764787308.1@sss.pgh.pa.us> Date: Wed, 03 Dec 2025 13:41:48 -0500 Message-ID: <689686.1764787308@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Tim Fors writes: > 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? It's sending one row at a time. You could improve that by asking for more than one row ("FETCH n" not just "FETCH"), but you'd have to iterate over the rows returned by each command. Another alternative is to forget about using a cursor, tell the server to just execute the query, but use libpq's "chunked results" mode to process rows before all of the query result has arrived. See https://www.postgresql.org/docs/current/libpq-single-row-mode.html > 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. Don't know where you read that, but it's not true of libpq. There might be other Postgres client libraries that can do that. regards, tom lane