{"record":{"id":"89ede0ae7793fb65","repo":"getsops/sops","slug":"database-malfunction-select-1-should-return-1-bu","errorCode":null,"errorMessage":"Database malfunction: SELECT 1 should return 1, but returned %d","messagePattern":"Database malfunction: SELECT 1 should return 1, but returned (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"audit/audit.go","lineNumber":129,"sourceCode":"// process will exit with status set to 1\ntype PostgresAuditor struct {\n\tDB *sql.DB\n}\n\n// NewPostgresAuditor is the constructor for a new PostgresAuditor struct\n// initialized with the given db connection string\nfunc NewPostgresAuditor(connStr string) (*PostgresAuditor, error) {\n\tdb, err := sql.Open(\"postgres\", connStr)\n\tpg := &PostgresAuditor{DB: db}\n\tif err != nil {\n\t\treturn pg, err\n\t}\n\tvar result int\n\terr = pg.DB.QueryRow(\"SELECT 1\").Scan(&result)\n\tif err != nil {\n\t\treturn pg, fmt.Errorf(\"Pinging audit database failed: %s\", err)\n\t} else if result != 1 {\n\t\treturn pg, fmt.Errorf(\"Database malfunction: SELECT 1 should return 1, but returned %d\", result)\n\t}\n\treturn pg, nil\n}\n\n// Handle persists the audit event by writing a row to the\n// 'audit_event' postgres table\nfunc (p *PostgresAuditor) Handle(event interface{}) {\n\tu, err := user.Current()\n\tif err != nil {\n\t\tlog.Fatalf(\"Error getting current user for auditing: %s\", err)\n\t}\n\tswitch event := event.(type) {\n\tcase DecryptEvent:\n\t\t// Save the event to the database\n\t\tlog.WithField(\"file\", event.File).\n\t\t\tDebug(\"Saving decrypt event to database\")\n\t\t_, err = p.DB.Exec(\"INSERT INTO audit_event (action, username, file) VALUES ($1, $2, $3)\", \"decrypt\", u.Username, event.File)\n\t\tif err != nil {","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/getsops/sops/blob/13442bb98183887d7a9ac09ec8ab0564673a59d8/audit/audit.go#L111-L147","documentation":"After SELECT 1 succeeds, NewPostgresAuditor sanity-checks the returned value. This error means the query executed but the row value was not 1, indicating the connection is talking to something that is not behaving like a healthy Postgres instance.","triggerScenarios":"QueryRow(\"SELECT 1\").Scan succeeds but result != 1 — practically only possible with a proxy/middleware rewriting queries, a corrupt driver, or a non-Postgres endpoint accepting the connection.","commonSituations":"Connecting through a misbehaving TCP proxy or load balancer that returns synthetic rows; pointing the DSN at a mock/fake server in tests; exotic connection pooling middleware intercepting queries.","solutions":["Bypass any proxy/load balancer and connect directly to Postgres to confirm SELECT 1 returns 1","Check for test doubles or connection interceptors (e.g. sqlmock leftovers) in the connection path","Restart the Postgres instance and re-run; rule out server-side corruption","Print the DSN and confirm the target is a genuine Postgres server"],"exampleFix":"// before\nDSN=postgres://user@proxy.internal:5432/auditdb  # proxy mangles queries\n// after\nDSN=postgres://user@db.internal:5432/auditdb      # direct connection","handlingStrategy":"validation","validationCode":"db, err := sql.Open(\"postgres\", dsn)\nif err != nil { return err }\nvar result int\nif err := db.QueryRow(\"SELECT 1\").Scan(&result); err != nil { return err }\nif result != 1 {\n    return fmt.Errorf(\"unexpected SELECT 1 result %d from %s\", result, hostFromDSN(dsn))\n}","typeGuard":null,"tryCatchPattern":"pg, err := audit.NewPostgresAuditor(dsn)\nif err != nil {\n    if strings.Contains(err.Error(), \"Database malfunction\") {\n        log.Fatalf(\"endpoint %s is not behaving like Postgres; check proxies\", dsn)\n    }\n    return err\n}","preventionTips":["Connect directly to Postgres, not through rewriting proxies","Never point production DSNs at test doubles","Verify the endpoint with psql before registering it"],"tags":["postgres","database","sanity-check","audit"],"backgroundTag":"database-sanity-check-failed","analyzedSha":"13442bb98183887d7a9ac09ec8ab0564673a59d8","analyzedAt":"2026-09-01T03:53:00.447Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}