|
| 1 | +--- |
| 2 | +title: add_compaction_policy() |
| 3 | +description: Set a policy to automatically compact unordered chunks in the columnstore |
| 4 | +products: [cloud, self_hosted] |
| 5 | +keywords: [columnstore, hypercore, policies, compaction, direct compress] |
| 6 | +--- |
| 7 | + |
| 8 | +import * as C from "@constants"; |
| 9 | + |
| 10 | +import SinceRelease from '@components/SinceRelease.astro'; |
| 11 | + |
| 12 | +import { Callout } from '@stainless-api/docs/components'; |
| 13 | + |
| 14 | +<SinceRelease version="2.29.0" product="tsdb" /> |
| 15 | + |
| 16 | +Create a [job](/reference/timescaledb/jobs-automation/add_job) that automatically compacts unordered {C.CHUNK}s in the |
| 17 | +{C.COLUMNSTORE}. Compaction combines overlapping batches within a {C.CHUNK} so queries no longer need an extra sort step |
| 18 | +to restore order. |
| 19 | + |
| 20 | +<Callout variant="note"> |
| 21 | + |
| 22 | +You usually don't need to add this policy yourself. [Direct compress](/build/data-management/write-data/insert#improve-performance-with-direct-compress) |
| 23 | +creates it for you, and it is the main reason {C.CHUNK}s end up unordered. Use `add_compaction_policy` when you want to |
| 24 | +tune the schedule or bound how much work each run does. Direct compress is a tech preview, so this policy is too. |
| 25 | + |
| 26 | +</Callout> |
| 27 | + |
| 28 | +The policy only processes {C.CHUNK}s that are fully in the {C.COLUMNSTORE} and marked as unordered. It skips partial |
| 29 | +{C.CHUNK}s, which the [{C.COLUMNSTORE} policy](/reference/timescaledb/hypercore/add_columnstore_policy) handles, and frozen {C.CHUNK}s. If your {C.HYPERTABLE} |
| 30 | +doesn't use direct compress, it is unlikely to have unordered {C.CHUNK}s and this policy has nothing to do. |
| 31 | + |
| 32 | +To view the policies that you set or the policies that already exist, see [informational views](/reference/timescaledb/informational-views/jobs). |
| 33 | + |
| 34 | +## Samples |
| 35 | + |
| 36 | +- **Add a compaction policy with the default 5 minute schedule**: |
| 37 | + |
| 38 | + ```sql |
| 39 | + SELECT add_compaction_policy('metrics'); |
| 40 | + ``` |
| 41 | + |
| 42 | +- **Run less often, bound the work per run, and skip {C.CHUNK}s still being written to**: |
| 43 | + |
| 44 | + ```sql |
| 45 | + SELECT add_compaction_policy('metrics', |
| 46 | + schedule_interval => INTERVAL '15 minutes', |
| 47 | + max_chunks => 10, |
| 48 | + max_batches => 500, |
| 49 | + inactive_for => INTERVAL '30 minutes'); |
| 50 | + ``` |
| 51 | + |
| 52 | +## Arguments |
| 53 | + |
| 54 | +The syntax is: |
| 55 | + |
| 56 | +```sql |
| 57 | +SELECT add_compaction_policy( |
| 58 | + hypertable = '<hypertable_name>', |
| 59 | + if_not_exists = true | false, |
| 60 | + schedule_interval = <interval>, |
| 61 | + initial_start = <timestamptz>, |
| 62 | + timezone = '<timezone>', |
| 63 | + max_chunks = <integer>, |
| 64 | + max_batches = <integer>, |
| 65 | + inactive_for = <interval> |
| 66 | +); |
| 67 | +``` |
| 68 | + |
| 69 | +| Name | Type | Default | Required | Description | |
| 70 | +|---------------------|-------------|--------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 71 | +| `hypertable` | REGCLASS | - | ✔ | Name of the {C.HYPERTABLE} to run this [job](/reference/timescaledb/jobs-automation/add_job) on. | |
| 72 | +| `if_not_exists` | BOOLEAN | `false` | ✖ | Set to `true` so this job fails with a warning rather than an error if a compaction policy already exists on `hypertable`. | |
| 73 | +| `schedule_interval` | INTERVAL | `5 minutes` | ✖ | Set the interval between the finish time of the last execution of this policy and the next start. When direct compress creates this policy, it uses `1 minute` instead. | |
| 74 | +| `initial_start` | TIMESTAMPTZ | `NULL` | ✖ | Set the time this job is first run. | |
| 75 | +| `timezone` | TEXT | `NULL` | ✖ | Set to a valid time zone to mitigate DST shifting. If `initial_start` is set, subsequent executions of this policy are aligned on `initial_start`. | |
| 76 | +| `max_chunks` | INTEGER | `NULL` | ✖ | Set the maximum number of {C.CHUNK}s to process in a single run, including {C.CHUNK}s that fail. Leave unset to process every eligible {C.CHUNK}. | |
| 77 | +| `max_batches` | INTEGER | `NULL` | ✖ | Set the maximum number of batches to combine in each {C.CHUNK}. Leave unset for no limit. | |
| 78 | +| `inactive_for` | INTERVAL | `NULL` | ✖ | Only compact {C.CHUNK}s that have not been written to for this interval. Leave unset to compact every eligible {C.CHUNK} regardless of when it was last written to. | |
| 79 | + |
| 80 | +## Returns |
| 81 | + |
| 82 | +|Column|Type|Description| |
| 83 | +|-|-|-| |
| 84 | +|`job_id`|INTEGER|{C.TIMESCALE_DB} background job ID created to implement this policy| |
0 commit comments