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 1iYdBM-0005L9-2K for pgsql-general@arkaria.postgresql.org; Sat, 23 Nov 2019 21:43:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iYdBK-0007Xv-Qv for pgsql-general@arkaria.postgresql.org; Sat, 23 Nov 2019 21:43:06 +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 1iYdBK-0007Xo-At for pgsql-general@lists.postgresql.org; Sat, 23 Nov 2019 21:43:06 +0000 Received: from mail-ot1-x342.google.com ([2607:f8b0:4864:20::342]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iYdBA-0004eA-Bx for pgsql-general@lists.postgresql.org; Sat, 23 Nov 2019 21:43:05 +0000 Received: by mail-ot1-x342.google.com with SMTP id 94so9312047oty.8 for ; Sat, 23 Nov 2019 13:42:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=3pzG4H7ofLhEvRZQQU1x9jj33mhG7oS3IFKu7DCIVr4=; b=IesT6nqJg+aE+G3l0EyrsvCxB4vusIMfG244cLPAIWewn9GaWb9hhTT3s7luCPX/Be Lru273ymqAAuTuPjQ6jCh8RTJVzMwpHylMN5OkC2GqpjoF2yEXdwq6yYAqnslWYfWbmt 76RYREVqZ4VuEfj4XSslb6kS02EY5X5oFXtUvL7KvorYLtULOu0HeZcN2DR3w6KbaF4w KelGf9MBZdBYKOWZqZ6lAC4JV8fAMKgaO6drvWB3C0P8ZqOExmLrxOQJAGgTpFzGHWUu gvxv/JNlbywo2Qcvv6+ya1E2PwbWxTLJ+Wfut/hvkwiDJV/tIQwMg+lU2+JitX6ecPxl JqFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=3pzG4H7ofLhEvRZQQU1x9jj33mhG7oS3IFKu7DCIVr4=; b=q3gZ4lWY6nN4wXZQDAgMoHYGcPDVMASdRl3usXDrDsn81d29v0T5hW3d0xCimdM3jl bGnt8glbryJB3kgzkM2fbOy92n++LcoHN/ZlJEjmZrJrYvU6xlPqfvwq627DesHXaCgc fUfSbAft4Iwte5zYDoDj3KlWPBN/dS9/PhfRm8xze+MZKqATUePsgtQlY4FIZNOIx+XP IYWqJ6UXojfD5WlNOjNtOLns6pAEMhXiwySf1haRMiG2qVnGuqxEiAglSPtynGWDcknE 2nkJUaJHUeqxGMgTZNwwAz/wyUmzcA3bDv9oz3AvHmv5sZAKNoV3MWUApQk/G07qqtbu 0Jfg== X-Gm-Message-State: APjAAAWgmlYxFpBm+ZuaLPk9BYiyQFEbel6IXSQTEmDrYypdPMHNYgtV QlSXQ+e8JzBDy4Ii5BSK+24MZkHJJWvtZg== X-Google-Smtp-Source: APXvYqymAdWMVnONESNkg3gcfgff1Sa8E6mLrKmgtoV9zHlDzjiYyXc1EIoFc4pS4SQot/ehqEgVfQ== X-Received: by 2002:a05:6830:3:: with SMTP id c3mr14813486otp.15.1574545375137; Sat, 23 Nov 2019 13:42:55 -0800 (PST) Received: from [192.168.1.10] (ip70-186-126-197.no.no.cox.net. [70.186.126.197]) by smtp.googlemail.com with ESMTPSA id m20sm657706otr.47.2019.11.23.13.42.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 23 Nov 2019 13:42:54 -0800 (PST) Subject: Re: Trouble incrementing a column To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <8273132a-8680-6cd5-e274-b42ff3066b87@gmail.com> Date: Sat, 23 Nov 2019 15:42:53 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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.