-
Notifications
You must be signed in to change notification settings - Fork 33
fix: persist GSI queue to PostgreSQL for crash safety #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LeeroyHannigan
wants to merge
5
commits into
main
Choose a base branch
from
fix/persistent-gsi-queue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d67591
fix: persist GSI queue to PostgreSQL for crash safety
LeeroyHannigan deff8dd
fix: address review — self-describing GSI queue, crash safety, migrat…
LeeroyHannigan a15928d
Merge remote-tracking branch 'origin/main' into fix/persistent-gsi-queue
LeeroyHannigan 14772b9
fix: harden persistent GSI queue + add integration tests (post-merge)
LeeroyHannigan d3d3266
refactor: extract shared lifecycle test helpers + fixture
LeeroyHannigan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
crates/storage-postgres/data_migrations/002_gsi_pending.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| -- Copyright 2026 ExtendDB contributors | ||
| -- SPDX-License-Identifier: Apache-2.0 | ||
| -- Persistent queue for async GSI propagation. | ||
| -- | ||
| -- Rows are inserted atomically within the base write transaction and consumed | ||
| -- by background workers in a single claim+apply+delete transaction, so an | ||
| -- uncommitted claim rolls back and the row is reprocessed after a crash. | ||
| -- | ||
| -- Each row is self-describing: `index_context` carries the base key schema, | ||
| -- attribute definitions, and the target index definitions captured at enqueue | ||
| -- time, so workers apply updates with zero catalog reads. A GSI's key schema | ||
| -- and projection are immutable after creation, so the snapshot can never be | ||
| -- stale relative to the live index definition. | ||
| -- | ||
| -- `worker_partition` is a stable hash of the base table key. All updates to a | ||
| -- given base item share a partition and are consumed by a single worker in `id` | ||
| -- order, preserving per-key FIFO ordering (so successive updates to the same | ||
| -- item never race in the index and converge to the latest state). | ||
|
|
||
| CREATE TABLE IF NOT EXISTS gsi_pending ( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| table_id TEXT NOT NULL, | ||
| worker_partition INTEGER NOT NULL, | ||
| old_item JSONB, | ||
| new_item JSONB, | ||
| index_context JSONB NOT NULL, | ||
| ready_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Serves the worker claim: WHERE worker_partition = $1 AND ready_at <= NOW() | ||
| -- ORDER BY id. The leading partition column also keeps each worker's scan | ||
| -- confined to its own rows. | ||
| CREATE INDEX IF NOT EXISTS idx_gsi_pending_claim | ||
| ON gsi_pending (worker_partition, ready_at, id); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,12 @@ use extenddb_storage::StreamCapture; | |
| use extenddb_storage::error::StorageError; | ||
| use extenddb_storage::util::{SortKeyValue, parse_sk, pk_to_text, sk_column, sk_info}; | ||
|
|
||
| use super::index::{enqueue_async_indexes, fetch_indexes_for_table, pk_hash, sync_indexes}; | ||
| use super::index::{async_index_context, fetch_indexes_for_table, has_async_indexes, sync_indexes}; | ||
| use super::query::check_condition; | ||
| use super::tx_helpers::write_stream_record_in_tx; | ||
| use super::{data_table_name, json_to_item}; | ||
| use crate::PostgresEngine; | ||
| use crate::gsi_queue::enqueue_gsi_pending; | ||
|
|
||
| impl PostgresEngine { | ||
| /// Implementation of `DataEngine::delete_item`. | ||
|
|
@@ -126,7 +127,6 @@ impl PostgresEngine { | |
| .transpose()?; | ||
| sync_indexes( | ||
| &mut tx, | ||
| &key_info.table_id, | ||
| &key_info.key_schema, | ||
| &key_info.attribute_definitions, | ||
| &indexes, | ||
|
|
@@ -155,26 +155,30 @@ impl PostgresEngine { | |
| ) | ||
| .await?; | ||
| } | ||
| tx.commit() | ||
| .await | ||
| .map_err(|e| StorageError::Internal(e.to_string()))?; | ||
|
|
||
| // Enqueue async GSI updates after commit (D-4). | ||
| if let Some(ref q) = self.gsi_queue { | ||
| enqueue_async_indexes( | ||
| q, | ||
| pk_hash(pk_text.as_ref()), | ||
| &key_info.account_id, | ||
| &key_info.table_name, | ||
| &key_info.table_id, | ||
| if has_async_indexes(&indexes, sys_delay) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could probably be called once at the beginning (right after |
||
| let gsi_ctx = async_index_context( | ||
| &key_info.key_schema, | ||
| &key_info.attribute_definitions, | ||
| &indexes, | ||
| sys_delay, | ||
| ); | ||
| enqueue_gsi_pending( | ||
| &mut tx, | ||
| &key_info.table_id, | ||
| old_item_for_idx.as_ref(), | ||
| None, | ||
| sys_delay, | ||
| &gsi_ctx, | ||
| ) | ||
| .await; | ||
| .await?; | ||
| } | ||
|
|
||
| tx.commit() | ||
| .await | ||
| .map_err(|e| StorageError::Internal(e.to_string()))?; | ||
|
|
||
| if let Some(ref q) = self.gsi_queue { | ||
| q.notify_workers(); | ||
| } | ||
|
|
||
| if return_old { | ||
|
|
@@ -264,7 +268,6 @@ impl PostgresEngine { | |
| .transpose()?; | ||
| sync_indexes( | ||
| &mut tx, | ||
| &key_info.table_id, | ||
| &key_info.key_schema, | ||
| &key_info.attribute_definitions, | ||
| &indexes, | ||
|
|
@@ -293,26 +296,30 @@ impl PostgresEngine { | |
| ) | ||
| .await?; | ||
| } | ||
| tx.commit() | ||
| .await | ||
| .map_err(|e| StorageError::Internal(e.to_string()))?; | ||
|
|
||
| // Enqueue async GSI updates after commit (D-4). | ||
| if let Some(ref q) = self.gsi_queue { | ||
| enqueue_async_indexes( | ||
| q, | ||
| pk_hash(pk_text.as_ref()), | ||
| &key_info.account_id, | ||
| &key_info.table_name, | ||
| &key_info.table_id, | ||
| if has_async_indexes(&indexes, sys_delay) { | ||
| let gsi_ctx = async_index_context( | ||
| &key_info.key_schema, | ||
| &key_info.attribute_definitions, | ||
| &indexes, | ||
| sys_delay, | ||
| ); | ||
| enqueue_gsi_pending( | ||
| &mut tx, | ||
| &key_info.table_id, | ||
| old_item_for_idx.as_ref(), | ||
| None, | ||
| sys_delay, | ||
| &gsi_ctx, | ||
| ) | ||
| .await; | ||
| .await?; | ||
| } | ||
|
|
||
| tx.commit() | ||
| .await | ||
| .map_err(|e| StorageError::Internal(e.to_string()))?; | ||
|
|
||
| if let Some(ref q) = self.gsi_queue { | ||
| q.notify_workers(); | ||
| } | ||
|
|
||
| if return_old { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recall where it's buried, but somewhere there is a catalog version identifier that should be updated when the metadata/system table schema is updated, so that extenddb migrate will know that there is a migration to perform. I believe it's in storage-postgres somewhere.