Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kn1Gw-0004LV-DX for pgsql-general@arkaria.postgresql.org; Wed, 09 Dec 2020 15:20:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kn1Gv-00087T-DR for pgsql-general@arkaria.postgresql.org; Wed, 09 Dec 2020 15:20:53 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kn0lu-0007AH-Me for pgsql-general@lists.postgresql.org; Wed, 09 Dec 2020 14:48:50 +0000 Received: from mail-qv1-xf34.google.com ([2607:f8b0:4864:20::f34]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kn0ls-0002mH-2z for pgsql-general@lists.postgresql.org; Wed, 09 Dec 2020 14:48:49 +0000 Received: by mail-qv1-xf34.google.com with SMTP id dm12so667403qvb.3 for ; Wed, 09 Dec 2020 06:48:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=Y7VOSSP3tPYAdMwY7zi+GHVuY3WvMEsu7ulUtmEID7o=; b=DQ43NX7IaVx3yzsUd4l4+g0bSuC+QKb0lR0YaNyHxI31fVvENrn2isDJoh3/mYPTWC bDU4axWUO32PthaSQk5UgCDDUFzyC05hfHKfrgOhmYVM2jGonZsp4hItL/IaZNR0mMF4 0Ukwgc2x5bMIpACdOMb5xTfVNRtVs+1PNj4GRkKeJ6rW+AAVX1ScEk2tTsV/Jfrs4L0C DKcxDurb9bYigfOrb3LMo5/hR06m80mgjrxj5tHV7jTVAdY3ni2FBfKZYZy6+R79dDb0 vPJf0ahb7Z8pMsgT6oQyH/fLzuHvkb5Hy5fx7T0WXt1BrOx6cGaEZ5VejmlWS4VI4OFh Rj+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=Y7VOSSP3tPYAdMwY7zi+GHVuY3WvMEsu7ulUtmEID7o=; b=iTSKkXjHrdWRFxfbZAMA2WuRPgtxg6Dc8iwJkMPS5uTslkIqRZFUGaUi3smmGCIl9h OREY2/+YzCuxKfu0HPFD7qnHLFRgKtub88N66jJJ69TldWClQ7gSzpBO75Lr/WEtZZrZ gCZ3j6aVesCpKatsMrQvba0ngNo/tODfqYUkBVB4N6jtvR2/B26+O9vfoJmhJ1UxD5My 0Syu+yzNMv4rKXzVto4jd0dSCA3G8Zz+7Xwek5znIH2txe4KfgbbciXPTNG7A8Lb6G7q h39S6R4Fy6geGmV4r1vlvz5nca7jtzWGBKTu76iIrfONl/p1mwLJYytItnK4X0xZ6C1L pX5A== X-Gm-Message-State: AOAM533zrntutMB1I3BQzZ4rexxC4NKi9lpkLvaPEUDPLLv9FieK7pmC nPg0zMR3wzkurBrHowxBnGdraQ9BPjo= X-Google-Smtp-Source: ABdhPJy1TSsNepGm2z921rFMJmoQQwPYWaOTUoapj14pvFiM5SaMXzJf2i2R6TE9YbTsJGQMFMLmOw== X-Received: by 2002:ad4:4e31:: with SMTP id dm17mr3361455qvb.27.1607525326071; Wed, 09 Dec 2020 06:48:46 -0800 (PST) Received: from [192.168.50.211] ([104.163.190.166]) by smtp.gmail.com with ESMTPSA id b14sm1012645qti.97.2020.12.09.06.48.44 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Dec 2020 06:48:45 -0800 (PST) Subject: Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made? To: "pgsql-generallists.postgresql.org" References: <36689061-0844-2dfd-a4ee-8006d8a15ca9@gmail.com> <1d0c8b21-25cc-2a3e-7f25-81f2de645f1e@gmail.com> From: electrotype Message-ID: <4ce0f5f5-d608-887b-b5ef-21ae187e3b58@gmail.com> Date: Wed, 9 Dec 2020 09:48:42 -0500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------A489E1167893E7A51C38161B" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------A489E1167893E7A51C38161B Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit > 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. --------------A489E1167893E7A51C38161B Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit
So I'm curious. Why does order matter ?

Dave Cramer


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.

--------------A489E1167893E7A51C38161B--