pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedbinary patch problems
20+ messages / 9 participants
[nested] [flat]
* binary patch problems
@ 2011-09-15 22:10 Dave Cramer <pg@fastcrypt.com>
0 siblings, 1 reply; 20+ messages in thread
From: Dave Cramer @ 2011-09-15 22:10 UTC (permalink / raw)
To: pgsql-jdbc
I have applied to the current code and the tests fail with ResultSet
already closed. I can't replicate this in eclipse, only in the command
line ant test. My guess is that somehow result sets are being shared
across tests. Anyone shed some light on this ? The patch I applied is
at http://wiki.postgresql.org/wiki/JDBC-BinaryTransfer version 13
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-16 13:20 Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
parent: Dave Cramer <pg@fastcrypt.com>
0 siblings, 2 replies; 20+ messages in thread
From: Mikko Tiihonen @ 2011-09-16 13:20 UTC (permalink / raw)
To: pgsql-jdbc
On 09/16/2011 01:10 AM, Dave Cramer wrote:
> I have applied to the current code and the tests fail with ResultSet
> already closed. I can't replicate this in eclipse, only in the command
> line ant test. My guess is that somehow result sets are being shared
> across tests. Anyone shed some light on this ? The patch I applied is
> at http://wiki.postgresql.org/wiki/JDBC-BinaryTransfer version 13
> Dave Cramer
I've been lately trying to figure out the same problem.
In separate emails I just sent the refreshed set of patches broken into separate patches out for easier review.
The trace shows that for some reason the C_5 portal is closed while still processing the results.
(3) FE=> Execute(portal=C_5,limit=1)
(3) FE=> Sync
(3) <=BE DataRow(len=305)
(3) <=BE PortalSuspended
(3) <=BE ReadyForQuery(T)
(3) FE=> Execute(portal=C_5,limit=1)
(3) FE=> Sync
(3) <=BE DataRow(len=305)
(3) <=BE PortalSuspended
(3) <=BE ReadyForQuery(T)
(3) FE=> CloseStatement(S_2)
(3) FE=> ClosePortal(C_5)
(3) FE=> Execute(portal=C_5,limit=1)
(3) FE=> Sync
(3) <=BE CloseComplete
(3) <=BE CloseComplete
(3) <=BE ErrorMessage(ERROR: portal "C_5" does not exist
Location: File: postgres.c, Routine: exec_execute_message, Line: 1844
Server SQLState: 34000)
org.postgresql.util.PSQLException: ERROR: portal "C_5" does not exist
Location: File: postgres.c, Routine: exec_execute_message, Line: 1844
Server SQLState: 34000
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2158)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1887)
at org.postgresql.core.v3.QueryExecutorImpl.fetch(QueryExecutorImpl.java:2090)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.next(AbstractJdbc2ResultSet.java:1870)
at org.postgresql.test.jdbc2.DatabaseEncodingTest.testEncoding(DatabaseEncodingTest.java:149)
The C_5 portal is closed because it is in the openPortalCleanupQueue containing PhantomReferences. The trace shows that S_2 is also closed at the same
time, which is correct because that statement is no longer in use.
If I debugged it correctly it looks like the query statement is closed by finalizer during the test which in turn closes the relevant portals.
If I add stmt.close() to the end of the test there are no failures as it keeps the GC from killing of the statement during the test.
But why I only seem to get it when running with binary transfer patches I do not know. So there must be something in the patches that I cannot spot.
-Mikko
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-16 13:40 Dave Cramer <pg@fastcrypt.com>
parent: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
1 sibling, 1 reply; 20+ messages in thread
From: Dave Cramer @ 2011-09-16 13:40 UTC (permalink / raw)
To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: pgsql-jdbc
Interestingly enough I only get that particular problem when I run it
directly after an ant clean, if I run subsequent tests then I get
result set is already closed errors.
I applied v13 patch successfully. Is there a necessity to use the
patches you have just sent ?
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Fri, Sep 16, 2011 at 8:20 AM, Mikko Tiihonen
<mikko.tiihonen@nitorcreations.com> wrote:
> On 09/16/2011 01:10 AM, Dave Cramer wrote:
>>
>> I have applied to the current code and the tests fail with ResultSet
>> already closed. I can't replicate this in eclipse, only in the command
>> line ant test. My guess is that somehow result sets are being shared
>> across tests. Anyone shed some light on this ? The patch I applied is
>> at http://wiki.postgresql.org/wiki/JDBC-BinaryTransfer version 13
>> Dave Cramer
>
> I've been lately trying to figure out the same problem.
> In separate emails I just sent the refreshed set of patches broken into
> separate patches out for easier review.
>
> The trace shows that for some reason the C_5 portal is closed while still
> processing the results.
>
> (3) FE=> Execute(portal=C_5,limit=1)
> (3) FE=> Sync
> (3) <=BE DataRow(len=305)
> (3) <=BE PortalSuspended
> (3) <=BE ReadyForQuery(T)
> (3) FE=> Execute(portal=C_5,limit=1)
> (3) FE=> Sync
> (3) <=BE DataRow(len=305)
> (3) <=BE PortalSuspended
> (3) <=BE ReadyForQuery(T)
> (3) FE=> CloseStatement(S_2)
> (3) FE=> ClosePortal(C_5)
> (3) FE=> Execute(portal=C_5,limit=1)
> (3) FE=> Sync
> (3) <=BE CloseComplete
> (3) <=BE CloseComplete
> (3) <=BE ErrorMessage(ERROR: portal "C_5" does not exist
> Location: File: postgres.c, Routine: exec_execute_message, Line: 1844
> Server SQLState: 34000)
> org.postgresql.util.PSQLException: ERROR: portal "C_5" does not exist
> Location: File: postgres.c, Routine: exec_execute_message, Line: 1844
> Server SQLState: 34000
> at
> org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2158)
> at
> org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1887)
> at
> org.postgresql.core.v3.QueryExecutorImpl.fetch(QueryExecutorImpl.java:2090)
> at
> org.postgresql.jdbc2.AbstractJdbc2ResultSet.next(AbstractJdbc2ResultSet.java:1870)
> at
> org.postgresql.test.jdbc2.DatabaseEncodingTest.testEncoding(DatabaseEncodingTest.java:149)
>
> The C_5 portal is closed because it is in the openPortalCleanupQueue
> containing PhantomReferences. The trace shows that S_2 is also closed at the
> same
> time, which is correct because that statement is no longer in use.
>
> If I debugged it correctly it looks like the query statement is closed by
> finalizer during the test which in turn closes the relevant portals.
> If I add stmt.close() to the end of the test there are no failures as it
> keeps the GC from killing of the statement during the test.
>
> But why I only seem to get it when running with binary transfer patches I do
> not know. So there must be something in the patches that I cannot spot.
>
> -Mikko
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-16 23:14 Oliver Jowett <oliver@opencloud.com>
parent: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
1 sibling, 2 replies; 20+ messages in thread
From: Oliver Jowett @ 2011-09-16 23:14 UTC (permalink / raw)
To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: pgsql-jdbc@postgresql.org, Dave Cramer <pg@fastcrypt.com>
On 17 September 2011 01:20, Mikko Tiihonen
<mikko.tiihonen@nitorcreations.com> wrote:
> If I debugged it correctly it looks like the query statement is closed by
> finalizer during the test which in turn closes the relevant portals.
> If I add stmt.close() to the end of the test there are no failures as it
> keeps the GC from killing of the statement during the test.
>
> But why I only seem to get it when running with binary transfer patches I do
> not know. So there must be something in the patches that I cannot spot.
(I am halfway through my first coffee of the day. YMMV)
The test code holds a reference to the returned ResultSet, but doesn't
keep a reference to the Statement itself after calling executeQuery().
Normally this isn't a problem because there is a strong ref from a
ResultSet to its owning Statement, so while you are using a particular
ResultSet the creating statement remains reachable.
The problem seems to be introduced in the patch to executeQuery if
(ForceBinaryTransfers). This delegates to a new PreparedStatement
("inner PreparedStatement") and splices its resultset ("inner
resultset") back into the surrounding Statement ("outer Statement")
and returns that.
However the inner ResultSet has the wrong parent Statement for that
context (ResultSet.getStatement() will return the "inner
PreparedStatement" not the "outer Statement") - which is a separate
problem, but also causes the GC problems as well, as now there's no
strong ref from the returned ResultSet to the Statement that the
client created it via.
So in the test code we end up with:
(a) a strong ref to the inner ResultSet, directly; and
(b) a strong ref to the inner PreparedStatement, via the statement
reference of the inner ResultSet.
.. but no strong ref to the outer Statement. So the outer Statement
becomes garbage right after the call to executeQuery(), and if you are
unlucky with GC timing, it can be finalized and closed under you,
which then closes the inner ResultSet.
Adding a call to stmt.close() means that the outer Statement still has
a strong reference directly from test code up until that point, so it
doesn't get finalized early.
At a glance, executeQuery() is also broken for queries that return
multiple resultsets if ForceBinaryTransfers is enabled - you only get
the first resultset back.
Oliver
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 06:18 Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
parent: Dave Cramer <pg@fastcrypt.com>
0 siblings, 0 replies; 20+ messages in thread
From: Mikko Tiihonen @ 2011-09-19 06:18 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc
On 09/16/2011 04:40 PM, Dave Cramer wrote:
> I applied v13 patch successfully. Is there a necessity to use the
> patches you have just sent ?
Not really. The only functional difference is with ResulSet.getBoolean of a numeric type,
which in the older v13 patches differed from the text protocol handling.
-Mikko
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 06:40 Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
parent: Oliver Jowett <oliver@opencloud.com>
1 sibling, 0 replies; 20+ messages in thread
From: Mikko Tiihonen @ 2011-09-19 06:40 UTC (permalink / raw)
To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc
On 09/17/2011 02:14 AM, Oliver Jowett wrote:
> On 17 September 2011 01:20, Mikko Tiihonen
> <mikko.tiihonen@nitorcreations.com> wrote:
>
>> If I debugged it correctly it looks like the query statement is closed by
>> finalizer during the test which in turn closes the relevant portals.
>> If I add stmt.close() to the end of the test there are no failures as it
>> keeps the GC from killing of the statement during the test.
>>
>> But why I only seem to get it when running with binary transfer patches I do
>> not know. So there must be something in the patches that I cannot spot.
>
> (I am halfway through my first coffee of the day. YMMV)
>
> The test code holds a reference to the returned ResultSet, but doesn't
> keep a reference to the Statement itself after calling executeQuery().
> Normally this isn't a problem because there is a strong ref from a
> ResultSet to its owning Statement, so while you are using a particular
> ResultSet the creating statement remains reachable.
>
> The problem seems to be introduced in the patch to executeQuery if
> (ForceBinaryTransfers). This delegates to a new PreparedStatement
> ("inner PreparedStatement") and splices its resultset ("inner
> resultset") back into the surrounding Statement ("outer Statement")
> and returns that.
>
> However the inner ResultSet has the wrong parent Statement for that
> context (ResultSet.getStatement() will return the "inner
> PreparedStatement" not the "outer Statement") - which is a separate
> problem, but also causes the GC problems as well, as now there's no
> strong ref from the returned ResultSet to the Statement that the
> client created it via.
>
> So in the test code we end up with:
> (a) a strong ref to the inner ResultSet, directly; and
> (b) a strong ref to the inner PreparedStatement, via the statement
> reference of the inner ResultSet.
>
> .. but no strong ref to the outer Statement. So the outer Statement
> becomes garbage right after the call to executeQuery(), and if you are
> unlucky with GC timing, it can be finalized and closed under you,
> which then closes the inner ResultSet.
>
> Adding a call to stmt.close() means that the outer Statement still has
> a strong reference directly from test code up until that point, so it
> doesn't get finalized early.
>
> At a glance, executeQuery() is also broken for queries that return
> multiple resultsets if ForceBinaryTransfers is enabled - you only get
> the first resultset back.
Thank you for the thorough analysis. Now we need to decide what to do
with the ForceBinaryTransfers testing/debugging aid.
It was originally added to ensure that while running the unit tests
as much as possible of the code paths would exercise the binary transfer
code. Normally binary transfers would only be activated after few
round-trips to the backend with the same prepared statement - which
basically never happens in unit tests.
We have at least three options:
1) drop the ForceBinaryTransfers
- it has helpped debug the binary transfer to a working state
- it is no longer that useful
2) leave it as is
- just add the hack to the one failing test to make it not fail
- it is still useful to run the test suite to verify functionality
stays correct with binary transfers
3) fix it
- less worries in the future and if some end user finds the
undocumented feature they won't get bitten by it
-Mikko
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 10:03 Dave Cramer <pg@fastcrypt.com>
parent: Oliver Jowett <oliver@opencloud.com>
1 sibling, 1 reply; 20+ messages in thread
From: Dave Cramer @ 2011-09-19 10:03 UTC (permalink / raw)
To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: Oliver Jowett <oliver@opencloud.com>; pgsql-jdbc
Hi Mikko,
My suggestion would be to fix it.
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Fri, Sep 16, 2011 at 7:14 PM, Oliver Jowett <oliver@opencloud.com> wrote:
> On 17 September 2011 01:20, Mikko Tiihonen
> <mikko.tiihonen@nitorcreations.com> wrote:
>
>> If I debugged it correctly it looks like the query statement is closed by
>> finalizer during the test which in turn closes the relevant portals.
>> If I add stmt.close() to the end of the test there are no failures as it
>> keeps the GC from killing of the statement during the test.
>>
>> But why I only seem to get it when running with binary transfer patches I do
>> not know. So there must be something in the patches that I cannot spot.
>
> (I am halfway through my first coffee of the day. YMMV)
>
> The test code holds a reference to the returned ResultSet, but doesn't
> keep a reference to the Statement itself after calling executeQuery().
> Normally this isn't a problem because there is a strong ref from a
> ResultSet to its owning Statement, so while you are using a particular
> ResultSet the creating statement remains reachable.
>
> The problem seems to be introduced in the patch to executeQuery if
> (ForceBinaryTransfers). This delegates to a new PreparedStatement
> ("inner PreparedStatement") and splices its resultset ("inner
> resultset") back into the surrounding Statement ("outer Statement")
> and returns that.
>
> However the inner ResultSet has the wrong parent Statement for that
> context (ResultSet.getStatement() will return the "inner
> PreparedStatement" not the "outer Statement") - which is a separate
> problem, but also causes the GC problems as well, as now there's no
> strong ref from the returned ResultSet to the Statement that the
> client created it via.
>
> So in the test code we end up with:
> (a) a strong ref to the inner ResultSet, directly; and
> (b) a strong ref to the inner PreparedStatement, via the statement
> reference of the inner ResultSet.
>
> .. but no strong ref to the outer Statement. So the outer Statement
> becomes garbage right after the call to executeQuery(), and if you are
> unlucky with GC timing, it can be finalized and closed under you,
> which then closes the inner ResultSet.
>
> Adding a call to stmt.close() means that the outer Statement still has
> a strong reference directly from test code up until that point, so it
> doesn't get finalized early.
>
> At a glance, executeQuery() is also broken for queries that return
> multiple resultsets if ForceBinaryTransfers is enabled - you only get
> the first resultset back.
>
> Oliver
>
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 10:11 Bodor Andras <bodri.mh3@gmail.com>
0 siblings, 2 replies; 20+ messages in thread
From: Bodor Andras @ 2011-09-19 10:11 UTC (permalink / raw)
To: pgsql-jdbc
Why don't you set prepareThreshold to 0 for your test cases?
In this case parameter passing starts immediately for the
prepared statement, and ForceBinaryTransfers can be dropped.
Andras
> Thank you for the thorough analysis. Now we need to decide what to do
> with the ForceBinaryTransfers testing/debugging aid.
>
> It was originally added to ensure that while running the unit tests
> as much as possible of the code paths would exercise the binary > > >
> transfer
> code. Normally binary transfers would only be activated after few
> round-trips to the backend with the same prepared statement - which
> basically never happens in unit tests.
>
> We have at least three options:
> 1) drop the ForceBinaryTransfers
> - it has helpped debug the binary transfer to a working state
> - it is no longer that useful
> 2) leave it as is
> - just add the hack to the one failing test to make it not fail
> - it is still useful to run the test suite to verify functionality
> stays correct with binary transfers
> 3) fix it
> - less worries in the future and if some end user finds the
> undocumented feature they won't get bitten by it
>
> -Mikko
>
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 13:53 Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
parent: Bodor Andras <bodri.mh3@gmail.com>
1 sibling, 1 reply; 20+ messages in thread
From: Mikko Tiihonen @ 2011-09-19 13:53 UTC (permalink / raw)
To: pgsql-jdbc
On 09/19/2011 01:11 PM, Bodor Andras wrote:
> Why don't you set prepareThreshold to 0 for your test cases?
> In this case parameter passing starts immediately for the
> prepared statement, and ForceBinaryTransfers can be dropped.
AbstractJdbc2Statement:
public boolean isUseServerPrepare() {
return (preparedQuery != null && m_prepareThreshold != 0 && m_useCount + 1 >= m_prepareThreshold);
}
If I read the above correctly then prepareThreshold of 0 disables prepared statements.
-Mikko
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 14:38 Bodor Andras <bodri.mh3@gmail.com>
parent: Bodor Andras <bodri.mh3@gmail.com>
1 sibling, 2 replies; 20+ messages in thread
From: Bodor Andras @ 2011-09-19 14:38 UTC (permalink / raw)
To: pgsql-jdbc
You are right. But then it is a bug in either the code,
or the documentation. It says:
prepareThreshold = int
Determine the number of PreparedStatement executions required
before switching over to use server side prepared statements.
I think, if zero executions are required, then the driver
should use prepared statement immediately. Both permanently
disabling and immediately enabling is a legitimate need of
the users, it should be clearly documented, how to do them.
Anyway, setting prepareThreshold = -1 will do what I meant.
But it is more logical to me, that 0 means zero and -1 means
infinity (even better: Integer.MAX_VALUE).
For the ForceBinaryTransfers I vote for dropping it, as
it adds unnecessary complications (and new bugs) to the
code.
Andras
>> Why don't you set prepareThreshold to 0 for your test cases?
>> In this case parameter passing starts immediately for the
>> prepared statement, and ForceBinaryTransfers can be dropped.
>> Andras
>>
>AbstractJdbc2Statement:
> public boolean isUseServerPrepare() {
> return (preparedQuery != null && m_prepareThreshold != 0 &&
>m_useCount + 1 >= m_prepareThreshold);
> }
>
>If I read the above correctly then prepareThreshold of 0 disables
>prepared statements.
>
>-Mikko
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 14:47 Tom Lane <tgl@sss.pgh.pa.us>
parent: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
0 siblings, 1 reply; 20+ messages in thread
From: Tom Lane @ 2011-09-19 14:47 UTC (permalink / raw)
To: pgsql-jdbc
Mikko Tiihonen <mikko.tiihonen@nitorcreations.com> writes:
> On 09/19/2011 01:11 PM, Bodor Andras wrote:
>> Why don't you set prepareThreshold to 0 for your test cases?
>> In this case parameter passing starts immediately for the
>> prepared statement, and ForceBinaryTransfers can be dropped.
> AbstractJdbc2Statement:
> public boolean isUseServerPrepare() {
> return (preparedQuery != null && m_prepareThreshold != 0 && m_useCount + 1 >= m_prepareThreshold);
> }
> If I read the above correctly then prepareThreshold of 0 disables prepared statements.
But "1" would do what you want, no?
FWIW, I'm hoping that this entire business of delaying the server-side
prepare will be obsolete as of 9.2. There is already code committed in
HEAD that allows "prepared" statements to be re-planned for each new set
of parameter values, with a somewhat-informed choice of whether and when
to switch over to the traditional generic-plan approach. It needs
tweaking for performance still, no doubt, but there is not going to be a
reason for the driver to do this anymore.
Obviously that's not much use for solving your immediate problem, but it
might lead you to conclude that putting a large amount of effort into
improving this mechanism would be a dead end.
regards, tom lane
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 15:15 Bodor Andras <bodri.mh3@gmail.com>
parent: Bodor Andras <bodri.mh3@gmail.com>
1 sibling, 0 replies; 20+ messages in thread
From: Bodor Andras @ 2011-09-19 15:15 UTC (permalink / raw)
To: pgsql-jdbc
Self correction: I did not read the documentation
carefully enough. prepareThreshold = 1 should be used.
Negative values are changed to 0 by the driver. But
it should be mentioned somewhere, that 0 switches it
off completely.
Bodor Andras wrote:
> You are right. But then it is a bug in either the code,
> or the documentation. It says:
>
> prepareThreshold = int
> Determine the number of PreparedStatement executions required before
> switching over to use server side prepared statements.
>
> I think, if zero executions are required, then the driver
> should use prepared statement immediately. Both permanently
> disabling and immediately enabling is a legitimate need of
> the users, it should be clearly documented, how to do them.
> Anyway, setting prepareThreshold = -1 will do what I meant.
> But it is more logical to me, that 0 means zero and -1 means
> infinity (even better: Integer.MAX_VALUE).
> For the ForceBinaryTransfers I vote for dropping it, as
> it adds unnecessary complications (and new bugs) to the
> code.
> Andras
>
>>> Why don't you set prepareThreshold to 0 for your test cases?
>>> In this case parameter passing starts immediately for the
>>> prepared statement, and ForceBinaryTransfers can be dropped.
>>> Andras
>>>
> >AbstractJdbc2Statement:
> > public boolean isUseServerPrepare() {
> > return (preparedQuery != null && m_prepareThreshold != 0 &&
> >m_useCount + 1 >= m_prepareThreshold);
> > }
> >
> >If I read the above correctly then prepareThreshold of 0 disables
> >prepared statements.
> >
> >-Mikko
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 17:26 Radosław Smogura <rsmogura@softperience.eu>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 20+ messages in thread
From: Radosław Smogura @ 2011-09-19 17:26 UTC (permalink / raw)
To: pgsql-jdbc; +Cc: Tom Lane <tgl@sss.pgh.pa.us>
Tom Lane <tgl@sss.pgh.pa.us> Monday 19 of September 2011 16:47:12
> Mikko Tiihonen <mikko.tiihonen@nitorcreations.com> writes:
> > On 09/19/2011 01:11 PM, Bodor Andras wrote:
> >> Why don't you set prepareThreshold to 0 for your test cases?
> >> In this case parameter passing starts immediately for the
> >> prepared statement, and ForceBinaryTransfers can be dropped.
> >
> > AbstractJdbc2Statement:
> > public boolean isUseServerPrepare() {
> >
> > return (preparedQuery != null && m_prepareThreshold != 0 &&
> > m_useCount + 1 >= m_prepareThreshold);
> >
> > }
> >
> > If I read the above correctly then prepareThreshold of 0 disables
> > prepared statements.
>
> But "1" would do what you want, no?
>
> FWIW, I'm hoping that this entire business of delaying the server-side
> prepare will be obsolete as of 9.2. There is already code committed in
> HEAD that allows "prepared" statements to be re-planned for each new set
> of parameter values, with a somewhat-informed choice of whether and when
> to switch over to the traditional generic-plan approach. It needs
> tweaking for performance still, no doubt, but there is not going to be a
> reason for the driver to do this anymore.
>
> Obviously that's not much use for solving your immediate problem, but it
> might lead you to conclude that putting a large amount of effort into
> improving this mechanism would be a dead end.
>
> regards, tom lane
The whole logic about this was, because PreparedStatement are not used in Java
to be prepared in means of database server. It's normal technique to write
even one shoot statements
ps = conn.prepareStatemn(SELECT * FROM foo where id = ?)
This simple techinique makes code more simple and saftly; it prevents SQL
injection, as well no one need to care about proper data escaping. So If
someone uses statements once, there may be no need to parsing plan for this
(prove why server should do this for you by invoking it 5 times).
As side effect of this popular concept many J2EE servers provides new pool,
with ecah connection (connection are pooled), you get statement pool, so web-
based and J2EE environments (which are mainly single-request based) may with
no effort form developer side use benefits of preparation.
The PG "one shot" statements will not make this obsolete
1) Driver will be backward compatible (it's still supports 7.x editions)
2) Above problem will be still present.
3) Java perpared statements uses ? inseted of positional parameters, so driver
need to parse query in any way.
Regards
Radek
http://www.softperience.eu/
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 18:18 Maciek Sakrejda <msakrejda@truviso.com>
parent: Radosław Smogura <rsmogura@softperience.eu>
0 siblings, 1 reply; 20+ messages in thread
From: Maciek Sakrejda @ 2011-09-19 18:18 UTC (permalink / raw)
To: Radosław Smogura <rsmogura@softperience.eu>; +Cc: pgsql-jdbc@postgresql.org, Tom Lane <tgl@sss.pgh.pa.us>
> The PG "one shot" statements will not make this obsolete
> 1) Driver will be backward compatible (it's still supports 7.x editions)
> 2) Above problem will be still present.
> 3) Java perpared statements uses ? inseted of positional parameters, so driver
> need to parse query in any way.
I think what Tom is saying is that you'll be able to set
prepareThreshold to 1 in the driver (always use named server-side
prepared statements) and the server will automagically do the right
thing. Right now, the big problem is that only unnamed prepared
statements include the "feature" of being able to consider parameters
in the query plan (since unnamed statements are not reused, there's no
sense in planning them before you have parameters). Other aspects of
using prepared statements through the jdbc driver wouldn't change, but
we should be able to forget about prepareThreshold (if I understand
this correctly), which would be very nice.
---
Maciek Sakrejda | System Architect | Truviso
1065 E. Hillsdale Blvd., Suite 215
Foster City, CA 94404
(650) 242-3500 Main
www.truviso.com
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-19 18:35 Kris Jurka <books@ejurka.com>
parent: Bodor Andras <bodri.mh3@gmail.com>
1 sibling, 0 replies; 20+ messages in thread
From: Kris Jurka @ 2011-09-19 18:35 UTC (permalink / raw)
To: Bodor Andras <bodri.mh3@gmail.com>; +Cc: pgsql-jdbc
On Mon, 19 Sep 2011, Bodor Andras wrote:
> For the ForceBinaryTransfers I vote for dropping it, as
> it adds unnecessary complications (and new bugs) to the
> code.
ForceBinaryTransfers doesn't just make the driver use a named protocol
level statement, so it isn't an equivalent substitution to recommend
adjusting prepareTreshold. The key to binary transfer is knowing what
types will be returned so you know if you can handle them as binary and
therefore whether to request them as binary or not. So the
ForceBinaryTransfers hack is for testing and does a half execution to get
the returned data types and then does a real execution with the true data
types.
So we need this to make use of our existing test suite because it doesn't
normally do multiple execution which would let us naturally discover the
type information. Whether it should be exposed to users or not is
debatable, but we need it internally.
Unless of course you are willing to add an extra network roundtrip to
every query execution to pickup the type information before execution.
Kris Jurka
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-20 00:39 Tom Lane <tgl@sss.pgh.pa.us>
parent: Maciek Sakrejda <msakrejda@truviso.com>
0 siblings, 1 reply; 20+ messages in thread
From: Tom Lane @ 2011-09-20 00:39 UTC (permalink / raw)
To: Maciek Sakrejda <msakrejda@truviso.com>; +Cc: Radosław Smogura <rsmogura@softperience.eu>; pgsql-jdbc
Maciek Sakrejda <msakrejda@truviso.com> writes:
> I think what Tom is saying is that you'll be able to set
> prepareThreshold to 1 in the driver (always use named server-side
> prepared statements) and the server will automagically do the right
> thing. Right now, the big problem is that only unnamed prepared
> statements include the "feature" of being able to consider parameters
> in the query plan (since unnamed statements are not reused, there's no
> sense in planning them before you have parameters).
Right. We've rejiggered the backend so that planning is delayed until
parameter values are available in all cases. (If the code finds that
the plans it's getting with the parameters aren't materially better than
a generic plan would be, it'll eventually go back to using generic
plans. But hopefully you will never need to club it over the head to
avoid doing that.)
Anyway, this isn't going to hit the street for another year, so there's
no reason to worry about it too much right now. I'm just saying you
shouldn't invest a lot of work in related areas without being aware that
that change is coming.
regards, tom lane
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-20 05:00 Craig Ringer <ringerc@ringerc.id.au>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 20+ messages in thread
From: Craig Ringer @ 2011-09-20 05:00 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Maciek Sakrejda <msakrejda@truviso.com>; Radosław Smogura <rsmogura@softperience.eu>; pgsql-jdbc
On 09/20/2011 08:39 AM, Tom Lane wrote:
> Maciek Sakrejda<msakrejda@truviso.com> writes:
>> I think what Tom is saying is that you'll be able to set
>> prepareThreshold to 1 in the driver (always use named server-side
>> prepared statements) and the server will automagically do the right
>> thing. Right now, the big problem is that only unnamed prepared
>> statements include the "feature" of being able to consider parameters
>> in the query plan (since unnamed statements are not reused, there's no
>> sense in planning them before you have parameters).
> Right. We've rejiggered the backend so that planning is delayed until
> parameter values are available in all cases.
Oooh, exciting. That'll cut down on a whole class of mailing list query
and get rid of a classic gotcha.
Time for me to build git and start playing.
--
Craig Ringer
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-20 14:00 Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
parent: Dave Cramer <pg@fastcrypt.com>
0 siblings, 1 reply; 20+ messages in thread
From: Mikko Tiihonen @ 2011-09-20 14:00 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: Oliver Jowett <oliver@opencloud.com>; pgsql-jdbc
On 09/19/2011 01:03 PM, Dave Cramer wrote:
> Hi Mikko,
>
> My suggestion would be to fix it.
Finally had some time to look into this.
Closing the result set of the describe query fixes the test failure for me.
-Mikko
inline patch 1: org/postgresql/jdbc2/AbstractJdbc2Statement.java (+2/-0 lines) download inline-1.patch
--- org/postgresql/jdbc2/AbstractJdbc2Statement.java.orig 2011-09-20 16:55:18.130947602 +0300
+++ org/postgresql/jdbc2/AbstractJdbc2Statement.java 2011-09-20 16:56:02.310694214 +0300
@@ -541,6 +541,8 @@
int flags2 = flags | QueryExecutor.QUERY_DESCRIBE_ONLY;
StatementResultHandler handler2 = new StatementResultHandler();
connection.getQueryExecutor().execute(queryToExecute, queryParameters, handler2, 0, 0, flags2);
+ ResultWrapper result2 = handler2.getResults();
+ result2.getResultSet().close();
}
StatementResultHandler handler = new StatementResultHandler();
> On Fri, Sep 16, 2011 at 7:14 PM, Oliver Jowett<oliver@opencloud.com> wrote:
>> On 17 September 2011 01:20, Mikko Tiihonen
>> <mikko.tiihonen@nitorcreations.com> wrote:
>>
>>> If I debugged it correctly it looks like the query statement is closed by
>>> finalizer during the test which in turn closes the relevant portals.
>>> If I add stmt.close() to the end of the test there are no failures as it
>>> keeps the GC from killing of the statement during the test.
>>>
>>> But why I only seem to get it when running with binary transfer patches I do
>>> not know. So there must be something in the patches that I cannot spot.
>>
>> (I am halfway through my first coffee of the day. YMMV)
>>
>> The test code holds a reference to the returned ResultSet, but doesn't
>> keep a reference to the Statement itself after calling executeQuery().
>> Normally this isn't a problem because there is a strong ref from a
>> ResultSet to its owning Statement, so while you are using a particular
>> ResultSet the creating statement remains reachable.
>>
>> The problem seems to be introduced in the patch to executeQuery if
>> (ForceBinaryTransfers). This delegates to a new PreparedStatement
>> ("inner PreparedStatement") and splices its resultset ("inner
>> resultset") back into the surrounding Statement ("outer Statement")
>> and returns that.
>>
>> However the inner ResultSet has the wrong parent Statement for that
>> context (ResultSet.getStatement() will return the "inner
>> PreparedStatement" not the "outer Statement") - which is a separate
>> problem, but also causes the GC problems as well, as now there's no
>> strong ref from the returned ResultSet to the Statement that the
>> client created it via.
>>
>> So in the test code we end up with:
>> (a) a strong ref to the inner ResultSet, directly; and
>> (b) a strong ref to the inner PreparedStatement, via the statement
>> reference of the inner ResultSet.
>>
>> .. but no strong ref to the outer Statement. So the outer Statement
>> becomes garbage right after the call to executeQuery(), and if you are
>> unlucky with GC timing, it can be finalized and closed under you,
>> which then closes the inner ResultSet.
>>
>> Adding a call to stmt.close() means that the outer Statement still has
>> a strong reference directly from test code up until that point, so it
>> doesn't get finalized early.
>>
>> At a glance, executeQuery() is also broken for queries that return
>> multiple resultsets if ForceBinaryTransfers is enabled - you only get
>> the first resultset back.
>>
>> Oliver
>>
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-20 14:47 Oliver Jowett <oliver@opencloud.com>
parent: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
0 siblings, 1 reply; 20+ messages in thread
From: Oliver Jowett @ 2011-09-20 14:47 UTC (permalink / raw)
To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; pgsql-jdbc
On 21 September 2011 02:00, Mikko Tiihonen
<mikko.tiihonen@nitorcreations.com> wrote:
> On 09/19/2011 01:03 PM, Dave Cramer wrote:
>>
>> Hi Mikko,
>>
>> My suggestion would be to fix it.
>
> Finally had some time to look into this.
> Closing the result set of the describe query fixes the test failure for me.
That code path only runs on the "inner" statement in this case, doesn't it?
Oliver
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: binary patch problems
@ 2011-09-20 18:19 Dave Cramer <pg@fastcrypt.com>
parent: Oliver Jowett <oliver@opencloud.com>
0 siblings, 0 replies; 20+ messages in thread
From: Dave Cramer @ 2011-09-20 18:19 UTC (permalink / raw)
To: Oliver Jowett <oliver@opencloud.com>; +Cc: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>; pgsql-jdbc
Hi Mikko,
So I applied that patch now I run out of connections and I have my
server configured for 100 connections.
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Tue, Sep 20, 2011 at 10:47 AM, Oliver Jowett <oliver@opencloud.com> wrote:
> On 21 September 2011 02:00, Mikko Tiihonen
> <mikko.tiihonen@nitorcreations.com> wrote:
>> On 09/19/2011 01:03 PM, Dave Cramer wrote:
>>>
>>> Hi Mikko,
>>>
>>> My suggestion would be to fix it.
>>
>> Finally had some time to look into this.
>> Closing the result set of the describe query fixes the test failure for me.
>
> That code path only runs on the "inner" statement in this case, doesn't it?
>
> Oliver
>
^ permalink raw reply [nested|flat] 20+ messages in thread
end of thread, other threads:[~2011-09-20 18:19 UTC | newest]
Thread overview: 20+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-09-15 22:10 binary patch problems Dave Cramer <pg@fastcrypt.com>
2011-09-16 13:20 ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2011-09-16 13:40 ` Dave Cramer <pg@fastcrypt.com>
2011-09-19 06:18 ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2011-09-16 23:14 ` Oliver Jowett <oliver@opencloud.com>
2011-09-19 06:40 ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2011-09-19 10:03 ` Dave Cramer <pg@fastcrypt.com>
2011-09-20 14:00 ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2011-09-20 14:47 ` Oliver Jowett <oliver@opencloud.com>
2011-09-20 18:19 ` Dave Cramer <pg@fastcrypt.com>
2011-09-19 10:11 Re: binary patch problems Bodor Andras <bodri.mh3@gmail.com>
2011-09-19 13:53 ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2011-09-19 14:47 ` Tom Lane <tgl@sss.pgh.pa.us>
2011-09-19 17:26 ` Radosław Smogura <rsmogura@softperience.eu>
2011-09-19 18:18 ` Maciek Sakrejda <msakrejda@truviso.com>
2011-09-20 00:39 ` Tom Lane <tgl@sss.pgh.pa.us>
2011-09-20 05:00 ` Craig Ringer <ringerc@ringerc.id.au>
2011-09-19 14:38 ` Bodor Andras <bodri.mh3@gmail.com>
2011-09-19 15:15 ` Bodor Andras <bodri.mh3@gmail.com>
2011-09-19 18:35 ` Kris Jurka <books@ejurka.com>
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