Connection hanging on INSERT apparently due to large batch size and 4 CPU cores

Поиск
Список
Период
Сортировка
Искать

Connection hanging on INSERT apparently due to large batch size and 4 CPU cores

От:
jgassner@gmail.com
Дата:
I am experiencing a problem while using the JDBC driver where one or
more connections hang soon after application startup.  The connections
don't hang immediately, but instead hang after performing a few
operations (such as SELECTS) - we are using a connection pool.  The
number of operations it takes for a connection to hang varies, but
typically we are able to reproduce the issue within an hour or two.
In all cases, however, when they do hang, it is while performing an
INSERT into one particular table of ours.  I'm not aware if it is
always the first INSERT for a particular connection that results in a
hang, but I have seen this to be the case in many db log traces.  The
table where the hanging INSERTS are occurring is the table that
receives, by far, the most number of INSERTS.

We are using a batched INSERT to perform the insert operation into
this table.  When we attempt to insert batches of size 600 rows, we
observe this hang issue.  However, we evetually found that lowering
the batch size to 100 avoided the hang issue.  While it is nice to
apparently get around the issue using a lower batch size, it would be
nice to understand what the actual problem is so we can be sure it
won't pop up again.  This is my question... Anyone know why lowering
the batch size fixed the problem and why the problem occurred in the
first place?  Is there a maximum amount of data that can be inserted
in one batch?

Here are some observations on the problem:

From a stack trace of the java side of the connection, I see that it
consistenly is hanging in a socketWrite and while binding the values:

Thread-94 [RUNNABLE]
java.net.SocketOutputStream.socketWrite0(native method)
java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
java.net.SocketOutputStream.write(SocketOutputStream.java:136)
java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
java.io.BufferedOutputStream.write(BufferedOutputStream.java:109)
java.io.FilterOutputStream.write(FilterOutputStream.java:80)
org.postgresql.core.PGStream.SendInteger2(PGStream.java:205)
org.postgresql.core.v3.QueryExecutorImpl.sendBind(QueryExecutorImpl.java:
840)
org.postgresql.core.v3.QueryExecutorImpl.sendOneQuery(QueryExecutorImpl.java:
1053)
org.postgresql.core.v3.QueryExecutorImpl.sendQuery(QueryExecutorImpl.java:
644)
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:
345)
org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:
2674)

Performing an strace on the PostgreSQL server process corresponding to
the hung connection shows that it is stuck inside a call to "sendto".

Using lsof I can see that the tcp connection is still in the
ESTABLISHED state.

So it appears that boths side are trying to write to each other at the
same time and the underlying tcp connection is good.

Threading: This is a multi-threaded java server application.  All
threads take connections from the same connection pool.  Most threads
aren't very busy, other than the threads inserting into the particular
table where the INSERTS are hanging.

Info on my setup:
JDBC driver: postgresql-8.3-603.jdbc3.jar
PostgreSQL Server: postgresql-8.3.3.tar.bz2 (built with the options  --
without-readline --disable-rpath --with-openssl on x86-64)
- Although it is built with ssl, our tests are not using ssl
JVM: Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-
b04)
Machine: SLES 10 SP2 (x86_64); 4 CPU cores
- Doesn't seem to happen on machines with only 2 CPU cores

All idea are appreciated.

Thanks,
John

Re: Connection hanging on INSERT apparently due to large batch size and 4 CPU cores

От:
Kris Jurka <books@ejurka.com>
Дата:


On Mon, 20 Oct 2008, jgassner@gmail.com wrote:

> We are using a batched INSERT to perform the insert operation into
> this table.  When we attempt to insert batches of size 600 rows, we
> observe this hang issue.  However, we evetually found that lowering
> the batch size to 100 avoided the hang issue.  While it is nice to
> apparently get around the issue using a lower batch size, it would be
> nice to understand what the actual problem is so we can be sure it
> won't pop up again.  This is my question... Anyone know why lowering
> the batch size fixed the problem and why the problem occurred in the
> first place?  Is there a maximum amount of data that can be inserted
> in one batch?

See the comments here starting at line #226.

http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/jdbc/pgjdbc/org/postgresql/core/v3/QueryExecutorImpl.java?annotate=1.42

The driver is already breaking your batch into sub-batches of 256 queries. 
We don't expect to get a large response from an insert statement, but 
that's apparently not true in your case.  Perhaps there is something 
unusual about your inserts?  Do they fail, resulting in large error 
messages?  Do they produce warnings or notices for things like 
escape_string_warning?  If you can reproduce this failure, then turning on 
additional logging via the loglevel=2 URL parameter will show the exact 
protocol level responses coming from the server and then we should be able 
to see what's really happening.  (You'd want to look at the protocol level 
logging for both the success and failure cases, because on the failure 
side, we never get around to processing the results.

Kris Jurka

Re: Connection hanging on INSERT apparently due to large batch size and 4 CPU cores

От:
John <jgassner@gmail.com>
Дата:
On Oct 22, 1:48 pm, bo...@ejurka.com (Kris Jurka) wrote:
> On Mon, 20 Oct 2008, jgass...@gmail.com wrote:
> > We are using a batched INSERT to perform the insert operation into
> > this table.  When we attempt to insert batches of size 600 rows, we
> > observe this hang issue.  However, we evetually found that lowering
> > the batch size to 100 avoided the hang issue.  While it is nice to
> > apparently get around the issue using a lower batch size, it would be
> > nice to understand what the actual problem is so we can be sure it
> > won't pop up again.  This is my question... Anyone know why lowering
> > the batch size fixed the problem and why the problem occurred in the
> > first place?  Is there a maximum amount of data that can be inserted
> > in one batch?
>
> See the comments here starting at line #226.
>
> http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/jdbc/pgjdbc/org/postgresq...
>
> The driver is already breaking your batch into sub-batches of 256 queries.
> We don't expect to get a large response from an insert statement, but
> that's apparently not true in your case.  Perhaps there is something
> unusual about your inserts?  Do they fail, resulting in large error
> messages?  Do they produce warnings or notices for things like
> escape_string_warning?  If you can reproduce this failure, then turning on
> additional logging via the loglevel=2 URL parameter will show the exact
> protocol level responses coming from the server and then we should be able
> to see what's really happening.  (You'd want to look at the protocol level
> logging for both the success and failure cases, because on the failure
> side, we never get around to processing the results.
>
> Kris Jurka
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-j...@postgresql.org)
> To make changes to your subscription:http://www.postgresql.org/mailpref/pgsql-jdbc

I was able to reproduce the issue with loglevel=2.  I believe I
located the connection in the log file that corresponds to the hung
connection.  Reviewing the log activity of this connection didn't show
any errors that were plain for me to see.  I searched for "warning"
and didn't get any hits either.

I do see that this connection (3) just stops logging at the log
message pasted below.  This concurs with my finding that it is hung
within a bind statement.  All the other connections continue logging,
so it was fairly obvious that this was the connection that was the
hung one.

Could it be related to the INSERT being done on a table that has 330
columns?

Thanks,
John

2008/10/25 15:16:30 | INFO   | jvm 1    | 15:16:30.415 (3)  FE=>
Execute(portal=null,limit=1)
2008/10/25 15:16:30 | INFO   | jvm 1    | 15:16:30.415 (3)  FE=>
Describe(statement=S_489)
2008/10/25 15:16:30 | INFO   | jvm 1    | 15:16:30.415 (3)  FE=>
Bind(stmt=S_489,portal=null,$1=<42BE2611-84F5-102B-A965-0022190B8F18>,
$2=<2008-10
-25 19:03:55.000000 +00:00:00>,$3=<101000>,$4=<0>,$5=<0>,$6=<0>,
$7=<101000>,$8=<7655123>,$9=<109100>,$10=,$11=,$12=,
$13=,$
14=,$15=<443>,$16=<16001>,$17=,$18=,$19=<3>,
$20=<दकEventGeneratorदक>,$21=,$22=<-1062731758>,$23=,
$24=,$25=<-10
62731759>,$26=,$27=,$28=,$29=,
$30=<दकqffदक fik fik >,$31=,$32=,$33=,$34=,
$35=
,$36=,$37=<2008-10-25 19:16:25.650000 +00:00:00>,$38=<2008-10-25
19:16:25.650000 +00:00:00>,$39=,$40=,$41=<0>,$42=,
$43=,$44=,$45=,$46=,$47=,$48=,$49=,
$50=,$51=,$52=,$53=,$54=,$55=,
$56=
,$57=,$58=,$59=,$60=,$61=,$62=,
$63=,$64=,$65=,$66=,$67=,$68=,
$69=,$7
0=,$71=,$72=,$73=,$74=,
$75=,$76=,$77=,$78=,$79=,$80=,
$81=,$82=
,$83=,$84=,$85=,$86=,
$87=,$88=<0>,$89=<101000>,$90=,$91=,$92=,
$93=,$94=<
NULL>,$95=,$96=,$97=,$98=,$99=,
$100=,$101=,$102=,$103=,$104=,
$105=,$106=,$
107=,$108=,$109=,$110=,$111=,$112=,
$113=,$114=,$115=,$116=,$117=,
$118=,$11
9=,$120=,$121=,$122=,$123=,$124=,
$125=,$126=,$127=,$128=,$129=,
$130=,$131=
,$132=,$133=,$134=,$135=,$136=,
$137=,$138=,$139=,$140=,$141=,
$142=,$143=,$144=,$145=,$146=,$147=,$148=,
$149=,$150=,$151=,$152=,$153=,
$154=,$155=,$156=,$157=,$158=,$159=,$160=,
$161=,$162=,$163=,$164=,$165=,
$166=,$167=
,$168=,$169=,$170=,$171=,$172=,
$173=,$174=,$175=,$176=,$177=,
$178=,$179=,$
180=,$181=,$182=,$183=,$184=,$185=,
$186=,$187=,$188=,$189=,$190=,
$191=,$19
2=,$193=,$194=,$195=,$196=,$197=,
$198=,$199=,$200=,$201=,$202=,
$203=,$204=
,$205=,$206=,$207=,$208=<0>,$209=,
$210=,$211=,$212=,$213=,
$214=,$215=<
NULL>,$216=,$217=,$218=,$219=,$220=,
$221=,$222=<-1062731760>,$223=<-1062731761>,$224=,
$225=,$226=<
0>,$227=<0>,$228=,$229=,$230=,$231=,
$232=,$233=,$234=,$235=,$236=,
$237=,$238=,$2
39=,$240=,$241=,$242=,$243=,$244=,
$245=,$246=,$247=,$248=,$249=,
$250=,$251
=,$252=,$253=,$254=,$255=,$256=,
$257=,$258=,$259=,$260=,$261=,
$262=,$263=<
NULL>,$264=,$265=,$266=,$267=,$268=,
$269=,$270=,$271=,$272=,$273=,
$274=,$275=,$276=,$277=,$278=,$279=,$280=,
$281=,$282=,$283=,$284=,$285=,
$286=,$287=,$288=,$289=,$290=,$291=,$292=,$293=,$294=,$295=,$296=,$297=,$298=,$299=,
$300=,$301=,$302=,$303=,$304=,
$305=,$306=,$307=,$308=,$309=,
$310=,$311=,$3
12=,$313=,$314=,$315=,$316=,$317=,
$318=,$319=,$320=,$321=,$322=,
$323=,$324
=,$325=,$326=,$327=,$328=,$329=,
$330=)

Re: Re: Connection hanging on INSERT apparently due to large batch size and 4 CPU cores

От:
Kris Jurka <books@ejurka.com>
Дата:
Вложения:


On Sat, 25 Oct 2008, John wrote:

> Could it be related to the INSERT being done on a table that has 330
> columns?
>

Yes, that's it (or at least part of it).  In the simple case of using the 
same, known parameter types for each batch entry, the data returned from 
the server has no dependence on parameter count.  The issue arises when 
binding a parameter with unspecified type.  In the attached 
SimpleDeadLockDemo, you can see the driver issuing repeated 
Describe(statement=S_1) which results in a ParameterDescription message 
whose size depends on the number of parameters.  With a large parameter 
count, the backend is going to fill its network buffer with this data and 
we'll get the deadlock you're seeing.

In this case we've supplied an explicit parameter type at parse time, so 
describing it isn't going to tell us anything new.  Even if it was going 
to provide us information, we only need to issue the describe once, not 
for every batch entry.  I'm not sure how complicated a solution might be, 
but at least I understand what's going on now.

Also attached (BatchDeadLock) is a test case that reliably locks up as 
you've described.

Kris Jurka
FAQ