From 6955f49ea652f24a36c8e5a2e928072d79057ac6 Mon Sep 17 00:00:00 2001 From: zer0stars <74260741+zer0stars@users.noreply.github.com> Date: Thu, 11 Jun 2026 14:53:54 -0400 Subject: [PATCH] feat(migrations): drop per-part latest projections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Superseded by signal_latest (00010) and the summary tables (00011). Projections are stored per part, so latest-state reads through them fan out across all ~140 parts of the signal table — measured p50 ~3s in prod vs ~110ms for raw primary-key reads. Dropping also removes the projection rebuild cost from every merge. --- .../00012_drop_latest_projections.sql | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 pkg/migrations/00012_drop_latest_projections.sql diff --git a/pkg/migrations/00012_drop_latest_projections.sql b/pkg/migrations/00012_drop_latest_projections.sql new file mode 100644 index 0000000..3d8bab4 --- /dev/null +++ b/pkg/migrations/00012_drop_latest_projections.sql @@ -0,0 +1,73 @@ +-- +goose Up + +-- The per-part latest projections inherited the signal table's part fan-out +-- (projections are stored per part) with fatter aggregate-state granules, and +-- measured slower than raw primary-key reads in prod (signalsLatest p50 ~3s +-- via projection vs ~110ms raw, 2026-06-11). signal_latest (00010) and the +-- summary tables (00011) replace them. Apply only after telemetry-api reads +-- the new tables (this migration ships in a later model-garage release to +-- enforce that ordering). + +-- +goose StatementBegin +ALTER TABLE signal DROP PROJECTION IF EXISTS signal_latest_by_subject_source_name; +-- +goose StatementEnd + +-- +goose StatementBegin +ALTER TABLE event DROP PROJECTION IF EXISTS event_latest_by_subject_source_name; +-- +goose StatementEnd + +-- +goose StatementBegin +ALTER TABLE signal RESET SETTING deduplicate_merge_projection_mode; +-- +goose StatementEnd + +-- +goose StatementBegin +ALTER TABLE event RESET SETTING deduplicate_merge_projection_mode; +-- +goose StatementEnd + +-- +goose Down + +-- Recreate the projections as defined in migration 00009. Re-materializing is +-- an operator decision (heavy mutation); without it the projections only +-- cover parts written after this rollback. + +-- +goose StatementBegin +ALTER TABLE signal MODIFY SETTING deduplicate_merge_projection_mode = 'rebuild'; +-- +goose StatementEnd + +-- +goose StatementBegin +ALTER TABLE event MODIFY SETTING deduplicate_merge_projection_mode = 'rebuild'; +-- +goose StatementEnd + +-- +goose StatementBegin +ALTER TABLE signal ADD PROJECTION IF NOT EXISTS signal_latest_by_subject_source_name ( + SELECT + subject, + source, + name, + max(timestamp), + min(timestamp), + argMax(value_number, timestamp), + argMax(value_string, timestamp), + argMaxIf(value_location, timestamp, (tupleElement(value_location, 'latitude') != 0) OR (tupleElement(value_location, 'longitude') != 0)), + maxIf(timestamp, (tupleElement(value_location, 'latitude') != 0) OR (tupleElement(value_location, 'longitude') != 0)), + argMax(producer, timestamp), + argMax(cloud_event_id, timestamp), + count() + GROUP BY subject, source, name +); +-- +goose StatementEnd + +-- +goose StatementBegin +ALTER TABLE event ADD PROJECTION IF NOT EXISTS event_latest_by_subject_source_name ( + SELECT + subject, + source, + name, + max(timestamp), + min(timestamp), + argMax(producer, timestamp), + argMax(cloud_event_id, timestamp), + count() + GROUP BY subject, source, name +); +-- +goose StatementEnd