Re: psql's FETCH_COUNT (cursor) is not being respected for CTEs
Daniel Verite <daniel@manitou-mail.org>
From: "Daniel Verite" <daniel@manitou-mail.org>
To: "Tom Lane" <tgl@sss.pgh.pa.us>
Cc: Robert Haas <robertmhaas@gmail.com>,
Jakub Wartak <jakub.wartak@enterprisedb.com>,
PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2023-01-12T12:27:32Z
Lists: pgsql-hackers
Attachments
- psql-fetchcount-single-row-mode.diff (text/x-patch) patch
Tom Lane wrote: > I agree that it seems like a good idea to try. > There will be more per-row overhead, but the increase in flexibility > is likely to justify that. Here's a POC patch implementing row-by-row fetching. If it wasn't for the per-row overhead, we could probably get rid of ExecQueryUsingCursor() and use row-by-row fetches whenever FETCH_COUNT is set, independently of the form of the query. However the difference in processing time seems to be substantial: on some quick tests with FETCH_COUNT=10000, I'm seeing almost a 1.5x increase on large datasets. I assume it's the cost of more allocations. I would have hoped that avoiding the FETCH queries and associated round-trips with the cursor method would compensate for that, but it doesn't appear to be the case, at least with a fast local connection. So in this patch, psql still uses the cursor method if the query starts with "select", and falls back to the row-by-row in the main code (ExecQueryAndProcessResults) otherwise. Anyway it solves the main issue of the over-consumption of memory for CTE and update/insert queries returning large resultsets. Best regards, -- Daniel Vérité https://postgresql.verite.pro/ Twitter: @DanielVerite
Commits
-
Further review for re-implementation of psql's FETCH_COUNT feature.
- c21d4c416ad6 17.0 landed
-
Re-implement psql's FETCH_COUNT feature atop libpq's chunked mode.
- 90f5178211cd 17.0 landed
-
Support retrieval of results in chunks with libpq.
- 4643a2b265e9 17.0 landed
-
Attempt to fix newly added Memoize regression test
- a3a836fb5e51 17.0 cited