pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Trouble incrementing a column
Date: Sat, 23 Nov 2019 15:42:53 -0600
Message-ID: <8273132a-8680-6cd5-e274-b42ff3066b87@gmail.com> (raw)
In-Reply-To: <CABwHSOuxyDcnZ7xhj1uQYT09=94-x6O051x=_iK8hiOXoAgCrA@mail.gmail.com>
References: <CABwHSOuxyDcnZ7xhj1uQYT09=94-x6O051x=_iK8hiOXoAgCrA@mail.gmail.com>

On 11/23/19 3:28 PM, Blake McBride wrote:
> Greetings,
>
> I am using PostgreSQL 10.10.  I am having trouble incrementing a column 
> for reasons I can't see.  It's probably some basic SQL thing.  Your help 
> is appreciated.
>
> create table my_table (
>     listid char(36) not null,
>     seq smallint not null,
>     item varchar(4096),
>     primary key (listid, seq)
> );
>
> insert into my_table (listid, seq) values ('abc', 1);
> insert into my_table (listid, seq) values ('abc', 2);
>
> -- the following works some of the time
> update my_table set seq=seq+1;
>
> -- the following doe not work for reasons I do not know
> update my_table set seq=seq+1 where listid='abc';
>
> What I get is a duplicate primary key.  I wouldn't think I'd get that 
> because I'd think the whole thing is done in a transaction so that 
> duplicate checks wouldn't be done till the end (essentially).
>
> Is there a clean way to do this?

A deferrable constraint might solve the problem.

https://www.commandprompt.com/blog/postgres_deferred_primary_keys/


https://www.postgresql.org/docs/9.6/sql-altertable.html

ALTER TABLE ... ALTER CONSTRAINT ... DEFERRABLE INITIALLY DEFERRED;



-- 
Angular momentum makes the world go 'round.





view thread (5+ messages)  latest in thread

Message-ID: <8273132a-8680-6cd5-e274-b42ff3066b87@gmail.com>
Permalink:  ../8273132a-8680-6cd5-e274-b42ff3066b87@gmail.com/
Also on:    postgresql.org/message-id/8273132a-8680-6cd5-e274-b42ff3066b87@gmail.com

 · 

reply

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: ronljohnsonjr@gmail.com
  Subject: Re: Trouble incrementing a column
  In-Reply-To: <8273132a-8680-6cd5-e274-b42ff3066b87@gmail.com>
  References: <CABwHSOuxyDcnZ7xhj1uQYT09=94-x6O051x=_iK8hiOXoAgCrA@mail.gmail.com> <8273132a-8680-6cd5-e274-b42ff3066b87@gmail.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