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.

9.8 KB
Raw Blame
-- Initial schema for persons database
-- Tables: person, persondistinctid, personlessdistinctid, personoverridemapping, 
--         personoverride, pendingpersonoverride, flatpersonoverride,
--         featureflaghashkeyoverride, cohortpeople, groups, grouptypemapping

-- Create sequence for person id
CREATE SEQUENCE IF NOT EXISTS posthog_person_id_seq;

-- Person table
CREATE TABLE IF NOT EXISTS posthog_person (
    id BIGINT NOT NULL DEFAULT nextval('posthog_person_id_seq') PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    properties_last_updated_at JSONB,
    properties_last_operation JSONB,
    properties JSONB NOT NULL DEFAULT '{}'::jsonb,
    is_identified BOOLEAN NOT NULL DEFAULT false,
    uuid UUID NOT NULL,
    version BIGINT,
    is_user_id INTEGER,
    team_id INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS posthog_per_team_id_bec4e5_idx ON posthog_person (team_id, id DESC);
CREATE INDEX IF NOT EXISTS posthog_person_team_id_325c1b73 ON posthog_person (team_id);
CREATE INDEX IF NOT EXISTS posthog_person_uuid_82b4a3ed ON posthog_person (uuid);

-- Idempotnently add `check constraint for properties size
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint 
        WHERE conname = 'check_properties_size' 
        AND conrelid = 'posthog_person'::regclass
    ) THEN
        ALTER TABLE posthog_person ADD CONSTRAINT check_properties_size 
            CHECK (pg_column_size(properties) <= 655360) NOT VALID;
    END IF;
END $$;

-- PersonDistinctId table
CREATE TABLE IF NOT EXISTS posthog_persondistinctid (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    distinct_id VARCHAR(400) NOT NULL,
    version BIGINT,
    person_id BIGINT NOT NULL REFERENCES posthog_person(id),
    team_id INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS posthog_persondistinctid_person_id_5d655bba ON posthog_persondistinctid (person_id);
CREATE UNIQUE INDEX IF NOT EXISTS unique_distinct_id_for_team ON posthog_persondistinctid (team_id, distinct_id);

-- PersonlessDistinctId table
CREATE TABLE IF NOT EXISTS posthog_personlessdistinctid (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    distinct_id VARCHAR(400) NOT NULL,
    is_merged BOOLEAN NOT NULL DEFAULT false,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    team_id INTEGER NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS unique_personless_distinct_id_for_team ON posthog_personlessdistinctid (team_id, distinct_id);

-- PersonOverrideMapping table
CREATE TABLE IF NOT EXISTS posthog_personoverridemapping (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    uuid UUID NOT NULL,
    team_id BIGINT NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS unique_uuid ON posthog_personoverridemapping (team_id, uuid);

-- PersonOverride table
CREATE TABLE IF NOT EXISTS posthog_personoverride (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    oldest_event TIMESTAMP WITH TIME ZONE NOT NULL,
    version BIGINT,
    team_id BIGINT NOT NULL,
    old_person_id INTEGER NOT NULL REFERENCES posthog_personoverridemapping(id),
    override_person_id INTEGER NOT NULL REFERENCES posthog_personoverridemapping(id)
);

CREATE INDEX IF NOT EXISTS posthog_personoverride_old_person_id_4c1deac0 ON posthog_personoverride (old_person_id);
CREATE INDEX IF NOT EXISTS posthog_personoverride_override_person_id_9f32aab1 ON posthog_personoverride (override_person_id);
CREATE INDEX IF NOT EXISTS posthog_personoverride_team_id_92291e67 ON posthog_personoverride (team_id);
CREATE UNIQUE INDEX IF NOT EXISTS unique_override_per_old_person_id ON posthog_personoverride (team_id, old_person_id);

-- Idempotently add check constraint
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint 
        WHERE conname = 'old_person_id_different_from_override_person_id' 
        AND conrelid = 'posthog_personoverride'::regclass
    ) THEN
        ALTER TABLE posthog_personoverride ADD CONSTRAINT old_person_id_different_from_override_person_id 
            CHECK (old_person_id != override_person_id);
    END IF;
END $$;

CREATE EXTENSION IF NOT EXISTS btree_gist;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint 
        WHERE conname = 'exclude_override_person_id_from_being_old_person_id' 
        AND conrelid = 'posthog_personoverride'::regclass
    ) THEN
        ALTER TABLE posthog_personoverride ADD CONSTRAINT exclude_override_person_id_from_being_old_person_id 
            EXCLUDE USING gist (team_id WITH =, override_person_id WITH =, old_person_id WITH <>) 
            DEFERRABLE INITIALLY DEFERRED;
    END IF;
END $$;

-- PendingPersonOverride table
CREATE TABLE IF NOT EXISTS posthog_pendingpersonoverride (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    team_id BIGINT NOT NULL,
    old_person_id UUID NOT NULL,
    override_person_id UUID NOT NULL,
    oldest_event TIMESTAMP WITH TIME ZONE NOT NULL
);

-- FlatPersonOverride table
CREATE TABLE IF NOT EXISTS posthog_flatpersonoverride (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    team_id BIGINT NOT NULL,
    old_person_id UUID NOT NULL,
    override_person_id UUID NOT NULL,
    oldest_event TIMESTAMP WITH TIME ZONE NOT NULL,
    version BIGINT
);

CREATE UNIQUE INDEX IF NOT EXISTS flatpersonoverride_unique_old_person_by_team ON posthog_flatpersonoverride (team_id, old_person_id);
CREATE INDEX IF NOT EXISTS posthog_fla_team_id_224253_idx ON posthog_flatpersonoverride (team_id, override_person_id);

-- Idempotently add check constraint
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint 
        WHERE conname = 'flatpersonoverride_check_circular_reference' 
        AND conrelid = 'posthog_flatpersonoverride'::regclass
    ) THEN
        ALTER TABLE posthog_flatpersonoverride ADD CONSTRAINT flatpersonoverride_check_circular_reference 
            CHECK (old_person_id != override_person_id);
    END IF;
END $$;

-- FeatureFlagHashKeyOverride table
CREATE TABLE IF NOT EXISTS posthog_featureflaghashkeyoverride (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    feature_flag_key VARCHAR(400) NOT NULL,
    hash_key VARCHAR(400) NOT NULL,
    person_id BIGINT NOT NULL REFERENCES posthog_person(id),
    team_id INTEGER NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS unique_hash_key_for_user_team_feature_flag ON posthog_featureflaghashkeyoverride (team_id, person_id, feature_flag_key);
CREATE INDEX IF NOT EXISTS posthog_featureflaghashkeyoverride_person_id_7e517f7c ON posthog_featureflaghashkeyoverride (person_id);
CREATE INDEX IF NOT EXISTS posthog_featureflaghashkeyoverride_team_id_b626eed2 ON posthog_featureflaghashkeyoverride (team_id);

-- CohortPeople table
CREATE TABLE IF NOT EXISTS posthog_cohortpeople (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    version INTEGER,
    cohort_id INTEGER NOT NULL,
    person_id BIGINT NOT NULL REFERENCES posthog_person(id)
);

CREATE INDEX IF NOT EXISTS posthog_coh_cohort__89c25f_idx ON posthog_cohortpeople (cohort_id, person_id);
CREATE INDEX IF NOT EXISTS posthog_cohortpeople_cohort_id_1f371733 ON posthog_cohortpeople (cohort_id);
CREATE INDEX IF NOT EXISTS posthog_cohortpeople_person_id_33da7d3f ON posthog_cohortpeople (person_id);

-- Group table
CREATE TABLE IF NOT EXISTS posthog_group (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    group_key VARCHAR(400) NOT NULL,
    group_type_index INTEGER NOT NULL,
    group_properties JSONB NOT NULL DEFAULT '{}'::jsonb,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    properties_last_updated_at JSONB NOT NULL DEFAULT '{}'::jsonb,
    properties_last_operation JSONB NOT NULL DEFAULT '{}'::jsonb,
    version BIGINT NOT NULL,
    team_id INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS posthog_group_team_id_b3aed896 ON posthog_group (team_id);
CREATE UNIQUE INDEX IF NOT EXISTS unique_team_group_key_group_type ON posthog_group (team_id, group_key, group_type_index);

-- GroupTypeMapping table
CREATE TABLE IF NOT EXISTS posthog_grouptypemapping (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    group_type VARCHAR(400) NOT NULL,
    group_type_index INTEGER NOT NULL,
    name_singular VARCHAR(400),
    name_plural VARCHAR(400),
    team_id INTEGER NOT NULL,
    project_id BIGINT,
    detail_dashboard_id INTEGER,
    default_columns TEXT[],
    created_at TIMESTAMP WITH TIME ZONE
);

CREATE INDEX IF NOT EXISTS posthog_group_type_i_proj_idx ON posthog_grouptypemapping (project_id, group_type_index);
CREATE INDEX IF NOT EXISTS posthog_group_type_proj_idx ON posthog_grouptypemapping (project_id, group_type);
CREATE INDEX IF NOT EXISTS posthog_grouptypemapping_detail_dashboard_id_54b0edbb ON posthog_grouptypemapping (detail_dashboard_id);
CREATE INDEX IF NOT EXISTS posthog_grouptypemapping_project_id_239c0515 ON posthog_grouptypemapping (project_id);
CREATE INDEX IF NOT EXISTS posthog_grouptypemapping_team_id_5fb54d04 ON posthog_grouptypemapping (team_id);
CREATE UNIQUE INDEX IF NOT EXISTS unique_group_type_index_for_project ON posthog_grouptypemapping (project_id, group_type_index);
CREATE UNIQUE INDEX IF NOT EXISTS unique_group_types_for_project ON posthog_grouptypemapping (project_id, group_type);

-- Idemptotently add other constraints found on the default table
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint 
        WHERE conname = 'group_type_index_is_less_than_or_equal_5' 
        AND conrelid = 'posthog_grouptypemapping'::regclass
    ) THEN
        ALTER TABLE posthog_grouptypemapping ADD CONSTRAINT group_type_index_is_less_than_or_equal_5 
            CHECK (group_type_index <= 5);
    END IF;
    
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint 
        WHERE conname = 'group_type_project_id_is_not_null' 
        AND conrelid = 'posthog_grouptypemapping'::regclass
    ) THEN
        ALTER TABLE posthog_grouptypemapping ADD CONSTRAINT group_type_project_id_is_not_null 
            CHECK (project_id IS NOT NULL);
    END IF;
END $$;