Skip to content

Uploads fail with 42P10 after v1.77.0 on instances that applied migrations 0065–0067 before #1352 (indexes without COLLATE "C", bucketid_objname dropped) #1394

Closed

Description

@Celtiore

Bug report

Describe the bug

After upgrading a self-hosted instance from v1.73.0 to v1.77.3, storage-api reports healthy, lists buckets and objects fine, but every write fails with HTTP 500:

{"statusCode":"500","error":"DatabaseError","message":"database error, code: 42P10"}

Stack (from the container logs): StoragePgDB.upsertObject → runQuery → mapPgError → DatabaseError 42P10. PostgreSQL 42P10 is invalid_column_reference: there is no unique or exclusion constraint matching the ON CONFLICT specification.

Affected: standard REST uploads (POST /object/{bucket}/{key}), with and without x-upsert, on every bucket (with or without allowed_mime_types). TUS and S3 multipart go through the same upsertObject, so they should be affected too.

Root cause

  1. feat: support addressing objects by versionId #1341 (shipped in v1.72.0 / v1.72.1, 2026-08-27) introduced the tenant migrations 0065-objects-key-version-index, 0066-objects-current-version-index, 0067-objects-null-version-index. In that version the indexes were defined without COLLATE "C":
    CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS idx_objects_current_version
        ON storage.objects (bucket_id, name) WHERE archived_at IS NULL;
  2. fix: add COLLATE "C" to name in versioned-object indexes #1352 (v1.72.2+, 2026-08-31) fixed this by adding COLLATE "C" in place in the same three migration files. Instances that had already applied 0065–0067 from v1.72.0/v1.72.1 never got the collated definition: the runner does not re-apply an already-executed id (and refreshes the hash on mismatch), and CREATE INDEX IF NOT EXISTS would be a no-op anyway.
  3. feat: drop bucketid_objname and add version list test coverage #1353 (v1.77.0) drops bucketid_objname (migration 0072) and switches the upsert arbiter to the new partial index (src/storage/database/pg.ts, upsertObject):
    const conflictTarget = await this.hasMigration("objects-current-version-index")
      ? 'ON CONFLICT (bucket_id, name COLLATE "C") WHERE archived_at IS NULL'
      : "ON CONFLICT (name, bucket_id)";
    On an instance whose idx_objects_current_version is (bucket_id, name) without the collation, PostgreSQL cannot infer an arbiter for (bucket_id, name COLLATE "C") WHERE archived_at IS NULL, and the old (bucket_id, name) unique index that used to arbitrate is gone → 42P10 on every insert.

So any self-hosted instance that ran v1.72.0 or v1.72.1 at some point (applied 0065–0067 before #1352) breaks on upgrade to ≥ v1.77.0, while a fresh install is fine. Rolling the image back to < v1.77.0 does not help either: bucketid_objname is already dropped and the old code targets it.

State observed on the broken instance

$ select id, name, executed_at from storage.migrations where id between 65 and 72;
65 | objects-key-version-index      | 2026-08-29   ← applied from v1.72.1
66 | objects-current-version-index  | 2026-08-29
67 | objects-null-version-index     | 2026-08-29
68..71 | (lifecycle, list-objects-with-versions, delete-marker index) | 2026-09-15
72 | drop-bucketid-objname-index    | 2026-09-15   ← v1.77.3

$ select indexdef from pg_indexes where schemaname='storage' and tablename='objects';
CREATE UNIQUE INDEX idx_objects_current_version ON storage.objects USING btree (bucket_id, name) WHERE (archived_at IS NULL)
CREATE UNIQUE INDEX idx_objects_null_version ON storage.objects USING btree (bucket_id, name) WHERE (NOT is_versioned)
CREATE UNIQUE INDEX objects_bucket_id_name_version_key ON storage.objects USING btree (bucket_id, name, version) NULLS NOT DISTINCT
-- bucketid_objname: gone (0072)

To Reproduce

  1. Self-hosted Supabase (docker compose), supabase/storage-api:v1.72.1, STORAGE_BACKEND=file, PostgreSQL 17. Start once so 0065–0067 are applied.
  2. Bump to supabase/storage-api:v1.77.3 (0068–0072 apply, container healthy).
  3. POST /storage/v1/object/{bucket}/test.txt with the service role → 500 / 42P10. GET, list, DELETE still work.

Expected behavior

Uploads keep working after the upgrade, i.e. the migration chain must guarantee the collated unique indexes exist before 0072 drops bucketid_objname.

Workaround applied

Recreate the three indexes with the definitions shipped in /app/migrations/tenant/0065–0067 (create the new one concurrently before dropping the old one so uniqueness is never lost):

CREATE UNIQUE INDEX CONCURRENTLY objects_bucket_id_name_version_key_c
    ON storage.objects (bucket_id, name COLLATE "C", version) NULLS NOT DISTINCT;
CREATE UNIQUE INDEX CONCURRENTLY idx_objects_current_version_c
    ON storage.objects (bucket_id, name COLLATE "C") WHERE archived_at IS NULL;
CREATE UNIQUE INDEX CONCURRENTLY idx_objects_null_version_c
    ON storage.objects (bucket_id, name COLLATE "C") WHERE NOT is_versioned;

DROP INDEX CONCURRENTLY storage.objects_bucket_id_name_version_key;
ALTER INDEX storage.objects_bucket_id_name_version_key_c RENAME TO objects_bucket_id_name_version_key;
DROP INDEX CONCURRENTLY storage.idx_objects_current_version;
ALTER INDEX storage.idx_objects_current_version_c RENAME TO idx_objects_current_version;
DROP INDEX CONCURRENTLY storage.idx_objects_null_version;
ALTER INDEX storage.idx_objects_null_version_c RENAME TO idx_objects_null_version;

Uploads, upserts, duplicate detection (400), listing and deletes are back to normal right after.

Suggested fix

A new tenant migration (or a guard in 0072) that checks pg_get_indexdef() of the three indexes and, when name COLLATE "C" is missing, recreates them with the SQL above — so that instances which applied 0065–0067 from v1.72.0/v1.72.1 are healed before bucketid_objname is dropped. Editing already-shipped migration files (#1352) cannot reach those instances.

System information

Activity

  1. self-assigned this
    on Sep 16, 2026
  2. TylerHillery commented on Sep 22, 2026

    @TylerHillery
    Contributor

    Thanks for the detailed report.

    This affects self-hosted installations that applied migrations 0065–0067 while running Storage v1.72.0–v1.72.3. The index definitions were corrected in v1.72.4, but previously applied migrations were not replayed. The failure becomes visible after upgrading to v1.77.0+, when the legacy bucketid_objname index is removed.

    We don’t plan to add an automatic repair migration given the limited affected release window and the operational cost of rebuilding these indexes for every installation. Self-hosted users who have not run those versions should skip them and go directly to v1.72.4+. If migrations 0065–0067 were already applied by an affected version, later upgrades do not repair the indexes. Manual repair is required before upgrading to v1.77.0+.

    First, confirm the definitions are missing COLLATE "C":

      SELECT indexname, indexdef
      FROM pg_indexes
      WHERE schemaname = 'storage'
        AND indexname IN (
          'objects_bucket_id_name_version_key',
          'idx_objects_current_version',
          'idx_objects_null_version'
        );

    Then, outside a transaction, replace them:

    ALTER INDEX storage.idx_objects_current_version
      RENAME TO idx_objects_current_version_pre_collate;
    
    CREATE UNIQUE INDEX CONCURRENTLY idx_objects_current_version
      ON storage.objects (bucket_id, name COLLATE "C")
      WHERE archived_at IS NULL;
    
    ALTER INDEX storage.objects_bucket_id_name_version_key
      RENAME TO objects_bucket_id_name_version_key_pre_collate;
    
    CREATE UNIQUE INDEX CONCURRENTLY objects_bucket_id_name_version_key
      ON storage.objects (bucket_id, name COLLATE "C", version)
      NULLS NOT DISTINCT;
    
    ALTER INDEX storage.idx_objects_null_version
      RENAME TO idx_objects_null_version_pre_collate;
    
    CREATE UNIQUE INDEX CONCURRENTLY idx_objects_null_version
      ON storage.objects (bucket_id, name COLLATE "C")
      WHERE NOT is_versioned;

    Verify uploads and upserts work, then remove the backups:

    DROP INDEX CONCURRENTLY storage.idx_objects_current_version_pre_collate;
    DROP INDEX CONCURRENTLY storage.objects_bucket_id_name_version_key_pre_collate;
    DROP INDEX CONCURRENTLY storage.idx_objects_null_version_pre_collate;

    Run each concurrent command separately and not inside a transaction. Please let me know if you have any additional questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions