pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedJDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
11+ messages / 3 participants
[nested] [flat]
* JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-06 20:51 electrotype <electrotype@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: electrotype @ 2020-12-06 20:51 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi,
Using JDBC, I batch insert multiple rows (/"executeBatch()/"). I then use
/'//getGeneratedKeys("id")/' to get the generated ids ("id" is a "/SERIAL PRIMARY KEY/" column).
My question: does the PostgreSQL JDBC driver /guarantees /that the order of the returned generated
ids will be the same as the rows to insert have been specified, using "/addBatch()/"?
The best "answer" to that question I have found is https://stackoverflow.com/a/16119489/843699 , but
it is not 100% clear.
Would it be possible to have an official answer on this?
Thanks in advance!
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-08 23:23 Dave Cramer <davecramer@postgres.rocks>
parent: electrotype <electrotype@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Dave Cramer @ 2020-12-08 23:23 UTC (permalink / raw)
To: electrotype <electrotype@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Sun, 6 Dec 2020 at 15:52, electrotype <electrotype@gmail.com> wrote:
> Hi,
>
> Using JDBC, I batch insert multiple rows (*"executeBatch()*"). I then use
> *'**getGeneratedKeys("id")*' to get the generated ids ("id" is a "*SERIAL
> PRIMARY KEY*" column).
>
> My question: does the PostgreSQL JDBC driver *guarantees *that the order
> of the returned generated ids will be the same as the rows to insert have
> been specified, using "*addBatch()*"?
>
>
> The best "answer" to that question I have found is
> https://stackoverflow.com/a/16119489/843699 , but it is not 100% clear.
>
> Would it be possible to have an official answer on this?
>
I can't see how they could possibly be out of order.
Dave Cramer
www.postgres.rocks
>
> Thanks in advance!
>
>
>
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-09 01:23 electrotype <electrotype@gmail.com>
parent: Dave Cramer <davecramer@postgres.rocks>
0 siblings, 1 reply; 11+ messages in thread
From: electrotype @ 2020-12-09 01:23 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
> I can't see how they could possibly be out of order.
>
Thanks, that what I think too. But, to be honest, I'd really like to see this written in some
documentation! In some cases, this small detail can be quite important.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-09 10:21 Dave Cramer <davecramer@postgres.rocks>
parent: electrotype <electrotype@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Dave Cramer @ 2020-12-09 10:21 UTC (permalink / raw)
To: electrotype <electrotype@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
So I'm curious. Why does order matter ?
Dave Cramer
www.postgres.rocks
On Wed, 9 Dec 2020 at 03:15, electrotype <electrotype@gmail.com> wrote:
> I can't see how they could possibly be out of order.
>
> Thanks, that what I think too. But, to be honest, I'd really like to see
> this written in some documentation! In some cases, this small detail can be
> quite important.
>
>
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-09 14:48 electrotype <electrotype@gmail.com>
parent: Dave Cramer <davecramer@postgres.rocks>
0 siblings, 2 replies; 11+ messages in thread
From: electrotype @ 2020-12-09 14:48 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
> So I'm curious. Why does order matter ?
>
> Dave Cramer
> www.postgres.rocks
When you have to save multiple new entities with subentities.
You first save all the parent entities in a single SQL batch insert, you get the generated ids, then
insert all the subentities in another single SQL batch insert. To know which "parent id" to use for
a given subentity of the second query, you need a way to associate a generated id with the correct
parent entity. The order of the parents in their batch, and the order of the generated ids, is the
only straighforward way.
I know all this could be made into a single SQL query, without having to associate the generated ids
to the parents manually. But sometimes you have to fight /really /hard agains your framework or JDBC
itself to write such more complex query, where two batch inserts are very natural.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-09 15:39 Dave Cramer <davecramer@postgres.rocks>
parent: electrotype <electrotype@gmail.com>
1 sibling, 0 replies; 11+ messages in thread
From: Dave Cramer @ 2020-12-09 15:39 UTC (permalink / raw)
To: electrotype <electrotype@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Wed, 9 Dec 2020 at 10:21, electrotype <electrotype@gmail.com> wrote:
> So I'm curious. Why does order matter ?
>
> Dave Cramer
> www.postgres.rocks
>
>
> When you have to save multiple new entities with subentities.
>
> You first save all the parent entities in a single SQL batch insert, you
> get the generated ids, then insert all the subentities in another single
> SQL batch insert. To know which "parent id" to use for a given subentity of
> the second query, you need a way to associate a generated id with the
> correct parent entity. The order of the parents in their batch, and the
> order of the generated ids, is the only straighforward way.
>
> I know all this could be made into a single SQL query, without having to
> associate the generated ids to the parents manually. But sometimes you have
> to fight *really *hard agains your framework or JDBC itself to write such
> more complex query, where two batch inserts are very natural.
>
Fair enough, however the spec does not say anything about the order. In
fact it doesn't even say which keys will be returned.
I don't think we can make any guarantees here.
Dave
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-09 15:41 David G. Johnston <david.g.johnston@gmail.com>
parent: electrotype <electrotype@gmail.com>
1 sibling, 1 reply; 11+ messages in thread
From: David G. Johnston @ 2020-12-09 15:41 UTC (permalink / raw)
To: electrotype <electrotype@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Wed, Dec 9, 2020 at 8:20 AM electrotype <electrotype@gmail.com> wrote:
> So I'm curious. Why does order matter ?
>
> Dave Cramer
> www.postgres.rocks
>
>
> When you have to save multiple new entities with subentities.
>
> You first save all the parent entities in a single SQL batch insert, you
> get the generated ids, then insert all the subentities in another single
> SQL batch insert. To know which "parent id" to use for a given subentity of
> the second query, you need a way to associate a generated id with the
> correct parent entity. The order of the parents in their batch, and the
> order of the generated ids, is the only straighforward way.
>
> I know all this could be made into a single SQL query, without having to
> associate the generated ids to the parents manually. But sometimes you have
> to fight *really *hard agains your framework or JDBC itself to write such
> more complex query, where two batch inserts are very natural.
>
Agreed.
However, this isn't really the purview of JDBC - I'm doubting it does
anything that would cause the order to be different than what is received,
and the batch items are sent and results processed sequentially.
The main question is whether any batch items are inserting multiple records
themselves - i.e., RETURNING * is producing multiple results. Whatever
order RETURNING * produces is what the driver will capture - but it isn't
responsible for guaranteeing that the order of multiple inserted records
going in matches what comes out. PostgreSQL needs to make that claim. I
don't see where it does (i've sent an email to see if adding such a claim
to the documentation is proper). Done manually one can always do "WITH
insert returning SELECT ORDER BY", but it doesn't seem workable for the
driver to try and do that when adding the returning clause, which I presume
is what is in scope here.
David J.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-09 19:28 electrotype <electrotype@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: electrotype @ 2020-12-09 19:28 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Agreed.
>
> However, this isn't really the purview of JDBC - I'm doubting it does anything that would cause
> the order to be different than what is received, and the batch items are sent and results
> processed sequentially.
>
> The main question is whether any batch items are inserting multiple records themselves - i.e.,
> RETURNING * is producing multiple results. Whatever order RETURNING * produces is what the driver
> will capture - but it isn't responsible for guaranteeing that the order of multiple inserted
> records going in matches what comes out. PostgreSQL needs to make that claim. I don't see where
> it does (i've sent an email to see if adding such a claim to the documentation is proper). Done
> manually one can always do "WITH insert returning SELECT ORDER BY", but it doesn't seem workable
> for the driver to try and do that when adding the returning clause, which I presume is what is in
> scope here.
>
> David J.
>
Thank you, it's appreciated! I'm sure this clarification would help other developers too.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-11 00:36 David G. Johnston <david.g.johnston@gmail.com>
parent: electrotype <electrotype@gmail.com>
0 siblings, 2 replies; 11+ messages in thread
From: David G. Johnston @ 2020-12-11 00:36 UTC (permalink / raw)
To: electrotype <electrotype@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Wed, Dec 9, 2020 at 1:31 PM electrotype <electrotype@gmail.com> wrote:
> Agreed.
>
>
> However, this isn't really the purview of JDBC - I'm doubting it does
> anything that would cause the order to be different than what is received,
> and the batch items are sent and results processed sequentially.
>
> The main question is whether any batch items are inserting multiple
> records themselves - i.e., RETURNING * is producing multiple results.
> Whatever order RETURNING * produces is what the driver will capture - but
> it isn't responsible for guaranteeing that the order of multiple inserted
> records going in matches what comes out. PostgreSQL needs to make that
> claim. I don't see where it does (i've sent an email to see if adding such
> a claim to the documentation is proper). Done manually one can always do
> "WITH insert returning SELECT ORDER BY", but it doesn't seem workable for
> the driver to try and do that when adding the returning clause, which I
> presume is what is in scope here.
>
> David J.
>
> Thank you, it's appreciated! I'm sure this clarification would help other
> developers too.
>
My take is that there is presently no guarantee, and that with current
efforts to add parallelism it is quite probable that observation of such
non-orderedness is simply a matter of time. With batching it seems best to
combine its use with single inserts in order to avoid this problem.
David J.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-11 11:01 Dave Cramer <davecramer@postgres.rocks>
parent: David G. Johnston <david.g.johnston@gmail.com>
1 sibling, 0 replies; 11+ messages in thread
From: Dave Cramer @ 2020-12-11 11:01 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: electrotype <electrotype@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, 10 Dec 2020 at 19:37, David G. Johnston <david.g.johnston@gmail.com>
wrote:
> On Wed, Dec 9, 2020 at 1:31 PM electrotype <electrotype@gmail.com> wrote:
>
>> Agreed.
>>
>>
>> However, this isn't really the purview of JDBC - I'm doubting it does
>> anything that would cause the order to be different than what is received,
>> and the batch items are sent and results processed sequentially.
>>
>> The main question is whether any batch items are inserting multiple
>> records themselves - i.e., RETURNING * is producing multiple results.
>> Whatever order RETURNING * produces is what the driver will capture - but
>> it isn't responsible for guaranteeing that the order of multiple inserted
>> records going in matches what comes out. PostgreSQL needs to make that
>> claim. I don't see where it does (i've sent an email to see if adding such
>> a claim to the documentation is proper). Done manually one can always do
>> "WITH insert returning SELECT ORDER BY", but it doesn't seem workable for
>> the driver to try and do that when adding the returning clause, which I
>> presume is what is in scope here.
>>
>> David J.
>>
>> Thank you, it's appreciated! I'm sure this clarification would help other
>> developers too.
>>
>
> My take is that there is presently no guarantee, and that with current
> efforts to add parallelism it is quite probable that observation of such
> non-orderedness is simply a matter of time. With batching it seems best to
> combine its use with single inserts in order to avoid this problem.
>
> David J.
>
I'd have to agree.
Dave Cramer
www.postgres.rocks
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
@ 2020-12-11 14:36 electrotype <electrotype@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
1 sibling, 0 replies; 11+ messages in thread
From: electrotype @ 2020-12-11 14:36 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
My take is that there is presently no guarantee, and that with current efforts to add parallelism it
is quite probable that observation of such non-orderedness is simply a matter of time. With batching
it seems best to combine its use with single inserts in order to avoid this problem.
>
> David J.
>
I wish that was not the conclusion, but at least it is clear!
Thanks for the help to both of you.
^ permalink raw reply [nested|flat] 11+ messages in thread
end of thread, other threads:[~2020-12-11 14:36 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-12-06 20:51 JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made? electrotype <electrotype@gmail.com>
2020-12-08 23:23 ` Dave Cramer <davecramer@postgres.rocks>
2020-12-09 01:23 ` electrotype <electrotype@gmail.com>
2020-12-09 10:21 ` Dave Cramer <davecramer@postgres.rocks>
2020-12-09 14:48 ` electrotype <electrotype@gmail.com>
2020-12-09 15:39 ` Dave Cramer <davecramer@postgres.rocks>
2020-12-09 15:41 ` David G. Johnston <david.g.johnston@gmail.com>
2020-12-09 19:28 ` electrotype <electrotype@gmail.com>
2020-12-11 00:36 ` David G. Johnston <david.g.johnston@gmail.com>
2020-12-11 11:01 ` Dave Cramer <davecramer@postgres.rocks>
2020-12-11 14:36 ` electrotype <electrotype@gmail.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