pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedTrouble incrementing a column
5+ messages / 4 participants
[nested] [flat]
* Trouble incrementing a column
@ 2019-11-23 21:28 Blake McBride <blake1024@gmail.com>
0 siblings, 2 replies; 5+ messages in thread
From: Blake McBride @ 2019-11-23 21:28 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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?
Thanks!
Blake McBride
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Trouble incrementing a column
@ 2019-11-23 21:42 Ron <ronljohnsonjr@gmail.com>
parent: Blake McBride <blake1024@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: Ron @ 2019-11-23 21:42 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Trouble incrementing a column
@ 2019-11-23 21:47 Tom Lane <tgl@sss.pgh.pa.us>
parent: Blake McBride <blake1024@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Tom Lane @ 2019-11-23 21:47 UTC (permalink / raw)
To: Blake McBride <blake1024@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
Blake McBride <blake1024@gmail.com> writes:
> 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).
Postgres only treats primary/unique keys that way if you explicitly
mark the constraint as DEFERRABLE. Otherwise, the uniqueness check is
made immediately as each row is updated, so it's very order-dependent
as to whether something like the above will work.
Note that you pay a fairly substantial performance penalty for deferring
the check, which is why it isn't the default, even though the SQL spec
says it ought to be.
This is documented in some obscure place [ ... looks around ... ]
ah, see "Non-Deferred Uniqueness Constraints" under Compatibility
in the CREATE TABLE reference page.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Trouble incrementing a column
@ 2019-11-25 00:51 Jeff Janes <jeff.janes@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: Jeff Janes @ 2019-11-25 00:51 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Blake McBride <blake1024@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>
On Sat, Nov 23, 2019 at 4:47 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Note that you pay a fairly substantial performance penalty for deferring
> the check, which is why it isn't the default, even though the SQL spec
> says it ought to be.
>
Do you know what the worst case scenario is for the performance of
deferring the check to the end of the statement (with deferred initially
immediate)? Upon testing, I get a penalty of 2 to 5%, which seems pretty
small, but I might not be testing the most adverse situation. See attached.
The main "cost" that prevents from using DII routinely is that they can't
receive foreign key constraints.
Cheers,
Jeff
Attachments:
bench_dii.sh (text/x-sh, 707B) download
psql <<'END'
create table jj_dii (id bigserial primary key, jj bigint unique deferrable initially immediate);
create table jj_did (id bigserial primary key, jj bigint unique deferrable initially deferred);
create table jj_nd (id bigserial primary key, jj bigint unique not deferrable);
insert into jj_nd select x,x::bigint*1000000 from generate_series(1,100000) f(x);
insert into jj_did select x,x::bigint*1000000 from generate_series(1,100000) f(x);
insert into jj_dii select x,x::bigint*1000000 from generate_series(1,100000) f(x);
END
for f in `seq 1 200`; do
for mode in `shuf -e nd dii`; do
echo "JJ $f $mode"
pgbench -T30 -f <(echo "update jj_$mode set jj=jj+1") -n -M prepared
done
done
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Trouble incrementing a column
@ 2019-11-25 01:08 Tom Lane <tgl@sss.pgh.pa.us>
parent: Jeff Janes <jeff.janes@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2019-11-25 01:08 UTC (permalink / raw)
To: Jeff Janes <jeff.janes@gmail.com>; +Cc: Blake McBride <blake1024@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>
Jeff Janes <jeff.janes@gmail.com> writes:
> On Sat, Nov 23, 2019 at 4:47 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Note that you pay a fairly substantial performance penalty for deferring
>> the check, which is why it isn't the default, even though the SQL spec
>> says it ought to be.
> Do you know what the worst case scenario is for the performance of
> deferring the check to the end of the statement (with deferred initially
> immediate)? Upon testing, I get a penalty of 2 to 5%, which seems pretty
> small, but I might not be testing the most adverse situation. See attached.
Hm, I would have expected more, though not factor-of-10 or anything
like that. But that's just vague recollection from when we put in
the feature. I'm not surprised if the numbers have moved since.
> The main "cost" that prevents from using DII routinely is that they can't
> receive foreign key constraints.
Yeah, that's an issue.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2019-11-25 01:08 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-11-23 21:28 Trouble incrementing a column Blake McBride <blake1024@gmail.com>
2019-11-23 21:42 ` Ron <ronljohnsonjr@gmail.com>
2019-11-23 21:47 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-11-25 00:51 ` Jeff Janes <jeff.janes@gmail.com>
2019-11-25 01:08 ` Tom Lane <tgl@sss.pgh.pa.us>
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