Skip to content

Conversation

@timb07
Copy link
Contributor

@timb07 timb07 commented Jul 2, 2025

Prior to this change, the current value of the table's PK sequence wasn't transferred to the repacked table's PK sequence. Without that, new rows would be inserted into the repacked table starting with a PK value of 1 (which would likely conflict with existing rows).

This change adds a command during swap to transfer the sequence value from one table to the other. There is also special handling for the edge case of a bigint conversion where the PK sequence has been set to negative (presumably after positive PK values were exhausted); in that case, the sequence is reset to the first positive bigint value not within the integer range, which is 2**31. Note that this edge case won't be handled correctly during a revert_swap(); addressing that is left as a TODO for later.

@github-actions
Copy link

github-actions bot commented Jul 2, 2025

Coverage Report Results

Name Stmts Miss Branch BrPart Cover
src/psycopack/__init__.py 6 0 0 0 100%
src/psycopack/_commands.py 118 0 10 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 167 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 357 0 120 0 100%
src/psycopack/_tracker.py 165 0 36 0 100%
tests/conftest.py 19 0 0 0 100%
tests/factories.py 40 0 8 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 723 0 4 0 100%
TOTAL 1732 0 206 0 100%

1 empty file skipped.

Prior to this change, the current value of the table's PK sequence
wasn't transferred to the repacked table's PK sequence. Without that,
new rows would be inserted into the repacked table starting with a PK
value of 1 (which would likely conflict with existing rows).

This change adds a command during swap to transfer the sequence value
from one table to the other. There is also special handling for the edge
case of a bigint conversion where the PK sequence has been set to negative
(presumably after positive PK values were exhausted); in that case, the
sequence is reset to the first positive bigint value not within the integer
range, which is 2**31. Note that this edge case won't be handled correctly
during a revert_swap(); addressing that is left as a TODO for later.
@timb07 timb07 force-pushed the fix-pk-sequence-value branch from aa16c41 to 0ca8f81 Compare July 2, 2025 05:51
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.

Thx!

@timb07 timb07 merged commit 68f0cf3 into main Jul 2, 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