From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Ken Tanzer <ken.tanzer@gmail.com>
Cc: Rich Shepard <rshepard@appl-ecosys.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Primary key data type: integer vs identity
Date: Fri, 19 Apr 2019 12:02:03 -0700
Message-ID: <4d387c8e-a261-0734-a10f-dfde7ce538f0@aklaver.com> (raw)
In-Reply-To: <CAD3a31VyBMjZUM7aXLfCEdkP_gQ4Agw9-vMFm6nB_eV7sOa3OA@mail.gmail.com>
References: <alpine.LNX.2.20.1904191050120.24219@salmo.appl-ecosys.com>
<c1df337c-8819-711b-496e-459e7fb88408@aklaver.com>
<alpine.LNX.2.20.1904191112120.24219@salmo.appl-ecosys.com>
<b0da4cc6-be57-ddaf-f762-ac4455569417@aklaver.com>
<CAD3a31VyBMjZUM7aXLfCEdkP_gQ4Agw9-vMFm6nB_eV7sOa3OA@mail.gmail.com>
On 4/19/19 11:32 AM, Ken Tanzer wrote:
> On Fri, Apr 19, 2019 at 11:20 AM Adrian Klaver
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, ken.tanzer@gmail.com, rshepard@appl-ecosys.com
Subject: Re: Primary key data type: integer vs identity
In-Reply-To: <4d387c8e-a261-0734-a10f-dfde7ce538f0@aklaver.com>
References: <alpine.LNX.2.20.1904191050120.24219@salmo.appl-ecosys.com> <c1df337c-8819-711b-496e-459e7fb88408@aklaver.com> <alpine.LNX.2.20.1904191112120.24219@salmo.appl-ecosys.com> <b0da4cc6-be57-ddaf-f762-ac4455569417@aklaver.com> <CAD3a31VyBMjZUM7aXLfCEdkP_gQ4Agw9-vMFm6nB_eV7sOa3OA@mail.gmail.com> <4d387c8e-a261-0734-a10f-dfde7ce538f0@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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