{"record":{"id":"4e1b32c3a9e24fa3","repo":"micro/go-micro","slug":"cannot-create-metadata-index","errorCode":null,"errorMessage":"cannot create metadata index","messagePattern":"cannot create metadata index","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/postgres/pgx/pgx.go","lineNumber":101,"sourceCode":"\t\tif err != nil {\n\t\t\treturn nil, Queries{}, err\n\t\t}\n\t}\n\n\treturn dbObj.conn, dbObj.tables[table], nil\n}\n\nfunc (s *sqlStore) initTable(database, table string) error {\n\tdb := s.databases[database].conn\n\n\t_, err := db.Exec(s.options.Context, fmt.Sprintf(createTable, database, table))\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"cannot create table\")\n\t}\n\n\t_, err = db.Exec(s.options.Context, fmt.Sprintf(createMDIndex, table, database, table))\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"cannot create metadata index\")\n\t}\n\n\t_, err = db.Exec(s.options.Context, fmt.Sprintf(createExpiryIndex, table, database, table))\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"cannot create expiry index\")\n\t}\n\n\ts.databases[database].tables[table] = NewQueries(database, table)\n\n\treturn nil\n}\n\nfunc (s *sqlStore) initDB(database string) error {\n\tif len(s.options.Nodes) == 0 {\n\t\ts.options.Nodes = []string{\"postgresql://root@localhost:26257?sslmode=disable\"}\n\t}\n\n\tsource := s.options.Nodes[0]","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/postgres/pgx/pgx.go#L83-L119","documentation":"After successfully creating the table, initTable executes a CREATE INDEX statement for the metadata column and wraps failures with 'cannot create metadata index'. It means the GIN/btree index on the metadata JSONB column could not be created, even though the table itself exists.","triggerScenarios":"First access to an uninitialized table where the CREATE INDEX for metadata fails: missing table (created concurrently?), insufficient privileges to create indexes, shared lock contention, or DB not supporting the index type.","commonSituations":"DB user can CREATE TABLE but not INDEX; concurrent store instances racing on initialization causing transient lock timeouts; managed Postgres restricting certain index types (e.g. no GIN extension).","solutions":["Grant INDEX/CREATE privilege: GRANT CREATE ON SCHEMA public TO <user> (or table owner must run the index DDL).","Check the wrapped PostgreSQL error for the specific reason.","Retry initialization if a concurrent instance held locks during index creation.","Create the index manually with the exact definition the store expects.","Confirm required extensions (e.g. for JSONB GIN indexes) are available."],"exampleFix":"// before\n// db.Exec(createMDIndex...) fails with permission denied\n// after\nGRANT CREATE ON SCHEMA public TO micro_user;\n// or pre-create index manually before app start","handlingStrategy":"validation","validationCode":"// before init\nvar canIdx bool\nconn.QueryRow(ctx, \"SELECT has_schema_privilege(current_user, 'public', 'CREATE')\").Scan(&canIdx)\n// verify extension support for the index method, e.g.:\n// SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname='...');","typeGuard":null,"tryCatchPattern":"if err := store.Init(); err != nil {\n\tvar pgErr *pgconn.PgError\n\tif errors.As(err, &pgErr) && pgErr.Code == \"42P17\" || pgErr.Code == \"42501\" {\n\t\t// pre-create index manually or fix privileges, then retry\n\t}\n\treturn err\n}","preventionTips":["Ensure the app DB role can create indexes (schema CREATE privilege or table ownership).","Serialize initialization (single instance or advisory lock) to avoid lock contention.","Pre-create indexes in migration scripts for restricted environments.","Check required extensions are installed in the target database.","Inspect wrapped PgError codes for precise diagnosis."],"tags":["postgres","sql","index","permissions"],"backgroundTag":"sql-index-creation-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}