Skip to content

Schema Diff: raising a sequence's MINVALUE above its current value generates SQL that PostgreSQL rejects #10298

Description

@dpage

Bug Description

Schema Diff generates an ALTER SEQUENCE that PostgreSQL refuses whenever the new MINVALUE is above the value the target sequence currently sits at.

Comparing a source sequence declared START 3 MINVALUE 3 against a target sitting at 1 produces:

ALTER SEQUENCE IF EXISTS test_schema_diff.seq_start_diff
    START 3
    MINVALUE 3;

which fails with:

ERROR:  RESTART value (1) cannot be less than MINVALUE (3)

PostgreSQL will not leave a sequence positioned outside its own bounds, so raising MINVALUE past the current value (or, symmetrically, lowering MAXVALUE below it) is only accepted when the same statement also repositions the sequence with RESTART. pgAdmin never adds one, so the change cannot be applied at all: the statement is rejected as a whole, and the rest of the sequence's differences go with it.

The same applies to editing a sequence in the dialog, since both paths render sequences/sql/default/update.sql.

Expected Behaviour

Where the new bounds would exclude the sequence's current value, the generated statement should reposition the sequence onto the nearest legal value, e.g.

ALTER SEQUENCE IF EXISTS test_schema_diff.seq_start_diff
    START 3
    MINVALUE 3
    RESTART 3;

Where the current value is already within the new bounds, nothing should change: repositioning a sequence that does not need it would hand out values that have already been used.

Context

Found by making the Schema Diff regression test assert that the SQL it generates actually applies (#10293).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions