Merged PR 1679451: Make first page cursor batch size configurable via GUC

### Does this PR have any customer impact?

### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)

### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)

### Are you introducing any new config? If yes, do you have tests with and without them being set?

### ChangeLog (Refer [Template](../oss/CHANGELOG.md))

### Description

Make first page cursor batch size configurable via GUC

----
#### AI description  (iteration 1)
#### PR Classification
This pull request implements a new feature by introducing a configurable GUC for the first page cursor batch size.

#### PR Summary
The changes enable users to control the default batch size for the first page of cursors through a custom GUC, ensuring it respects underlying limitations.
- `src/configs/system_configs.c`: Added a custom integer variable (`defaultCursorFirstPageBatchSize`) with a default value and proper bounds via the GUC interface.
- `src/commands/aggregation_commands.c`: Updated the first page query data generation to use the new configurable variable instead of a hardcoded value.
- Test files under `/oss/pg_documentdb/src/test/regress/`: Extended both SQL and expected output tests to verify the behavior of the GUC under various configurations.
<!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->

Related work items: #4225119
This commit is contained in:
Santiago Fernandez Madero authored and Shuai Tian committed 2025-05-29 12:12:31 +08:00
1 parent b204b58b82
commit 0d616cdcde
6 files changed
+65 -11

No files matched your search

@@ -146,15 +146,6 @@ static Datum HandleFirstPageRequest(pgbson *querySpec, int64_t cursorId,
static int64_t GenerateCursorId(int64_t inputValue);
/* Generates a base QueryData used for the first page */
inline static QueryData
GenerateFirstPageQueryData(void)
{
QueryData queryData = { 0 };
queryData.batchSize = 101;
return queryData;
}
/* --------------------------------------------------------- */
/* Top level exports */
@@ -116,6 +116,9 @@ char *CurrentOpApplicationName = DEFAULT_CURRENT_OP_APPLICATION_NAME;
#define DEFAULT_AGGREGATION_STAGES_LIMIT 1000
int MaxAggregationStagesAllowed = DEFAULT_AGGREGATION_STAGES_LIMIT;
#define DEFAULT_CURSOR_FIRST_PAGE_BATCH_SIZE 101
int DefaultCursorFirstPageBatchSize = DEFAULT_CURSOR_FIRST_PAGE_BATCH_SIZE;
#define DEFAULT_INDEX_TERM_COMPRESSION_THRESHOLD INT_MAX
int IndexTermCompressionThreshold = DEFAULT_INDEX_TERM_COMPRESSION_THRESHOLD;
@@ -378,4 +381,11 @@ InitializeSystemConfigurations(const char *prefix, const char *newGucPrefix)
NULL, &VectorPreFilterIterativeScanMode, DEFAULT_VECTOR_ITERATIVE_SCAN_MODE,
VECTOR_ITERATIVE_SCAN_OPTIONS,
PGC_USERSET, 0, NULL, NULL, NULL);
DefineCustomIntVariable(
psprintf("%s.defaultCursorFirstPageBatchSize", newGucPrefix),
gettext_noop("The default batch size for the first page of a cursor."),
NULL, &DefaultCursorFirstPageBatchSize,
DEFAULT_CURSOR_FIRST_PAGE_BATCH_SIZE, 1, INT_MAX,
PGC_USERSET, 0, NULL, NULL, NULL);
}
@@ -1674,8 +1674,7 @@ ExpandAggregationFunction(Query *query, ParamListInfo boundParams, PlannedStmt *
pgbson *pipeline = DatumGetPgBson(aggregationConst->constvalue);
QueryData queryData = { 0 };
queryData.batchSize = 101;
QueryData queryData = GenerateFirstPageQueryData();
bool enableCursorParam = false;
bool setStatementTimeout = false;
Query *finalQuery;
@@ -928,6 +928,33 @@ SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "
{ "cursor" : { "id" : { "$numberLong" : "0" }, "ns" : "db.movies", "firstBatch" : [ ] }, "ok" : { "$numberDouble" : "1.0" } }
(1 row)
-- GUC to change default batch size should be honored but should follow the 16MB limit
BEGIN;
set local documentdb.defaultCursorFirstPageBatchSize = 10;
with cte as (SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "get_aggregation_cursor_smalldoc_test", "$db" : "db" }')) SELECT documentdb_api_catalog.bson_dollar_project(cte.cursorPage,
'{ "ok": 1, "cursor.ns": 1, "batchCount": { "$size": { "$ifNull": [ "$cursor.firstBatch", "$cursor.nextBatch" ] } }}') FROM cte;
bson_dollar_project
-------------------------------------------------------------------------------------------------------------------------------------------------
{ "cursor" : { "ns" : "db.get_aggregation_cursor_smalldoc_test" }, "ok" : { "$numberDouble" : "1.0" }, "batchCount" : { "$numberInt" : "10" } }
(1 row)
set local documentdb.defaultCursorFirstPageBatchSize = 5;
with cte as (SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "get_aggregation_cursor_smalldoc_test", "$db" : "db" }')) SELECT documentdb_api_catalog.bson_dollar_project(cte.cursorPage,
'{ "ok": 1, "cursor.ns": 1, "batchCount": { "$size": { "$ifNull": [ "$cursor.firstBatch", "$cursor.nextBatch" ] } }}') FROM cte;
bson_dollar_project
------------------------------------------------------------------------------------------------------------------------------------------------
{ "cursor" : { "ns" : "db.get_aggregation_cursor_smalldoc_test" }, "ok" : { "$numberDouble" : "1.0" }, "batchCount" : { "$numberInt" : "5" } }
(1 row)
set local documentdb.defaultCursorFirstPageBatchSize = 500;
with cte as (SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "get_aggregation_cursor_test", "$db" : "db" }')) SELECT documentdb_api_catalog.bson_dollar_project(cte.cursorPage,
'{ "ok": 1, "cursor.ns": 1, "batchCount": { "$size": { "$ifNull": [ "$cursor.firstBatch", "$cursor.nextBatch" ] } }}') FROM cte;
bson_dollar_project
---------------------------------------------------------------------------------------------------------------------------------------
{ "cursor" : { "ns" : "db.get_aggregation_cursor_test" }, "ok" : { "$numberDouble" : "1.0" }, "batchCount" : { "$numberInt" : "2" } }
(1 row)
END;
-- testing with batchSize that doesn't drain
-- first drain one time with a batchSize of 2 - this leaves a cursor state around
SELECT * FROM aggregation_cursor_test.drain_aggregation_query(loopCount => 1, pageSize => 1, pipeline => '{ "": [{ "$skip": 2 }]}');
@@ -260,6 +260,20 @@ SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "
SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "movies", "ntoreturn":1 , "batchSize":1, "$db" : "test" }');
SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "movies", "ntoreturn":1 , "limit":1, "$db" : "test" }');
-- GUC to change default batch size should be honored but should follow the 16MB limit
BEGIN;
set local documentdb.defaultCursorFirstPageBatchSize = 10;
with cte as (SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "get_aggregation_cursor_smalldoc_test", "$db" : "db" }')) SELECT documentdb_api_catalog.bson_dollar_project(cte.cursorPage,
'{ "ok": 1, "cursor.ns": 1, "batchCount": { "$size": { "$ifNull": [ "$cursor.firstBatch", "$cursor.nextBatch" ] } }}') FROM cte;
set local documentdb.defaultCursorFirstPageBatchSize = 5;
with cte as (SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "get_aggregation_cursor_smalldoc_test", "$db" : "db" }')) SELECT documentdb_api_catalog.bson_dollar_project(cte.cursorPage,
'{ "ok": 1, "cursor.ns": 1, "batchCount": { "$size": { "$ifNull": [ "$cursor.firstBatch", "$cursor.nextBatch" ] } }}') FROM cte;
set local documentdb.defaultCursorFirstPageBatchSize = 500;
with cte as (SELECT cursorPage FROM documentdb_api.find_cursor_first_page('db', '{ "find" : "get_aggregation_cursor_test", "$db" : "db" }')) SELECT documentdb_api_catalog.bson_dollar_project(cte.cursorPage,
'{ "ok": 1, "cursor.ns": 1, "batchCount": { "$size": { "$ifNull": [ "$cursor.firstBatch", "$cursor.nextBatch" ] } }}') FROM cte;
END;
-- testing with batchSize that doesn't drain
-- first drain one time with a batchSize of 2 - this leaves a cursor state around
SELECT * FROM aggregation_cursor_test.drain_aggregation_query(loopCount => 1, pageSize => 1, pipeline => '{ "": [{ "$skip": 2 }]}');