{"record":{"id":"7f0521229c7ee3e4","repo":"jackc/pgx","slug":"rewrite-query-failed-w","errorCode":null,"errorMessage":"rewrite query failed: %w","messagePattern":"rewrite query failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conn.go","lineNumber":515,"sourceCode":"\noptionLoop:\n\tfor len(arguments) > 0 {\n\t\tswitch arg := arguments[0].(type) {\n\t\tcase QueryExecMode:\n\t\t\tmode = arg\n\t\t\targuments = arguments[1:]\n\t\tcase QueryRewriter:\n\t\t\tqueryRewriter = arg\n\t\t\targuments = arguments[1:]\n\t\tdefault:\n\t\t\tbreak optionLoop\n\t\t}\n\t}\n\n\tif queryRewriter != nil {\n\t\tsql, arguments, err = queryRewriter.RewriteQuery(ctx, c, sql, arguments)\n\t\tif err != nil {\n\t\t\treturn pgconn.CommandTag{}, fmt.Errorf(\"rewrite query failed: %w\", err)\n\t\t}\n\t}\n\n\t// Always use simple protocol when there are no arguments.\n\tif len(arguments) == 0 {\n\t\tmode = QueryExecModeSimpleProtocol\n\t}\n\n\tdefer func() {\n\t\tif err != nil {\n\t\t\tif sc := c.statementCache; sc != nil {\n\t\t\t\tsc.Invalidate(sql)\n\t\t\t}\n\n\t\t\tif sc := c.descriptionCache; sc != nil {\n\t\t\t\tsc.Invalidate(sql)\n\t\t\t}\n\t\t}","sourceCodeStart":497,"sourceCodeEnd":533,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/conn.go#L497-L533","documentation":"A custom QueryRewriter supplied via query options failed while rewriting the SQL and arguments before execution. The rewriter's own error is wrapped with %w; pgx itself did not fail — the user-provided RewriteQuery callback did.","triggerScenarios":"Thrown at conn.go:515 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Unwrap the error with errors.Unwrap or errors.As to inspect the rewriter's failure","Fix the bug inside the custom QueryRewriter implementation","Test the rewriter independently of query execution"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}