mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-25 19:59:54 -05:00
### Does this PR have any customer impact? No ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) PoC/Feature ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) Yes ### Are you introducing any new config? If yes, do you have tests with and without them being set? Yes ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description Today, persisted cursors require the connections to be held. This is bad since Connections/Backends are a limited resource. This introduces the option to save the cursor state in a file on disk and reuse that across backends. This is behind a GUC due to the presence of the following: 1) Needs more testing and validation 2) Needs backpressure to ensure that disk consumption doesn't balloon up 3) There is a procedure added to clean up these files but it is not wired up 4) We need to handle ultra-large responses (beyond 4 GB it fails outright at the moment). This needs more graceful handling to split into chunks. ---- #### AI description (iteration 1) #### PR Classification This pull request is a new feature implementation that introduces file‐based persisted cursors to allow query results to be “drained” and stored on disk without holding on to active database connections. #### PR Summary This PR adds support for persisting cursor state to the filesystem in both the pg_documentdb and pgmongo modules, enabling connections to be released while still allowing cursors to be later resumed. The changes include: - **`infrastructure/cursor_store.c` and `cursor_store.h`** – New APIs for creating, writing to, reading from, and closing persisted cursor files. - **Commands integration** – Modifications in `commands/cursors.c`, `commands/aggregation_commands.c`, and `cursor_private.h` to use file-based cursor functions for draining and continuation. - **SQL test suites** – Addition of new test files (e.g. in `.../regress/sql/bson_aggregation_cursor_tests_file.sql`) to validate cursor behavior across various query scenarios. - **Configuration updates** – New custom GUCs in `feature_flag_configs.c` and adjustments in test PostgreSQL configuration files to enable and control file-based persisted cursors. - **Executor enhancements** – Updates in cursor draining and query plan execution functions to support both in-memory and file-based cursor paging. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
70 lines
2.8 KiB
C
70 lines
2.8 KiB
C
/*-------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
*
|
|
* include/commands/cursor_private.h
|
|
*
|
|
* Private declarations of functions and types shared between
|
|
* cursors.c and aggregation_cursors.c
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
|
|
#ifndef CURSOR_PRIVATE_H
|
|
#define CURSOR_PRIVATE_H
|
|
|
|
bool DrainStreamingQuery(HTAB *cursorMap, Query *query, int batchSize,
|
|
int32_t *numIterations, uint32_t accumulatedSize,
|
|
pgbson_array_writer *arrayWriters);
|
|
pgbson * DrainTailableQuery(HTAB *cursorMap, Query *query, int batchSize,
|
|
int32_t *numIterations, uint32_t accumulatedSize,
|
|
pgbson_array_writer *arrayWriter);
|
|
bool CreateAndDrainPersistedQuery(const char *cursorName, Query *query,
|
|
int batchSize, int32_t *numIterations, uint32_t
|
|
accumulatedSize,
|
|
pgbson_array_writer *arrayWriter, bool isHoldCursor,
|
|
bool closeCursor);
|
|
bytea * CreateAndDrainPersistedQueryWithFiles(const char *cursorName, Query *query,
|
|
int batchSize, int32_t *numIterations,
|
|
uint32_t
|
|
accumulatedSize,
|
|
pgbson_array_writer *arrayWriter, bool
|
|
closeCursor);
|
|
bool DrainPersistedCursor(const char *cursorName, int batchSize,
|
|
int32_t *numIterations, uint32_t accumulatedSize,
|
|
pgbson_array_writer *arrayWriter);
|
|
bytea * DrainPersistedFileCursor(const char *cursorName, int batchSize,
|
|
int32_t *numIterations, uint32_t accumulatedSize,
|
|
pgbson_array_writer *arrayWriter,
|
|
bytea *cursorFileState);
|
|
|
|
bool CreateAndDrainPointReadQuery(const char *cursorName, Query *query,
|
|
int32_t *numIterations, uint32_t
|
|
accumulatedSize,
|
|
pgbson_array_writer *arrayWriter);
|
|
|
|
TupleDesc ConstructCursorResultTupleDesc(AttrNumber maxAttrNum);
|
|
|
|
Datum PostProcessCursorPage(pgbson_writer *cursorDoc,
|
|
pgbson_array_writer *arrayWriter,
|
|
pgbson_writer *topLevelWriter, int64_t cursorId,
|
|
pgbson *continuation, bool persistConnection,
|
|
pgbson *lastContinuationToken,
|
|
TupleDesc tupleDesc);
|
|
|
|
HTAB * CreateCursorHashSet(void);
|
|
HTAB * CreateTailableCursorHashSet(void);
|
|
void BuildContinuationMap(pgbson *continuationValue, HTAB *cursorMap);
|
|
void BuildTailableCursorContinuationMap(pgbson *continuationValue, HTAB *cursorMap);
|
|
void SerializeContinuationsToWriter(pgbson_writer *writer, HTAB *cursorMap);
|
|
void SerializeTailableContinuationsToWriter(pgbson_writer *writer, HTAB *cursorMap);
|
|
|
|
pgbson * DrainSingleResultQuery(Query *query);
|
|
|
|
void SetupCursorPagePreamble(pgbson_writer *topLevelWriter,
|
|
pgbson_writer *cursorDoc,
|
|
pgbson_array_writer *arrayWriter,
|
|
const char *namespaceName, bool isFirstPage,
|
|
uint32_t *accumulatedLength);
|
|
|
|
#endif
|