Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 06B5963375D for ; Fri, 6 Mar 2009 07:59:24 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 26736-07 for ; Fri, 6 Mar 2009 07:59:21 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f173.google.com (mail-ew0-f173.google.com [209.85.219.173]) by mail.postgresql.org (Postfix) with ESMTP id A1168632E7D for ; Fri, 6 Mar 2009 07:59:20 -0400 (AST) Received: by ewy21 with SMTP id 21so154958ewy.19 for ; Fri, 06 Mar 2009 03:59:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=3KeEUvdzKxreYn/WqELiSbIDS6Lb5DYUOJGql/oH48Y=; b=S4k4zAFGgZYZUGamq8D+srD4Tlvx/eym5gZC3PdBXSJoKaAC0pZA2kgZ4b1dXojKjU XFngnpWm9WH9jUJOD64DyfhR/xSZESTHs6upZ50hfDMGLIMN1i8G/V7X5AYo6sB6jlJw n4mJTtBr+WZvK2sgq2cClZ2e5etowN5YXwY7o= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=IzJsA8UuZG1CLjprDYUfjdAYlnNSp70jrSySp530MSX8I+YoUtwTAFqgs7O+CSyFGP GKAPuD8nYGqLvhsNRM+HDARmH3kWmkMa+mq64AIIs+UN2iaamjZq/n71+wfGKKhpmjq9 Gl7qGX+BAUq7PfV/wswmKUIBtN3icQpcxk104= MIME-Version: 1.0 Received: by 10.216.0.81 with SMTP id 59mr1204746wea.77.1236340757672; Fri, 06 Mar 2009 03:59:17 -0800 (PST) Date: Fri, 6 Mar 2009 12:59:17 +0100 Message-ID: <9fb413bd0903060359j6659f02bmab90b2eb6b1160f6@mail.gmail.com> Subject: Batch insert order and current_timestamp From: Agustin CS To: pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary=0016367fa168aeb1bd04647203b8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200903/23 X-Sequence-Number: 19353 --0016367fa168aeb1bd04647203b8 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi all I'm getting a strange issue with row inserts using PreparedStatement/Statement batches. I'm trying to use a table as a queue, so lower levels receive data from devices (changes in their attributes) and add those changes into the table (one row per change) using batches. From upper levels another thread each several seconds wakes up and checks for rows in this table. If some of them are available it reads a certain number (for example 100) and after that those 100 are removed, so next time upper level will try to read other 100, and so on. The table looks like this: ID sequence self-increment (inc = +1) timestamp otherFields... The insert query used for the batch at the PreparedStatement looks like this: insert into table (timestamp,otherFields) values(current_timestamp,...) so the default value (nextval(sequence)) is used for the ID column. I was expecting to have a global order only by using this ID as order key, but according to an answer from people of #postgresql@freenode.org it seems that's not enough. It seems two consecutive inserts can get disordered, that is, the first one could have a lower ID but a newer timestamp (assuming that have been inserted from different batches, for example if lower level includes multiple threads inserting rows using their own batches). Considering that I can see all rows inserted in the same batch having the same timestamp, the right way to select rows from the upper level should be ORDER BY timestamp,ID. Anyway, if this "disorder" can really happen then the global ordering is lost. For example: - INSERT 50 rows from lower levels from low-thread 1 - SELECT FROM UPPER LEVELS - INSERT 50 rows from lower levels from low-thread 2 Because of this problem insert1-row50 may get a lower ID, but a newer timestamp than insert2-row1. Then if the upper level is extracting 50 rows each time and launches the query right in the middle of those inserts it will get a wrong "global order", even if it's using the ORDER BY timestamp,ID. That is, each select is locally ordered (it's guaranteed by the ORDER clause) but there's a disorder between 2 consecutive selects. I'm asking about this because i've found exactly this problem, two rows in the table where the first one has lower id but newer timestamp. Right now assuming that each one would be part of different selects is a situation forced by me teorically, because i think there is a possible problem there. To summarize, the questions are: - if i launch a batch having row1, row2 and row3 (inserted in this order into the batch) in a table with a sequence PK and another timestamp column (with current_timestamp as value) can i be sure that lower id means "not newer" timestamp? - in the table will i allways get the rows ordered in this way related to the ID? i mean, row1 will allways have lower ID than row2 and row3, and so on? - what about locking the table for inserts? it may be the last way to do this, but depending on the concrete scenario maybe it would be a good solution. Thx in advance and regards --0016367fa168aeb1bd04647203b8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi all

I'm getting a strange issue with row inserts using Prepar= edStatement/Statement batches.

I'm trying to use a table as a qu= eue, so lower levels receive data from devices (changes in their attributes= )
and add those changes into the table (one row per change) using batches.If some of them are available it reads a certain= number (for example 100) and after that those 100 are removed,
so next time upper level will try to read other 100, and so on.

The = table looks like this:
ID sequence self-increment (inc =3D +1)
timest= amp
otherFields...

The insert query used for the batch at the Pre= paredStatement looks like this:
insert into table (timestamp,otherFields) values(current_timestamp,...) so = the default value (nextval(sequence))
is used for the ID column.

= I was expecting to have a global order only by using this ID as order key, = but according to an answer from
people of #postgresql@freenode.o= rg it seems that's not enough. It seems two consecutive inserts can= get
disordered, that is, the first one could have a lower ID but a newe= r timestamp (assuming that have been
inserted from different batches, for example if lower level includes multip= le threads inserting rows using their
own batches).

Considering t= hat I can see all rows inserted in the same batch having the same timestamp, the right way
to select rows from the u= pper level should be ORDER BY timestamp,ID.

Anyway, if this "di= sorder" can really happen then the global ordering is lost. For exampl= e:
- INSERT 50 rows from lower levels from low-thread 1
- SELECT FROM UPPER= LEVELS
- INSERT 50 rows from lower levels from low-thread 2

Beca= use of this problem insert1-row50 may get a lower ID, but a newer timestamp= than insert2-row1.

Then if the upper level is extracting 50 rows each time and launches th= e query right in the middle of those
inserts it will get a wrong "g= lobal order", even if it's using the ORDER BY timestamp,ID. That i= s, each
select is locally ordered (it's guaranteed by the ORDER clause) but the= re's a disorder between 2 consecutive
selects.

I'm asking= about this because i've found exactly this problem, two rows in the ta= ble where the first one has lower
id but newer timestamp. Right now assuming that each one would be part of d= ifferent selects is a situation
forced by me teorically, because i think= there is a possible problem there.

To summarize, the questions are:=
- if i launch a batch having row1, row2 and row3 (inserted in this order in= to the batch) in a table with a sequence PK
and another timestamp column= (with current_timestamp as value) can i be sure that lower id means "= not newer" timestamp?
- in the table will i allways get the rows ordered in this way related to t= he ID? i mean, row1 will allways have lower ID
than row2 and row3, and s= o on?
- what about locking the table for inserts? it may be the last way= to do this, but depending on the concrete scenario
maybe it would be a good solution.

Thx in advance and regards
--0016367fa168aeb1bd04647203b8--