Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XaQlj-0002fY-OE for pgsql-jdbc@arkaria.postgresql.org; Sat, 04 Oct 2014 14:57:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XaQli-0005Ec-ML for pgsql-jdbc@arkaria.postgresql.org; Sat, 04 Oct 2014 14:57:10 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XaQlg-0005ET-Ll for pgsql-jdbc@postgresql.org; Sat, 04 Oct 2014 14:57:08 +0000 Received: from mout.kundenserver.de ([212.227.17.13]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XaQlZ-0006o6-0J for pgsql-jdbc@postgresql.org; Sat, 04 Oct 2014 14:57:06 +0000 Received: from ayaki.localdomain (106-68-71-91.dyn.iinet.net.au [106.68.71.91]) by mrelayeu.kundenserver.de (node=mreue101) with ESMTP (Nemesis) id 0MbhXp-1XtTkV3gVp-00Iz37; Sat, 04 Oct 2014 16:56:45 +0200 Message-ID: <54300AA6.3040309@2ndquadrant.com> Date: Sat, 04 Oct 2014 22:56:38 +0800 From: Craig Ringer Organization: 2nd Quadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.7.0 MIME-Version: 1.0 To: Vitalii Tymchyshyn , Tom Lane CC: John R Pierce , PG-JDBC Mailing List Subject: Re: Disk buffering of resultsets References: <541E7FC3.3000201@2ndquadrant.com> <3F6D0B4E-2FA9-42D0-8C2C-DC68E6D0D641@gmail.com> <54205F94.9010102@hogranch.com> <542091F0.10706@hogranch.com> <1321.1411433175@sss.pgh.pa.us> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:XcxoFtq7wOP+/xJCgG7HMDBef4/8+G1VaYHKKti3E3V 1qgp2Vx62Lh5xNJ1zg1b+ZHzBoFWjIz9iLsuGD4FigY92Y+LFK F3je9fm/YrLJ8uDHlESST1EPg8+KlSHxrxSjTfh1QiaL+L3SDT Odgu4n9gbac2crZQjR1OdvOt6VItPy23FFm281iBcf3hQCy3bX QOw3qrzECNs9Z8f67yxoaIRTEmq2A+Lh4gjwfKw/8cBm8n3GJb uxZ1KAKc8+wIW3krLvCpumCT+AOPbndxnI/tHAzAx8V9TOiOw0 SGsFBz6oOegAtdw4AuWTuP57wlheWdcjxP5cWOjkpjggmHAIKo HJl92+yf9MVX8XsMbEvSa9KTiIhsDwDc8eFiS2ynk X-UI-Out-Filterresults: notjunk:1; X-Pg-Spam-Score: -2.1 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org On 09/23/2014 09:39 AM, Vitalii Tymchyshyn wrote: > Some more thoughts: > 1) is it really not possible to stream results in autocommit mode? The > only problem I can see is that connection can't be used (e.g. by other > statement objects) until after all the results are fetched. So what? In > many cases it's OK. It is possible to stream results in autocommit mode. You can't do so without an explicit transaction using SQL-level cursors (DECLARE and FETCH) unless you use "WITH HOLD" cursors, which have their own problems. But you can do so at the protocol level, so long as you don't send another query until you've consumed all the results you want or don't require any more results. For details, see the Execute message documentation in the v3 protocol, particularly the notes about the row-count limit. Basically, you send repeated Execute messages to fetch chunks of rows, until you don't get any more rows or don't want any more. Here's something I wrote on the topic recently, which is accurate to the best of my knowledge: http://stackoverflow.com/a/25907922/398670 > 2) another alternative to temp files are direct buffers. They are not > exactly the same, but are not counted towards heap and can be swapped > out by system if needed. "Direct buffer" appears to referer to a Java NIO ByteBuffer. Right? They are indeed an alternative, something more like BigMemory. The number of options around are exactly why I think this needs to be a simple interface that you can plug implementations into. Start with a tempfile interface, but allow for future needs. > 3) For any disk saving I'd > - postpone it as long as possible (e.g. until after connection is used > by another statement) You can't really do that. You could fetch and flush results just before issuing another statement, as part of the execute call, but not afterwards. I think that's what you meant, though. This could lead to surprises where the execution of the next statement throws an error that's really from the prior statement, though. It also makes no sense, as when you execute a new statement, the resultset of the prior statement is automatically closed. So your app would have to have already fetched the whole resultset. If you're talking about adding support for resultsets that aren't auto-closed when the next statement is run, that's a different matter entirely to what we've discussed so far. It might be useful, but it's not the same as just spilling big results to disk. > - do as little as possible (may be by saving frames from network as is > with little decoding) That really won't work well. We have to read the stream and parse the messages. Otherwise we'll fail to see asynchronous notifications, error messages, etc. Not only that, PgJDBC doesn't have access to "network frames" anyway. It's working with a stream-oriented socket. The lowest level it can possibly work with is the byte stream. > - do it in background (a little conflicting to postponing, but more > thinking is needed). Return first fetchSize rows and start copying > network to disk in background thread. You don't want to go there. It's horribly complicated to work with background threads portably in the JDBC driver. For an example, see the recent discussion of Timer handling. Not only that, it's also pointless because there's only one TCP connection to work with. So while the "background" thread is saving the resultset, everything else that wants to use the connection has to wait anyway. > Anyway one needs to go through everything to issue next command, does > not he? Yes, per the protocol documentation. > The question is if all of this needs to be saved or simply > skipped (as soon as result set is closed). If we're fetching chunks of the resultset progressively, we can just close the resultset and close the server-side portal. I'm pretty sure this is already possible in PgJDBC (when you set a fetch size) though I'd need to write a test case and do some code reading to be totally sure. I think you might be trying to solve a different problem to what Denis is talking about. -- Craig Ringer http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc