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.94.2) (envelope-from ) id 1tUtMo-003by8-0i for pgsql-general@arkaria.postgresql.org; Mon, 06 Jan 2025 20:06:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tUtMn-00CBBS-56 for pgsql-general@arkaria.postgresql.org; Mon, 06 Jan 2025 20:06:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tUtMm-00CBBK-R0 for pgsql-general@lists.postgresql.org; Mon, 06 Jan 2025 20:06:24 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tUtMk-000Cl6-1m for pgsql-general@lists.postgresql.org; Mon, 06 Jan 2025 20:06:23 +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 506K6Lha1335671; Mon, 6 Jan 2025 15:06:21 -0500 From: Tom Lane To: "Daniel Verite" cc: "Stijn Sanders" , pgsql-general@lists.postgresql.org Subject: Re: Will PQsetSingleRowMode get me results faster? In-reply-to: <140214eb-a8ce-4285-afdd-7770c5a9d274@manitou-mail.org> References: <140214eb-a8ce-4285-afdd-7770c5a9d274@manitou-mail.org> Comments: In-reply-to "Daniel Verite" message dated "Mon, 06 Jan 2025 20:25:26 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1335669.1736193981.1@sss.pgh.pa.us> Date: Mon, 06 Jan 2025 15:06:21 -0500 Message-ID: <1335670.1736193981@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Daniel Verite" writes: > Stijn Sanders wrote: >> From what I notice using LibPQ, it appears a query needs to complete >> before resulting data is being transferred to the client. Please >> correct me if I'm wrong. > No, the query does not need to complete. If you run something like > "select * from bigtable" in single-row or chunked mode, you should > see that the first results are typically available immediately to the > application, while the query is far from finished. > But it depends on the query plan. A lot of queries cannot produce any > result until the final stage of their execution. For these, you can't > expect any difference in how fast the first results are available > client-side. Right. But there's yet another moving part here: when creating a plan for a cursor query, the planner will give some preference (not infinite preference, but some) to plans that are expected to produce their first result row sooner. For example it might pick an indexscan plan on a suitably-ordered index over a seqscan-and-sort plan, even if the indexscan is estimated to take more time to run to completion. So in principle, you might get best results by defining your query with DECLARE CURSOR and then using PQsetSingleRowMode on the FETCH. But it'd really depend on the particular query whether this gives any benefit. regards, tom lane