database-sequence

Sequences not affected by transactions?

谁都会走 提交于 2019-11-27 22:43:45
I have a table create table testtable( testtable_rid serial not null, data integer not null, constraint pk_testtable primary key(testtable_rid) ); So lets say I do this code about 20 times: begin; insert into testtable (data) values (0); rollback; and then I do begin; insert into testtable (data) values (0); commit; And finally a select * from testtable Result: row0: testtable_rid=21 | data=0 Expected result: row0: testtable_rid=1 | data=0 As you can see, sequences do not appear to be affected by transaction rollbacks. They continue to increment as if the transaction was committed and then the

Postgres manually alter sequence

六月ゝ 毕业季﹏ 提交于 2019-11-27 10:20:36
I'm trying to set a sequence to a specific value. SELECT setval('payments_id_seq'), 21, true This gives an error: ERROR: function setval(unknown) does not exist Using ALTER SEQUENCE doesn't seem to work either? ALTER SEQUENCE payments_id_seq LASTVALUE 22 How can this be done? Ref: https://www.postgresql.org/docs/current/static/functions-sequence.html NPE The parentheses are misplaced: SELECT setval('payments_id_seq', 21, true); # next value will be 22 Otherwise you're calling setval with a single argument, while it requires two or three. This syntax isn't valid in any version of PostgreSQL:

Sequences not affected by transactions?

不羁岁月 提交于 2019-11-27 04:00:51
问题 I have a table create table testtable( testtable_rid serial not null, data integer not null, constraint pk_testtable primary key(testtable_rid) ); So lets say I do this code about 20 times: begin; insert into testtable (data) values (0); rollback; and then I do begin; insert into testtable (data) values (0); commit; And finally a select * from testtable Result: row0: testtable_rid=21 | data=0 Expected result: row0: testtable_rid=1 | data=0 As you can see, sequences do not appear to be

Postgres manually alter sequence

余生颓废 提交于 2019-11-26 12:51:19
问题 I\'m trying to set a sequence to a specific value. SELECT setval(\'payments_id_seq\'), 21, true This gives an error: ERROR: function setval(unknown) does not exist Using ALTER SEQUENCE doesn\'t seem to work either? ALTER SEQUENCE payments_id_seq LASTVALUE 22 How can this be done? Ref: https://www.postgresql.org/docs/current/static/functions-sequence.html 回答1: The parentheses are misplaced: SELECT setval('payments_id_seq', 21, true); # next value will be 22 Otherwise you're calling setval with

How to reset postgres' primary key sequence when it falls out of sync?

大憨熊 提交于 2019-11-25 21:56:28
问题 I ran into the problem that my primary key sequence is not in sync with my table rows. That is, when I insert a new row I get a duplicate key error because the sequence implied in the serial datatype returns a number that already exists. It seems to be caused by import/restores not maintaining the sequence properly. 回答1: -- Login to psql and run the following -- What is the result? SELECT MAX(id) FROM your_table; -- Then run... -- This should be higher than the last result. SELECT nextval(