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.

2.2 KB
Raw Blame
-- Shadow tables for running personhog-identity against the validation table
-- set (see PERSON_TABLE in the identity config). The person table is shadowed
-- by personhog_person_tmp; identity's sync plane writes the distinct id
-- mapping in the same transaction, and its ids come from the tmp person
-- sequence, so the mapping (and the hash-key-override rows the delete saga
-- clears by person_id) must live in the same namespace — the real tables'
-- ids belong to posthog_person, and posthog_persondistinctid's FK rejects
-- tmp-namespace ids outright.
--
-- This migration is idempotent and safe to re-run.

CREATE TABLE IF NOT EXISTS personhog_persondistinctid_tmp (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY,
    distinct_id VARCHAR(400) NOT NULL,
    version BIGINT,
    person_id BIGINT NOT NULL,
    team_id INTEGER NOT NULL,
    is_deleted BOOLEAN NOT NULL DEFAULT false,
    CONSTRAINT personhog_persondistinctid_tmp_pkey PRIMARY KEY (id),
    -- Mirrors posthog_persondistinctid's FK so a dangling person_id fails on
    -- the validation set the same way it would on the real pair.
    CONSTRAINT personhog_persondistinctid_tmp_person_id_fkey
        FOREIGN KEY (team_id, person_id) REFERENCES personhog_person_tmp (team_id, id)
);

-- The stub-create ON CONFLICT (team_id, distinct_id) infers this index.
CREATE UNIQUE INDEX IF NOT EXISTS personhog_persondistinctid_tmp_team_distinct_idx
    ON personhog_persondistinctid_tmp (team_id, distinct_id);

CREATE INDEX IF NOT EXISTS personhog_persondistinctid_tmp_person_id_idx
    ON personhog_persondistinctid_tmp (person_id);

CREATE TABLE IF NOT EXISTS personhog_featureflaghashkeyoverride_tmp (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY,
    feature_flag_key VARCHAR(400) NOT NULL,
    hash_key VARCHAR(400) NOT NULL,
    person_id BIGINT NOT NULL,
    team_id INTEGER NOT NULL,
    CONSTRAINT personhog_featureflaghashkeyoverride_tmp_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX IF NOT EXISTS personhog_featureflaghashkeyoverride_tmp_unique_idx
    ON personhog_featureflaghashkeyoverride_tmp (team_id, person_id, feature_flag_key);

CREATE INDEX IF NOT EXISTS personhog_featureflaghashkeyoverride_tmp_person_id_idx
    ON personhog_featureflaghashkeyoverride_tmp (person_id);