Skip to content

Commit 7c1cde3

Browse files
committed
Fix compatibility with PostgreSQL 9.x: rows affected.
Starting with PostgreSQL 9.0, libpq's PQcmdTuples() returns row count for SELECT queries (previously it returned empty string). Because we're using this value to detect number of changed rows, both: "postgres_rewrite" directive and "$postgres_affected" variable were working incorrectly. Reported by Yichun Zhang (agentzh).
1 parent 0bd9db4 commit 7c1cde3

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/ngx_postgres_processor.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,12 @@ ngx_postgres_process_response(ngx_http_request_t *r, PGresult *res)
353353
pgctx->var_rows = PQntuples(res);
354354

355355
/* set $postgres_affected */
356-
affected = PQcmdTuples(res);
357-
affected_len = ngx_strlen(affected);
358-
if (affected_len) {
359-
pgctx->var_affected = ngx_atoi((u_char *) affected, affected_len);
356+
if (ngx_strncmp(PQcmdStatus(res), "SELECT", sizeof("SELECT") - 1)) {
357+
affected = PQcmdTuples(res);
358+
affected_len = ngx_strlen(affected);
359+
if (affected_len) {
360+
pgctx->var_affected = ngx_atoi((u_char *) affected, affected_len);
361+
}
360362
}
361363

362364
if (pglcf->rewrites) {

0 commit comments

Comments
 (0)