: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(ingestion): prune person partitions in batched person queries (#112106)
Zach Brown0c794864973e
diff --git a/nodejs/src/common/persons/repositories/postgres-person-repository.test.ts b/nodejs/src/common/persons/repositories/postgres-person-repository.test.tsindex 6ac5332ce190..850db8d850d0 100644--- a/nodejs/src/common/persons/repositories/postgres-person-repository.test.ts+++ b/nodejs/src/common/persons/repositories/postgres-person-repository.test.ts@@ -341,6 +341,48 @@ describe('PostgresPersonRepository', () => {expect(Number(rows.rows[0].version)).toBe(deathVersion)})+ it.each([+ [+ 'fetchPersonsByDistinctIds',+ (_person: InternalPerson) =>+ repository.fetchPersonsByDistinctIds([{ teamId: team.id, distinctId: 'pruning-person' }]),+ ],+ [+ 'fetchPersonsByPersonIds',+ (person: InternalPerson) =>+ repository.fetchPersonsByPersonIds([{ teamId: team.id, personId: person.uuid }]),+ ],+ [+ 'updatePersonsBatch',+ (person: InternalPerson) =>+ repository.updatePersonsBatch([buildPersonUpdate(person, 'pruning-person', person.version)]),+ ],+ ])('%s plans against only its team partition', async (tag, run) => {+ const person = await createTestPerson(team.id, 'pruning-person')+ const query = jest.spyOn(postgres, 'query')+ await run(person)+ const [, sql, values] = query.mock.calls.find(([, , , callTag]) => callTag === tag)!++ // The planner locks every person partition it cannot prune, so the lock count shows the pruning.+ const lockedPartitions = await postgres.transaction(+ PostgresUse.PERSONS_WRITE,+ 'pruningProbe',+ async (tx) => {+ await postgres.query(tx, `EXPLAIN ${sql}`, values, 'pruningProbe')+ const { rows } = await postgres.query<{ partitions: number }>(+ tx,+ `SELECT count(DISTINCT l.relation)::int AS partitions FROM pg_locks l+ JOIN pg_inherits i ON i.inhrelid = l.relation+ WHERE l.pid = pg_backend_pid() AND i.inhparent = 'posthog_person'::regclass`,+ [],+ 'pruningProbe'+ )+ return rows[0].partitions+ }+ )+ expect(lockedPartitions).toBe(1)+ })+it.each([['moveDistinctIds',diff --git a/nodejs/src/common/persons/repositories/postgres-person-repository.ts b/nodejs/src/common/persons/repositories/postgres-person-repository.tsindex c37a29067606..54f6d3275ab4 100644--- a/nodejs/src/common/persons/repositories/postgres-person-repository.ts+++ b/nodejs/src/common/persons/repositories/postgres-person-repository.ts@@ -405,7 +405,9 @@ export class PostgresPersonRepositoryAND posthog_persondistinctid.distinct_id = batch.distinct_idWHEREposthog_persondistinctid.is_deleted = false- AND posthog_person.is_deleted = false`+ AND posthog_person.is_deleted = false+ -- Lets the planner prune person partitions; the join alone locks all of them.+ AND posthog_person.team_id = ANY($1::integer[])`const { rows } = await this.postgres.query<RawPerson & { distinct_id: string }>(useReadReplica ? PostgresUse.PERSONS_READ : PostgresUse.PERSONS_WRITE,@@ -514,7 +516,9 @@ export class PostgresPersonRepositoryposthog_person.last_seen_atFROM posthog_personWHERE (posthog_person.team_id, posthog_person.uuid) IN (SELECT * FROM UNNEST($1::integer[], $2::uuid[]))- AND posthog_person.is_deleted = false`+ AND posthog_person.is_deleted = false+ -- Lets the planner prune person partitions; the IN list alone locks all of them.+ AND posthog_person.team_id = ANY($1::integer[])`const { rows } = await this.postgres.query<RawPerson>(useReadReplica ? PostgresUse.PERSONS_READ : PostgresUse.PERSONS_WRITE,@@ -2150,6 +2154,7 @@ export class PostgresPersonRepositorytry {// Use UNNEST to pass arrays, keeping query structure constant for prepared statement reuse// Note: batch column names are prefixed with 'new_' to avoid any potential confusion with table columns+ // The team_id = ANY filter lets the planner prune partitions; the join alone locks all of them.const { rows } = await this.postgres.query<RawPerson>(PostgresUse.PERSONS_WRITE,`@@ -2172,6 +2177,7 @@ export class PostgresPersonRepository$8::text[]) AS batch(batch_uuid, batch_team_id, new_properties, new_properties_last_updated_at, new_properties_last_operation, new_is_identified, new_created_at, new_last_seen_at)WHERE p.uuid = batch.batch_uuid AND p.team_id = batch.batch_team_id AND p.is_deleted = false+ AND p.team_id = ANY($2::integer[])RETURNING ${PERSON_COLUMNS_PREFIXED}`,[