Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XoyOv-00013a-B1 for pgsql-jdbc@arkaria.postgresql.org; Thu, 13 Nov 2014 17:41:45 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XoyOu-0000D6-SG for pgsql-jdbc@arkaria.postgresql.org; Thu, 13 Nov 2014 17:41:44 +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 1XoyOu-0000D0-1h for pgsql-jdbc@postgresql.org; Thu, 13 Nov 2014 17:41:44 +0000 Received: from mail-pd0-x22f.google.com ([2607:f8b0:400e:c02::22f]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XoyOq-0005Jk-8g for pgsql-jdbc@postgresql.org; Thu, 13 Nov 2014 17:41:42 +0000 Received: by mail-pd0-f175.google.com with SMTP id y13so14910815pdi.6 for ; Thu, 13 Nov 2014 09:41:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=1ELFSIgM6LwHe1x5Vpw800SKGxso/Iip9wMTt55toFg=; b=KaNfJ4EU9Q0Z3Pqq34dg5Fa/Wbis80l1gXFHI0LK8M9wHSw3NtSCTePiH23k7Ak88n CNyjscT9iGEe2PI7QWiOhFT9nWZ8YO9ScgJCnmrpOF3YZz1ZIEtUBBajJ70rszBXbWr7 ZN2Cb3lkvaz2nU7EjsI5IYRhma+obUgj9DfMghfPVJzL81yAXPGJ5O8EoAAlYzQYAppz eLBoO9mAvzdC9dvWs8P9BT5q4UIbdrlHWInAK2JtBDCuCLACHst+jYIlTUkE4nJiCHhK iRdTP7yeoQX1bynlukf1xv0xZx02nJ/NBT37NL9lV0924Yhcbhw7YhQTz9rNAngA9ZCo jtTQ== X-Received: by 10.70.41.232 with SMTP id i8mr4156738pdl.21.1415900498893; Thu, 13 Nov 2014 09:41:38 -0800 (PST) Received: from [10.10.21.129] (otra.opentable.com. [199.16.144.7]) by mx.google.com with ESMTPSA id of9sm25432349pbc.6.2014.11.13.09.41.37 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Nov 2014 09:41:38 -0800 (PST) Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) Subject: Re: New significance of holdable result sets in Java 8 From: Steven Schlansker In-Reply-To: Date: Thu, 13 Nov 2014 09:41:36 -0800 Cc: "pgsql-jdbc@postgresql.org JDBC" Content-Transfer-Encoding: quoted-printable Message-Id: References: <771672CB-9567-4966-8CE9-9D84E4F858D8@gmail.com> <75C17D7A-95D9-471F-AF64-EFE83D971654@gmail.com> <5855156D-8010-4735-A63D-DC5DF5EABD27@gmail.com> <49F13D7D-3726-4482-B35A-2AD607E88EBF@gmail.com> To: Marko Topolnik X-Mailer: Apple Mail (2.1878.6) X-Pg-Spam-Score: -2.7 (--) 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 Nov 13, 2014, at 4:08 AM, Marko Topolnik wrot= e: > Yes, your approach makes perfect sense---as long as you don't care about = transactional semantics. >=20 > If the transaction boundary is moved all the way to the end of serving th= e response, the following happens: >=20 > 1. 200 OK status code reported, but transaction still not committed; > 2. transaction may roll back due to errors in transfer; > 3. thanks to buffering, the transfer may also fail _after_ the transactio= n has committed. >=20 > So, not only have you established quite messy and illogical transaction s= emantics for your client (for example, a typical onSuccess callback on the = HTTP request cannot assume the transaction succeeded); the client has no id= ea _in principle_ what happened to the transaction if transfer is aborted f= or any reason. >=20 > In an earlier incarnation of my project I did indeed have setup exactly a= s you describe it, but after realizing the above problems, I had to back of= f and revert to standard transaction boundaries. >=20 This is entirely correct. We decided that this was OK for our use case, as in general queries that re= turned enough data to be worth streaming were simple SELECTs which were not= expected to commingle with UPDATEs or otherwise promise any transactional = guarantees (other than the read was a consistent snapshot). We also changed our API such that after all results streamed, it emitted a = status report. e.g. HTTP 200 OK { "results" : [..., ...], "status": {"success": true} } A little janky, but it worked well for us. >=20 > On 12. stu. 2014., at 23:04, Steven Schlansker wrote: >=20 >>=20 >> On Nov 12, 2014, at 11:20 AM, Marko Topolnik = wrote: >>>=20 >>> I am aware of this approach; I even discuss it as a typical workaround = in my blog post [1]. The problem is, this completely dispenses with the det= ailed and complex setup present in the View layer: the response format can = be driven by the Accept HTTP header, encoding may be adapted to the request= , and so on. I did pretty much that in my Clojure projects because Clojure'= s ecosystem isn't as well-developed so roll-your-own solutions are the norm= . With Spring, however, you lose a great deal when you give your Controller= method access to the raw response. >>>=20 >>> I have already developed a Stream-based REST service along the lines I = discuss in the mentioned blog post and the advantages are quite clear to me= . It is a direction that Spring would probably like to move in as well. >>>=20 >>> [1] http://www.airpair.com/v1/posts/spring-streams-memory-efficiency >>> ... >>> You still seem to dictate the output format directly from the Controlle= r, the avoidance of which is motivating my approach. >>>=20 >>=20 >> Indeed, after reading your blog post, it looks like we have 95% the exac= t same solution. >>=20 >> The essential difference I propose is that instead of having a holdable = ResultSet (which requires PG-JDBC changes) >> you hold the Connection/Statement/ResultSet with transaction open for th= e duration. >>=20 >> This way the transaction stays open for the duration of your request. S= o you establish a stream all the way through >> from end client -> PG. >>=20 >> With your suggestion, a holdable ResultSet, you run into what Dave menti= oned: >>=20 >> http://www.postgresql.org/docs/9.3/static/sql-declare.html >>> In the current implementation, the rows represented by a held cursor ar= e copied into a temporary file or memory area so that they remain available= for subsequent transactions. >>=20 >> So in fact you are just materializing the result set server side. This = IMO defeats the entire purpose - you cannot begin streaming out results unt= il the materialize finishes (the holdable cursor will not be available unti= l said materialize is done, I believe? or at least the transaction will no= t commit until that point?) Therefore my proposal is significantly more ef= ficient and will never surprisingly materialize gigabytes of temporary disk= usage. >>=20 >> Makes more sense? >>=20 >=20 --=20 Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc