Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?

From: Dave Cramer <davecramer(at)postgres(dot)rocks>
To: electrotype <electrotype(at)gmail(dot)com>
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
Date: 2020-12-09 15:39:06
Message-ID: CADK3HH+Uvw3=_diiVMYbT83tGzH9V+cPTqPkqfm1Coxaz73rWw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 9 Dec 2020 at 10:21, electrotype <electrotype(at)gmail(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2020-12-09 15:41:12 Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
Previous Message Niels Jespersen 2020-12-09 15:33:03 SV: Tools showing table partitions as tables in listings of tables