ALTER MATERIALIZED VIEW SET TTL

Sets the time-to-live (TTL) period on a materialized view, automatically dropping partitions older than the specified duration.

Syntax

ALTER MATERIALIZED VIEW viewName SET TTL n timeUnit

Where timeUnit is: HOURS | DAYS | WEEKS | MONTHS | YEARS (or shorthand: h, d, w, M, y)

Parameters

ParameterDescription
viewNameName of the materialized view to modify
nNumber of time units to retain
timeUnitTime unit for the retention period

When to use

Set a TTL when:

  • You only need recent aggregated data (e.g., last 30 days)
  • Disk space is a concern for long-running views
  • Compliance requires automatic data expiration

How it works

QuestDB automatically drops partitions that exceed the TTL. Data removal happens at partition boundaries, not row-by-row.

note

The TTL period must be a whole number multiple of the view's partition size. For example, a view with PARTITION BY DAY can have TTL 7 DAYS but not TTL 36 HOURS.

Time units

UnitSingularPluralShorthand
HoursHOURHOURSh
DaysDAYDAYSd
WeeksWEEKWEEKSw
MonthsMONTHMONTHSM
YearsYEARYEARSy

Examples

Keep 3 days of data
ALTER MATERIALIZED VIEW trades_hourly SET TTL 3 DAYS;
Keep 12 hours of data (shorthand)
ALTER MATERIALIZED VIEW trades_hourly SET TTL 12h;
Keep 1 year of data
ALTER MATERIALIZED VIEW trades_daily SET TTL 1 YEAR;

Behavior

AspectDescription
GranularityData dropped at partition boundaries only
IndependenceView TTL is separate from base table TTL
Immediate effectExpired partitions dropped on next maintenance cycle

Permissions (Enterprise)

Changing TTL requires the ALTER MATERIALIZED VIEW permission:

Grant alter permission
GRANT ALTER MATERIALIZED VIEW ON trades_hourly TO user1;

Errors

ErrorCause
materialized view does not existView with specified name doesn't exist
invalid TTLTTL not a multiple of partition size
invalid time unitUnrecognized time unit
permission deniedMissing ALTER MATERIALIZED VIEW permission (Enterprise)

See also