Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fpTaE-000834-G6 for pgsql-general@arkaria.postgresql.org; Tue, 14 Aug 2018 07:17:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fpTaB-0003uz-W5 for pgsql-general@arkaria.postgresql.org; Tue, 14 Aug 2018 07:17:35 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fpTaB-0003uq-KN for pgsql-general@lists.postgresql.org; Tue, 14 Aug 2018 07:17:35 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fpTa8-0001qk-Gw for pgsql-general@lists.postgresql.org; Tue, 14 Aug 2018 07:17:34 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id C939F4154F; Tue, 14 Aug 2018 09:17:30 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id YBPzGSHIHy2T; Tue, 14 Aug 2018 09:17:29 +0200 (CEST) Message-ID: <1534231047.2379.8.camel@cybertec.at> Subject: Re: 'Identifier' columns From: Laurenz Albe To: David Favro , pgsql-general@lists.postgresql.org Date: Tue, 14 Aug 2018 09:17:27 +0200 In-Reply-To: <20180814022005.E2B66FB1@mail.meta-dynamic.com> References: <20180814022005.E2B66FB1@mail.meta-dynamic.com> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David Favro wrote: > A couple of questions about auto-assigned identifier columns, > forgive my ignorance, I'm used to other methods to create IDs... > > 1. If creating a new application [i.e. no "legacy" reasons to do > anything] using PostgreSQL 10, when creating an "auto-assigned > integer ID" column, what are the advantages/disadvantages of using > the 'SERIAL' datatype [or equivalent explicitly created SEQUENCE w/ > nextval() used as default for column] versus the SQL-standard > 'integer GENERATED AS IDENTITY'? All other things being equal, it > would seem a no-brainer to follow the standard. Absolutely. Use GENERATED ALWAYS AS IDENTITY. > 2. When using the SQL-standard 'integer GENERATED AS IDENTITY' > column, after inserting a column, what is the recommended method to > find the ID of the just-inserted row? Is there no SQL-standard way? > The docs seem to imply (without explicitly stating) that a SEQUENCE > is used behind the scenes hence 'currval()' could be used, but I > didn't see in the docs any mention of what the underlying sequence's > name is, or how to specify a name. Perhaps 'lastval()' would work, > but not in all cases and in any event it has a sloppy feel to me. The best way is to use the (non-standard) RETURNING clause: INSERT ... RETURNING id; Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com