diff --git a/lib/events/pgevents/pgevents.go b/lib/events/pgevents/pgevents.go index df59ee4e74da..4672a661f54a 100644 --- a/lib/events/pgevents/pgevents.go +++ b/lib/events/pgevents/pgevents.go @@ -79,10 +79,22 @@ const ( ); CREATE INDEX events_search_session_events_idx ON events (session_id, event_time, event_id) WHERE session_id != '00000000-0000-0000-0000-000000000000';` - dateIndex = "CREATE INDEX events_creation_time_idx ON events USING brin (creation_time);" - schemaV1TableWithDateIndex = schemaV1Table + "\n" + dateIndex - schemaV1CockroachSetRowExpiry = "ALTER TABLE events SET (ttl_expiration_expression = '((creation_time AT TIME ZONE ''UTC'') + (%d * INTERVAL ''1 microsecond'')) AT TIME ZONE ''UTC'' ');" - schemaV1CockroachUnsetRowExpiry = "ALTER TABLE events RESET (ttl_expiration_expression);" + dateIndex = "CREATE INDEX events_creation_time_idx ON events USING brin (creation_time);" + schemaV1TableWithDateIndex = schemaV1Table + "\n" + dateIndex + + // the 'n'::INTERVAL expression will saturate at around 292 years (which is + // perfectly acceptable for a retention period of the audit log), and the + // sum between a TIMESTAMPTZ set around 2024 and an INTERVAL of up to 292 + // years is always representable + // + // the string to INTERVAL cast should be stable, unlike any integer to + // INTERVAL cast (see https://github.com/cockroachdb/cockroach/issues/57876) + schemaV1CockroachSetRowExpirySeconds = "ALTER TABLE events SET (ttl_expiration_expression = '((creation_time AT TIME ZONE ''UTC'') + (''%d''::INTERVAL)) AT TIME ZONE ''UTC''');" + // the asymmetry here is intended, crdb requires "RESET (ttl)" to disable + // row-level TTL on a table, whereas "RESET (ttl_expiration_expression)" + // would remove the expression in favor of ttl_expire_after (and it will + // error out if ttl_expire_after is unset) + schemaV1CockroachUnsetRowExpiry = "ALTER TABLE events RESET (ttl);" ) // Config is the configuration struct to pass to New. @@ -264,11 +276,10 @@ func configureCockroachDBRetention(ctx context.Context, cfg *Config, pool *pgxpo expiryQuery = schemaV1CockroachUnsetRowExpiry } else { cfg.Log.DebugContext(ctx, "Configuring CockroachDB native row expiry") - expiryQuery = fmt.Sprintf(schemaV1CockroachSetRowExpiry, cfg.RetentionPeriod) + expiryQuery = fmt.Sprintf(schemaV1CockroachSetRowExpirySeconds, int64(cfg.RetentionPeriod.Seconds())) } _, err := pool.Exec(ctx, expiryQuery, pgx.QueryExecModeExec) return trace.Wrap(err) - } func buildSchema(isCockroach bool, cfg *Config) (schemas []string, err error) {