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.

fix(persons): raise a dedup survivor's version above its victims (#87475)

Eli Reisman2a099567af65
diff --git a/posthog/management/commands/persons_dedup.py b/posthog/management/commands/persons_dedup.py
index d170774ffe99..3adea84dc366 100644
--- a/posthog/management/commands/persons_dedup.py
+++ b/posthog/management/commands/persons_dedup.py
@@ -74,6 +74,26 @@ ClickHouse stores only person.uuid, never the bigint, so a dedup needs no ClickH
That holds only because the survivor keeps the uuid: delete the last row for a uuid and
ClickHouse is left visible but unbacked. GATE_NO_SURVIVOR_SQL prevents that. Do not weaken it.
+THE SURVIVOR'S VERSION
+
+Keeping the uuid backed is not the same as keeping it current. The ClickHouse `person` table is
+ReplacingMergeTree(version) keyed on (team_id, uuid), and every read path resolves a person with
+argMax(..., version), so the highest version ever written under that uuid wins. Both members of
+a duplicate group wrote under the same key with independent counters, and the survivor rule
+ranks reachability above version, so the row we keep is routinely the lower-versioned one. Its
+later property updates then lose to a row we just deleted, and ClickHouse serves stale
+properties for as long as the gap takes to close -- which for a large gap is never.
+
+The raise lifts a lone survivor above the victims removed with it so its next update outranks
+them. It is not a ClickHouse write: nothing here produces to Kafka, so ClickHouse converges on
+the survivor's next ordinary property update rather than immediately. A group with more than one
+survivor is merge-required and is left alone, because giving both the same version makes their
+ClickHouse rows tie instead of resolve.
+
+It is on by default, because a run that omits it leaves the divergence in place silently.
+--no-raise-survivor-version opts out, which also drops the UPDATE and the wider lock this takes
+on rows live ingestion writes, and gives up the correction for that run.
+
Outside the persons DB the bigint survives in places this command cannot repair, all of which
fail safe. Drain these before a large run rather than reasoning about them mid-flight:
@@ -107,6 +127,7 @@ import os
import json
import time
import logging
+import argparse
from collections.abc import Callable, Iterator
from contextlib import contextmanager
from datetime import UTC, datetime
@@ -498,6 +519,27 @@ WHERE p.team_id = %(team)s
FOR UPDATE
"""
+# --raise-survivor-version only. Takes every lock the batch will need -- victims and the
+# survivors the raise updates -- in one ordered statement, before the gates and the backup.
+#
+# Without it the transaction locks victims, spends the gates and an fsync'd backup, and only
+# then reaches for the survivor. Ingestion's updatePersonsBatch matches on (team_id, uuid)
+# (nodejs postgres-person-repository.ts), so one of its statements needs both rows: if it takes
+# the survivor and blocks on our victim while we are still writing the backup, the two deadlock.
+# Postgres would abort one and the batch would retry, but it can bounce an ingestion write too.
+#
+# Collapsing both acquisitions into one statement leaves no window between them. ORDER BY id
+# makes our order deterministic; ingestion's order inside its own statement is the planner's, so
+# this narrows the race rather than removing it, and DeadlockDetected stays the backstop.
+LOCK_BATCH_GROUPS_SQL = f"""
+SELECT p.id FROM posthog_person p
+WHERE p.team_id = %(team)s
+ AND EXISTS (SELECT 1 FROM {VICTIMS_TABLE}_batch b
+ WHERE b.team_id = p.team_id AND b.uuid = p.uuid)
+ORDER BY p.id
+FOR UPDATE
+"""
+
FETCH_PERSONS_SQL = f"""
SELECT {", ".join("p." + c for c in PERSON_COLUMNS)}
FROM posthog_person p
@@ -549,6 +591,67 @@ USING {VICTIMS_TABLE}_batch b
WHERE p.team_id = b.team_id AND p.id = b.id AND p.team_id = %(team)s
"""
+# Headroom above the ceiling we can see. ClickHouse's ceiling for a uuid can exceed every
+# version currently in Postgres, because a member deleted before this command existed also
+# wrote under the same key. Matches the margin the delete paths already use for the same
+# reason (posthog/models/person/deletion.py and the nodejs person repository).
+SURVIVOR_VERSION_MARGIN = 100
+
+# The batch's duplicate groups, the highest version among the victims we are about to remove,
+# and each group's survivors. Shared by the dry-run count and the update so both describe the
+# same rows. Victims are locked by the time this runs, so their versions cannot move under it.
+_SURVIVOR_VERSION_CTE = f"""
+WITH victim_ceiling AS (
+ SELECT b.team_id, b.uuid, max(COALESCE(p.version, 0)) AS ceiling
+ FROM {VICTIMS_TABLE}_batch b
+ JOIN posthog_person p ON p.team_id = b.team_id AND p.id = b.id
+ GROUP BY b.team_id, b.uuid
+),
+survivor AS (
+ SELECT c.ceiling, s.id AS survivor_id, s.team_id, COALESCE(s.version, 0) AS survivor_version,
+ count(*) OVER (PARTITION BY c.team_id, c.uuid) AS survivors_in_group
+ FROM victim_ceiling c
+ JOIN posthog_person s ON s.team_id = c.team_id AND s.uuid = c.uuid
+ WHERE NOT EXISTS (SELECT 1 FROM {VICTIMS_TABLE}_batch b2
+ WHERE b2.team_id = s.team_id AND b2.id = s.id)
+)
+"""
+
+# Only a lone survivor can be raised: a group with two survivors is merge-required, and giving
+# both the same version would make their ClickHouse rows tie rather than resolve. Those go to
+# the merge pass untouched.
+_LONE_SURVIVOR_PREDICATE = """
+ AND v.survivors_in_group = 1
+"""
+
+# The version guard reads p.version, the live row, not the CTE's survivor_version.
+#
+# LOCK_BATCH_GROUPS_SQL already holds FOR UPDATE on this row, so nothing can move it under us
+# and the two are equal in practice. The distinction still matters: read committed re-checks an
+# UPDATE's qual against the latest tuple after taking the row lock, and that re-check sees a new
+# p.version but not a new CTE, which is evaluated once. Guarding on the CTE would make this
+# statement correct only because of a lock three statements earlier, and silently wrong if
+# anyone reordered the transaction. Guarding on p.version makes it safe on its own, and a
+# survivor a writer pushed above the ceiling is skipped rather than lowered.
+RAISE_SURVIVOR_VERSION_SQL = f"""
+{_SURVIVOR_VERSION_CTE}
+UPDATE posthog_person p
+SET version = v.ceiling + {SURVIVOR_VERSION_MARGIN}
+FROM survivor v
+WHERE p.team_id = %(team)s AND p.id = v.survivor_id
+{_LONE_SURVIVOR_PREDICATE}
+ AND COALESCE(p.version, 0) <= v.ceiling
+"""
+
+# The dry-run count has no row to re-check, so it reads the same snapshot the CTE took.
+COUNT_SURVIVOR_VERSION_SQL = f"""
+{_SURVIVOR_VERSION_CTE}
+SELECT count(*) FROM survivor v
+WHERE v.team_id = %(team)s
+{_LONE_SURVIVOR_PREDICATE}
+ AND v.survivor_version <= v.ceiling
+"""
+
VERIFY_ORPHANS_SQL = """
SELECT count(*) FROM posthog_persondistinctid pdi
WHERE pdi.team_id = %(team)s
@@ -573,6 +676,11 @@ WRITE_PRIVILEGES = (
("posthog_person", "DELETE"),
("posthog_cohortpeople", "DELETE"),
)
+# Only --raise-survivor-version writes to a person row, so the grant is only required then.
+SURVIVOR_VERSION_PRIVILEGES = (
+ *WRITE_PRIVILEGES,
+ ("posthog_person", "UPDATE"),
+)
# Aurora cancels a long read-only query on a reader with 40001, or drops the session with
# 57P01, when the reader has to catch up. statement_timeout = 0 does not prevent either.
@@ -786,6 +894,18 @@ class Command(BaseCommand):
default=100,
help="give up after this many batches lost to lock contention",
)
+ parser.add_argument(
+ "--raise-survivor-version",
+ action=argparse.BooleanOptionalAction,
+ default=True,
+ help=(
+ "raise a lone survivor's version above the versions of the victims removed with "
+ "it, so its later property updates outrank their ClickHouse rows instead of "
+ "being ignored. On by default: leaving it off silently leaves the survivor's "
+ "ClickHouse row stale. --no-raise-survivor-version opts out, which also drops "
+ "the UPDATE and the wider lock on rows live ingestion writes"
+ ),
+ )
parser.add_argument(
"--resume-from",
help="skip staging and load the victims still to delete from a checkpoint file",
@@ -906,7 +1026,14 @@ class Command(BaseCommand):
backend_pid = _scalar(conn, "SELECT pg_backend_pid()")
on_replica = _is_replica(conn)
- missing = _check_privileges(conn, READ_PRIVILEGES if mode in READ_MODES else WRITE_PRIVILEGES)
+ required_privileges: tuple[tuple[str, str], ...]
+ if mode in READ_MODES:
+ required_privileges = READ_PRIVILEGES
+ elif options["raise_survivor_version"]:
+ required_privileges = SURVIVOR_VERSION_PRIVILEGES
+ else:
+ required_privileges = WRITE_PRIVILEGES
+ missing = _check_privileges(conn, required_privileges)
if missing:
raise CommandError(
"operator role lacks required grants: " + ", ".join(missing) + ". "
@@ -1144,6 +1271,7 @@ class Command(BaseCommand):
batches = 0
pruned_total = 0
lock_retries = 0
+ raised = 0
# A live-leak team can make a staged victim reachable mid-run, which is expected and
# is handled per batch below. A large share going that way is not expected and means
# the staging query and the gate disagree, so stop rather than grind through the set.
@@ -1199,10 +1327,13 @@ class Command(BaseCommand):
# still reports success, which on the large teams is a fraction of a percent.
checked += in_batch
with conn.cursor() as cur:
+ if options["raise_survivor_version"]:
+ raised += _scalar(conn, COUNT_SURVIVOR_VERSION_SQL, {"team": team})
cur.execute(RETIRE_BATCH_SQL)
continue
batch_started = time.monotonic()
+ raised_in_batch = 0
try:
with conn.cursor() as cur:
cur.execute("BEGIN")
@@ -1217,6 +1348,12 @@ class Command(BaseCommand):
"SELECT set_config('statement_timeout', %s, true)",
(f"{options['txn_statement_timeout_ms']}ms",),
)
+ # Takes the survivors' locks too, so the raise later in this transaction
+ # adds no second acquisition point. Runs first, and LOCK_VICTIMS_SQL below
+ # then re-locks rows already held, which costs nothing and keeps its
+ # rowcount check meaning exactly what it did before.
+ if options["raise_survivor_version"]:
+ cur.execute(LOCK_BATCH_GROUPS_SQL, {"team": team})
cur.execute(LOCK_VICTIMS_SQL, {"team": team})
locked = cur.rowcount
# Re-check inside the transaction: these teams still receive writes, and a
@@ -1266,6 +1403,11 @@ class Command(BaseCommand):
cur.execute("ROLLBACK")
raise CommandError(f"backed up {backed_up} row(s) but staged {in_batch}; rolled back")
+ # Before the delete, while the victims' versions are still readable, and
+ # inside it, so a rollback takes the raise with it.
+ if options["raise_survivor_version"]:
+ cur.execute(RAISE_SURVIVOR_VERSION_SQL, {"team": team})
+ raised_in_batch = cur.rowcount
cur.execute(DELETE_COHORT_SQL)
cur.execute(DELETE_PERSONS_SQL, {"team": team})
removed = cur.rowcount
@@ -1320,6 +1462,9 @@ class Command(BaseCommand):
with conn.cursor() as cur:
cur.execute(RETIRE_BATCH_SQL)
deleted += removed
+ # Only after the commit: a batch lost to lock contention is retried, and counting
+ # the rolled-back raise here would double it.
+ raised += raised_in_batch
if options["checkpoint_every"] and batches % options["checkpoint_every"] == 0:
self._write_checkpoint(conn, checkpoint_path, team=team, staged=staged - deleted)
if options["sleep_ms"] > 0:
@@ -1330,12 +1475,19 @@ class Command(BaseCommand):
batch=batches,
deleted=removed,
total=deleted,
+ survivors_raised=raised_in_batch if options["raise_survivor_version"] else None,
elapsed_s=round(time.monotonic() - batch_started, 2),
)
if not apply_changes:
logger.info(
- "persons_dedup.dry_run_ok", team_id=team, mode=mode, batches=batches, checked=checked, staged=staged
+ "persons_dedup.dry_run_ok",
+ team_id=team,
+ mode=mode,
+ batches=batches,
+ checked=checked,
+ staged=staged,
+ survivors_to_raise=raised if options["raise_survivor_version"] else None,
)
return
@@ -1347,6 +1499,7 @@ class Command(BaseCommand):
deleted=deleted,
pruned=pruned_total,
lock_retries=lock_retries,
+ survivors_raised=raised if options["raise_survivor_version"] else None,
backup=str(backup_path),
)
diff --git a/posthog/management/commands/test/test_persons_dedup.py b/posthog/management/commands/test/test_persons_dedup.py
index 499e423ea3d7..e1b589ec25d6 100644
--- a/posthog/management/commands/test/test_persons_dedup.py
+++ b/posthog/management/commands/test/test_persons_dedup.py
@@ -733,6 +733,190 @@ class TestPersonsDedupSurvivorSelection:
assert _persons(persons_conn) == 2
+def _version(conn: psycopg.Connection, person_id: int) -> int:
+ return _count(conn, "SELECT version FROM posthog_person WHERE id = %s", (person_id,))
+
+
+class TestPersonsDedupSurvivorVersionFloor:
+ # ClickHouse keys person rows on (team_id, uuid) and resolves them with argMax(..., version),
+ # so both members of a duplicate group compete under one key. The survivor rule ranks
+ # reachability above version, so the row we keep is routinely the lower-versioned one and its
+ # later updates lose to the row we just deleted. These cover the raise that prevents that.
+
+ @pytest.mark.parametrize(
+ "victim_version,survivor_version,raised",
+ [(1500, 3, True), (5, 5, True), (5, 88, False)],
+ ids=["survivor below the ceiling", "survivor level with it", "survivor above it"],
+ )
+ def test_a_survivor_is_raised_only_when_a_victim_outranks_it(
+ self, persons_conn, tmp_path, victim_version, survivor_version, raised
+ ):
+ # Level with the ceiling still has to be raised: equal versions tie in ClickHouse and
+ # ReplacingMergeTree picks between them arbitrarily, so the survivor has to end up
+ # strictly above. Tightening the guard to a strict "<" would leave that tie standing.
+ # Above the ceiling is left alone rather than inflated for no gain.
+ uuid = _uuid(200 if raised else 202)
+ orphan = _add_person(persons_conn, uuid, version=victim_version)
+ survivor = _add_person(persons_conn, uuid, version=survivor_version)
+ _add_distinct_id(persons_conn, survivor, f"did-{200 if raised else 202}")
+
+ _run("delete-unreferenced", tmp_path, apply=True, raise_survivor_version=True)
+
+ assert _persons(persons_conn) == 1
+ assert _count(persons_conn, "SELECT count(*) FROM posthog_person WHERE id = %s", (orphan,)) == 0
+ expected = victim_version + persons_dedup_command.SURVIVOR_VERSION_MARGIN if raised else survivor_version
+ assert _version(persons_conn, survivor) == expected
+
+ def test_the_raise_clears_every_victim_in_a_group_not_just_one(self, persons_conn, tmp_path):
+ # The ceiling is a max over the group's victims. Taking any single victim's version
+ # would leave the survivor below a sibling that also wrote to the same ClickHouse key.
+ uuid = _uuid(201)
+ _add_person(persons_conn, uuid, version=40)
+ _add_person(persons_conn, uuid, version=900)
+ _add_person(persons_conn, uuid, version=7)
+ survivor = _add_person(persons_conn, uuid, version=0)
+ _add_distinct_id(persons_conn, survivor, "did-201")
+
+ _run("delete-unreferenced", tmp_path, apply=True, raise_survivor_version=True)
+
+ assert _persons(persons_conn) == 1
+ assert _version(persons_conn, survivor) == 900 + persons_dedup_command.SURVIVOR_VERSION_MARGIN
+
+ def test_a_survivor_already_above_its_victims_is_left_alone(self, persons_conn, tmp_path):
+ # Raising unconditionally would inflate the counter of every survivor that never had
+ # the problem, for no gain.
+ uuid = _uuid(202)
+ _add_person(persons_conn, uuid, version=5)
+ survivor = _add_person(persons_conn, uuid, version=88)
+ _add_distinct_id(persons_conn, survivor, "did-202")
+
+ _run("delete-unreferenced", tmp_path, apply=True, raise_survivor_version=True)
+
+ assert _version(persons_conn, survivor) == 88
+
+ def test_a_merge_required_group_keeps_both_versions_untouched(self, persons_conn, tmp_path):
+ # Two reachable rows plus an unreachable third. Repair may remove the third, but giving
+ # both survivors the same version would make their ClickHouse rows tie rather than
+ # resolve, so the group is left for the merge pass.
+ uuid = _uuid(203)
+ unreachable = _add_person(persons_conn, uuid, version=770)
+ a = _add_person(persons_conn, uuid, version=2)
+ b = _add_person(persons_conn, uuid, version=4)
+ _add_distinct_id(persons_conn, a, "did-203a")
+ _add_distinct_id(persons_conn, b, "did-203b")
+
+ _run("repair", tmp_path, apply=True, raise_survivor_version=True)
+
+ assert _count(persons_conn, "SELECT count(*) FROM posthog_person WHERE id = %s", (unreachable,)) == 0
+ assert _version(persons_conn, a) == 2
+ assert _version(persons_conn, b) == 4
+
+ @pytest.mark.parametrize(
+ "kwargs,raised",
+ [({}, True), ({"raise_survivor_version": False}, False)],
+ ids=["on by default", "--no-raise-survivor-version opts out"],
+ )
+ def test_the_raise_is_on_unless_a_run_opts_out(self, persons_conn, tmp_path, kwargs, raised):
+ # A run that has to remember a flag to stay correct will eventually forget it, and the
+ # cost of forgetting is a survivor whose ClickHouse row never updates again. The opt-out
+ # still has to work, because it is the rollback if the wider lock ever contends.
+ uuid = _uuid(204)
+ _add_person(persons_conn, uuid, version=600)
+ survivor = _add_person(persons_conn, uuid, version=1)
+ _add_distinct_id(persons_conn, survivor, "did-204")
+
+ _run("delete-unreferenced", tmp_path, apply=True, **kwargs)
+
+ assert _persons(persons_conn) == 1
+ expected = 600 + persons_dedup_command.SURVIVOR_VERSION_MARGIN if raised else 1
+ assert _version(persons_conn, survivor) == expected
+
+ def test_a_dry_run_counts_the_raises_without_making_them(self, persons_conn, tmp_path):
+ # The flag has to be measurable before it writes to a row live ingestion also writes.
+ uuid = _uuid(205)
+ _add_person(persons_conn, uuid, version=500)
+ survivor = _add_person(persons_conn, uuid, version=0)
+ _add_distinct_id(persons_conn, survivor, "did-205")
+
+ with capture_logs() as logs:
+ _run("delete-unreferenced", tmp_path, raise_survivor_version=True)
+
+ assert _persons(persons_conn) == 2, "a dry run deletes nothing"
+ assert _version(persons_conn, survivor) == 0, "a dry run raises nothing"
+ dry = [log for log in logs if log["event"] == "persons_dedup.dry_run_ok"]
+ assert dry and dry[0]["survivors_to_raise"] == 1
+
+ def test_the_raise_is_rolled_back_with_the_delete(self, persons_conn, tmp_path, monkeypatch):
+ # The raise sits inside the delete transaction. If a later statement aborts it, a
+ # survivor left carrying a raised version would claim a ClickHouse ceiling for rows
+ # that were never removed.
+ uuid = _uuid(206)
+ _add_person(persons_conn, uuid, version=300)
+ survivor = _add_person(persons_conn, uuid, version=0)
+ _add_distinct_id(persons_conn, survivor, "did-206")
+
+ def _explode(*args: Any, **kwargs: Any) -> None:
+ raise RuntimeError("boom")
+
+ monkeypatch.setattr(persons_dedup_command.Command, "_backup", _explode)
+ with pytest.raises(RuntimeError):
+ _run("delete-unreferenced", tmp_path, apply=True, raise_survivor_version=True)
+
+ assert _persons(persons_conn) == 2
+ assert _version(persons_conn, survivor) == 0
+
+ @pytest.mark.parametrize(
+ "raise_flag,expect_locked",
+ [(True, True), (False, False)],
+ ids=["flag on locks the survivor", "flag off leaves it alone"],
+ )
+ def test_the_survivor_lock_is_taken_up_front_and_only_when_asked(
+ self, persons_conn, tmp_path, monkeypatch, raise_flag, expect_locked
+ ):
+ # The raise runs after the gates and an fsync'd backup. Taking the survivor's lock there
+ # rather than up front reopens a deadlock: ingestion's updatePersonsBatch matches on
+ # (team_id, uuid), so one statement needs both rows, and it can hold the survivor while
+ # blocking on our victim. Probing during the backup proves the lock is already held.
+ # The off case guards the other half -- the wider lock must not be paid by runs that did
+ # not ask for it.
+ uuid = _uuid(207 if raise_flag else 208)
+ _add_person(persons_conn, uuid, version=400)
+ survivor = _add_person(persons_conn, uuid, version=0)
+ _add_distinct_id(persons_conn, survivor, f"did-{207 if raise_flag else 208}")
+
+ observed: dict[str, Any] = {}
+ real_backup = persons_dedup_command.Command._backup
+
+ def backup_then_probe(self, conn, team, path):
+ result = real_backup(self, conn, team, path)
+ with persons_db_connection(writer=True, autocommit=True) as other:
+ with other.cursor() as cur:
+ # Session-level, not SET LOCAL: this connection is in autocommit, where
+ # LOCAL is discarded with the implicit transaction and the probe would
+ # wait forever instead of failing fast.
+ cur.execute("SET lock_timeout = '250ms'")
+ try:
+ cur.execute("SELECT id FROM posthog_person WHERE id = %s FOR UPDATE", (survivor,))
+ observed["locked"] = False
+ except psycopg.errors.LockNotAvailable:
+ observed["locked"] = True
+ return result
+
+ monkeypatch.setattr(persons_dedup_command.Command, "_backup", backup_then_probe)
+ _run("delete-unreferenced", tmp_path, apply=True, raise_survivor_version=raise_flag)
+
+ assert observed["locked"] is expect_locked
+ expected_version = 400 + persons_dedup_command.SURVIVOR_VERSION_MARGIN if raise_flag else 0
+ assert _version(persons_conn, survivor) == expected_version
+
+ def test_the_update_grant_is_only_required_when_the_flag_is_set(self):
+ # The command runs today without UPDATE on posthog_person. Demanding it unconditionally
+ # would abort every run whose role was granted exactly what the old modes needed.
+ assert ("posthog_person", "UPDATE") not in persons_dedup_command.WRITE_PRIVILEGES
+ assert ("posthog_person", "UPDATE") in persons_dedup_command.SURVIVOR_VERSION_PRIVILEGES
+ assert set(persons_dedup_command.WRITE_PRIVILEGES) < set(persons_dedup_command.SURVIVOR_VERSION_PRIVILEGES)
+
+
class TestPersonsDedupVerify:
def test_verify_fails_while_resolvable_duplicates_remain(self, persons_conn, tmp_path):
# An orphan repair would have taken: work is genuinely outstanding.