Files
documentdb/pg_documentdb/sql/udfs/ttl/ttl_support_functions--latest.sql
T
Santiago Fernandez Madero 322fba0605 Merged PR 1566855: [OSS] Fix TTL indexes in documentdb
### 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?

### Release Note (Refer [Template](../docs/release_notes/pgmongo_template.md))

### Description

[OSS] Fix TTL indexes in documentdb

This moves the function to C and instead of querying the indexes and then getting the collection shards from pg_dist_shard table, it uses the C hook to get the table shards where for non-distributed it returns the base table and then uses that information to drop the expired records.

----
#### AI description  (iteration 1)
#### PR Classification
Bug fix

#### PR Summary
This pull request addresses issues with TTL indexes in DocumentDB by refactoring and optimizing the deletion of expired rows.
- `ttl_index.c`: Refactored the function to delete expired rows, added a new function `delete_expired_rows`, and optimized the deletion process.
- `ttl_support_functions--latest.sql` and `delete_expired_rows--latest.sql`: Replaced PL/pgSQL procedures with C language implementations for better performance.
- `collection.c`: Moved the function `GetMongoCollectionShardOidsAndNames` from `coll_stats.c` to `collection.c` for better organization.
2025-02-19 17:00:35 +08:00

32 lines
1.6 KiB
SQL

DROP FUNCTION IF EXISTS __API_SCHEMA_INTERNAL_V2__.delete_expired_rows_for_index(
IN collection_id bigint,
IN index_id bigint,
IN index_key __CORE_SCHEMA__.bson,
IN index_pfe __CORE_SCHEMA__.bson,
IN current_datetime bigint,
IN index_expiry int,
IN ttl_batch_size int,
IN shard_id bigint);
/*
* Procedure to delete expired rows specified by ttl indexes. The procedure goes over
* all the valid TTL indexes and deletes a small batch of expired rows from one of the
* shards of the corresponding collection. As longs as we delete some data from one of
* the shards of the collection, we exit out of the function to make forward progress and
* to keep delete deletion prcess short, so that it does not hold locks for a long period.
*
* Note since we go over all the shards for the collection, the solution works for all sharded
* and unsharded collection.
*
* The method takes the maximum number of documents than can be deleted in a batch as a parameter.
* A value of -1 (default) instructs the caller to use the default batch size avilable via GUC.
* Ability of pass the batch size parameter is useful for testing.
*
* IMP: This procedure is designed only to be called from pg_cron ttl purger task.
*/
CREATE OR REPLACE PROCEDURE __API_SCHEMA_INTERNAL_V2__.delete_expired_rows(IN p_batch_size int default -1)
LANGUAGE c
AS 'MODULE_PATHNAME', $procedure$delete_expired_rows$procedure$;
COMMENT ON PROCEDURE __API_SCHEMA_INTERNAL_V2__.delete_expired_rows(int)
IS 'Drops expired rows from applicable collections based on TTL indexes.';