{"record":{"id":"423caf7243916280","repo":"micro/go-micro","slug":"couldn-t-insert-record-key-423caf","errorCode":null,"errorMessage":"Couldn't insert record ${key}","messagePattern":"Couldn't insert record (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/postgres/postgres.go","lineNumber":531,"sourceCode":"\n\tmetadata := make(Metadata)\n\tfor k, v := range r.Metadata {\n\t\tmetadata[k] = v\n\t}\n\n\tvar expiry time.Time\n\tif r.Expiry != 0 {\n\t\texpiry = time.Now().Add(r.Expiry)\n\t}\n\n\tif expiry.IsZero() {\n\t\t_, err = st.Exec(r.Key, r.Value, metadata, nil)\n\t} else {\n\t\t_, err = st.Exec(r.Key, r.Value, metadata, expiry)\n\t}\n\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Couldn't insert record \"+r.Key)\n\t}\n\n\treturn nil\n}\n\n// Delete records with keys\nfunc (s *sqlStore) Delete(key string, opts ...store.DeleteOption) error {\n\tvar options store.DeleteOptions\n\tfor _, o := range opts {\n\t\to(&options)\n\t}\n\n\t// create the db if not exists\n\tif err := s.createDB(options.Database, options.Table); err != nil {\n\t\treturn err\n\t}\n\n\tst, err := s.prepare(options.Database, options.Table, \"delete\")","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/postgres/postgres.go#L513-L549","documentation":"The legacy postgres store's Write method executes the upsert statement (with expiry or NULL) and wraps failures with 'Couldn't insert record <key>'. Like its pgx counterpart, this indicates the database rejected the INSERT/UPSERT for the given key.","triggerScenarios":"Calling store.Write when the prepared statement's Exec fails: constraint violations, connection failure, value exceeding bytea/field limits, invalid metadata JSON marshaling already consumed upstream, or table missing.","commonSituations":"Connection pool exhaustion under load; oversized record values; DB user lacking INSERT/UPDATE privileges; server clock skew affecting expiry timestamps; primary key conflicts on concurrent writes to the same key when the DDL lacks ON CONFLICT coverage.","solutions":["Inspect the wrapped driver error to distinguish constraint/permission/connection causes.","Verify the DB user has INSERT and UPDATE privileges on the table.","Retry transient failures with backoff; ensure the table exists (re-run configure).","Reduce record value size or increase column capacity if limits are hit.","Check for conflicting concurrent writers to the same key."],"exampleFix":"// before\nst, _ := db.Prepare(writeStmt)\n_, err := st.Exec(key, hugeValue, metadata, expiry) // value > 1GB limit\n// after\nif len(value) > maxValueSize { return ErrValueTooLarge }\n_, err := st.Exec(key, value, metadata, expiry)","handlingStrategy":"retry","validationCode":"// before write\nif r.Key == \"\" || len(r.Value) > maxByteaSize { return ErrInvalidRecord }\nif err := db.Ping(); err != nil { // reconnect }","typeGuard":"func isInsertErr(err error) bool {\n\treturn err != nil && strings.Contains(err.Error(), \"Couldn't insert record\")\n}","tryCatchPattern":"err := store.Write(rec)\nif err != nil {\n\tif isInsertErr(err) && isTransientNetErr(err) {\n\t\treturn retryWithBackoff(func() error { return store.Write(rec) })\n\t}\n\treturn err\n}","preventionTips":["Grant INSERT/UPDATE privileges on the store table to the app role.","Retry only transient driver errors; surface constraint violations to the caller.","Cap record value sizes within bytea limits.","Keep the table present: run idempotent schema checks at startup.","Serialize hot-key writers or rely on upsert semantics to avoid conflicts."],"tags":["postgres","sql","insert","write"],"backgroundTag":"sql-write-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}