autoinc
functions for autoincrementing fields
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
autoinc | 1.0 | FUNC | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4881 | autoinc | No | Yes | No | Yes | No | No | - |
| Related | pg_idkit pgx_ulid pg_uuidv7 permuteseq pg_hashids sequential_uuids topn quantile |
|---|
Version
| PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|
| 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
Install
Note: This is a built-in contrib extension of PostgreSQL
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');
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.