Re: Two sequences associated with one identity column

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Rumpi Gravenstein <rgravens(at)gmail(dot)com>
Cc: Colin 't Hart <colinthart(at)gmail(dot)com>, PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Two sequences associated with one identity column
Date: 2025-10-30 16:32:57
Message-ID: 77e6127d-2d1f-440e-b9fe-e1a523ff1329@aklaver.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/30/25 08:22, Rumpi Gravenstein wrote:
> I've seen two indexes created on the same table/column when you create a
> primary key as part of table create ddl and then also run a separate
> create index statement for the same table/column.

Yes it is possible to create two indexes on a given table column, the
issue here though is, from this post:

https://www.postgresql.org/message-id/CAMon-aQ0Zs-Otkp1%3Dzk3WYt7Am-_oQHKEmD_rZ1CNGBAgM_jzw%40mail.gmail.com

The two indexes are coming from:

--
-- Name: <sequence1>; Type: SEQUENCE; Schema: <schema>; Owner: <owner>
--

ALTER TABLE <schema>.<tablename> ALTER COLUMN id ADD GENERATED ALWAYS
AS IDENTITY (
SEQUENCE NAME <schema>.<sequence1>
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);

--
-- Name: <sequence2>; Type: SEQUENCE; Schema: <schema>; Owner: <owner>
--

ALTER TABLE <schema>.<tablename> ALTER COLUMN id ADD GENERATED ALWAYS
AS IDENTITY (
SEQUENCE NAME <schema>.<sequence2>
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);

That is two GENERATED ALWAYS AS IDENTITY sequences being created for the
PK. That should not happen.

>
> On Thu, Oct 30, 2025 at 10:42 AM Adrian Klaver
> <adrian(dot)klaver(at)aklaver(dot)com <mailto:adrian(dot)klaver(at)aklaver(dot)com>> wrote:
>
> On 10/30/25 01:55, Colin 't Hart wrote:
> >           relname          | relnamespace | relpersistence
> > --------------------------+--------------+----------------o
> >   <sequence1>  |    524799410 | p
> >   <sequence2>  |    524799410 | p
> > (2 rows)
> >
>
> Well so much for that guess. I was exploring the idea that the sequence
> may have been unlogged at some point and you had both a logged(p) and
> unlogged(u) instance of each.
>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)aklaver(dot)com <mailto:adrian(dot)klaver(at)aklaver(dot)com>
>
>
>
>
> --
> Rumpi Gravenstein

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ron Johnson 2025-10-30 17:00:05 Re: Why isn't my table auto-analyzed/vacuumed?
Previous Message Dimitrios Apostolou 2025-10-30 15:55:18 Why isn't my table auto-analyzed/vacuumed?