From 4b79ce38d4900a8071904d17ef79f5a4933dd82b Mon Sep 17 00:00:00 2001 From: Zinan Ren Date: Fri, 9 May 2025 13:02:55 +0000 Subject: [PATCH] Merged PR 1668682: [Vector Search] Add GUC for the default search parameter calculation ### Does this PR have any customer impact? Yes ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) Perf ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) No ### 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 Adding the GUC documentdb.enableVectorCalculateDefaultSearchParam to enable or disable the calculation of default search parameters. The default value is on Adding feature counts for the usage of default search parameters. ---- #### AI description (iteration 1) #### PR Classification This PR implements a new configuration option to enable or disable the dynamic calculation of default search parameters for vector search indexes. #### PR Summary The pull request adds a GUC flag that controls whether default search parameters (e.g., nProbes for IVF and efSearch for HNSW) are computed dynamically based on collection size. It updates internal calculation functions, test expectations, and feature counters accordingly. - `src/vector/vector_index_kind_impl.c`: Revised the IVF and HNSW search parameter calculation functions to conditionally compute default parameters when the new GUC is enabled. - `src/configs/feature_flag_configs.c` and `include/vector/vector_common.h`: Introduced the new boolean GUC `enableVectorCalculateDefaultSearchParam`. - Test files (e.g., files under `/oss/internal/pg_documentdb_distributed/src/test/regress/expected/` and corresponding `pgmongo` tests): Updated expected outputs and query plans to reflect the new default parameter values. - Feature counter outputs and related SQL tests: Modified to include metrics for the new default search parameter usage. Related work items: #4194082 --- ...aggregation_pipeline_tests_vector_hnsw.out | 74 +++++++++ ..._aggregation_pipeline_tests_vector_ivf.out | 154 ++++++++++++++++-- .../expected/mongo_feature_counters.out | 12 +- ...aggregation_pipeline_tests_vector_hnsw.sql | 16 ++ ..._aggregation_pipeline_tests_vector_ivf.sql | 37 +++-- .../regress/sql/mongo_feature_counters.sql | 4 +- pg_documentdb/include/utils/feature_counter.h | 5 +- pg_documentdb/include/vector/vector_common.h | 6 +- .../src/configs/feature_flag_configs.c | 15 +- .../src/infrastructure/feature_counter.c | 3 + .../src/vector/vector_index_kind_impl.c | 52 ++++-- 11 files changed, 324 insertions(+), 54 deletions(-) diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out index 24a154ac..e9f129a4 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out @@ -569,6 +569,43 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Order By: (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) (17 rows) +ROLLBACK; +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SET LOCAL documentdb.enableVectorCalculateDefaultSearchParam = off; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "some sentence", "v" : [ { "$numberInt" : "11" }, { "$numberInt" : "16" }, { "$numberDouble" : "2.1000000000000000888" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "13.726616969513441902" } } } + { "_id" : { "$numberInt" : "2" }, "a" : "some sentence", "v" : [ { "$numberInt" : "12" }, { "$numberInt" : "17" }, { "$numberDouble" : "3.1000000000000000888" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "15.225636516225479866" } } } + { "_id" : { "$numberInt" : "3" }, "a" : "some sentence", "v" : [ { "$numberInt" : "13" }, { "$numberInt" : "18" }, { "$numberDouble" : "4.0999999999999996447" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "16.769615416601034497" } } } + { "_id" : { "$numberInt" : "4" }, "a" : "some sentence", "v" : [ { "$numberInt" : "14" }, { "$numberInt" : "19" }, { "$numberDouble" : "5.0999999999999996447" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "18.347206738825054373" } } } + { "_id" : { "$numberInt" : "5" }, "a" : "some sentence", "v" : [ { "$numberInt" : "15" }, { "$numberInt" : "20" }, { "$numberDouble" : "6.0999999999999996447" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "19.950439081164354604" } } } +(5 rows) + +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT collection.document FROM documentdb_data.documents_8201_8200016 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8201'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '5'::bigint) agg_stage_0 + Node: host=localhost port=58070 dbname=regression + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Limit + Output: collection.document, ((vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection.document, (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + CosmosSearch Custom Params: { "efSearch" : 40 } + -> Index Scan using foo_1 on documentdb_data.documents_8201_8200016 collection + Output: collection.document + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) +(17 rows) + ROLLBACK; -- more than 10000 and less than 1M documents, use default efSearch 40 -- generate 9000 documents and insert into collection @@ -616,6 +653,43 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Order By: (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) (17 rows) +ROLLBACK; +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SET LOCAL documentdb.enableVectorCalculateDefaultSearchParam = off; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "some sentence", "v" : [ { "$numberInt" : "11" }, { "$numberInt" : "16" }, { "$numberDouble" : "2.1000000000000000888" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "13.726616969513441902" } } } + { "_id" : { "$numberInt" : "2" }, "a" : "some sentence", "v" : [ { "$numberInt" : "12" }, { "$numberInt" : "17" }, { "$numberDouble" : "3.1000000000000000888" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "15.225636516225479866" } } } + { "_id" : { "$numberInt" : "3" }, "a" : "some sentence", "v" : [ { "$numberInt" : "13" }, { "$numberInt" : "18" }, { "$numberDouble" : "4.0999999999999996447" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "16.769615416601034497" } } } + { "_id" : { "$numberInt" : "4" }, "a" : "some sentence", "v" : [ { "$numberInt" : "14" }, { "$numberInt" : "19" }, { "$numberDouble" : "5.0999999999999996447" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "18.347206738825054373" } } } + { "_id" : { "$numberInt" : "5" }, "a" : "some sentence", "v" : [ { "$numberInt" : "15" }, { "$numberInt" : "20" }, { "$numberDouble" : "6.0999999999999996447" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "19.950439081164354604" } } } +(5 rows) + +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT collection.document FROM documentdb_data.documents_8201_8200016 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8201'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '5'::bigint) agg_stage_0 + Node: host=localhost port=58070 dbname=regression + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Limit + Output: collection.document, ((vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection.document, (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + CosmosSearch Custom Params: { "efSearch" : 40 } + -> Index Scan using foo_1 on documentdb_data.documents_8201_8200016 collection + Output: collection.document + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) +(17 rows) + ROLLBACK; --------------------------------------------------------------------- -- hnsw search with filter diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out index 322270ed..32152743 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out @@ -512,7 +512,40 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' (17 rows) ROLLBACK; --- more than 10000 and less than 1M documents, use documents / 1000 as nProbes +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SET LOCAL documentdb.enableVectorCalculateDefaultSearchParam = off; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "some sentence", "v" : [ { "$numberInt" : "11" }, { "$numberInt" : "16" }, { "$numberDouble" : "2.1000000000000000888" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "13.726616969513441902" } } } +(1 row) + +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT collection.document FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '5'::bigint) agg_stage_0 + Node: host=localhost port=58070 dbname=regression + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Limit + Output: collection.document, ((vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection.document, (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + CosmosSearch Custom Params: { "nProbes" : 1 } + -> Index Scan using foo_1 on documentdb_data.documents_8101_8100018 collection + Output: collection.document + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) +(17 rows) + +ROLLBACK; +-- more than 10000 and less than 1M documents, nProbes = 10000/(numOfRows/numlist) = 10000/(10150/150) = 148 -- generate 9000 documents and insert into collection select batch_insert_testing_vector_documents('aggregation_pipeline_ivf_nprobes', 1151, 9000, 2000); NOTICE: Inserting 9000 documents into aggregation_pipeline_ivf_nprobes @@ -552,23 +585,110 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Output: collection.document, ((vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) -> Custom Scan (DocumentDBApiQueryScan) Output: collection.document, (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - CosmosSearch Custom Params: { "nProbes" : 10 } + CosmosSearch Custom Params: { "nProbes" : 148 } + -> Index Scan using foo_1 on documentdb_data.documents_8101_8100018 collection + Output: collection.document + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) +(17 rows) + +ROLLBACK; +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SET LOCAL documentdb.enableVectorCalculateDefaultSearchParam = off; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "1" }, "a" : "some sentence", "v" : [ { "$numberInt" : "11" }, { "$numberInt" : "16" }, { "$numberDouble" : "2.1000000000000000888" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "13.726616969513441902" } } } +(1 row) + +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT collection.document FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '5'::bigint) agg_stage_0 + Node: host=localhost port=58070 dbname=regression + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Limit + Output: collection.document, ((vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection.document, (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + CosmosSearch Custom Params: { "nProbes" : 1 } + -> Index Scan using foo_1 on documentdb_data.documents_8101_8100018 collection + Output: collection.document + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) +(17 rows) + +ROLLBACK; +-- nProbes = 10000/(numOfRows/numlist) = 10000/(10150/1) = 1 +CALL documentdb_api.drop_indexes('db', '{ "dropIndexes": "aggregation_pipeline_ivf_nprobes", "index": "foo_1"}'); + retval +--------------------------------------------------------------------- + { "ok" : true, "nIndexesWas" : { "$numberLong" : "1" } } +(1 row) + +SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_ivf_nprobes", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "foo_1", "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 1, "similarity": "L2", "dimensions": 3 } } ] }', true); +NOTICE: ivfflat index created with little data +DETAIL: This will cause low recall. +HINT: Drop the index until the table has more data. +NOTICE: ivfflat index created with little data +DETAIL: This will cause low recall. +HINT: Drop the index until the table has more data. +NOTICE: ivfflat index created with little data +DETAIL: This will cause low recall. +HINT: Drop the index until the table has more data. +NOTICE: ivfflat index created with little data +DETAIL: This will cause low recall. +HINT: Drop the index until the table has more data. +NOTICE: ivfflat index created with little data +DETAIL: This will cause low recall. +HINT: Drop the index until the table has more data. +NOTICE: ivfflat index created with little data +DETAIL: This will cause low recall. +HINT: Drop the index until the table has more data. +NOTICE: ivfflat index created with little data +DETAIL: This will cause low recall. +HINT: Drop the index until the table has more data. +NOTICE: ivfflat index created with little data +DETAIL: This will cause low recall. +HINT: Drop the index until the table has more data. + create_indexes_non_concurrently +--------------------------------------------------------------------- + { "raw" : { "defaultShard" : { "numIndexesBefore" : { "$numberInt" : "0" }, "numIndexesAfter" : { "$numberInt" : "1" }, "createdCollectionAutomatically" : false, "ok" : { "$numberInt" : "1" } } }, "ok" : { "$numberInt" : "1" } } +(1 row) + +ANALYZE; +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT collection.document FROM documentdb_data.documents_8101_8100018 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8101'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "k" : { "$numberInt" : "5" }, "path" : "v", "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ] }'::documentdb_core.bson, 'vector'::text), 3, true)) LIMIT '5'::bigint) agg_stage_0 + Node: host=localhost port=58070 dbname=regression + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Limit + Output: collection.document, ((vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection.document, (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + CosmosSearch Custom Params: { "nProbes" : 1 } -> Index Scan using foo_1 on documentdb_data.documents_8101_8100018 collection Output: collection.document Order By: (vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) (17 rows) ROLLBACK; --- more than 1M documents, use sqrt(documents) as nProbes --- generate 1000000 documents and insert into collection --- select batch_insert_testing_vector_documents('aggregation_pipeline_ivf_nprobes', 10151, 990000, 5000); --- ANALYZE; --- BEGIN; --- SET LOCAL enable_seqscan to off; --- SET LOCAL documentdb.enableVectorPreFilter = on; --- SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); --- EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); --- ROLLBACK; --------------------------------------------------------------------- -- ivf search with filter SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_ivf_filter', '{ "_id": 6, "meta":{ "a": "some sentence", "b": 1 }, "c": true , "v": [3.0, 5.0, 1.1 ] }'); @@ -1066,7 +1186,7 @@ SELECT set_config('documentdb' || '.next_collection_id', '' || :vector__next_col SELECT set_config('documentdb' || '.next_collection_index_id', '' || :vector__next_collection_index_id, FALSE); set_config --------------------------------------------------------------------- - 8114 + 8115 (1 row) --------------------------------------------------------------------- @@ -3490,10 +3610,10 @@ SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createI ERROR: Error in specification { "key" : { "v" : "cosmosSearch" }, "name" : "foo_1", "cosmosSearchOptions" : { "kind" : "vector-ivf", "numLists" : 16, "similarity" : "L2", "dimensions" : 2001, "compression" : [ "half" ] } } :: caused by :: compression must be a string not array SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_ivf_halfvec", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "foo_1", "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 16, "similarity": "L2", "dimensions": 2001 } } ] }', true); ERROR: column cannot have more than 2000 dimensions for ivfflat index -CONTEXT: SQL statement "CREATE INDEX documents_rum_index_8143 ON documentdb_data.documents_8105 USING ivfflat(CAST(documentdb_api_internal.bson_extract_vector(document, 'v'::text) AS public.vector(2001)) public.vector_l2_ops) WITH (lists = 16) WHERE documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL" +CONTEXT: SQL statement "CREATE INDEX documents_rum_index_8144 ON documentdb_data.documents_8105 USING ivfflat(CAST(documentdb_api_internal.bson_extract_vector(document, 'v'::text) AS public.vector(2001)) public.vector_l2_ops) WITH (lists = 16) WHERE documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL" SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_ivf_halfvec", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "foo_1", "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 16, "similarity": "L2", "dimensions": 2001 } } ] }', true); ERROR: column cannot have more than 2000 dimensions for ivfflat index -CONTEXT: SQL statement "CREATE INDEX documents_rum_index_8144 ON documentdb_data.documents_8105 USING ivfflat(CAST(documentdb_api_internal.bson_extract_vector(document, 'v'::text) AS public.vector(2001)) public.vector_l2_ops) WITH (lists = 16) WHERE documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL" +CONTEXT: SQL statement "CREATE INDEX documents_rum_index_8145 ON documentdb_data.documents_8105 USING ivfflat(CAST(documentdb_api_internal.bson_extract_vector(document, 'v'::text) AS public.vector(2001)) public.vector_l2_ops) WITH (lists = 16) WHERE documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL" SET client_min_messages TO WARNING; SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_ivf_halfvec", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "foo_1", "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 16, "similarity": "L2", "dimensions": 2000 } } ] }', true); create_indexes_non_concurrently @@ -3514,7 +3634,7 @@ CALL documentdb_api.drop_indexes('db', '{ "dropIndexes": "aggregation_pipeline_i SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_ivf_halfvec", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "half_index", "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 16, "similarity": "L2", "dimensions": 4001, "compression": "half" } } ] }', true); ERROR: column cannot have more than 4000 dimensions for ivfflat index -CONTEXT: SQL statement "CREATE INDEX documents_rum_index_8146 ON documentdb_data.documents_8105 USING ivfflat(CAST(documentdb_api_internal.bson_extract_vector(document, 'v'::text) AS public.halfvec(4001)) public.halfvec_l2_ops) WITH (lists = 16) WHERE documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL" +CONTEXT: SQL statement "CREATE INDEX documents_rum_index_8147 ON documentdb_data.documents_8105 USING ivfflat(CAST(documentdb_api_internal.bson_extract_vector(document, 'v'::text) AS public.halfvec(4001)) public.halfvec_l2_ops) WITH (lists = 16) WHERE documentdb_api_internal.bson_extract_vector(document, 'v'::text) IS NOT NULL" SET client_min_messages TO WARNING; SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_ivf_halfvec", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "half_index", "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 16, "similarity": "L2", "dimensions": 4000, "compression": "half" } } ] }', true); create_indexes_non_concurrently @@ -4047,7 +4167,7 @@ SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_ivf_halfvec', '{ "_ SET client_min_messages TO WARNING; SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_ivf_halfvec", "indexes": [ { "key": { "larage_vector": "cosmosSearch" }, "name": "large_half_index", "cosmosSearchOptions": { "kind": "vector-ivf", "similarity": "COS", "dimensions": 3, "compression": "half" } } ] }', true); ERROR: "2.2222222e+13" is out of range for type halfvec -CONTEXT: SQL statement "CREATE INDEX documents_rum_index_8156 ON documentdb_data.documents_8107 USING ivfflat(CAST(documentdb_api_internal.bson_extract_vector(document, 'larage_vector'::text) AS public.halfvec(3)) public.halfvec_cosine_ops) WITH (lists = 100) WHERE documentdb_api_internal.bson_extract_vector(document, 'larage_vector'::text) IS NOT NULL" +CONTEXT: SQL statement "CREATE INDEX documents_rum_index_8157 ON documentdb_data.documents_8107 USING ivfflat(CAST(documentdb_api_internal.bson_extract_vector(document, 'larage_vector'::text) AS public.halfvec(3)) public.halfvec_cosine_ops) WITH (lists = 100) WHERE documentdb_api_internal.bson_extract_vector(document, 'larage_vector'::text) IS NOT NULL" SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_ivf_halfvec", "indexes": [ { "key": { "larage_vector": "cosmosSearch" }, "name": "large_half_index", "cosmosSearchOptions": { "kind": "vector-ivf", "similarity": "COS", "dimensions": 3 } } ] }', true); create_indexes_non_concurrently --------------------------------------------------------------------- diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/mongo_feature_counters.out b/internal/pg_documentdb_distributed/src/test/regress/expected/mongo_feature_counters.out index 537be5dd..72a7c4af 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/mongo_feature_counters.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/mongo_feature_counters.out @@ -274,9 +274,9 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorInde SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorCollNonExistent", "pipeline": [{ "$vectorSearch": { "queryVector": [8.0, 1.0, 8.0, 7], "limit": 1, "path": "myvector4", "numCandidates": 10 } }, { "$project": { "myvector4": 1, "_id": 0 }} ]}'); ERROR: $vectorSearch must be the first stage in the pipeline SELECT documentdb_distributed_test_helpers.get_feature_counter_pretty(true); - get_feature_counter_pretty + get_feature_counter_pretty --------------------------------------------------------------------- - {"Feature_usage":[{"command_insert" : 2}, {"create_index_vector" : 2}, {"create_index_vector_ip" : 1}, {"create_index_vector_l2" : 1}, {"create_index_vector_type_ivfflat" : 2}, {"project" : 2}, {"search_vector_ivfflat" : 2}, {"vector_search_mongo" : 2}]} + {"Feature_usage":[{"command_insert" : 2}, {"create_index_vector" : 2}, {"create_index_vector_ip" : 1}, {"create_index_vector_l2" : 1}, {"create_index_vector_type_ivfflat" : 2}, {"project" : 2}, {"search_vector_default_nprobes" : 2}, {"search_vector_ivfflat" : 2}, {"vector_search_mongo" : 2}]} (1 row) -- check vector indexes @@ -337,12 +337,12 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorInde { "_id" : { "$numberInt" : "2" }, "elem" : "some sentence hnsw", "vector_hnsw" : [ { "$numberDouble" : "8.0" }, { "$numberDouble" : "1.0" }, { "$numberDouble" : "8.0" }, { "$numberInt" : "8" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "0.54622507455841007307" } } } (1 row) -SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "vector_ivf_half", "nProbes": 10} } } ], "cursor": {} }'); +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "vector_ivf_half" } } } ], "cursor": {} }'); document --------------------------------------------------------------------- (0 rows) -SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0, 1.0 ], "k": 2, "path": "vector_hnsw_half", "efSearch": 5 } } } ], "cursor": {} }'); +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0, 1.0 ], "k": 2, "path": "vector_hnsw_half" } } } ], "cursor": {} }'); document --------------------------------------------------------------------- (0 rows) @@ -368,9 +368,9 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorInde ROLLBACK; SELECT documentdb_distributed_test_helpers.get_feature_counter_pretty(true); - get_feature_counter_pretty + get_feature_counter_pretty --------------------------------------------------------------------- - {"Feature_usage":[{"command_insert" : 2}, {"create_index_vector" : 4}, {"create_index_vector_cos" : 2}, {"create_index_vector_compression_half" : 2}, {"create_index_vector_l2" : 2}, {"create_index_vector_type_hnsw" : 2}, {"create_index_vector_type_ivfflat" : 2}, {"match" : 2}, {"search" : 6}, {"search_vector_compression_half" : 2}, {"search_vector_hnsw" : 3}, {"search_vector_ivfflat" : 3}, {"search_vector_pre_filter" : 2}, {"index_scan_with_limit" : 2}]} + {"Feature_usage":[{"command_insert" : 2}, {"create_index_vector" : 4}, {"create_index_vector_cos" : 2}, {"create_index_vector_compression_half" : 2}, {"create_index_vector_l2" : 2}, {"create_index_vector_type_hnsw" : 2}, {"create_index_vector_type_ivfflat" : 2}, {"match" : 2}, {"search" : 6}, {"search_vector_compression_half" : 2}, {"search_vector_default_nprobes" : 1}, {"search_vector_default_efsearch" : 1}, {"search_vector_hnsw" : 3}, {"search_vector_ivfflat" : 3}, {"search_vector_pre_filter" : 2}, {"index_scan_with_limit" : 2}]} (1 row) -- aggregation operators counters diff --git a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw.sql b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw.sql index c1a54dfa..5f87172e 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw.sql +++ b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw.sql @@ -227,6 +227,14 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); ROLLBACK; +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SET LOCAL documentdb.enableVectorCalculateDefaultSearchParam = off; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); +ROLLBACK; + -- more than 10000 and less than 1M documents, use default efSearch 40 -- generate 9000 documents and insert into collection select batch_insert_testing_vector_documents('aggregation_pipeline_hnsw_efsearch', 1151, 9000, 2000); @@ -239,6 +247,14 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); ROLLBACK; +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SET LOCAL documentdb.enableVectorCalculateDefaultSearchParam = off; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_efsearch", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); +ROLLBACK; + ---------------------------------------------------------------------------------------------------- -- hnsw search with filter SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_hnsw_filter', '{ "_id": 6, "meta":{ "a": "some sentence", "b": 1 }, "c": true , "v": [3.0, 5.0, 1.1 ] }'); diff --git a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_ivf.sql b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_ivf.sql index cf74beeb..f2b2998d 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_ivf.sql +++ b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_ivf.sql @@ -189,7 +189,15 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); ROLLBACK; --- more than 10000 and less than 1M documents, use documents / 1000 as nProbes +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SET LOCAL documentdb.enableVectorCalculateDefaultSearchParam = off; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); +ROLLBACK; + +-- more than 10000 and less than 1M documents, nProbes = 10000/(numOfRows/numlist) = 10000/(10150/150) = 148 -- generate 9000 documents and insert into collection select batch_insert_testing_vector_documents('aggregation_pipeline_ivf_nprobes', 1151, 9000, 2000); ANALYZE; @@ -201,17 +209,24 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); ROLLBACK; --- more than 1M documents, use sqrt(documents) as nProbes --- generate 1000000 documents and insert into collection --- select batch_insert_testing_vector_documents('aggregation_pipeline_ivf_nprobes', 10151, 990000, 5000); --- ANALYZE; +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SET LOCAL documentdb.enableVectorCalculateDefaultSearchParam = off; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); +ROLLBACK; --- BEGIN; --- SET LOCAL enable_seqscan to off; --- SET LOCAL documentdb.enableVectorPreFilter = on; --- SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); --- EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); --- ROLLBACK; +-- nProbes = 10000/(numOfRows/numlist) = 10000/(10150/1) = 1 +CALL documentdb_api.drop_indexes('db', '{ "dropIndexes": "aggregation_pipeline_ivf_nprobes", "index": "foo_1"}'); +SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_ivf_nprobes", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "foo_1", "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 1, "similarity": "L2", "dimensions": 3 } } ] }', true); +ANALYZE; + +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_nprobes", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ] } } } ], "cursor": {} }'); +ROLLBACK; ---------------------------------------------------------------------------------------------------- -- ivf search with filter diff --git a/internal/pg_documentdb_distributed/src/test/regress/sql/mongo_feature_counters.sql b/internal/pg_documentdb_distributed/src/test/regress/sql/mongo_feature_counters.sql index 60439fa7..d8bee368 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/sql/mongo_feature_counters.sql +++ b/internal/pg_documentdb_distributed/src/test/regress/sql/mongo_feature_counters.sql @@ -123,8 +123,8 @@ ANALYZE; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "vector_ivf", "nProbes": 10} } } ], "cursor": {} }'); SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0, 1.0 ], "k": 2, "path": "vector_hnsw", "efSearch": 5 } } } ], "cursor": {} }'); -SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "vector_ivf_half", "nProbes": 10} } } ], "cursor": {} }'); -SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0, 1.0 ], "k": 2, "path": "vector_hnsw_half", "efSearch": 5 } } } ], "cursor": {} }'); +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "vector_ivf_half" } } } ], "cursor": {} }'); +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "vectorIndexCollFC", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0, 1.0 ], "k": 2, "path": "vector_hnsw_half" } } } ], "cursor": {} }'); BEGIN; SET LOCAL documentdb.enableVectorPreFilter = on; diff --git a/pg_documentdb/include/utils/feature_counter.h b/pg_documentdb/include/utils/feature_counter.h index 351f6da0..eddbd532 100644 --- a/pg_documentdb/include/utils/feature_counter.h +++ b/pg_documentdb/include/utils/feature_counter.h @@ -19,7 +19,7 @@ #include #define MAX_FEATURE_NAME_LENGTH 255 -#define MAX_FEATURE_COUNT 270 +#define MAX_FEATURE_COUNT 273 /* Internal features that are not exposed */ #define INTERNAL_FEATURE_TYPE MAX_FEATURE_COUNT @@ -285,6 +285,9 @@ typedef enum FEATURE_STAGE_SEARCH_VECTOR, FEATURE_STAGE_SEARCH_VECTOR_COMPRESSION_HALF, FEATURE_STAGE_SEARCH_VECTOR_COMPRESSION_PQ, + FEATURE_STAGE_SEARCH_VECTOR_DEFAULT_NPROBES, + FEATURE_STAGE_SEARCH_VECTOR_DEFAULT_EFSEARCH, + FEATURE_STAGE_SEARCH_VECTOR_DEFAULT_LSEARCH, FEATURE_STAGE_SEARCH_VECTOR_DISKANN, FEATURE_STAGE_SEARCH_VECTOR_EXACT, FEATURE_STAGE_SEARCH_VECTOR_GEN_EMBEDDINGS, diff --git a/pg_documentdb/include/vector/vector_common.h b/pg_documentdb/include/vector/vector_common.h index 1b51fba4..6dfa2257 100644 --- a/pg_documentdb/include/vector/vector_common.h +++ b/pg_documentdb/include/vector/vector_common.h @@ -51,7 +51,6 @@ /* dynamic calculation of nprobes or efSearch depending on collection size */ #define VECTOR_SEARCH_SMALL_COLLECTION_ROWS 10000 -#define VECTOR_SEARCH_1M_COLLECTION_ROWS 1000000 /* metadata field names */ #define VECTOR_METADATA_FIELD_NAME "__cosmos_meta__" @@ -83,4 +82,9 @@ extern int VectorPreFilterIterativeScanMode; extern bool EnableVectorCompressionHalf; extern bool EnableVectorCompressionPQ; +/* + * GUC to enable vector search default search parameter calculation. + */ +extern bool EnableVectorCalculateDefaultSearchParameter; + #endif diff --git a/pg_documentdb/src/configs/feature_flag_configs.c b/pg_documentdb/src/configs/feature_flag_configs.c index 97ea3675..5dba4bc5 100644 --- a/pg_documentdb/src/configs/feature_flag_configs.c +++ b/pg_documentdb/src/configs/feature_flag_configs.c @@ -39,6 +39,10 @@ bool EnableVectorCompressionHalf = DEFAULT_ENABLE_VECTOR_COMPRESSION_HALF; #define DEFAULT_ENABLE_VECTOR_COMPRESSION_PQ true bool EnableVectorCompressionPQ = DEFAULT_ENABLE_VECTOR_COMPRESSION_PQ; +#define DEFAULT_ENABLE_VECTOR_CALCULATE_DEFAULT_SEARCH_PARAM true +bool EnableVectorCalculateDefaultSearchParameter = + DEFAULT_ENABLE_VECTOR_CALCULATE_DEFAULT_SEARCH_PARAM; + /* * SECTION: Indexing feature flags */ @@ -198,14 +202,21 @@ InitializeFeatureFlagConfigurations(const char *prefix, const char *newGucPrefix NULL, &EnableVectorCompressionHalf, DEFAULT_ENABLE_VECTOR_COMPRESSION_HALF, PGC_USERSET, 0, NULL, NULL, NULL); - DefineCustomBoolVariable - ( + DefineCustomBoolVariable( psprintf("%s.enableVectorCompressionPQ", newGucPrefix), gettext_noop( "Enables support for vector index compression product quantization"), NULL, &EnableVectorCompressionPQ, DEFAULT_ENABLE_VECTOR_COMPRESSION_PQ, PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomBoolVariable( + psprintf("%s.enableVectorCalculateDefaultSearchParam", newGucPrefix), + gettext_noop( + "Enables support for vector index default search parameter calculation"), + NULL, &EnableVectorCalculateDefaultSearchParameter, + DEFAULT_ENABLE_VECTOR_CALCULATE_DEFAULT_SEARCH_PARAM, + PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomBoolVariable( psprintf("%s.enable_large_unique_index_keys", newGucPrefix), gettext_noop("Whether or not to enable large index keys on unique indexes."), diff --git a/pg_documentdb/src/infrastructure/feature_counter.c b/pg_documentdb/src/infrastructure/feature_counter.c index 664c1a64..6eb83e2b 100644 --- a/pg_documentdb/src/infrastructure/feature_counter.c +++ b/pg_documentdb/src/infrastructure/feature_counter.c @@ -296,6 +296,9 @@ static char FeatureMapping[MAX_FEATURE_COUNT][MAX_FEATURE_NAME_LENGTH] = { [FEATURE_STAGE_SEARCH_VECTOR] = "search_vector", [FEATURE_STAGE_SEARCH_VECTOR_COMPRESSION_HALF] = "search_vector_compression_half", [FEATURE_STAGE_SEARCH_VECTOR_COMPRESSION_PQ] = "search_vector_compression_pq", + [FEATURE_STAGE_SEARCH_VECTOR_DEFAULT_NPROBES] = "search_vector_default_nprobes", + [FEATURE_STAGE_SEARCH_VECTOR_DEFAULT_EFSEARCH] = "search_vector_default_efsearch", + [FEATURE_STAGE_SEARCH_VECTOR_DEFAULT_LSEARCH] = "search_vector_default_lsearch", [FEATURE_STAGE_SEARCH_VECTOR_DISKANN] = "search_vector_diskann", [FEATURE_STAGE_SEARCH_VECTOR_EXACT] = "search_vector_exact", [FEATURE_STAGE_SEARCH_VECTOR_GEN_EMBEDDINGS] = "search_vector_gen_embeddings", diff --git a/pg_documentdb/src/vector/vector_index_kind_impl.c b/pg_documentdb/src/vector/vector_index_kind_impl.c index 64b4c67f..f950a8b9 100644 --- a/pg_documentdb/src/vector/vector_index_kind_impl.c +++ b/pg_documentdb/src/vector/vector_index_kind_impl.c @@ -631,6 +631,8 @@ CalculateIVFSearchParamBson(bytea *indexOptions, Cardinality indexRows, return searchParamBson; } + ReportFeatureUsage(FEATURE_STAGE_SEARCH_VECTOR_DEFAULT_NPROBES); + /* Calculate the default nProbes */ int numLists = -1; if (indexOptions == NULL) @@ -645,27 +647,40 @@ CalculateIVFSearchParamBson(bytea *indexOptions, Cardinality indexRows, } int defaultNumProbes = -1; - if (numLists < 0) + if (numLists <= 0) { defaultNumProbes = IVFFLAT_DEFAULT_NPROBES; } else { - /* nProbes - * < 10000 rows: numLists - * < 1M rows: rows / 1000 - * >= 1M rows: sqrt(rows) */ - if (indexRows < VECTOR_SEARCH_SMALL_COLLECTION_ROWS) + if (EnableVectorCalculateDefaultSearchParameter) { - defaultNumProbes = numLists; - } - else if (indexRows < VECTOR_SEARCH_1M_COLLECTION_ROWS) - { - defaultNumProbes = indexRows / 1000; + /* nProbes < 10000 rows: numLists */ + if (indexRows < VECTOR_SEARCH_SMALL_COLLECTION_ROWS) + { + defaultNumProbes = numLists; + } + else + { + /* >= 10000 rows: we calculate nProbes based on estimated rowsPerCluster + * To make sure we don't scan too many rows */ + double rowsPerCluster = indexRows / numLists; + defaultNumProbes = ceil(VECTOR_SEARCH_SMALL_COLLECTION_ROWS / + rowsPerCluster); + if (defaultNumProbes > numLists) + { + defaultNumProbes = numLists; + } + + if (defaultNumProbes < 1) + { + defaultNumProbes = 1; + } + } } else { - defaultNumProbes = sqrt(indexRows); + defaultNumProbes = IVFFLAT_DEFAULT_NPROBES; } } @@ -700,6 +715,8 @@ CalculateHNSWSearchParamBson(bytea *indexOptions, Cardinality indexRows, return searchParamBson; } + ReportFeatureUsage(FEATURE_STAGE_SEARCH_VECTOR_DEFAULT_EFSEARCH); + /* Calculate the default efSearch */ int efConstruction = -1; @@ -721,9 +738,16 @@ CalculateHNSWSearchParamBson(bytea *indexOptions, Cardinality indexRows, } else { - if (indexRows < VECTOR_SEARCH_SMALL_COLLECTION_ROWS) + if (EnableVectorCalculateDefaultSearchParameter) { - defaultEfSearch = efConstruction; + if (indexRows < VECTOR_SEARCH_SMALL_COLLECTION_ROWS) + { + defaultEfSearch = efConstruction; + } + else + { + defaultEfSearch = HNSW_DEFAULT_EF_SEARCH; + } } else {