You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merged PR 1751463: [TTL] use index hints to force ordered index scan on composite TTL indexes
### 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
In this PR, for TTL indexes, created as a composite index, we force an ordered scan while querying for eligible rows using index hints.
Composite indexes support ordered scan on the index allowing the scan to be really efficient on the index. Without composite index, the planner uses a bitmap index scan which requires fetching all index pages and creating a bitmap. If there are a lot of documents to deleted, this can put a lot of pressure on the disk IOPS due to repeated fetching of large amount of index pages.
When TTL index is created as a composite index, we effectively bypass the planner via the use of index hints and forces postgres to use the composite index. When index hint is provided the planner pins an ordered scan on the index provided as a hint.
Below is an example of how an additional condition is added, as an index hint, to the query for fetching purge eligible rows:
```sql
SELECT ctid FROM mongo_data.documents_1963502_19635007
WHERE bson_dollar_lt(document, '{ "ttl" : { "$date" : { "$numberLong" : "1657900030775" } } }'::bson)
AND helio_api_internal.bson_dollar_index_hint(document, 'ttl_index'::text, '{"key": {"ttl": 1}}'::bson, isSparse)
```
-- 21. TTL index with forced ordered scan via index hints
333
+
334
+
setdocumentdb.enableExtendedExplainPlans to on;
335
+
SETdocumentdb.enableNewCompositeIndexOpClass to on;
336
+
setdocumentdb_rum.preferOrderedIndexScan to on;
337
+
338
+
-- if documentdb_extended_rum exists, set alternate index handler
339
+
SELECTpg_catalog.set_config('documentdb.alternate_index_handler_name', 'extended_rum', false), extname FROM pg_extension WHERE extname ='documentdb_extended_rum';
340
+
341
+
-- Delete all other indexes from previous tests to reduce flakiness
-- make sure jobs are scheduled and disable it to avoid flakiness on the test as it could run on its schedule and delete documents before we run our commands in the test
SELECT bson_dollar_unwind(cursorpage, '$cursor.firstBatch') FROMdocumentdb_api.list_indexes_cursor_first_page('db','{ "listIndexes": "ttlCompositeOrderedScan" }') ORDER BY1;
386
+
SELECTcount(*) from ( SELECT shard_key_value, object_id, document fromdocumentdb_api.collection('db', 'ttlCompositeOrderedScan') order by object_id) as a;
387
+
388
+
-- Call ttl purge procedure with a batch size of 100
389
+
BEGIN;
390
+
SET client_min_messages TO LOG;
391
+
SET LOCAL documentdb.logTTLProgressActivity to on;
SELECTcount(*) from ( SELECT shard_key_value, object_id, document fromdocumentdb_api.collection('db', 'ttlCompositeOrderedScan') order by object_id) as a;
406
+
407
+
408
+
-- TTL indexes behaves like normal indexes that are used in queries (cx can provide .hint() to force)
409
+
BEGIN;
410
+
SET LOCAL documentdb.enableIndexOrderbyPushdown to on;
411
+
SET LOCAL documentdb.enableNewCompositeIndexOpClass to on;
-- Check TTL deletes work on sharded (should delete 800 docs, 100 for each shard)
432
+
SELECTcount(*) from ( SELECT shard_key_value, object_id, document fromdocumentdb_api.collection('db', 'ttlCompositeOrderedScan') order by object_id) as a;
SELECTcount(*) from ( SELECT shard_key_value, object_id, document fromdocumentdb_api.collection('db', 'ttlCompositeOrderedScan') order by object_id) as a;
435
+
436
+
437
+
-- Check for Ordered Indes Scan on the ttl index
438
+
439
+
BEGIN;
440
+
SET LOCAL documentdb.enableIndexOrderbyPushdown to on;
441
+
SET LOCAL documentdb.enableNewCompositeIndexOpClass to on;
442
+
EXPLAIN(analyze on, verbose on, costs off, timing off, summary off) SELECT ctid FROMdocumentdb_data.documents_20006_2000124
0 commit comments