TS.ADD key timestamp value [RETENTION retentionPeriod] [ENCODING <COMPRESSED|UNCOMPRESSED>] [CHUNK_SIZE size] [DUPLICATE_POLICY policy] [ON_DUPLICATE policy_ovr] [IGNORE ignoreMaxTimediff ignoreMaxValDiff] [LABELS [label value ...]]
@timeseries
,
@write
,
@slow
,
Append a sample to a time series
key
is key name for the time series.
timestamp
is Unix time (integer, in milliseconds) specifying the sample timestamp or *
to set the sample timestamp to the Unix time of the server's clock.
Unix time is the number of milliseconds that have elapsed since 00:00:00 UTC on 1 January 1970, the Unix epoch, without adjustments made due to leap seconds.
value
is (double) numeric data value of the sample. The double number should follow RFC 7159 (JSON standard). In particular, the parser rejects overly large values that do not fit in binary64. It does not accept NaN or infinite values.
When specified key does not exist, a new time series is created.
if a COMPACTION_POLICY configuration parameter is defined, compacted time series would be created as well.
If timestamp
is older than the retention period compared to the maximum existing timestamp, the sample is discarded and an error is returned.
When adding a sample to a time series for which compaction rules are defined:
Explicitly adding samples to a compacted time series (using TS.ADD
, TS.MADD
, TS.INCRBY
, or TS.DECRBY
) may result in inconsistencies between the raw and the compacted data. The compaction process may override such samples.
The following arguments are optional because they can be set by TS.CREATE
.
RETENTION retentionPeriod
is maximum retention period, compared to the maximum existing timestamp, in milliseconds.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See RETENTION
in TS.CREATE
.
ENCODING enc
specifies the series sample encoding format.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See ENCODING
in TS.CREATE
.
CHUNK_SIZE size
is memory size, in bytes, allocated for each data chunk.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See CHUNK_SIZE
in TS.CREATE
.
DUPLICATE_POLICY policy
is policy for handling insertion (TS.ADD
and TS.MADD
) of multiple samples with identical timestamps.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See DUPLICATE_POLICY
in TS.CREATE
.
ON_DUPLICATE policy_ovr
is overwrite key and database configuration for DUPLICATE_POLICY, the policy for handling samples with identical timestamps.
This override is effective only for this single command and does not set the time series duplication policy (which can be set with TS.ALTER
).
policy_ovr
can be one of the following values:
BLOCK
: ignore any newly reported value and reply with an errorFIRST
: ignore any newly reported valueLAST
: override with the newly reported valueMIN
: only override if the value is lower than the existing valueMAX
: only override if the value is higher than the existing valueSUM
: If a previous sample exists, add the new sample to it so that the updated value is set to (previous + new). If no previous sample exists, the value is set to the new value.This argument has no effect when a new time series is created by this command.
IGNORE ignoreMaxTimediff ignoreMaxValDiff
is the policy for handling duplicate samples. A new sample is considered a duplicate and is ignored if the following conditions are met:
DUPLICATE_POLICY
IS LAST
;timestamp ≥ max_timestamp
);timestamp - max_timestamp
) is less than or equal to IGNORE_MAX_TIME_DIFF
;abs(value - value_at_max_timestamp
) is less than or equal to IGNORE_MAX_VAL_DIFF
.where max_timestamp
is the timestamp of the sample with the largest timestamp in the time series, and value_at_max_timestamp
is the value at max_timestamp
.
When not specified: set to the global IGNORE_MAX_TIME_DIFF and IGNORE_MAX_VAL_DIFF, which are, by default, both set to 0.
These parameters are used when creating a new time series to set the per-key parameters, and are ignored when called with an existing time series (the existing per-key configuration parameters is used).
LABELS {label value}...
is set of label-value pairs that represent metadata labels of the time series.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See LABELS
in TS.CREATE
.
RETENTION
, ENCODING
, CHUNK_SIZE
, DUPLICATE_POLICY
, IGNORE
, and LABELS
are used only when creating a new time series, and ignored when adding samples to an existing time series.RETENTION
and LABELS
introduces additional time complexity.
If a compaction rule exists on a time series, the performance of TS.ADD
can be reduced.
The complexity of TS.ADD
is always O(M)
, where M
is the number of compaction rules or O(1)
with no compaction.
Create a temperature time series, set its retention to 1 year, and append a sample.
127.0.0.1:6379> TS.ADD temperature:3:11 1548149183000 27 RETENTION 31536000000
(integer) 1548149183000
Add a sample to the time series, setting the sample's timestamp to the current Unix time of the server's clock.
127.0.0.1:6379> TS.ADD temperature:3:11 * 30
(integer) 1662042954573
One of the following:
IGNORE
in TS.CREATE
), the reply will be the largest timestamp in the time series.BLOCK
, or when timestamp
is older than the retention period compared to the maximum existing timestamp.