sentry icon indicating copy to clipboard operation
sentry copied to clipboard

Duplicate PK im migration

Open aamarques opened this issue 2 years ago • 5 comments

Environment

self-hosted (https://develop.sentry.dev/self-hosted/)

Version

22.7.0, 22.8.0

Steps to Reproduce

Hi there. I have received an error (I don't know how) about duplicate PK when migrating between versions.

Steps to reproduce: Its an update version.

SENTRY_IMAGE=getsentry/sentry:22.8.0 ./install.sh

Expected Result

Migration without error or any help to solve this.

Actual Result

Running migrations:
  Applying replays.0001_init_replays... OK
  Applying replays.0002_rename_to_segment_id... OK
  Applying sentry.0306_sentry_functions_code... OK
  Applying sentry.0307_remove_alert_type_state... OK
  Applying sentry.0308_remove_alert_type_column... OK
  Applying sentry.0309_fix_many_to_many_field... OK
  Applying sentry.0310_sentry_functions_add_webhooks... OK
  Applying sentry.0311_backfill_slack_settings... OK
  Applying sentry.0312_has_profiles_flag... OK
  Applying sentry.0313_sentry_functions_env_variables... OK
Creating missing DSNs
Correcting Group.num_comments counter
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 91, in inner
    return func(self, sql, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/base.py", line 86, in execute
    return self.cursor.execute(sql, clean_bad_params(params))
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "sentry_groupedmessage_pkey"
DETAIL:  Key (id)=(243922) already exists.

aamarques avatar Sep 07 '22 09:09 aamarques

The function fix_group_counters() is where the process is interrupted. But how is that value a duplicated PK ? Why the repair process stops and cleans up instead of warning?

aamarques avatar Sep 07 '22 10:09 aamarques

postgres=# UPDATE sentry_groupedmessage SET num_comments = (
postgres(#      SELECT COUNT(*) from sentry_activity
postgres(#      WHERE type = 8 and group_id = sentry_groupedmessage.id);
ERROR:  duplicate key value violates unique constraint "sentry_groupedmessage_pkey"
DETAIL:  Key (id)=(243922) already exists.

aamarques avatar Sep 07 '22 11:09 aamarques

Routing to @getsentry/open-source for triage. ⏲️

getsentry-release avatar Sep 07 '22 15:09 getsentry-release

This seems to be a duplicate issue of something you posted a few months ago here:

https://github.com/getsentry/self-hosted/issues/1498

What's the status of your db?

hubertdeng123 avatar Sep 07 '22 16:09 hubertdeng123

This seems to be a duplicate issue of something you posted a few months ago here:

getsentry/self-hosted#1498

What's the status of your db?

@hubertdeng123 my DB and sentry is running, I think because that fix_group_counters() after update and migration. And yes I have opened that issue a few months ago and nothing results. :( and then the bot closes it :(

image

aamarques avatar Sep 20 '22 15:09 aamarques

Now I got a new error updating from 22.8.0 to 22.9.0 Note that duplicated key is not the previous one. Every time it changes. Why and when is this PK being duplicated?

Any help would be apreciatted :)

Running migrations for default
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, nodestore, replays, sentry, sessions, sites, social_auth
Running migrations:
  Applying sentry.0314_bit_int_for_org_and_project_id...Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 91, in inner
    return func(self, sql, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/base.py", line 86, in execute
    return self.cursor.execute(sql, clean_bad_params(params))
psycopg2.errors.UniqueViolation: could not create unique index "sentry_grouprelease_pkey"
DETAIL:  Key (id)=(89240) is duplicated.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 77, in inner
    raise_the_exception(self.db, e)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 75, in inner
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 18, in inner
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 95, in inner
    raise exc_info[0](msg).with_traceback(exc_info[2])
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 91, in inner
    return func(self, sql, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/base.py", line 86, in execute
    return self.cursor.execute(sql, clean_bad_params(params))
psycopg2.errors.UniqueViolation: UniqueViolation('could not create unique index "sentry_grouprelease_pkey"\nDETAIL:  Key (id)=(89240) is duplicated.\n')
SQL: ALTER TABLE "sentry_grouprelease" ALTER COLUMN "project_id" TYPE bigint USING "project_id"::bigint

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/sentry", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/site-packages/sentry/runner/__init__.py", line 188, in main
    func(**kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/runner/decorators.py", line 29, in inner
    return ctx.invoke(f, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/runner/commands/upgrade.py", line 100, in upgrade
    _upgrade(not noinput, traceback, verbosity, not no_repair, with_nodestore)
  File "/usr/local/lib/python3.8/site-packages/sentry/runner/commands/upgrade.py", line 51, in _upgrade
    dj_call_command(
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 148, in call_command
    return command.execute(*args, **defaults)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 232, in handle
    post_migrate_state = executor.migrate(
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.8/site-packages/sentry/new_migrations/monkey/executor.py", line 79, in apply_migration
    return super().apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.8/site-packages/sentry/new_migrations/migrations.py", line 15, in apply
    return super().apply(project_state, schema_editor, collect_sql)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 534, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/schema.py", line 122, in _alter_field
    super()._alter_field(
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 680, in _alter_field
    self.execute(
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/sentry_sdk/integrations/django/__init__.py", line 560, in execute
    return real_execute(self, sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 77, in inner
    raise_the_exception(self.db, e)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 75, in inner
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 18, in inner
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 95, in inner
    raise exc_info[0](msg).with_traceback(exc_info[2])
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 91, in inner
    return func(self, sql, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/base.py", line 86, in execute
    return self.cursor.execute(sql, clean_bad_params(params))
django.db.utils.IntegrityError: UniqueViolation('could not create unique index "sentry_grouprelease_pkey"\nDETAIL:  Key (id)=(89240) is duplicated.\n')
SQL: ALTER TABLE "sentry_grouprelease" ALTER COLUMN "project_id" TYPE bigint USING "project_id"::bigint
1
Error in set-up-and-migrate-database.sh:12.
'$dcr web upgrade' exited with status 1
-> ./install.sh:main:36
--> set-up-and-migrate-database.sh:source:12

Cleaning up...
postgres_1                                  | ERROR:  column "type" of relation "sentry_groupedmessage" does not exist at character 299
postgres_1                                  | STATEMENT:  INSERT INTO "sentry_groupedmessage" ("project_id", "logger", "level", "message", "view", "num_comments", "platform", "status", "times_seen", "last_seen", "first_seen", "first_release_id", "resolved_at", "active_at", "time_spent_total", "time_spent_count",
"score", "is_public", "data", "short_id", "type") VALUES (1, '', 40, 'UndefinedColumn(''column sentry_groupedmessage.type does not exist\nLINE 1: ...sage"."data", "sentry_groupedmessage"."short_id", "sentry_gr...\n                                                             ^\n'')', 'sentry.tasks.store.save_event', 0, 'python', 0, 1, '2022-09-21T09:04:08.925550+00:00'::timestamptz, '2022-09-21T09:04:08.925550+00:00'::timestamptz, 702, NULL, '2022-09-21T09:04:08.925550+00:00'::timestamptz, 0, 0, 1663751048, false, 'eJy1ks1uEzEUhadpS0pLoWWJhGRl0yAhR2wjdYGqCFWKEKggeRExcsY3E4N/xvadabOoxLYv0CfhKXiCrnkSPK5AEMpsELPxHZ9vrs/1nM+9S5cN2VaWZbiqwG2w7ViC99a7HuvHuqhV5SW6TfY0vgUw6FcUefgUaEDrgQbeQA5NFNwW24mMBuSCI3fbl+7eMDVsuKrB9VnWy7L3RsBCGhAnVtXaDI+KtJLb1nnpbV2B0BACL4G2roiwEIixSOBCBpyZ6enrCXkxJpTSFhrQQXve4DkZ3Nkk6mFpPeZS/MbEz2eG/MvzYWaOnu2evZ2OydlkOjl593cHa2evqZW3H6H4w+EapWxZgu8k4o9QXcCPqgNpJJx36abWeWG1jnLonElxXFivf2Xilbud5QY7iKF4423pudbSlJMUuPspPgupwHANbpc9+Zm4kZiPKhuw9BBGcx6AViu3d8vXpkBpjXuQ8goXUNQIbj/lVcgQbaxylKggP5e4zNED5IrPQbmHV3WKZ1LdI9bc4WpM/ntg20s5SLMoW/A0y+Fyj+23Gzxg7qEA2YBwj1+9vPn65dvxdb+m3wG+zjU1', 217, 1) RETURNING "sentry_groupedmessage"."id"

aamarques avatar Sep 21 '22 09:09 aamarques

Running migrations: Applying sentry.0314_bit_int_for_org_and_project_id... OK Applying sentry.0315_add_type_to_group... OK Applying sentry.0316_add_context_field_to_groupowner... OK Creating missing DSNs Correcting Group.num_comments counter

▶ Migrating file storage ...

Done.

aamarques avatar Sep 27 '22 16:09 aamarques

Sorry, must've missed this in my notification feed. What was your fix for this?

hubertdeng123 avatar Sep 27 '22 16:09 hubertdeng123

Sorry, must've missed this in my notification feed. What was your fix for this?

I did all recommendations here https://github.com/getsentry/self-hosted/issues/1498 without success. Removing all registers related to that ID worked.

PS: I did it in a duplicated environment first and I am not recommending it as the solution to all. This worked for me :) Do it at your own risk. No guarantee that will work for you.

aamarques avatar Oct 10 '22 10:10 aamarques

thanks!

hubertdeng123 avatar Oct 11 '22 16:10 hubertdeng123