From 0d616cdcdebdb464894ecae9013c848382218d03 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Tue, 20 May 2025 21:05:45 +0000 Subject: [PATCH] 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. Related work items: #4225119 --- .../aggregation/bson_aggregation_pipeline.h | 13 +++++++++ .../src/commands/aggregation_commands.c | 9 ------- pg_documentdb/src/configs/system_configs.c | 10 +++++++ pg_documentdb/src/planner/documents_planner.c | 3 +-- .../bson_aggregation_cursor_tests.out | 27 +++++++++++++++++++ .../sql/bson_aggregation_cursor_tests.sql | 14 ++++++++++ 6 files changed, 65 insertions(+), 11 deletions(-) diff --git a/pg_documentdb/include/aggregation/bson_aggregation_pipeline.h b/pg_documentdb/include/aggregation/bson_aggregation_pipeline.h index 35321bac..6098519e 100644 --- a/pg_documentdb/include/aggregation/bson_aggregation_pipeline.h +++ b/pg_documentdb/include/aggregation/bson_aggregation_pipeline.h @@ -124,4 +124,17 @@ void ParseInputForNGroupAccumulators(const bson_value_t *inputDocument, bson_value_t *input, bson_value_t *elementsToFetch, const char *opName); + +extern int DefaultCursorFirstPageBatchSize; + +/* Generates a base QueryData used for the first page */ +inline static QueryData +GenerateFirstPageQueryData(void) +{ + QueryData queryData = { 0 }; + queryData.batchSize = DefaultCursorFirstPageBatchSize; + return queryData; +} + + #endif diff --git a/pg_documentdb/src/commands/aggregation_commands.c b/pg_documentdb/src/commands/aggregation_commands.c index be3d2216..16f955a0 100644 --- a/pg_documentdb/src/commands/aggregation_commands.c +++ b/pg_documentdb/src/commands/aggregation_commands.c @@ -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 */ diff --git a/pg_documentdb/src/configs/system_configs.c b/pg_documentdb/src/configs/system_configs.c index 89e5e6d5..b48c381b 100644 --- a/pg_documentdb/src/configs/system_configs.c +++ b/pg_documentdb/src/configs/system_configs.c @@ -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); } diff --git a/pg_documentdb/src/planner/documents_planner.c b/pg_documentdb/src/planner/documents_planner.c index ee9028bd..d1c4076f 100644 --- a/pg_documentdb/src/planner/documents_planner.c +++ b/pg_documentdb/src/planner/documents_planner.c @@ -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; diff --git a/pg_documentdb/src/test/regress/expected/bson_aggregation_cursor_tests.out b/pg_documentdb/src/test/regress/expected/bson_aggregation_cursor_tests.out index e45433fd..74b49037 100644 --- a/pg_documentdb/src/test/regress/expected/bson_aggregation_cursor_tests.out +++ b/pg_documentdb/src/test/regress/expected/bson_aggregation_cursor_tests.out @@ -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 }]}'); diff --git a/pg_documentdb/src/test/regress/sql/bson_aggregation_cursor_tests.sql b/pg_documentdb/src/test/regress/sql/bson_aggregation_cursor_tests.sql index 2315ce1a..1a571147 100644 --- a/pg_documentdb/src/test/regress/sql/bson_aggregation_cursor_tests.sql +++ b/pg_documentdb/src/test/regress/sql/bson_aggregation_cursor_tests.sql @@ -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 }]}');