Set the time-to-live (TTL)
Both the INSERT and UPDATE commands support setting a time for data in a column to expire, including collections and user-defined types.
The USING TTL clause can be used to set a specific lifetime for a value when inserting or updating data in a table.
To determine the current time-to-live for a record, use the TTL function.
You can also set a global TTL for a table.
|
The database storage engine can only encode TTL timestamps through The TTL date overflow policy ( |
Set the TTL
Insert data into a table, and use the USING TTL clause to set the expiration period.
This example sets the TTL to 200 seconds:
INSERT INTO cycling.calendar (
race_id, race_name, race_start_date, race_end_date
) VALUES (
200, 'placeholder', '2015-05-27', '2015-05-27'
)
USING TTL 200;
Change the TTL
The time-to-live value can be updated with the USING TTL clause in an UPDATE statement.
The example sets the TTL to 300 and updates the race name.
UPDATE cycling.calendar USING TTL 300
SET race_name = 'Tour de France - Stage 12'
WHERE race_id = 200
AND race_start_date = '2015-05-27'
AND race_end_date = '2015-05-27';
To remove a columns TTL, set USING TTL to 0:
UPDATE cycling.calendar USING TTL 0
SET race_name = 'Tour de France - Stage 12'
WHERE race_id = 200
AND race_start_date = '2015-05-27'
AND race_end_date = '2015-05-27';
Get the TTL
Issue a SELECT statement with the TTL function to determine how much longer the data has to live:
SELECT TTL(race_name) FROM cycling.calendar WHERE race_id = 200;
The result prints the current TTL:
ttl(race_name)
----------------
298
(1 rows)
If you repeat the SELECT statement after some time, the time-to-live value will decrease:
ttl(race_name)
----------------
198
(1 rows)