autoinc
autoinc
autoinc : functions for autoincrementing fields
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4881 | autoinc | autoinc | 1.0 | FUNC | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-- | No | Yes | No | Yes | no | no |
| Relationships | |
|---|---|
| See Also | pg_idkit pgx_ulid pg_uuidv7 permuteseq pg_hashids sequential_uuids topn quantile |
Packages
| PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|
1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
This is a built-in contrib extension ship with the PostgreSQL kernel
Install
Create this extension with:
CREATE EXTENSION autoinc;Usage
Provides a trigger function that auto-increments specified columns using sequences.
CREATE EXTENSION autoinc;Trigger Function
| Function | Description |
|---|---|
autoinc() | Auto-increment columns from sequences on insert (and optionally update) |
Parameters are pairs of (column name, sequence name). The value is replaced only if initially zero or NULL.
Examples
CREATE SEQUENCE next_id;
CREATE TABLE ids (id int4, idesc text);
CREATE TRIGGER ids_autoinc
BEFORE INSERT ON ids
FOR EACH ROW
EXECUTE FUNCTION autoinc('id', 'next_id');
INSERT INTO ids (idesc) VALUES ('first'); -- id is auto-assigned from next_id
-- Multiple auto-increment columns
CREATE TRIGGER multi_autoinc
BEFORE INSERT ON my_table
FOR EACH ROW
EXECUTE FUNCTION autoinc('col1', 'seq1', 'col2', 'seq2');Last updated on