pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Problem with identity column & related sequences
3+ messages / 2 participants
[nested] [flat]

* Problem with identity column & related sequences
@ 2021-09-28 18:38  Jeff Hoffmann <jeff@propertykey.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Jeff Hoffmann @ 2021-09-28 18:38 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

I am using postgresql-12.8.  I am using I am making use of an identity
column for part of a scripts to process some updated data.  Because of
the way the script is called I don't necessarily know if this column
is going to exist in the table I am working on so I have a step that
will conditionally create the column if it doesn't already exist, i.e.

alter table mytable add column if not exists unique_id integer
generated always as identity;

This works great if the unique_id column doesn't exist.  If the column
does exist, I get the notice

NOTICE:  column "unique_id" of relation "mytable" already exists, skipping
ALTER TABLE

As far as the messages are concerned everything worked as expected.
The problem is that even though the column already exists it skipped
the first part of the command it and seems to have followed through at
least a portion of the second part and created a second sequence to
handle the generated identity value even though an existing sequence
already exists for the existing column.  Then when I try to update the
table I end up getting an error

ERROR:  more than one owned sequence found

which I guess makes sense based on what happened but it seems like the
"if not exists" should short circuit the whole thing and result in
nothing changing.  Now I'm stuck & I have to effectively drop the
column and re-add the column.  I found some references to other "more
than one owned sequence" issues from a couple of years back but this
seems to be a different issue.  My question is whether this is the
expected behavior and if so is there another way to get what I want
from a similar command (or commands)?  Right now I'm going through a
rather clunky plpgsql function to check if the column exists instead
of relying on the "if not exists logic".


-- 
Jeff Hoffmann
PropertyKey





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Problem with identity column & related sequences
@ 2021-09-28 19:13  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Jeff Hoffmann <jeff@propertykey.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2021-09-28 19:13 UTC (permalink / raw)
  To: Jeff Hoffmann <jeff@propertykey.com>; +Cc: pgsql-general@lists.postgresql.org

Jeff Hoffmann <jeff@propertykey.com> writes:
> I am using postgresql-12.8.  I am using I am making use of an identity
> column for part of a scripts to process some updated data.  Because of
> the way the script is called I don't necessarily know if this column
> is going to exist in the table I am working on so I have a step that
> will conditionally create the column if it doesn't already exist, i.e.

> alter table mytable add column if not exists unique_id integer
> generated always as identity;

You're right, this sort of thing does not work very well in v12 and
before.  We fixed it in v13, but the changes seemed far too invasive
to risk a back-patch [1].

			regards, tom lane

[1] https://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=1281a5c90





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Problem with identity column & related sequences
@ 2021-09-28 19:35  Jeff Hoffmann <jeff@propertykey.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Jeff Hoffmann @ 2021-09-28 19:35 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org

Thanks. I was sort of expecting that answer but I didn't see where it
was addressed specifically.  Unfortunately I'm stuck on v12 for the
time being so I guess it's back to the workaround.

On Tue, Sep 28, 2021 at 2:13 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Jeff Hoffmann <jeff@propertykey.com> writes:
> > I am using postgresql-12.8.  I am using I am making use of an identity
> > column for part of a scripts to process some updated data.  Because of
> > the way the script is called I don't necessarily know if this column
> > is going to exist in the table I am working on so I have a step that
> > will conditionally create the column if it doesn't already exist, i.e.
>
> > alter table mytable add column if not exists unique_id integer
> > generated always as identity;
>
> You're right, this sort of thing does not work very well in v12 and
> before.  We fixed it in v13, but the changes seemed far too invasive
> to risk a back-patch [1].
>
>                         regards, tom lane
>
> [1] https://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=1281a5c90



-- 
Jeff Hoffmann
Head Plate Spinner
PropertyKey
Office: 612-800-6444
jeff@propertykey.com





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2021-09-28 19:35 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-09-28 18:38 Problem with identity column & related sequences Jeff Hoffmann <jeff@propertykey.com>
2021-09-28 19:13 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-09-28 19:35   ` Jeff Hoffmann <jeff@propertykey.com>

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