Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

5.3 KB
Raw Blame
-- Outbox for the ClickHouse distinct id feed (clickhouse_person_distinct_id),
-- written by personhog-identity and drained by the personhog-publisher
-- deployment.
--
-- Two row shapes share the table. A create or attach settles inline with no
-- op record, so it writes one row per mapping in the same transaction as the
-- mapping insert. A merge or delete saga already records every repointed or
-- tombstoned mapping in lifecycle_op_person.moved, so its completion writes
-- one pointer row per op and the publisher resolves the pointer against
-- `moved` when it drains. (cursor_person_id, cursor_mapping_ordinal) is the
-- last (person_id, position in `moved`) already published; resolution reads
-- the rows strictly after it, so a large op drains across several claims.
--
-- No foreign key to lifecycle_op on purpose: lifecycle GC skips an op that
-- still has a pointer, and a cascade would silently drop unpublished work.
--
-- The publisher deletes a row once Kafka acks it, so the table is near empty
-- at steady state and its statistics describe an empty table. The partial
-- index on pending rows keeps the claim an index scan whatever the planner
-- estimates; the fixed autovacuum thresholds run vacuum on churn instead of
-- on size; fillfactor 90 keeps the cursor update a HOT update.
--
-- SAFE: creates new empty tables and indexes only; takes no locks on existing
-- tables. Idempotent and safe to re-run.

CREATE TABLE IF NOT EXISTS person_publish_outbox (
    id                     BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    team_id                INTEGER NOT NULL,
    kind                   TEXT NOT NULL,
    -- create and attach rows
    person_uuid            UUID,
    distinct_id            VARCHAR(400),
    version                BIGINT,
    -- op pointers
    op_id                  UUID,
    cursor_person_id       BIGINT NOT NULL DEFAULT 0,
    cursor_mapping_ordinal BIGINT NOT NULL DEFAULT 0,
    created_at             TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
    attempts               INTEGER NOT NULL DEFAULT 0,
    last_error             TEXT,
    given_up_at            TIMESTAMP WITH TIME ZONE,
    CONSTRAINT person_publish_outbox_kind_check
        CHECK (kind IN ('create', 'attach', 'merge_op', 'delete_op')),
    -- The two shapes are exclusive: a mapping row with an op_id would hold
    -- back that op's GC for nothing.
    CONSTRAINT person_publish_outbox_shape_check CHECK (
        (kind IN ('create', 'attach')
            AND person_uuid IS NOT NULL AND distinct_id IS NOT NULL AND version IS NOT NULL
            AND op_id IS NULL)
        OR (kind IN ('merge_op', 'delete_op')
            AND op_id IS NOT NULL
            AND person_uuid IS NULL AND distinct_id IS NULL AND version IS NULL)
    )
) WITH (
    autovacuum_vacuum_scale_factor = 0,
    autovacuum_vacuum_threshold = 10000,
    autovacuum_analyze_scale_factor = 0,
    autovacuum_analyze_threshold = 10000,
    autovacuum_vacuum_cost_delay = 0,
    fillfactor = 90
);

CREATE INDEX IF NOT EXISTS person_publish_outbox_pending
    ON person_publish_outbox (id) WHERE given_up_at IS NULL;

-- One pointer per op: completion inserts it once, inside the step's
-- compare-and-swap transaction.
CREATE UNIQUE INDEX IF NOT EXISTS person_publish_outbox_op_id
    ON person_publish_outbox (op_id) WHERE op_id IS NOT NULL;

COMMENT ON TABLE person_publish_outbox IS
    'Distinct id rows and lifecycle op pointers not yet published to Kafka by personhog-publisher. Rows with given_up_at set need an operator.';

-- Validation set twin, used by an identity on the _tmp tables.
CREATE TABLE IF NOT EXISTS person_publish_outbox_tmp (
    id                     BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    team_id                INTEGER NOT NULL,
    kind                   TEXT NOT NULL,
    person_uuid            UUID,
    distinct_id            VARCHAR(400),
    version                BIGINT,
    op_id                  UUID,
    cursor_person_id       BIGINT NOT NULL DEFAULT 0,
    cursor_mapping_ordinal BIGINT NOT NULL DEFAULT 0,
    created_at             TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
    attempts               INTEGER NOT NULL DEFAULT 0,
    last_error             TEXT,
    given_up_at            TIMESTAMP WITH TIME ZONE,
    CONSTRAINT person_publish_outbox_tmp_kind_check
        CHECK (kind IN ('create', 'attach', 'merge_op', 'delete_op')),
    CONSTRAINT person_publish_outbox_tmp_shape_check CHECK (
        (kind IN ('create', 'attach')
            AND person_uuid IS NOT NULL AND distinct_id IS NOT NULL AND version IS NOT NULL
            AND op_id IS NULL)
        OR (kind IN ('merge_op', 'delete_op')
            AND op_id IS NOT NULL
            AND person_uuid IS NULL AND distinct_id IS NULL AND version IS NULL)
    )
) WITH (
    autovacuum_vacuum_scale_factor = 0,
    autovacuum_vacuum_threshold = 10000,
    autovacuum_analyze_scale_factor = 0,
    autovacuum_analyze_threshold = 10000,
    autovacuum_vacuum_cost_delay = 0,
    fillfactor = 90
);

CREATE INDEX IF NOT EXISTS person_publish_outbox_tmp_pending
    ON person_publish_outbox_tmp (id) WHERE given_up_at IS NULL;

CREATE UNIQUE INDEX IF NOT EXISTS person_publish_outbox_tmp_op_id
    ON person_publish_outbox_tmp (op_id) WHERE op_id IS NOT NULL;

COMMENT ON TABLE person_publish_outbox_tmp IS
    'Validation set twin of person_publish_outbox.';