Skip to content

perf(postgres): write a create-only put in one autocommit statement - #987

Open
parsnips wants to merge 4 commits into
ExtendDB:mainfrom
parsnips:perf/create-only-put
Open

parsnips wants to merge 4 commits into
ExtendDB:mainfrom
parsnips:perf/create-only-put

Conversation

@parsnips

@parsnips parsnips commented Nov 13, 2026 •

Copy link
Copy Markdown

What

On PostgreSQL, a PutItem whose condition says only that the item must not exist is now one autocommit INSERT ... ON CONFLICT DO NOTHING, when the table has no index and no stream to maintain. Before, it always took the transaction path: BEGIN, SELECT ... FOR UPDATE, the insert and COMMIT.

  • crates/core/src/expression/ast.rs: requires_absent_item(condition, key_schema, maps), a pure function next to removes_only. It answers true when the key alone decides the condition: attribute_not_exists on a top-level key attribute (literal name or #name), NOT attribute_exists on one, and AND or OR of such terms when they agree. A #name that does not resolve answers false, so the request keeps the path that reports the validation error. Another backend can use it too.
  • crates/storage-postgres/src/data/put_item.rs: the decision in put_item_impl and one method, insert_absent, for both key shapes. A row inserted means the item did not exist. No row means the condition failed, and an autocommit read fetches the item for ConditionalCheckFailedException, as the transaction path reports it. If that read finds no item, a delete committed between the two statements, and the insert is tried again, up to the existing MAX_CREATE_RACE_ATTEMPTS (5). After the insert the put calls fix_up_autocommit, as the unconditional autocommit put does. One function, insert_if_absent_sql, builds the insert statement for this path and for the transaction path.
  • The put takes this path when the table has no secondary or vector index and no stream, and the write is not already slow (GRACE_WRITE, 500 ms). ReturnValues = ALL_OLD and ReturnConsumedCapacity do not keep it on the transaction path, as they do for an unconditional put: a create-only put that succeeds found no old item, so it has none to return or to size.
  • [storage.postgres] create_only_puts, default true: false keeps these puts on the transaction path. PostgresStorageConfig gets the field (string values accepted, for environment overrides), the server factory passes it to PostgresEngine::with_create_only_puts, and PostgresConfig does not change. The init template has a commented line, and the admin guide's [storage.postgres] table has a row, which names the cases that keep the transaction path and says when to turn the switch off.
  • docs/design/02-high-level-design.md, section 6.3: a bullet on create-only puts.

The answers do not change. An insert of a key that another transaction is inserting or deleting waits for it, so one of several concurrent create-only puts of a key wins, as before. One difference in timing: when the condition fails, the read does not wait for a write in progress on the item, as the locking read does. It returns the latest committed item, and the put orders at that read.

Why

Closes #986

Create-only puts are the usual way to create an item only if it is new, and the PartiQL INSERT statement is one. For a new item they did twice the statements of an unconditional put.

Measured on main at fd8e144 against this branch's first revision on fd8e144. I did not measure again after the merge of main at 44cd436 or after the change for ALL_OLD and ReturnConsumedCapacity: the requests of this benchmark ask for neither, so they took the same statements before and after it. Release builds (cargo build --release --locked -p extenddb) in a Debian 12 container, PostgreSQL 16 in a second container (fsync, synchronous_commit and full_page_writes off, shared_buffers 2 GB, pool size 20), on a 24-core x86_64 Linux host that also ran other work. A fresh deployment for each measurement, three measurements of each build, in turns. The load: a table keyed on two binary attributes with no index and no stream, items of about 300 bytes, PutItem with attribute_not_exists(#pk) on new keys, the same on keys that exist (each fails its condition), and PartiQL INSERT on new keys. Ten seconds for each step. CPU is the server process's and the PostgreSQL container's, divided by the requests; statements are from pg_stat_statements.

32 requests in flight:

Load Build Requests/s, per measurement ExtendDB CPU per request PostgreSQL CPU per request Statements per request
PutItem, new key main 15,526 · 15,519 · 15,522 258 µs 211 µs 6
branch 20,870 · 20,892 · 20,968 217 µs 155 µs 3
PartiQL INSERT, new key main 15,097 · 15,188 · 15,136 291 µs 214 µs 6
branch 20,342 · 20,348 · 20,406 250 µs 156 µs 3
PutItem, key exists main 18,278 · 18,511 · 18,436 229 µs 168 µs 5
branch 16,700 · 16,699 · 16,833 258 µs 176 µs 4

The statements on main for a new key: BEGIN, two catalog reads (indexes, vector indexes), SELECT ... FOR UPDATE, the insert and COMMIT; on the branch, the two catalog reads and the insert.

Other concurrency levels, requests/s per measurement:

Load Build 1 in flight 128 in flight
PutItem, new key main 2,735 · 2,827 · 2,824 15,432 · 15,479 · 15,496
branch 3,875 · 3,901 · 3,849 21,503 · 21,550 · 21,591
PartiQL INSERT, new key main 2,351 · 2,457 · 2,389 14,965 · 15,100 · 15,011
branch 3,652 · 3,367 · 3,200 20,720 · 20,805 · 20,784
PutItem, key exists main 3,374 · 3,185 · 3,498 19,292 · 19,355 · 19,308
branch 3,394 · 3,447 · 3,418 16,965 · 17,004 · 17,059
  • Creates of new items: about 35% more requests per second at 32 in flight, 39% at 128 and 40% at 1, for both PutItem and PartiQL INSERT. Server CPU per request fell by 14% to 16%, PostgreSQL CPU by 27%.
  • A put whose condition fails is about 9% slower at 32 in flight and 12% at 128, the same at 1. The insert sends and checks the whole item, where the transaction path stopped at the locking read. Creates of new items are the common case for this condition; a workload where most creates fail can set create_only_puts = false.

The micro-benchmark in tests/create_only_put.rs (ignored by default, like ttl_bench.rs) measures the two paths in one binary on one database: attribute_not_exists(pk) AND attribute_not_exists(zz) gives the same answer as attribute_not_exists(pk), but its second term is not a key attribute, so it keeps the transaction path. On this branch:

EXTENDDB_TEST_PG_CONNECTION_STRING=... cargo test --release -p extenddb-storage-postgres \
  --test create_only_put -- --ignored --test-threads=1 --nocapture bench_

shape                                          p50 us   p90 us   p99 us  puts/s (median)
create-only, new item                             110      125      161            25633
same answer, transaction path, new item           184      210      272            16254
create-only, item exists                          145      169      232            18518
same answer, transaction path, item exists        131      147      192            21384
unconditional, new item (reference)               111      127      172            25214

Testing done

  • crates/core: unit tests for requires_absent_item: the forms that qualify, and the ones that do not (a non-key attribute, a nested path, an unresolved #name, terms that disagree, other functions and comparisons).
  • crates/storage-postgres/tests/create_only_put.rs (new, needs EXTENDDB_TEST_PG_CONNECTION_STRING; CI's storage step runs it through --tests):
    • New and existing items on a hash table and on a hash-and-range table, for each form of the condition, without and with a request for the old item. A put that creates the item returns no old item. The failure names the stored item, and the item does not change.
    • attribute_not_exists on a non-key attribute still overwrites.
    • Eight concurrent create-only puts of one key, 20 rounds: one winner, and every loser gets the winner's item.
    • A table with a GSI keeps the transaction path, and the index gets its rows.
    • The delete between the failed insert and the read: a BEFORE INSERT trigger parks the insert on an advisory lock while a winner commits. Another session asks for ACCESS EXCLUSIVE on the table, which queues behind the parked insert and then holds the put's read until it deletes the winner. The put retries and creates the item, on both table shapes. Five such deletes in a row make it give up with the create-race error and write nothing.
    • With an outside transaction holding the item's row lock: on, the put answers at once, also when it asks for the old item; with create_only_puts off, it waits for the lock, as on the transaction path.
    • The gate (the trigger, the session that holds it, the outside connection, the lock waits) is in tests/common/insert_gate.rs, and put_create_race.rs uses it too. The scratch database is the one of tests/common/stream_disable_fixture.rs. put_create_race.rs keeps the scratch copy that it has on main.
  • tests/gsi_stale_writer.rs: the driver also sends a create-only put of a new item, which writes without a transaction when the pause is before the statement, and is repaired after a GSI build passed it.
  • config.rs: create_only_puts defaults to true and accepts "false" from an environment override.
  • Mutants, each run against the tests above; each made at least one fail: the matcher accepting a non-key attribute; the fast path on a table with an index; no retry after the delete; no read after a failed insert; no fix_up_autocommit; no retry bound; the switch ignored; NOT not negating (the core unit tests). At 16858bd, three more: a request for the old item sent back to the transaction path (the row-lock test fails); a gate trigger that does not wait (seven gate tests fail, three in create_only_put.rs and four in put_create_race.rs); insert_if_absent_sql without ON CONFLICT on the range shape (nine tests fail in the two files).
  • At 16858bd (main at 44cd436 merged in): cargo test --locked -p extenddb-core -p extenddb-storage-postgres against PostgreSQL 16: 1,619 passed, 2 failed, 6 ignored. The two are catalog_nul_backup_jobs cases that fail the same way on main on this host: the filesystem backup store refuses its root because the checkout's parent directories are group-writable.
  • The pytest suite (devtools/run-tests --extenddb --pytest --filter <word>) against a release build of 16858bd, on a fresh deployment, for the filters conditional, put_item, consumed_capacity, item_operations and partiql: 72, 89, 25, 129 and 3,135 passed, with no failure. One test errored in its setup: test_metrics_accounts.py, which the conditional filter selects by a parameter name, needs the throttling_enabled setting, and my deployment has it off.
  • Before the merge, at 059006f, the whole pytest suite ran against release builds of main and of this branch, on fresh deployments. In the files for items, conditions, PartiQL (other than authorization), batches, transactions, concurrency, queries, scans, updates, deletes, streams and indexes (62 files): 3,867 passed on both, and the same 2 failed on both (test_gsi_async propagation-delay tests). No test of the data path differed. I did not repeat the whole suite at 16858bd.
  • I did not run cargo test --workspace or the dual-target conformance runs against Amazon DynamoDB.
cargo fmt --all -- --check                                  # clean
cargo clippy --locked --all-targets --features extenddb-storage-cassandra/test-hooks,extenddb-storage-mongodb/test-hooks -- -D warnings
cargo clippy --locked --all-targets --features extenddb-engine/backup-test-hooks -- -D warnings
                                                            # both clean at 16858bd, toolchain 1.97.0

With -W clippy::pedantic at 16858bd, the changed files have no new warnings, but two functions that were already over the too_many_lines limit grew: put_item_impl from 340 to 346 lines and server_components_factory from 111 to 116.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace): not run locally; the crates that change pass at 16858bd, except the two catalog_nul_backup_jobs cases above, which fail the same way on main on this host
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic): no new warnings, apart from the two longer functions above; the clippy.yml commands pass with -D warnings
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: n/a. One backend's write path for one class of condition; no wire, trait, auth, on-disk or CLI change. The new [storage.postgres] create_only_puts key is in the admin guide. If you want an ADR for the default, I will add one.

Breaking changes

None for the wire or for deployments: the answers are the same, and existing configuration files work unchanged. For Rust code that uses the crate: extenddb_storage_postgres::PostgresStorageConfig, which is public, has a new field, so a struct literal of it needs create_only_puts or ..Default::default() (as tests/backup_restore.rs and tests/key_ring_rows.rs now have).


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

🤖 Generated with Claude Code

https://claude.ai/code/session_012kcReu8W93hWBjX8JAwW8P

A PutItem whose condition says only that the item must not exist, such
as attribute_not_exists(pk), always took the transaction path. For a new
item that is four statements: BEGIN, SELECT ... FOR UPDATE,
INSERT ... ON CONFLICT DO NOTHING and COMMIT. The PartiQL INSERT
statement is also such a put.

Every stored item carries its key attributes, so such a condition holds
against no item and fails against every item. The key alone decides it.
When the put has nothing else to do in a transaction (no secondary or
vector index, no stream, no old item to return or to size, and not a
slow write), it is now one autocommit INSERT ... ON CONFLICT DO NOTHING:

- A row inserted means the item did not exist.
- No row means the condition failed. A second statement reads the item
  for ConditionalCheckFailedException, as the transaction path reports
  it. That read does not wait for a write in progress on the item; it
  returns the latest committed item.
- If a delete committed between the two statements, the read finds no
  item and the insert is tried again, up to the existing limit of five
  inserts for a create race.
- After the statement, the put calls fix_up_autocommit, as the
  unconditional autocommit put does, so a GSI created meanwhile is
  repaired.

extenddb_core::expression::requires_absent_item decides which conditions
qualify: attribute_not_exists on a top-level key attribute, through a
literal name or a #name; attribute_exists on one under NOT; and AND or
OR of such terms when they agree. It is a pure function of the parsed
condition, next to removes_only, so another backend can use it too. A
#name that does not resolve does not qualify, so its validation error
does not change.

[storage.postgres] create_only_puts (default true) turns the path off;
with false, such puts take the transaction path again. The engine reads
it through PostgresEngine::with_create_only_puts, so PostgresConfig does
not change. PostgresStorageConfig, which is public, gains the field: a
struct literal of it outside this crate needs the field or
..Default::default(), as tests/backup_restore.rs now has.

When the item exists, the put is a little slower than before: the insert
sends the whole item, and PostgreSQL checks the conflict, where the
transaction path stopped at the locking read.

Tests:
- crates/core: unit tests for requires_absent_item.
- tests/create_only_put.rs: absent and present items on a hash table and
  a hash-and-range table, for each form of the condition (literal name,
  #name, NOT attribute_exists, AND, OR); a condition
  on a non-key attribute still overwrites; concurrent create-only puts
  of one key have one winner, and every loser gets the winner's item; a
  table with a GSI keeps the transaction path; a delete between the
  failed insert and the read (forced with a gate trigger and an ACCESS
  EXCLUSIVE lock) makes the put retry and create the item, on both table
  shapes, and five such deletes make it give up; with the switch off, a
  create-only put waits for a row lock again; a micro-benchmark, ignored
  by default.
- tests/gsi_stale_writer.rs: a create-only put that read "no index" and
  wrote after a GSI build is repaired.
- config: create_only_puts defaults to true and accepts a string from an
  environment override.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: michael parsons <michael@twisp.com>

@LeeroyHannigan LeeroyHannigan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. I checked this against PostgreSQL 16:

  • The statement counts match the description. A create-only put of a new key takes 3 statements instead of 6, and one of an existing key takes 4 instead of 5. I counted 300 requests per shape with pg_stat_statements.
  • The classifier lets only attribute_not_exists and attribute_exists on key attributes through. I added 25 assertions on edge cases (comparisons, begins_with, size, nested paths, a #name for a non-key attribute, tautologies), and each one stays on the transaction path.
  • The crate's tests pass, including your new ones and the create-race tests. The five failures I saw come from a backup-root ownership check on my host, and they fail the same way on main.
  • 535 wire tests for conditional puts, the create race and PartiQL INSERT pass against a server built from this head.
  • I didn't reproduce the end-to-end throughput tables. They need two release builds and a load driver, which I didn't run on this shared host. The micro-benchmark's ordering matches the description on a debug build.

Before it merges:

  1. It conflicts with main in server_components_factory (crates/storage-postgres/src/lib.rs). #1003 replaced PostgresEngine::new(...) with PostgresEngine::open(..., DataConnection::Opened(...)) and a drop(data_connection). Please rebase and chain .with_create_only_puts(...) after main's open(...). CI hasn't run at this head yet.
  2. tests/create_only_put.rs repeats helpers the crate's tests already have. connect() and open_gate_fully() match the ones in tests/put_create_race.rs, the trigger SQL in gated_table is the text of install_gate, and base_conn, shipped_migrations and scratch exist in tests/common/stream_disable_fixture.rs. Please move the gate helpers into tests/common/ and use them from both files, or say in the description why you need a second copy.
  3. The admin guide row (05-admin-guide.md:137) leaves out two cases that keep the transaction path: ReturnConsumedCapacity, and a write that is already slower than 500 milliseconds. It should also say when to turn the switch off: a workload whose create-only puts mostly find the item runs slower with it on.

Optional: a create-only put that succeeds found no item, so ALL_OLD and ReturnConsumedCapacity don't need to keep it on the transaction path. Item 3 then needs no new words. The insert-if-absent statement text is also built twice for each key shape.

parsnips and others added 2 commits November 23, 2026 21:09
One conflict, in server_components_factory
(crates/storage-postgres/src/lib.rs): ExtendDB#1003 replaced
PostgresEngine::new(...) with PostgresEngine::open(...,
DataConnection::Opened(...)) and a drop(data_connection). The factory now
has main's open(...) with .with_create_only_puts(...) chained after it,
before the drop.

tests/key_ring_rows.rs, which main added, builds PostgresStorageConfig as
a struct literal. It gets ..Default::default() for the create_only_puts
field, as tests/backup_restore.rs has.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012kcReu8W93hWBjX8JAwW8P
Signed-off-by: michael parsons <michael@twisp.com>
…atement

Changes from the review of 19 Nov.

A create-only put that asked for the old item (ReturnValues = ALL_OLD or
ReturnConsumedCapacity) took the transaction path. A create-only put that
succeeds found no old item, so it has none to return or to size. Such a
put now takes the one-statement path too. A table with a secondary index,
a vector index or a stream, and a write that is already slow
(GRACE_WRITE), keep the transaction path as before.

The statement that inserts an item only if its key is absent was built in
the transaction path and again in insert_absent. One function,
insert_if_absent_sql, now builds it for both.

Tests:
- tests/common/insert_gate.rs has the gate trigger, the gate session, the
  outside connection and the lock waits. put_create_race.rs and
  create_only_put.rs both use it, and their own copies are gone.
- create_only_put.rs uses the scratch database of
  common/stream_disable_fixture.rs in place of its own copy.
- The create-only tests also run with return_old set: a put that creates
  the item returns no old item, a put that finds the item fails with it,
  and a failed put answers while an outside transaction holds the row
  lock, which the transaction path does not.

Docs: the admin guide row for create_only_puts names the cases that keep
the transaction path (an index, a stream, a write over 500 ms) and says
when to turn the switch off. The design bullet says the same.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012kcReu8W93hWBjX8JAwW8P
Signed-off-by: michael parsons <michael@twisp.com>
@parsnips
parsnips requested a review from demohan as a code owner November 24, 2026 05:28
@parsnips

Copy link
Copy Markdown
Author

Thanks for the review. The changes are at 16858bd.

1. Conflict with main. I merged main at 44cd436 instead of a rebase, so the commit that you reviewed keeps its hash. I can rebase if you prefer one commit. server_components_factory has PostgresEngine::open(..., DataConnection::Opened(...)) from #1003 with .with_create_only_puts(...) chained after it, before the drop(data_connection). The merge needed one more line: tests/key_ring_rows.rs, which is new on main, builds PostgresStorageConfig as a struct literal, so it gets ..Default::default().

2. Test helpers. The gate is in tests/common/insert_gate.rs: the trigger, the session that holds the gate, the outside connection, and the lock waits. put_create_race.rs and create_only_put.rs both use it, and their copies are gone. create_only_put.rs also takes Scratch, base_conn and scratch from tests/common/stream_disable_fixture.rs in place of its own copy. I did not change the scratch copy that put_create_race.rs has on main.

3. Admin guide. I took the optional change first: ALL_OLD and ReturnConsumedCapacity do not keep a create-only put on the transaction path now. The row names the cases that are left (a secondary index, a vector index, a stream, and a write that has already taken more than 500 milliseconds), and it says when to set the switch to false. The design bullet says the same.

Optional items. Both are done. The eligibility change is above. insert_if_absent_sql builds the insert statement for the transaction path and for insert_absent.

Tests at 16858bd.

  • cargo test -p extenddb-core -p extenddb-storage-postgres against PostgreSQL 16: 1,619 passed, and the same 2 catalog_nul_backup_jobs cases failed that fail on main on this host (the backup root's parent directories are group-writable).
  • New cases: the create-only tests also run with a request for the old item. A put that creates the item returns no old item, a put that finds the item fails with it, and a failed put answers while an outside transaction holds the row lock.
  • Three more mutants, each caught: a request for the old item sent back to the transaction path; a gate trigger that does not wait (it fails the gate tests of both files); insert_if_absent_sql without ON CONFLICT on the range shape.
  • Wire tests against a release build, for the filters conditional, put_item, consumed_capacity, item_operations and partiql: 3,450 passed and none failed. One setup error is in test_metrics_accounts.py, which needs throttling_enabled on.
  • Both clippy.yml commands pass with -D warnings.

I did not measure throughput again. The requests of the benchmark do not ask for the old item, so they took the same statements before and after the change. CI has not run at this head.

@LeeroyHannigan LeeroyHannigan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I checked this head (16858bd) against PostgreSQL 16.

  • You fixed the three items of my last review and took both optional points. server_components_factory keeps main's open(...) and drop(data_connection), and chains .with_create_only_puts(...) after it. The gate helpers are in tests/common/insert_gate.rs, and both test files use them. The admin guide row names the cases that keep the transaction path, and it says when to turn the switch off.
  • A create-only put with ALL_OLD or ReturnConsumedCapacity now takes the one-statement path. At the storage interface it answers as the transaction path does, on both key shapes, with and without a stored item. A new key gets Ok(None), and an existing key gets ConditionFailed with the whole stored item.
  • I ran 24 cases where another transaction updated, deleted or inserted the row and then committed or rolled back. In each case the put waited for it and then answered from the committed row. It wrote nothing on a row that existed, and it created the item only when the row was gone.
  • Six writers made 240 create-only puts of one key while other sessions deleted and updated it. I ran three rounds with the switch on and three with it off. No write was lost or overwritten.
  • The tests catch all 10 of my mutants of the eligibility check, the moved gate trigger and the classifier.
  • On a release build, a create-only put of a new key takes 3 statements, and one of an existing key takes 4. The merge base takes 6 and 5. This holds with ReturnValues: ALL_OLD, ReturnValuesOnConditionCheckFailure: ALL_OLD and ReturnConsumedCapacity (TOTAL and INDEXES). I counted 300 requests per shape with pg_stat_statements.
  • I reran the throughput tables at 1, 32 and 128 in flight. I used the generator from the #954 description, with a mode added for keys that exist. At 32 in flight, server CPU per request for new keys fell 19% for PutItem and 16% for INSERT. PostgreSQL CPU per request fell 27% and 29%. For a key that exists, it rose 9% on the server and 4% on PostgreSQL. The description gives 16% and 14% for the server, 27% for PostgreSQL, and 13% and 5% for a key that exists. On my host (load 15 to 34), requests per second moved more from round to round than the effects. So I can't judge the throughput ratios.

Before it merges, it needs another merge of main. Main moved to sqlx 0.9.1 (#1166), whose query functions take impl SqlSafeStr, so a &String no longer builds. The merge conflicts in crates/storage-postgres/src/data/put_item.rs and tests/put_create_race.rs. Eleven other sites merge without a conflict but don't build: the four statements of insert_absent, one in tests/common/insert_gate.rs and six in tests/create_only_put.rs. The resolution I tried keeps insert_if_absent_sql(...) at both conflict sites and passes it through sqlx::AssertSqlSafe(...). It keeps the gate helpers out of put_create_race.rs, moves main's AssertSqlSafe into install_gate, and wraps the eleven sites the same way. With it the merge builds, and the crate's tests pass, apart from five that fail on my host's backup-root ownership, as they do on main.

CI hasn't run at this head yet.

main moved to sqlx 0.9.1 (ExtendDB#1166), whose query functions take
`impl SqlSafeStr`, so a `&String` no longer builds.

Conflicts:
- crates/storage-postgres/src/data/put_item.rs: both transaction-path
  inserts keep `insert_if_absent_sql(...)` and pass it through
  `sqlx::AssertSqlSafe(...)`.
- crates/storage-postgres/tests/put_create_race.rs: the gate helpers stay
  in tests/common/insert_gate.rs; main's `AssertSqlSafe` moves into
  `install_gate` there.

Eleven sites merged without a conflict but did not build, and are wrapped
the same way: the four statements of `insert_absent`, one in
tests/common/insert_gate.rs and six in tests/create_only_put.rs.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012kcRer5geZYy7TBPP5U5sD
Signed-off-by: michael parsons <michael@twisp.com>
@parsnips

parsnips commented Dec 4, 2026

Copy link
Copy Markdown
Author

🤖 Agent: Thanks for the second pass. Main is merged at 64c093c.

Merge of main. I merged main at 1a04dba and took the resolution you tried.

  • crates/storage-postgres/src/data/put_item.rs: both transaction-path inserts keep insert_if_absent_sql(...) and pass it through sqlx::AssertSqlSafe(...).
  • tests/put_create_race.rs: the gate helpers stay in tests/common/insert_gate.rs, and main's AssertSqlSafe moves into install_gate there.
  • The eleven sites that merged without a conflict but did not build are wrapped the same way: the four statements of insert_absent, one in tests/common/insert_gate.rs and six in tests/create_only_put.rs. insert_absent runs its statements in a loop, so it wraps insert_sql.as_str() and select_sql.as_str().

Tests at 64c093c.

  • cargo test --no-fail-fast -p extenddb-core -p extenddb-storage-postgres against PostgreSQL 16: 1,777 passed and 2 failed. The 2 are the catalog_nul_backup_jobs cases that fail on this host's backup-root check (an ancestor directory is group-writable). create_only_put (8 passed, 1 ignored) and put_create_race (9 passed) are in that run.
  • Both clippy.yml commands pass with -D warnings on the pinned 1.97.0 toolchain.

I did not run the wire tests or the throughput benchmark at this head. The merge changes how the statement text is passed to sqlx, not the statements.

@navareamol navareamol self-assigned this Dec 4, 2026

@navareamol navareamol left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I ran this head and its merge base 1a04dba, both as release builds on PostgreSQL 16. Counting statements in the PostgreSQL log, a create only PutItem or PartiQL INSERT on a new key takes 3 statements here and 6 at the merge base, and a put on a key that exists takes 4 and 5. With create_only_puts set to false the head goes back to 6 and 5. About 40 request shapes answer the same on both builds, including ALL_OLD, the item returned on a failed condition, ConsumedCapacity for new and existing items, legacy Expected, conditions that stay on the transaction path, and a table with a GSI and a stream. Twenty rounds of 12 writers on one key gave exactly one winner each round, and a delete and create churn on one key produced no 5xx. The PostgreSQL wire suites for conditional writes, the create race and PartiQL writes pass on both builds, so they do not tell the two apart; the statement count does. Clippy with the two feature sets of clippy.yml on 1.97.0 and fmt are clean on this head.

The speed figures in the description are bigger than mine. I measured server CPU per request with a different client at 32 requests in flight, three runs each. PutItem on a new key went from 546, 536 and 538 microseconds to 491, 489 and 488 (9 to 10 percent lower, the description says 16). INSERT went from 582, 579 and 573 to 530, 523 and 524 (9 percent lower, the description says 14). PutItem on an existing key went from 496, 493 and 490 to 526, 518 and 521 (6 percent higher, the description says 13). My client limited the request rate to about 2,900 per second, so I cannot say anything about the throughput numbers. Could you either rerun the CPU figures at this head and replace the table, or say in the description that they were measured at fd8e144 on your host?

I will approve once CI has run green at this head and the CPU figures in the description match a rerun at this head or say which commit they were taken at.

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.

[Feature] PostgreSQL: write a create-only PutItem (attribute_not_exists on the key) as one autocommit INSERT

3 participants