-
Notifications
You must be signed in to change notification settings - Fork 694
Docs: update description of serial #19443
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
base: main
Are you sure you want to change the base?
Changes from all commits
326f594
57fbfcc
5a5788b
add1769
2a8ac7c
b287133
fdfc88d
851df1c
e912a5e
54f4153
b104b0f
daf37ed
c982680
601a719
825f944
c3b2c5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# ALTER SEQUENCE | ||
|
||
Modifies the parameters of an existing `Sequence` object associated with a [Serial](../types/serial.md) column. | ||
|
||
## Syntax | ||
|
||
```yql | ||
ALTER SEQUENCE [ IF EXISTS ] path_to_sequence | ||
[ INCREMENT [ BY ] increment ] | ||
[ START [ WITH ] start_value ] | ||
[ RESTART [ [ WITH ] restart_value ]]; | ||
``` | ||
|
||
## Parameters | ||
|
||
* `path_to_sequence` — the absolute path to the sequence object. | ||
|
||
The path is constructed as `<path_to_table>/_serial_column_<column_name>`, | ||
where `<path_to_table>` is the absolute path to the table, and `<column_name>` is the name of the column with the `Serial` data type. | ||
For example, for the column `user_id` in the table `/local/users`, the corresponding `Sequence` path will be `/local/users/_serial_column_user_id`. | ||
|
||
* `IF EXISTS` — when used, the statement does not return an error if the sequence does not exist at the specified path. | ||
|
||
* `INCREMENT [ BY ] increment` — sets the increment step for the sequence. Default: 1. | ||
|
||
* `START [ WITH ] start_value` — sets a new start value for the sequence. Changing this parameter with `ALTER SEQUENCE` does not affect the current value; the new start value is used with `ALTER SEQUENCE RESTART` if no value is specified. Default: 1. | ||
|
||
* `RESTART [ [ WITH ] restart_value ]` — sets the current sequence value to the specified `restart_value`. If no value is specified, it sets the current value to the start value. | ||
|
||
## Examples | ||
|
||
```yql | ||
CREATE TABLE users ( | ||
user_hash Uint64, | ||
user_id Serial, | ||
name Utf8, | ||
email Utf8, | ||
PRIMARY KEY (user_hash, user_id) | ||
); | ||
``` | ||
|
||
Change the increment step for `user_id` and set the current value to 1000: | ||
|
||
```yql | ||
ALTER SEQUENCE `/Root/users/_serial_column_user_id` | ||
INCREMENT BY 5 | ||
RESTART 1000; | ||
``` | ||
|
||
An alternative way to change the current value is to first set a new start value, and then `RESTART` the `Sequence`. After this, subsequent calls to `RESTART` without an explicit value will set the current value to 1000: | ||
|
||
```yql | ||
ALTER SEQUENCE `/Root/users/_serial_column_user_id` INCREMENT BY 5 START WITH 1000; | ||
ALTER SEQUENCE `/Root/users/_serial_column_user_id` RESTART; | ||
``` | ||
|
||
## See also | ||
|
||
* [{#T}](create-table.md) | ||
* [{#T}](../types/serial.md) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,3 +97,9 @@ | |
|
||
{% endif %} | ||
|
||
{% if feature_serial %} | ||
|
||
* [ALTER SEQUENCE](alter-sequence.md) | ||
|
||
{% endif %} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Serial Types | ||
|
||
Serial types are integer data types with an associated value-generation mechanism. They create auto-increment columns: each new row inserted into a table automatically generates a unique value for this column (similar to the [SERIAL](https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL) type in PostgreSQL or the [AUTO_INCREMENT](https://dev.mysql.com/doc/refman/9.0/en/example-auto-increment.html) property in MySQL). | ||
|
||
## Description | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This header doesn't bring any extra value to the reader. Instead, split the text to some meaningful sections, for example: automatic sequence creation, underlying data types, working with such columns after creation, etc. |
||
|
||
When a column of a serial type is defined, a separate schema object called a `Sequence` is created and bound to this column. This object is a private sequence generator and it is hidden from the user. The `Sequence` will be destroyed together with the table. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add sequence to glossary.md, link to its definition on first usage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, below is enough content to make a
Then, a large chunk of this goes away, and you can just say that a sequence (link) is automatically created for each table column of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "hidden from the user" part doesn't seem to be true, given we now have the |
||
|
||
The `Sequence` object supports several parameters that determine its behavior. These parameters can be altered after creation using the [ALTER SEQUENCE](../syntax/alter-sequence.md) command. | ||
|
||
By default, values generated by the `Sequence` start from one, are incremented by one with each new value, and are limited according to the chosen type. | ||
|
||
{% note info %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we move away sequence-related content to a separate article, this is roughly when the serial-related content would start; thus, there is no need for it to be in a note block. |
||
|
||
Serial columns are supported both for columns included in the primary key and for non-key columns. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to double-check: is this true for both row-oriented and column-oriented tables? If not, this needs to be clarified. |
||
|
||
However, such columns cannot be [altered](../syntax/alter_table/family#mod-column-groups) or [dropped](../syntax/alter_table/columns.md) from the table — attempting to perform these operations will result in an error. | ||
|
||
{% endnote %} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing header and explanation for the table |
||
| Type | Maximum Value | Underlying Type | | ||
|-------------|----------------------|----------| | ||
| `SmallSerial` | $2^15–1$ | `Int16` | | ||
| `Serial2` | $2^15–1$ | `Int16` | | ||
| `Serial` | $2^31–1$ | `Int32` | | ||
| `Serial4` | $2^31–1$ | `Int32` | | ||
| `Serial8` | $2^63–1$ | `Int64` | | ||
| `BigSerial` | $2^63–1$ | `Int64` | | ||
|
||
If a sequence reaches its maximum value, insertion results in an error: | ||
|
||
```text | ||
Error: Failed to get next val for sequence: /dev/test/users/_serial_column_user_id, status: SCHEME_ERROR | ||
<main>: Error: sequence [OwnerId: <some>, LocalPathId: <some>] doesn't have any more values available, code: 200503 | ||
``` | ||
|
||
{% note info %} | ||
|
||
The next value is allocated by the generator before the actual insertion into the table and is considered used even if the row is not successfully inserted (for example, in case of transaction rollback). | ||
As a result, the values in such a column may have gaps and may not form a continuous sequence. | ||
|
||
{% endnote %} | ||
|
||
Tables with `Serial` columns support [copy](../../../reference/ydb-cli/tools-copy.md), [rename](../../../reference/ydb-cli/commands/tools/rename.md), [dump](../../../reference/ydb-cli/export-import/tools-dump.md), [restore](../../../reference/ydb-cli/export-import/import-file.md), and [import](../../../reference/ydb-cli/export-import/import-s3.md)/[export](../../../reference/ydb-cli/export-import/export-s3.md) CLI operations. | ||
|
||
## Usage Example | ||
|
||
Carefully choose the columns for your [PRIMARY KEY](../../../dev/primary-key/row-oriented.md). To ensure scalability and high performance, avoid using monotonically increasing primary keys; this pattern directs all inserts to the last partition, concentrating load on a single server. | ||
|
||
Instead, use a hash of the entire primary key (or a portion of it) as the first key element to distribute data evenly across cluster partitions. | ||
|
||
```yql | ||
CREATE TABLE users ( | ||
user_hash Uint64, | ||
user_id Serial, | ||
name Utf8, | ||
email Utf8, | ||
PRIMARY KEY (user_hash, user_id) | ||
); | ||
``` | ||
|
||
The `user_hash` field can be calculated on the application side, for example, by applying a hash function to the `email` column. | ||
|
||
``` yql | ||
UPSERT INTO users (user_hash, name, email) VALUES (123456789, 'Alice', 'alice@example.com'); | ||
INSERT INTO users (user_hash, name, email) VALUES (987654321, 'Bob', 'bob@example.com'); | ||
REPLACE INTO users (user_hash, name, email) VALUES (111111111, 'John', 'john@example.com'); | ||
``` | ||
|
||
Result (example `user_hash` values are used): | ||
|
||
| user_hash | email | name | user_id | | ||
|-------------|-----------------------|-------|---------| | ||
| 123456789 | `alice@example.com` | Alice | 1 | | ||
| 987654321 | `bob@example.com` | Bob | 2 | | ||
| 111111111 | `john@example.com` | John | 3 | | ||
|
||
You can also explicitly specify a value for a column of type `Serial` during insertion, for example, when restoring data. In this case, the insertion behaves like a regular integer column, and the sequence remains unaffected: | ||
|
||
``` yql | ||
UPSERT INTO users (user_hash, user_id, name, email) VALUES (222222222, 10, 'Peter', 'peter@example.com'); | ||
``` | ||
|
||
### Suboptimal Schema Example | ||
|
||
```yql | ||
CREATE TABLE users_bad ( | ||
user_id Serial, | ||
name Utf8, | ||
email Utf8, | ||
PRIMARY KEY (user_id) | ||
); | ||
``` | ||
|
||
In this example, the auto-increment column is the sole component of the primary key, resulting in an uneven load and creating a bottleneck on the last partition. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||
# ALTER SEQUENCE | ||||||
|
||||||
Изменяет параметры уже существующего объекта `Sequence`, привязанного к колонке [Serial](../types/serial.md) типа. | ||||||
|
||||||
## Синтаксис | ||||||
|
||||||
```yql | ||||||
ALTER SEQUENCE [ IF EXISTS ] path_to_sequence | ||||||
[ INCREMENT [ BY ] increment ] | ||||||
[ START [ WITH ] start_value ] | ||||||
[ RESTART [ [ WITH ] restart_value ]]; | ||||||
``` | ||||||
|
||||||
## Параметры | ||||||
|
||||||
* `path_to_sequence` - абсолютный путь до объекта `Sequence`. | ||||||
|
||||||
Путь формируется как `<path_to_table>/_serial_column_{column_name}`, | ||||||
где `<path_to_table>` — абсолютный путь до таблицы, a `{column_name}` — имя колонки типа `Serial`. | ||||||
Например, для таблицы с путём `/local/users` и колонки `user_id` путь к соответствующему `Sequence` будет `/local/users/_serial_column_user_id`. | ||||||
* `IF EXISTS` - при использовании этой конструкции, выражение не возвращает ошибку, если не существует `Sequence` по указаному пути. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://ydb.tech/docs/ru/yql/reference/syntax/drop-view?version=main тут тоже написано про выражение |
||||||
* `INCREMENT [ BY ] increment` - задает шаг изменения последовательности. Значение по умолчанию: 1. | ||||||
* `START [ WITH ] start_value` - устанавливает новое стартовое значение для последовательности. Изменение этого параметра через `ALTER SEQUENCE` не влияет на текущее значение последовательности, но будет использовано, если выполнить `ALTER SEQUENCE RESTART` без указания значения. Значение по умолчанию: 1. | ||||||
* `RESTART [ [ WITH ] restart_value ]` - изменяет текущее значение последовательности на указанное в `restart_value`. Если значение не указано, текущее значение последовательности будет установлено в текущее стартовое значение. | ||||||
|
||||||
## Примеры | ||||||
|
||||||
``` yql | ||||||
CREATE TABLE users ( | ||||||
user_hash Uint64, | ||||||
user_id Serial, | ||||||
name Utf8, | ||||||
email Utf8, | ||||||
PRIMARY KEY (user_hash, user_id) | ||||||
); | ||||||
``` | ||||||
|
||||||
Изменить шаг последовательности для `user_id` и установить текущее значение равным 1000: | ||||||
|
||||||
```yql | ||||||
ALTER SEQUENCE `/Root/users/_serial_column_user_id` | ||||||
INCREMENT BY 5 | ||||||
RESTART 1000; | ||||||
``` | ||||||
|
||||||
Альтернативный способ изменить текущее значение — сначала изменить стартовое значение, а затем выполнить `RESTART` (после этого последующие сбросы через `RESTART` без указания `restart_value` будут устанавливать текущее значение в 1000): | ||||||
|
||||||
```yql | ||||||
ALTER SEQUENCE `/Root/users/_serial_column_user_id` INCREMENT BY 5 START WITH 1000; | ||||||
ALTER SEQUENCE `/Root/users/_serial_column_user_id` RESTART; | ||||||
``` |
Uh oh!
There was an error while loading. Please reload this page.