Skip to content
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

sql: support CREATE SEQUENCE #5811

Closed
petermattis opened this issue Apr 1, 2016 · 15 comments
Closed

sql: support CREATE SEQUENCE #5811

petermattis opened this issue Apr 1, 2016 · 15 comments
Assignees
Labels
docs-todo O-community Originated from the community
Milestone

Comments

@petermattis
Copy link
Collaborator

...and add support for the nextval(), currval() and setval() functions. Note that these functions are atomic, but not transactional. We can implement them using the KV {Inc,Get,Set} operations. We'll need to figure out where to store the sequence key. Postgres says that a sequence creates a special single-row table, which could work for us as well. Something like:

CREATE TABLE sequence (
  val INT64
);

Note that Postgres supports tying the lifetime of a sequence to the lifetime of a specific column in a table. That would require adding a field to ColumnDescriptor that would allowing cascading the deletion of a column (either by dropping the column or the entire table) to deletion of the associated sequences.

@JackKrupansky
Copy link

Sort of related to the issue I filed on SERIAL:
sql: support SERIAL PK for appending rows to a table #4491

@maddyblue
Copy link
Contributor

Update: nope, the following comment is wrong, kept for the archives.

One reason to do this as not just a wrapper around unique_rowid() is for sorting. Since a sequence usually results in things being inserted with increasing IDs, you can use those IDs as a sort column (otherwise you'd have to use a column with default NOW() or something). Sure, you get some things out of order, but in general it'll be correct.

@petermattis
Copy link
Collaborator Author

unique_rowid is only slightly less sorted than a sequence backed by KV Inc. Am I missing something?

@maddyblue
Copy link
Contributor

unique_rowid is only sorted per node since the node ID is MSB of the output. A sequence would be sorted per cluster.

@petermattis
Copy link
Collaborator Author

We never made that change. unique_rowid() has the timestamp in the upper bits and the node ID in the lower bits.

@maddyblue
Copy link
Contributor

I thought it was the opposite. Strike my comment then.

@petermattis
Copy link
Collaborator Author

@paperstreet Should we close this issue?

@danhhz
Copy link
Contributor

danhhz commented Jun 8, 2016

I think leave it open and stick it in Later until someone needs it. SEQUENCE can be used for other things than implementing SERIAL

@danhhz danhhz added this to the Later milestone Jun 8, 2016
@bdarnell
Copy link
Contributor

A need for this was reported in #12481

@dianasaur323
Copy link
Contributor

A user also mentioned an issue with this when trying to load in pg dumped file. @nstewart

@vilterp vilterp self-assigned this Oct 10, 2017
@vilterp
Copy link
Contributor

vilterp commented Oct 11, 2017

@petermattis Why was lastval left out of this issue? It doesn't seem necessary (since you can always use currval, and probably should to avoid accidentally getting a value from the wrong sequence), but was just wondering what your reasoning was.

@petermattis
Copy link
Collaborator Author

No reason, just an oversight.

@hjames9
Copy link
Contributor

hjames9 commented Dec 15, 2017

Would it be possible to support the ALTER SEQUENCE support for changing the increment and the starting value? For example:

ALTER SEQUENCE campaigns_id_seq INCREMENT BY 2 START WITH 31337 RESTART WITH 31337;

INCREMENT BY 2 == increments sequence by 2 on every next value
START WITH 31337 == starts the sequence at 31337
RESTART WITH 31337 == Anytime ALTER SEQUENCE RESTART is called, it'll set the starting value back at 31337

@vilterp
Copy link
Contributor

vilterp commented Dec 19, 2017

@hjames9 In master, you can ALTER these:

  • INCREMENT BY
  • START WITH

And support for these is planned (tracking issue here: #19723):

  • ALTER SEQUENCE ... RESTART WITH x
  • ALTER SEQUENCE ... RESTART

@vilterp
Copy link
Contributor

vilterp commented Apr 26, 2018

We shipped this in 2.0, minus a couple small PG compatibility issues, which have been filed with the label A-sql-sequences: A-sql-sequences Sequence handling in SQL

@vilterp vilterp closed this as completed Apr 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-todo O-community Originated from the community
Projects
None yet
Development

No branches or pull requests

9 participants