Skip to content

Conversation

@timb07
Copy link
Contributor

@timb07 timb07 commented Sep 4, 2025

If the table is being converted to a bigint pk and the pk sequence has been set to negative, we want to reset the sequence value to 2**31 so that it can start using the new bigint range (and not run out of negative values when the sequence advances to 1).

Previously that reset was done in the swap stage. But that led to errors, since the old table was being updated by a trigger, and it only had an integer pk.

This change moves the sequence reset to the clean_up stage after the trigger has been dropped, which fixes the bug. The change also ensures that the sequence on the new table has the same min value as the old sequence (which could be negative).

Fixes #49

@github-actions
Copy link

github-actions bot commented Sep 4, 2025

Coverage Report Results

Name Stmts Miss Branch BrPart Cover
src/psycopack/__init__.py 6 0 0 0 100%
src/psycopack/_commands.py 116 0 6 0 100%
src/psycopack/_conn.py 5 0 0 0 100%
src/psycopack/_const.py 3 0 0 0 100%
src/psycopack/_cur.py 24 0 2 0 100%
src/psycopack/_identifiers.py 12 0 2 0 100%
src/psycopack/_introspect.py 174 0 16 0 100%
src/psycopack/_logging.py 2 0 0 0 100%
src/psycopack/_psycopg.py 5 0 0 0 100%
src/psycopack/_registry.py 58 0 6 0 100%
src/psycopack/_repack.py 364 0 124 0 100%
src/psycopack/_tracker.py 165 0 36 0 100%
tests/conftest.py 19 0 0 0 100%
tests/factories.py 41 0 10 0 100%
tests/test_cur.py 20 0 2 0 100%
tests/test_fixtures.py 5 0 0 0 100%
tests/test_package.py 3 0 0 0 100%
tests/test_repack.py 786 0 10 0 100%
TOTAL 1808 0 214 0 100%

1 empty file skipped.

@timb07 timb07 force-pushed the fix-pk-sequence-value-update-negative branch from d942807 to faa4e62 Compare September 4, 2025 06:26
@timb07 timb07 force-pushed the disallow-deferrable-unique-constraint branch 2 times, most recently from a589f56 to 3d22d07 Compare September 4, 2025 06:41
@timb07 timb07 force-pushed the fix-pk-sequence-value-update-negative branch from faa4e62 to b6346a5 Compare September 4, 2025 06:43
@timb07 timb07 requested review from flpcury and marcelofern and removed request for flpcury September 4, 2025 06:47
Copy link
Collaborator

@marcelofern marcelofern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. One comment for consideration

Comment on lines 1402 to 1410
@pytest.mark.xfail(
raises=NumericValueOutOfRange,
reason="""
E psycopg.errors.NumericValueOutOfRange: integer out of range
E CONTEXT: SQL function "psycopack_repacked_6723301_fun" statement 1
E SQL statement "SELECT "public"."psycopack_repacked_6723301_fun"(NEW."id", NEW."id")"
E PL/pgSQL function psycopack_repacked_6723301_tgr() line 4 at PERFORM
""",
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering what happens here for other different types of primary keys and if the code handles them. I.e., if we parametrise the test like this:

@pytest.mark.parametrize(
    "initial_pk_type",
    (
        "integer",
        "serial",
        "smallint",
        "smallserial",
    ),
)

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added another commit to cover the above. With slight adjustment to a test factory, the code handles all these PK types.

@timb07 timb07 force-pushed the disallow-deferrable-unique-constraint branch from 3d22d07 to f7314da Compare September 9, 2025 03:39
@timb07 timb07 requested a review from a team as a code owner September 9, 2025 03:39
@timb07 timb07 force-pushed the fix-pk-sequence-value-update-negative branch from 5384935 to 652f9e0 Compare September 9, 2025 03:45
@timb07 timb07 merged commit 5cf1a26 into disallow-deferrable-unique-constraint Sep 9, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants