pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

extra sequences created for custom-named sequence defaults

Open pranavks608 opened this issue 3 months ago • 2 comments

Hi Team,

please support on below:

Title: pgschema plan creates extra sequences for tables with custom-named sequence defaults

Description: When running pgschema plan against a database where tables have integer DEFAULT nextval('custom_seq_name'::regclass) columns, pgschema generates additional CREATE SEQUENCE statements using the PostgreSQL default naming convention (__seq), even though the column already references a different custom-named sequence.

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

ALTER TABLE ONLY orders ADD CONSTRAINT orders_pkey PRIMARY KEY (id);

  1. Create a fresh database and apply the above schema manually
  2. Run: pgschema plan --host localhost --db testdb --user testuser --schema public --file schema.sql --output-sql delta.sql --output-human stdout

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.

Impact:

  • Orphaned sequences accumulate in the database after each upgrade cycle
  • When comparing schemas after migration, extra sequences appear that are not in the desired state
  • Could cause confusion during audits or schema validation

Environment:

  • pgschema version: 1.12.1
  • PostgreSQL: 17

pranavks608 avatar Sep 02 '26 05:09 pranavks608

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.

pranavks608 avatar Sep 02 '26 16:09 pranavks608

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).

tianzhou avatar Sep 03 '26 06:09 tianzhou