Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hHYm3-0005hY-3C for pgsql-general@arkaria.postgresql.org; Fri, 19 Apr 2019 19:02:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hHYm1-00041d-OB for pgsql-general@arkaria.postgresql.org; Fri, 19 Apr 2019 19:02:09 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hHYm1-00041W-B1 for pgsql-general@lists.postgresql.org; Fri, 19 Apr 2019 19:02:09 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hHYly-0001hr-PW for pgsql-general@lists.postgresql.org; Fri, 19 Apr 2019 19:02:08 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 7BD315EF; Fri, 19 Apr 2019 15:02:05 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 19 Apr 2019 15:02:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=3 T3TnYOLHG94OD4aw5ZinzBRsF9hpKkFSH4zsqR/psE=; b=gzptvZ3hRKP564boo TynWq/hMRR5d01bc3iuoyQXRVCTV2mer/Ef+YzMiCMt6gczR5Zqdy3bLrHElG7Em xumltLQ5R7FhuDHQMGfunaRR+DA5dVV48sXgWn7KwfEo/PipP2C1a8Mawia28CXs WlxFeWHOqq3s5l5DV41aakYW+aQbBLz63Mm1nBwFjz3hLENIva7xkH0Ez450faSq R+Xh8TyLyNWGjhFwGZv3b2HT5y0n34ZAcik4v9cvamMYpKdvA3Qz+9FxkWF+epWF RyzlzoiQjDmyX5CrnIU3Wk8lAD+iJ/5bpxBd6EGGSbmL9xzvYMJC+zIZ91F1MSc6 rGXsw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=3T3TnYOLHG94OD4aw5ZinzBRsF9hpKkFSH4zsqR/p sE=; b=gEGm3VgykmFC8z/p2A9UQAHvqQZcesuiNMoVF+bdiGWXQXbvXhJJL+I+H lZZCcKoVnUmIoHspsZiFbZ4uz8wOd9uTcILvlc/itl6L7RR7fyFzPT+JHZWy3quK IBFfngvZsBLK0ryNYHRMdoUeNdcoF3E1+FUwC5C6cg6RCWQRmpAyn/vwAhWTi3Jq 9GIPacyaDNfi+UrREYceylaEaDO9EcYqouoCTbPLMig1FBUi6911igeDXRhsSO8w UrF0CMV7Qe0EUyxI5NwO/0nYV/tu5d45IQnf0yYebxayjIQgxxFvDjsHWwnBsYty slGZ9NNkY621vw9iFNhF82ueLIlkg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrfeekgddviecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecukfhppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhho mheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvg hrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id C80A8E4753; Fri, 19 Apr 2019 15:02:03 -0400 (EDT) Subject: Re: Primary key data type: integer vs identity To: Ken Tanzer Cc: Rich Shepard , "pgsql-generallists.postgresql.org" References: From: Adrian Klaver Message-ID: <4d387c8e-a261-0734-a10f-dfde7ce538f0@aklaver.com> Date: Fri, 19 Apr 2019 12:02:03 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/19/19 11:32 AM, Ken Tanzer wrote: > On Fri, Apr 19, 2019 at 11:20 AM Adrian Klaver > > wrote: > > On 4/19/19 11:14 AM, Rich Shepard wrote: > > On Fri, 19 Apr 2019, Adrian Klaver wrote: > > > >> If it is working for you now I see no reason to switch. > > > > Adrian, > > > > It does work. I just learned about the SQL identity type and want > to learn > > when it's most appropriate to use. The databases I develop all > work with > > integers as primary keys and reading about the type didn't > clarify (for me) > > when it should be used. > > Mainly for folks that want cross database SQL compliance. It is not a > type so much as a way of specifying an auto-increment column. > > > It also sounds like it has advantages in terms of tying your sequence > directly to the column.  If you drop a serial column, it doesn't drop > the sequence. A serial column will: test=> create table serial_test(id serial); CREATE TABLE test=> \d serial_test Table "public.serial_test" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+----------------------------------------- id | integer | | not null | nextval('serial_test_id_seq'::regclass) test=> select * from serial_test_id_seq ; last_value | log_cnt | is_called ------------+---------+----------- 1 | 0 | f (1 row) test=> drop table serial_test ; DROP TABLE test=> select * from serial_test_id_seq ; ERROR: relation "serial_test_id_seq" does not exist LINE 1: select * from serial_test_id_seq ; If you just use a sequence as a default value it will not unless you make it OWNED by the table per the link I posted upstream. > Once I've upgraded to 10+, I might look at converting my existing serial > columns.  Peter Eisentraut wrote a good piece(1) on identity columns, > including a function for converting existing serial columns.  I've > copied the function below, but had two questions about it: > > 1)  Would the function as written also reassign ownership to that table > column?  (I see the update to pg_depend and pg_attribute, but don't know > enough about them to know if that changes ownership) > 2)  Would one have to be a superuser to do this? > > Thanks, > Ken -- Adrian Klaver adrian.klaver@aklaver.com