|
| 1 | +# Retention Policies |
| 2 | + |
| 3 | +A retention policy automatically drops old chunks from a hypertable or continuous aggregate on a scheduled basis. This keeps storage consumption bounded without requiring manual intervention and is the standard approach for managing time-series data lifecycle in TimescaleDB. |
| 4 | + |
| 5 | +Each hypertable or continuous aggregate supports at most one retention policy. |
| 6 | + |
| 7 | +[See also: add_retention_policy](https://docs.tigerdata.com/api/latest/data_retention/add_retention_policy/) |
| 8 | + |
| 9 | +## Drop Modes |
| 10 | + |
| 11 | +Two mutually exclusive drop modes are available: |
| 12 | + |
| 13 | +- **`dropAfter`**: Drops chunks whose data falls outside a time window relative to the current time. This is the standard mode. |
| 14 | +- **`dropCreatedBefore`**: Drops chunks created before a specified interval ago, regardless of the data they contain. |
| 15 | + |
| 16 | +Exactly one of `dropAfter` or `dropCreatedBefore` must be specified. Providing both or neither raises an exception. |
| 17 | + |
| 18 | +## Basic Example |
| 19 | + |
| 20 | +```csharp |
| 21 | +using CmdScale.EntityFrameworkCore.TimescaleDB.Configuration.RetentionPolicy; |
| 22 | +using Microsoft.EntityFrameworkCore; |
| 23 | +using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| 24 | + |
| 25 | +public class ApplicationLogConfiguration : IEntityTypeConfiguration<ApplicationLog> |
| 26 | +{ |
| 27 | + public void Configure(EntityTypeBuilder<ApplicationLog> builder) |
| 28 | + { |
| 29 | + builder.HasKey(x => new { x.Id, x.Time }); |
| 30 | + |
| 31 | + builder.IsHypertable(x => x.Time) |
| 32 | + .WithChunkTimeInterval("1 day"); |
| 33 | + |
| 34 | + // Drop chunks older than 30 days, running the job daily |
| 35 | + builder.WithRetentionPolicy( |
| 36 | + dropAfter: "30 days", |
| 37 | + scheduleInterval: "1 day"); |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## Using `dropCreatedBefore` |
| 43 | + |
| 44 | +```csharp |
| 45 | +public class ApiRequestLogConfiguration : IEntityTypeConfiguration<ApiRequestLog> |
| 46 | +{ |
| 47 | + public void Configure(EntityTypeBuilder<ApiRequestLog> builder) |
| 48 | + { |
| 49 | + builder.HasKey(x => new { x.Id, x.Time }); |
| 50 | + |
| 51 | + builder.IsHypertable(x => x.Time) |
| 52 | + .WithChunkTimeInterval("1 day"); |
| 53 | + |
| 54 | + // Drop chunks created more than 30 days ago |
| 55 | + builder.WithRetentionPolicy( |
| 56 | + dropCreatedBefore: "30 days", |
| 57 | + scheduleInterval: "1 day"); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +> :warning: **Note:** Due to a known bug in TimescaleDB ([#9446](https://github.com/timescale/timescaledb/issues/9446)), `alter_job` fails when used with `drop_created_before` policies. The library works around this by skipping the `alter_job` call for `drop_created_before` policies. As a result, job scheduling parameters (`scheduleInterval`, `maxRuntime`, `maxRetries`, `retryPeriod`) are accepted by the API but have no effect at the database level when `dropCreatedBefore` is used. |
| 63 | +
|
| 64 | +## Complete Example |
| 65 | + |
| 66 | +```csharp |
| 67 | +using CmdScale.EntityFrameworkCore.TimescaleDB.Configuration.RetentionPolicy; |
| 68 | +using Microsoft.EntityFrameworkCore; |
| 69 | +using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| 70 | + |
| 71 | +public class ApplicationLogConfiguration : IEntityTypeConfiguration<ApplicationLog> |
| 72 | +{ |
| 73 | + public void Configure(EntityTypeBuilder<ApplicationLog> builder) |
| 74 | + { |
| 75 | + builder.HasKey(x => new { x.Id, x.Time }); |
| 76 | + |
| 77 | + builder.IsHypertable(x => x.Time) |
| 78 | + .WithChunkTimeInterval("1 day"); |
| 79 | + |
| 80 | + builder.WithRetentionPolicy( |
| 81 | + dropAfter: "30 days", |
| 82 | + initialStart: new DateTime(2025, 10, 1, 3, 0, 0, DateTimeKind.Utc), |
| 83 | + scheduleInterval: "1 day", |
| 84 | + maxRuntime: "30 minutes", |
| 85 | + maxRetries: 3, |
| 86 | + retryPeriod: "5 minutes"); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +public class ApplicationLog |
| 91 | +{ |
| 92 | + public Guid Id { get; set; } |
| 93 | + public DateTime Time { get; set; } |
| 94 | + public string ServiceName { get; set; } = string.Empty; |
| 95 | + public string Level { get; set; } = string.Empty; |
| 96 | + public string Message { get; set; } = string.Empty; |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Supported Parameters |
| 101 | + |
| 102 | +| Parameter | Description | Type | Database Type | Default Value | |
| 103 | +| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------------- | ------------------------------------------- | |
| 104 | +| `dropAfter` | The interval after which chunks are dropped. Mutually exclusive with `dropCreatedBefore`. | `string?` | `INTERVAL` | — | |
| 105 | +| `dropCreatedBefore` | The interval before which chunks created are dropped. Based on chunk creation time. Only supports `INTERVAL`. Not available for integer-based time columns. Mutually exclusive with `dropAfter`. | `string?` | `INTERVAL` | — | |
| 106 | +| `initialStart` | The first time the policy job is scheduled to run, as a UTC `DateTime`. If `null`, the first run is based on the `scheduleInterval`. | `DateTime?` | `TIMESTAMPTZ` | `null` | |
| 107 | +| `scheduleInterval` | The interval at which the retention policy job runs. | `string?` | `INTERVAL` | `'1 day'` | |
| 108 | +| `maxRuntime` | The maximum amount of time the job is allowed to run before being stopped. If `null`, there is no time limit. | `string?` | `INTERVAL` | `'00:00:00'` | |
| 109 | +| `maxRetries` | The number of times the job is retried if it fails. | `int?` | `INTEGER` | `-1` | |
| 110 | +| `retryPeriod` | The amount of time the scheduler waits between retries of a failed job. | `string?` | `INTERVAL` | Equal to the `scheduleInterval` by default. | |
0 commit comments