Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cgAZp-00042D-U4 for pgsql-performance@arkaria.postgresql.org; Tue, 21 Feb 2017 13:33:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cgAZp-0000Lq-CP for pgsql-performance@arkaria.postgresql.org; Tue, 21 Feb 2017 13:33:57 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cgAYA-0006l7-33 for pgsql-performance@postgresql.org; Tue, 21 Feb 2017 13:32:14 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cgAY7-0002kg-P3 for pgsql-performance@postgresql.org; Tue, 21 Feb 2017 13:32:12 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v1LDW9V3017680; Tue, 21 Feb 2017 08:32:09 -0500 From: Tom Lane To: Mike Beaton cc: pgsql-performance@postgresql.org Subject: Re: Correct use of cursors for very large result sets in Postgres In-reply-to: References: <21063.1487349580@sss.pgh.pa.us> <26739.1487439829@sss.pgh.pa.us> Comments: In-reply-to Mike Beaton message dated "Sun, 19 Feb 2017 07:54:18 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <17678.1487683929.1@sss.pgh.pa.us> Date: Tue, 21 Feb 2017 08:32:09 -0500 Message-ID: <17679.1487683929@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Mike Beaton writes: > New TL;DR (I'm afraid): PostgreSQL is always generating a huge buffer file > on `FETCH ALL FROM CursorToHuge`. I poked into this and determined that it's happening because pquery.c executes FETCH statements the same as it does with any other tuple-returning utility statement, ie "run it to completion and put the results in a tuplestore, then send the tuplestore contents to the client". I think the main reason nobody worried about that being non-optimal was that we weren't expecting people to FETCH very large amounts of data in one go --- if you want the whole query result at once, why are you bothering with a cursor? This could probably be improved, but it would (I think) require inventing an additional PortalStrategy specifically for FETCH, and writing associated code paths in pquery.c. Don't know when/if someone might get excited enough about it to do that. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance