Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Batch person deletes #26873

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions posthog/management/commands/delete_persons.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@


class Command(BaseCommand):
help = "Fix state for person distinct IDs in ClickHouse after person deletion and id re-use for a single team"
help = "Delete a batch of persons from postgres"

def add_arguments(self, parser):
parser.add_argument("--team-id", default=None, type=int, help="Specify a team to fix data for.")
parser.add_argument("--team-id", default=None, type=int, help="Specify a team to delete persons from.")
parser.add_argument(
"--person-ids", default=None, type=str, help="Specify a list of comma separated person ids to be deleted."
)
parser.add_argument("--limit", default=100, type=int, help="Number of rows to be deleted")
parser.add_argument("--batch-size", default=1000, type=int, help="Number of rows to be deleted per batch")
parser.add_argument("--batches", default=1, type=int, help="Number of batches to run")
parser.add_argument("--live-run", action="store_true", help="Run changes, default is dry-run")

def handle(self, *args, **options):
run(options)


def run(options, sync: bool = False):
def run(options):
live_run = options["live_run"]
team_id = options["team_id"]
person_ids = options["person_ids"].split(",") if options["person_ids"] else None
limit = options["limit"]
batches = options["batches"]
batch_size = options["batch_size"]

if not team_id:
logger.error("You must specify --team-id to run this script")
Expand All @@ -40,7 +42,7 @@ def run(options, sync: bool = False):
logger.info(f"-> Team ID: {team_id}")
if person_ids:
logger.info(f"-> Person IDs: {person_ids}")
logger.info(f"-> Limit: {limit} ")
logger.info(f"-> Batches: {batches} of {batch_size}")

select_query = f"""
SELECT id
Expand Down Expand Up @@ -70,13 +72,13 @@ def run(options, sync: bool = False):

with connection.cursor() as cursor:
prepared_person_distinct_ids_query = cursor.mogrify(
delete_query_person_distinct_ids, {"team_id": team_id, "limit": limit, "person_ids": person_ids}
delete_query_person_distinct_ids, {"team_id": team_id, "limit": batch_size, "person_ids": person_ids}
)
prepared_person_override_query = cursor.mogrify(
delete_query_person_override, {"team_id": team_id, "limit": limit, "person_ids": person_ids}
delete_query_person_override, {"team_id": team_id, "limit": batch_size, "person_ids": person_ids}
)
prepared_person_query = cursor.mogrify(
delete_query_person, {"team_id": team_id, "limit": limit, "person_ids": person_ids}
delete_query_person, {"team_id": team_id, "limit": batch_size, "person_ids": person_ids}
)

logger.info(f"Delete query to run:")
Expand All @@ -96,13 +98,22 @@ def run(options, sync: bool = False):

logger.info(f"Executing delete query...")

# distinct_ids are deleted by cascade
with connection.cursor() as cursor:
cursor.execute(delete_query_person_distinct_ids, {"team_id": team_id, "limit": limit, "person_ids": person_ids})
logger.info(f"Deleted {cursor.rowcount} distinct_ids")
cursor.execute(delete_query_person_override, {"team_id": team_id, "limit": limit, "person_ids": person_ids})
logger.info(f"Deleted {cursor.rowcount} person overrides")
cursor.execute(delete_query_person, {"team_id": team_id, "limit": limit, "person_ids": person_ids})
logger.info(f"Deleted {cursor.rowcount} persons")
for i in range(0, batches):
logger.info(f"Deleting batch {i + 1} of {batches} ({batch_size} rows)")
with connection.cursor() as cursor:
cursor.execute(
delete_query_person_distinct_ids, {"team_id": team_id, "limit": batch_size, "person_ids": person_ids}
)
logger.info(f"Deleted {cursor.rowcount} distinct_ids")
cursor.execute(
delete_query_person_override, {"team_id": team_id, "limit": batch_size, "person_ids": person_ids}
)
logger.info(f"Deleted {cursor.rowcount} person overrides")
cursor.execute(delete_query_person, {"team_id": team_id, "limit": batch_size, "person_ids": person_ids})
logger.info(f"Deleted {cursor.rowcount} persons")

if cursor.rowcount < batch_size:
logger.info(f"Exiting early as we received less than {batch_size} rows")
break

logger.info("Done")
Loading