// SQL snippet

Find auto-increment and identity columns

Auto-generated key columns come in engine-specific flavors: IDENTITY, serial, AUTO_INCREMENT. A census of them tells you which sequences are approaching overflow and which tables depend on generated ids.

Main statement
PostgreSQL
Também cobre
SQL Server MySQL SQLite

The task

Planning a key-type migration starts with knowing every generated key column in the schema and how each one is declared.

The snippet

Written for PostgreSQL. Versions for SQL Server, MySQL and SQLite are under Variations.

PostgreSQL
SELECT table_schema, table_name, column_name,
       is_identity, identity_generation, column_default
FROM information_schema.columns
WHERE table_schema = 'public'
  AND (is_identity = 'YES'
       OR column_default LIKE 'nextval%')
ORDER BY table_name;

Como funciona

PostgreSQL has two kinds of auto-generated key: identity columns (is_identity = 'YES', available since PostgreSQL 10) and serial columns, which are a plain DEFAULT nextval(...) on a sequence. The OR catches both, and identity_generation tells ALWAYS from BY DEFAULT. The difference matters when you alter them: an identity column is changed with ALTER COLUMN ... SET/RESTART, a serial one through its sequence. For overflow checks, pair this output with last_value from the sequences recipe.

Variations

SQL Server

SQL Server
SELECT SCHEMA_NAME(t.schema_id) AS schema_name,
       t.name AS table_name,
       ic.name AS column_name,
       TYPE_NAME(ic.system_type_id) AS type_name,
       ic.seed_value, ic.increment_value,
       ic.last_value   -- NULL until the first insert
FROM sys.identity_columns ic
JOIN sys.tables t ON t.object_id = ic.object_id
ORDER BY schema_name, table_name;

MySQL

MySQL
SELECT table_name, column_name, column_type, extra
FROM information_schema.columns
WHERE table_schema = 'mydb' AND extra = 'auto_increment'
ORDER BY table_name;

SQLite

SQLite
-- a single-column INTEGER PRIMARY KEY is the rowid alias and
-- auto-generates; AUTOINCREMENT only stops ids being reused
SELECT m.name AS table_name, ti.name AS column_name,
       m.sql LIKE '%AUTOINCREMENT%' AS has_autoincrement
FROM sqlite_master m
JOIN pragma_table_info(m.name) ti ON ti.pk = 1
WHERE m.type = 'table' AND m.name NOT LIKE 'sqlite_%'
  AND upper(ti.type) = 'INTEGER'
  AND (SELECT COUNT(*) FROM pragma_table_info(m.name) p
       WHERE p.pk > 0) = 1;

Questions that come up

What is the difference between IDENTITY and SERIAL in PostgreSQL?

Both draw ids from a sequence. IDENTITY is the SQL-standard form: the sequence belongs to the column, GENERATED ALWAYS rejects hand-supplied values unless the INSERT says OVERRIDING SYSTEM VALUE, and roles that can insert need no separate grant on the sequence. SERIAL is shorthand for an integer column with a nextval() default; the sequence is a separate object that needs its own USAGE grant, and the default can be changed or dropped like any other. Prefer IDENTITY for new tables.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.