Skip to content

Keep the hash chain valid across retention - #79

Open
WiktorB2004 wants to merge 8 commits into
add-integrity-verifyfrom
add-integrity-retention
Open

WiktorB2004 wants to merge 8 commits into
add-integrity-verifyfrom
add-integrity-retention

Conversation

@WiktorB2004

Copy link
Copy Markdown
Owner

Summary

Closes #71. Builds on #78.

With integrity on, drop_expired() now records each partition it drops in the hash chain, so verify() can tell a retired partition from a missing one.

Before the first detach, drop_expired() seals the finished transactions. It then verifies the expired partitions (verify_before_drop=True by default). This work runs on a second connection from connection.engine and is committed before any detach. Before each detach it appends a retire block. The block is written under the sealer's lock, hashed like every other block, and committed. It is inserted with ON CONFLICT (retired_partition) WHERE retired_partition IS NOT NULL DO NOTHING, so a retried or finalized detach adds no second block. An empty partition gets one too.

A retire block is now bound to the chain. It names the partition, its table and streams, and the seal range, count, rows and digest of the partition's segments sealed before it. verify() accepts it only when that matches the chain. A segment sealed after the retire block is SEGMENT_MISSING once the partition is gone. ERASED_RECEIPT_RETIRED needs the record's segment to be covered by the retire block. RETIRED_EARLY checks every stream of the partition.

drop_expired() keeps an expired partition and reports it in the result's new skipped list when:

  • it holds rows the chain has not sealed yet;
  • verify finds a failure in it, or in the chain itself;
  • its detach or drop gets 55P03 (lock not granted within lock_timeout) or 55000 (a sibling's detach is pending);
  • rows reached it after its retire block;
  • it was left detached by an earlier call and has no valid retire block (not_retired).

An expired partition left detached by an earlier call (a drop refused with 55P03 or another error, or a crash after the detach) is dropped by the next call when its retire block is valid and still covers it.

No partition is dropped without a retire block. A crash after the retire block and before the detach leaves a partition that still verifies clean. A crash after the detach and before the drop leaves a detached table, which verify reports as DETACHED_PARTITION.

Before the chain is started (the block table is empty), drop_expired() behaves as without integrity. A block table with blocks but no genesis is refused.

The second connection is taken from the pool of connection.engine. A pool with no free connection for it raises PartitionError at once, not after pool_timeout. The verify runs once before the drops, so a sealed row changed in place after it is dropped with its partition; the docstring says so.

The result is still a list of the dropped names, now a DroppedPartitions subclass with skipped. Without integrity, drop_expired() behaves as before: it uses no second connection, skips nothing and raises PartitionLockTimeoutError as it used to.

Integrity stays private: nothing is exported and there are no docs pages. The new parameters and fields say "Private for now".

Test plan

  • Tests added or updated (or N/A — say why)
  • uv run pytest passes locally, including the PostgreSQL tests in tests/db
  • Schema, SQL or partition changes checked on PostgreSQL 14 (AUDIT_TEST_PG_IMAGE=postgres:14) as well as the default image
  • ruff check, ruff format --check and mypy pass
  • Docs and docstrings updated if the public API changed
  • CHANGELOG.md Unreleased bullet if callers should notice

The docstrings are updated. There are no docs pages or changelog entry, because integrity is still private.

@WiktorB2004 WiktorB2004 mentioned this pull request Nov 27, 2026
4 of 6 tasks
A retire block now names its table, partition and streams, and the seal
range, count, rows and digest of the partition's segments sealed before
it. verify accepts it only when that matches the chain; a segment sealed
after it is SEGMENT_MISSING once the partition is gone, and
ERASED_RECEIPT_RETIRED needs the record's segment to be covered.
For drop_expired: the chain is checked whole, the other partitions are
taken as present, and scrub counts are left out.
With integrity on, drop_expired seals, verifies the expired partitions
(verify_before_drop, on by default) and writes a retire block before each
detach, on a second connection. A partition with unsealed rows, a failed
verify, a lock not granted (55P03) or a pending sibling detach (55000) is
kept and reported in the result's skipped list. Without integrity nothing
changes.
With integrity on and an empty block table, nothing has been sealed, so
drop_expired drops as it does without integrity. A block table with
blocks but no genesis is still refused.
drop_expired with integrity takes a second connection from the engine.
A pool with no free one now raises PartitionError naming that need,
instead of waiting for pool_timeout. A pool that shares one connection
is refused the same way.
The check after a detach read the blocks after the retire block again
for every partition. The Retirer now keeps the segments and retire
blocks of the expired partitions from one incremental scan.
A partition left detached by a drop refused with 55P03, another drop
error, or a crash after the detach was never dropped. drop_expired now
finds these tables as health does, and drops the expired ones whose
retire block is valid and still covers them. One without a valid retire
block is kept and reported as not_retired.
@WiktorB2004
WiktorB2004 force-pushed the add-integrity-retention branch from 9f876d7 to 9bb6864 Compare November 27, 2026 02:37

This branch has not been deployed

No deployments
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.

1 participant