fix(db-mongodb): serialize paginated count and docs inside transactions#17053
Open
brozmarek wants to merge 1 commit into
Open
fix(db-mongodb): serialize paginated count and docs inside transactions#17053brozmarek wants to merge 1 commit into
brozmarek wants to merge 1 commit into
Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
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.
What
Serializes paginated count and document queries when a MongoDB transaction
sessionis active.packages/db-mongodb/src/utilities/aggregatePaginate.ts: when asessionis present, await the aggregate docs query before awaiting the count query. The existingPromise.allpath is preserved when no session is active, so non-transactional reads keep their parallel performancepackages/db-mongodb/package.json: bumpmongoose-paginate-v2from1.9.4to1.9.5, which brings in the equivalent session-aware fix for the regularModel.paginatepath (aravindnc/mongoose-paginate-v2#242). I contributed to that upstream PR, so this Payload change applies the same transaction-session fix to the adapter-owned aggregate pagination pathThis covers both pagination entry points in the adapter: the custom
aggregatePaginateutility, used byfind.tsandqueryDrafts.tswhen joins or sort aggregation are involved, and the standardModel.paginatepath used byfind.ts,queryDrafts.ts,findVersions.ts, andfindGlobalVersions.ts.Why
MongoDB allows only one in-flight operation per transaction session at a time. Payload's pagination previously issued the count query and the docs query concurrently on the same session, which violates that rule and can produce errors such as:
This can affect paginated collection, draft, and version queries executed inside a Payload transaction. The serialization is gated on
session, so the only cost is one extra round trip per count for transactional reads. Non-transactional paginated reads are unchanged.Validation
Added a Mongo-only regression test in
test/database/int.spec.ts(should run paginated find operations inside a transaction session) that runs both pagination paths inside a single transaction session:payload.findon thesimplecollection, which exercisesModel.paginate.payload.findonpostssorted bycategory.title, which forces theaggregatePaginatebranch viabuildSortParam's relationship sort aggregation.Both queries now succeed within the same session and return the expected
totalDocs.Run against a local MongoDB replica set because transactions require a replica set. With the default test Docker service:
For my local no-auth MongoDB replica set, I used:
Without the fix, the test fails with the transaction-number error above. With the fix, it passes.
Also verified:
pnpm --filter @payloadcms/translations build:typespnpm --filter payload build:typespnpm --filter @payloadcms/db-mongodb build:types