postgresql_16:9.6.41.2 autoinc — Functions for Autoincrementing Fields
PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > spi — Server Programming Interface features/examples > autoinc — Functions for Autoincrementing Fields
autoinc() is a trigger that stores the next value of a sequence into an integer field. This has some overlap with the built-in “serial column” feature, but it is not the same: autoinc() will override attempts to substitute a different field value during inserts, and optionally it can be used to increment the field during updates, too.
To use, create a BEFORE INSERT (or optionally BEFORE INSERT OR UPDATE) trigger using this function. Specify two trigger arguments: the name of the integer column to be modified, and the name of the sequence object that will supply values. (Actually, you can specify any number of pairs of such names, if you'd like to update more than one autoincrementing column.)
There is an example in autoinc.example.