ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_ci_job_artifac...

https://sentry.gitlab.net/gitlab/gitlabcom/issues/1048924/

PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_ci_job_artifacts_on_job_id_and_file_type"
DETAIL:  Key (job_id, file_type)=(352146382, 1) already exists.

  active_record/connection_adapters/postgresql_adapter.rb:611:in `async_exec_params'
    @connection.exec_params(sql, type_casted_binds)
  active_record/connection_adapters/postgresql_adapter.rb:611:in `block (2 levels) in exec_no_cache'
    @connection.exec_params(sql, type_casted_binds)
  active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
    yield
  active_support/concurrency/share_lock.rb:187:in `yield_shares'
    yield
  active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
    @lock.yield_shares(compatible: [:load]) do
...
(251 additional frame(s) were not displayed)

ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_ci_job_artifacts_on_job_id_and_file_type"
DETAIL:  Key (job_id, file_type)=(352146382, 1) already exists.
: INSERT INTO "ci_job_artifacts" ("project_id", "job_id", "file_type", "size", "created_at", "updated_at", "file", "file_sha256", "file_format") VALUES (14779138, 352146382, 1, 1616923, '2019-11-15 15:18:47.352017', '2019-11-15 15:18:47.352017', 'artifacts.zip', '\x62653964323331326539613766333135356436356364356262653165353336376139663263656434353039363936366234323132326433303633353636336232', 2) RETURNING "id"

ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_ci_job_artifacts_on_job_id_and_file_type"
DETAIL:  Key (job_id, file_type)=(352146382, 1) already exists.
: INSERT INTO "ci_job_artifacts" ("project_id", "job_id", "file_type", "size", "created_at", "updated_at", "file", "file_sha256", "file_format") VALUES (14779138, 352146382, 1, 1616923, '2019-11-15 15:18:47.352017', '2019-11-15 15:18:47.352017', 'artifacts.zip', '\x62653964323331326539613766333135356436356364356262653165353336376139663263656434353039363936366234323132326433303633353636336232', 2) RETURNING "id"

Proposal

Considering that this specific bug was fixed we want to make sure similar issues won't return 500 state. We prefer instead to return 422 Unprocessable Entity and have the Runner stop retrying to upload the artifact.

Edited by Fabio Pitino - PTO until Jan 1