-
Notifications
You must be signed in to change notification settings - Fork 21.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support PostgreSQL 10 whose sequences do not know increment_by
#28789
Conversation
…min_value` Addresses rails#28780 - Each sequence does not know `increment_by` or `min_value` since PostgreSQL 10. A new system catalog `pg_sequences` knows both. postgres/postgres@1753b1b - Since `pg_sequences` does not exist PostgreSQL 9.6 or earlier version This commit addresses by removing `increment_by` or `min_value` - `setval` 3rd argument needs to set to `false` only when the table has no rows to avoid `nextval(<sequence_name>)` returns 2 where 1 is expected. - Replaced `min_value` with `1` since it is necessary only when the table has no rows. If someone wants to reset sequence to non 1 when table has no rows new pull requests/commits required.
(@rails-bot has picked a reviewer for you, use r? to override) |
@yahonda will |
No. The value set at
postgres=# create database test;
CREATE DATABASE
postgres=# \q
$ psql -d test
psql (10devel)
Type "help" for help.
|
Dang - I think we need to address somehow in this PR |
To get the This pull request now aims to create the same statement available for whichever PostgreSQL version. activerecord_unittest=# \x
Expanded display is on.
activerecord_unittest=# select * FROM "public"."topics_id_seq";
-[ RECORD 1 ]---
last_value | 100
log_cnt | 0
is_called | t activerecord_unittest=# select * from pg_sequences where schemaname = 'public' and sequencename = 'topics_id_seq';
-[ RECORD 1 ]-+--------------------
schemaname | public
sequencename | topics_id_seq
sequenceowner | yahonda
data_type | bigint
start_value | 1
min_value | 1
max_value | 9223372036854775807
increment_by | 1
cycle | f
cache_size | 1
last_value | 100
activerecord_unittest=# |
Let me update this pull request using |
Is there any discussions on this topic in the PostgreSQL community - seems like a very abrupt change for users to deal with. |
Here is the link to pgsql-hackers mailing list discussion for this change. and commit. |
@pixeltrix assigning to you since you already got the initial context |
@yahonda is there a way of use |
test=#
test=# alter sequence test_seq_for_alter_sequence_min_value_100 start 1;
ERROR: START value (1) cannot be less than MINVALUE (100)
test=#
test=# create sequence test_seq_for_alter_sequence_min_value_100 minvalue 100;
CREATE SEQUENCE
test=# \x
Expanded display is on.
test=# select * from test_seq_for_alter_sequence_min_value_100;
-[ RECORD 1 ]---
last_value | 100
log_cnt | 0
is_called | f
test=# select * from pg_sequences where sequencename = 'test_seq_for_alter_sequence_min_value_100';
-[ RECORD 1 ]-+------------------------------------------
schemaname | public
sequencename | test_seq_for_alter_sequence_min_value_100
sequenceowner | yahonda
data_type | bigint
start_value | 100
min_value | 100
max_value | 9223372036854775807
increment_by | 1
cycle | f
cache_size | 1
last_value |
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 100
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 101
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 102
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 103
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 104
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 105
test=#
test=# alter sequence test_seq_for_alter_sequence_min_value_100 start 1;
ERROR: START value (1) cannot be less than MINVALUE (100)
test=# |
@yahonda there's a |
Yeah, there is an interesting option. But actually https://www.postgresql.org/docs/9.6/static/sql-altersequence.html
test=# alter sequence test_seq_for_alter_sequence_min_value_100 restart 1;
ERROR: RESTART value (1) cannot be less than MINVALUE (100) What we can do is setting
test=# \q
[yahonda@li366-176 ~]$ psql -d test
psql (10devel)
Type "help" for help.
test=# create sequence test_seq_for_alter_sequence_min_value_100 minvalue 100;
CREATE SEQUENCE
test=# \x
Expanded display is on.
test=# select * from test_seq_for_alter_sequence_min_value_100;
-[ RECORD 1 ]---
last_value | 100
log_cnt | 0
is_called | f
test=# select * from pg_sequences where sequencename = 'test_seq_for_alter_sequence_min_value_100';
-[ RECORD 1 ]-+------------------------------------------
schemaname | public
sequencename | test_seq_for_alter_sequence_min_value_100
sequenceowner | yahonda
data_type | bigint
start_value | 100
min_value | 100
max_value | 9223372036854775807
increment_by | 1
cycle | f
cache_size | 1
last_value |
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 100
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 101
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 102
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 103
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 104
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 105
test=# alter sequence test_seq_for_alter_sequence_min_value_100 restart 1;
ERROR: RESTART value (1) cannot be less than MINVALUE (100)
test=# alter sequence test_seq_for_alter_sequence_min_value_100 minvalue 1 restart 1;
ALTER SEQUENCE
test=# select nextval('test_seq_for_alter_sequence_min_value_100');
-[ RECORD 1 ]
nextval | 1
test=#
|
I guess we have to go with a I suspect this will be the first of many such conditionals. |
Agreed. Let me update the commit to handle |
Closing this pull request. Opened another pull request #28864 which handles |
Another fix for rails#28780 based on discussions at rails#28789 - In PostgreSQL 10 each sequence does not know its `min_value`. A new system catalog `pg_sequence` shows it as `seqmin`. Refer postgres/postgres@1753b1b - `setval` 3rd argument needs to set to `false` only when the table has no rows to avoid `nextval(<sequence_name>)` returns `2` where `1` is expected. - `min_value` is only necessary when the table has no rows. It used to be necessary since the 3rd argument of `setval` is always `false`.
Summary
Addresses #28780
Each sequence does not know
increment_by
ormin_value
sincePostgreSQL 10. A new system catalog
pg_sequences
knows both. postgres/postgres@1753b1bSince
pg_sequences
does not exist PostgreSQL 9.6 or earlier versionThis commit addresses by removing
increment_by
ormin_value
setval
3rd argument needs to set tofalse
only when the table hasno rows to avoid
nextval(<sequence_name>)
returns 2 where 1 isexpected.
Replaced
min_value
with1
since it is necessary only whenthe table has no rows. If someone wants to reset sequence to non 1 when table has no rows new pull requests/commits required.