Steps to Reproduce:
Given a schema.sql with a custom-named sequence:
sql
CREATE SEQUENCE my_custom_id_seq
START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
CREATE TABLE orders (
id integer DEFAULT nextval('my_custom_id_seq'::regclass) NOT NULL,
product_name varchar(255),
quantity integer
);
Expected Result:
Plan shows no changes (database matches desired state). No extra sequences created.
Actual Result:
pgschema generates an additional sequence in delta.sql:
sql
CREATE SEQUENCE IF NOT EXISTS orders_id_seq ...;
This sequence (orders_id_seq) is never referenced by the table — the table uses my_custom_id_seq. The extra sequence is orphaned.
Additional finding — functional impact (not just orphaned sequences)
pgschema also changes the column DEFAULT to the auto-created sequence. Observed on a real upgrade (pgschema 1.12.1, PostgreSQL 17).
The desired schema has multiple tables sharing one custom sequence:
sql
CREATE TABLE t_a ( id integer DEFAULT nextval('shared_seq'::regclass) NOT NULL, ... );
CREATE TABLE t_b ( id integer DEFAULT nextval('shared_seq'::regclass) NOT NULL, ... );
After a pgschema upgrade, the DB ends up with:
- Extra auto-created sequences: t_a_id_seq, t_b_id_seq
- Column defaults rewired to those auto sequences instead of the custom one:
t_a.id -> nextval('t_a_id_seq'::regclass) (expected: shared_seq)
t_b.id -> nextval('t_b_id_seq'::regclass) (expected: shared_seq)
Impact beyond orphaned objects:
- Tables intended to draw IDs from a single shared sequence now use separate sequences, changing ID-allocation behavior.
- Not idempotent: a second pgschema plan against the same schema reports changes (drop the auto sequences + ALTER COLUMN ... SET DEFAULT back to the custom sequence), so a single apply does not converge to the desired state.
Expected: pgschema should preserve the explicit DEFAULT nextval('<custom_seq>') from the desired schema and not create/rewire to
__seq.
Environment: pgschema 1.12.1, PostgreSQL 17.
Closing as a duplicate of #573, which has the same root cause: pgschema treats any integer column with a nextval(...) default as SERIAL without verifying the sequence is actually owned by that column. See the analysis there; the fix will cover this case (shared/custom-named sequences and the wrongly created <table>_<column>_seq).