Repository navigation
Run the single-statement deck writes at AUTOCOMMIT - #99
Merged
Merged
Conversation
POST /decks and PUT /decks/{id} each send one statement, so a BEGIN and COMMIT around it only add two round trips. They now use an AUTOCOMMIT Transaction, the same pattern chat-app uses for its single-statement reads.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
POST /decks/andPUT /decks/{id}/each run one statement:INSERT ... ON CONFLICT ... RETURNINGandUPDATE ... RETURNING. Wrapping a single statement in BEGIN and COMMIT only adds two round trips, because Postgres already runs a single statement atomically. Both use cases now take a newDatabase.autocommit_transaction, the pattern modern-python/chat-app#64 introduced for its single-statement reads.How it works
autocommit_transactionis adb_retry.Transactionwithisolation_levelset toAUTOCOMMIT. The isolation level comes from its own provider,autocommit_isolation_level._start_transactionreturns early at autocommit, so it sends no BEGIN, andcommit()sends nothing when no transaction was started. Each write is now one round trip instead of three.async with self.transaction:block, sopostgres_retryrecovery is unchanged. A retry after a lost connection is safe. Create is idempotent on its key, and update writes the same values again.UpdateCardsUseCasekeeps the normal transaction. It raisesNotFoundErrorafter the UPDATE when fewer rows come back, and it needs the rollback to undo the rows that did match.Tests
tests/test_autocommit_writes.pyruns both use cases against a real primary engine. It checks that asyncpg reports no open transaction while the statement runs. Both tests failed before the DI change. The create test deletes its deck afterwards, because an autocommit write can't be rolled back. The update test uses a missing id, so it writes nothing.db_sessionfixture overridesautocommit_isolation_leveltoNone. Switching the shared test connection to AUTOCOMMIT would roll back the outer test transaction, as in chat-app.just testpasses 80 tests at 100% coverage.just lintis clean.