Skip to content

autoinc

functions for autoincrementing fields

autoinc · functions for autoincrementing fields

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4881autoincautoinc1.0FUNCPostgreSQLC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno

Packages

PG19PG18PG17PG16PG15
1.01.01.01.01.0
This contrib extension ships with PostgreSQL.

Install

Create this extension with:

CREATE EXTENSION autoinc;

Usage

autoinc: auto-increment trigger function

Provides a trigger function that auto-increments specified columns using sequences.

CREATE EXTENSION autoinc;

Trigger Function

FunctionDescription
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');

Table reader