Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 1EDA5633485 for ; Wed, 26 Aug 2009 06:02:05 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 04756-09 for ; Wed, 26 Aug 2009 09:01:55 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f216.google.com (mail-ew0-f216.google.com [209.85.219.216]) by mail.postgresql.org (Postfix) with ESMTP id 3E50E6332F3 for ; Wed, 26 Aug 2009 06:01:55 -0300 (ADT) Received: by ewy12 with SMTP id 12so4541398ewy.24 for ; Wed, 26 Aug 2009 02:01:53 -0700 (PDT) MIME-Version: 1.0 Received: by 10.210.135.20 with SMTP id i20mr7897861ebd.18.1251277313397; Wed, 26 Aug 2009 02:01:53 -0700 (PDT) In-Reply-To: <4A946B7A.60408@opencloud.com> References: <895e58dd0908241736q3575cdc2v461d879c5b5f9dcb@mail.gmail.com> <4A9334C4.2060503@opencloud.com> <895e58dd0908250815m1a8aada0qc9d3f0bde9c7a303@mail.gmail.com> <895e58dd0908250951m1aa4baabnf1695c9e681aa278@mail.gmail.com> <4A946B7A.60408@opencloud.com> Date: Wed, 26 Aug 2009 02:01:51 -0700 Message-ID: <895e58dd0908260201g59762012ka2349eada052ab6d@mail.gmail.com> Subject: Re: PGStream synchronization From: Maciek Sakrejda To: Oliver Jowett Cc: pgsql-jdbc@postgresql.org Content-Type: multipart/mixed; boundary=0015174bef6ac7d707047207b328 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/61 X-Sequence-Number: 19910 --0015174bef6ac7d707047207b328 Content-Type: text/plain; charset=ISO-8859-1 > Right, so long as you get the atomicity correct I think that will work. > This will probably require more finegrained synchronization since > currently there's effectively one big lock that's held for the entire > duration of the query, which doesn't work so well here if you want > close() to interrupt any ongoing operation in a timely fashion. I wasn't quite sure why this would impact regular (non-COPY) queries, but after poking around in the source some more, I see what you are saying. We don't really have a good message-level synchronization point other than synchronizing on QueryExecutorImpl (i.e., its own synchronized methods), but that will lead to blocking indefinitely on close(). This is good enough for most communication since all the relevant methods in QueryExecutorImpl are synchronized anyway, but stop() gets around this to avoid blocking on all the other connection activity before shutdown. I'm a little wary of introducing message-level synchronization since (a) it touches a *lot* of core code and (b) there might be a non-negligible performance impact. I'm not really sure if there's another clean approach, though. I'm attaching a patch that takes another dirty approach, but other than the fact that it seems to work, I don't really like it at all. The basic idea is to cancel whatever you're doing before closing the connection: if there's an active copy, have the executor send a CopyFail; otherwise, have the ProtocolConnectionImpl request a query cancellation. Then close the connection without fear of blocking (although this should probably be synchronized with QueryExecutorImpl since, theoretically, we could have close() trigger a cancel and then another thread could start another query before we actually send the Terminate message on PGStream). However, this seems like a hack. QUERY CANCELED is not really the right error for when a connection is shutting down while your query is running. It should really be some sort of CONNECTION EXCEPTION, and we should just run into that automatically when we send the Terminate message, but for that, we're back to protocol message-level synchronization. If we do want to synchronize at the message level, is there a reasonable way to gauge the performance impact of this change? I.e., is there a standard pgsql-jdbc performance suite for measuring relative performance changes? -- Maciek Sakrejda | Software Engineer | Truviso (650) 242-3500 Main (650) 242-3501 F msakrejda@truviso.com www.truviso.com --0015174bef6ac7d707047207b328 Content-Type: text/x-patch; charset=US-ASCII; name="close.diff" Content-Disposition: attachment; filename="close.diff" Content-Transfer-Encoding: base64 X-Attachment-Id: f_fytsvvoj0 ZGlmZiAtLWdpdCBhL29yZy9wb3N0Z3Jlc3FsL2NvcmUvdjMvUHJvdG9jb2xDb25uZWN0aW9uSW1w bC5qYXZhIGIvb3JnL3Bvc3RncmVzcWwvY29yZS92My9Qcm90b2NvbENvbm5lY3Rpb25JbXBsLmph dmEKaW5kZXggNGVjYWM2Yy4uNzhlMWJjYSAxMDA2NDQKLS0tIGEvb3JnL3B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