From: Maciek Sakrejda <msakrejda@truviso.com>
To: Oliver Jowett <oliver@opencloud.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: PGStream synchronization
Date: Wed, 26 Aug 2009 02:01:51 -0700
Message-ID: <895e58dd0908260201g59762012ka2349eada052ab6d@mail.gmail.com> (raw)
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>
> 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
Attachments:close.diff (text/x-patch, 1.5K) downloaddiff --git a/org/postgresql/core/v3/ProtocolConnectionImpl.java b/org/postgresql/core/v3/ProtocolConnectionImpl.javaindex 4ecac6c..78e1bca 100644--- a/org/postgresql/core/v3/ProtocolConnectionImpl.java+++ b/org/postgresql/core/v3/ProtocolConnectionImpl.java@@ -126,7 +126,16 @@ class ProtocolConnectionImpl implements ProtocolConnection {
public void close() {
if (closed)
return ;
-+ try+ {+ executor.tryCurrentCopyCancel();+ sendQueryCancel();+ }+ catch (SQLException e)+ {+ if (logger.logDebug())+ logger.debug("Discarding SQLException on close:", e);+ }
try
{
if (logger.logDebug())
diff --git a/org/postgresql/core/v3/QueryExecutorImpl.java b/org/postgresql/core/v3/QueryExecutorImpl.javaindex 37a57f5..ab2c953 100644--- a/org/postgresql/core/v3/QueryExecutorImpl.java+++ b/org/postgresql/core/v3/QueryExecutorImpl.java@@ -736,6 +736,12 @@ public class QueryExecutorImpl implements QueryExecutor {
op.init(this, rowFormat, fieldFormats);
}
+ void tryCurrentCopyCancel() throws SQLException {+ if (lockedFor instanceof CopyOperation) {+ cancelCopy((CopyOperationImpl) lockedFor);+ }+ }+
/**
* Finishes a copy operation and unlocks connection discarding any exchanged data.
* @param op the copy operation presumably currently holding lock on this connection
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: msakrejda@truviso.com, oliver@opencloud.com
Subject: Re: PGStream synchronization
In-Reply-To: <895e58dd0908260201g59762012ka2349eada052ab6d@mail.gmail.com>
References: <895e58dd0908241736q3575cdc2v461d879c5b5f9dcb@mail.gmail.com> <4A9334C4.2060503@opencloud.com> <895e58dd0908250815m1a8aada0qc9d3f0bde9c7a303@mail.gmail.com> <895e58dd0908250951m1aa4baabnf1695c9e681aa278@mail.gmail.com> <4A946B7A.60408@opencloud.com> <895e58dd0908260201g59762012ka2349eada052ab6d@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox