Skip to content

Commit 77bef8e

Browse files
committedDec 27, 2011
Add nginx-style debug logging.
Based on patch from Yichun Zhang (agentzh). Change-Id: If2094a3a300500b1a6710e59d148e83abf3b6da4 Signed-off-by: Piotr Sikora <[email protected]>
1 parent 06ca0dc commit 77bef8e

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
 

‎src/ngx_postgres_processor.c

+36-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ ngx_postgres_process_events(ngx_http_request_t *r)
5959
goto failed;
6060
}
6161

62+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
63+
"postgres: process events");
64+
6265
switch (pgdt->state) {
6366
case state_db_connect:
6467
dd("state_db_connect");
@@ -115,6 +118,9 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc,
115118

116119
pgrc = PQconnectPoll(pgdt->pgconn);
117120

121+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
122+
"postgres: polling while connecting, rc:%d", (int) pgrc);
123+
118124
if (pgrc == PGRES_POLLING_READING || pgrc == PGRES_POLLING_WRITING) {
119125

120126
/*
@@ -134,8 +140,16 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc,
134140
pgrc = PQconnectPoll(pgdt->pgconn);
135141
dd("re-polling rc:%d", (int) pgrc);
136142

143+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
144+
"postgres: re-polling while connecting, rc:%d",
145+
(int) pgrc);
146+
137147
if (pgrc == PGRES_POLLING_READING || pgrc == PGRES_POLLING_WRITING)
138148
{
149+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
150+
"postgres: busy while connecting, rc:%d",
151+
(int) pgrc);
152+
139153
dd("returning NGX_AGAIN");
140154
return NGX_AGAIN;
141155
}
@@ -178,6 +192,9 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc,
178192
}
179193
#endif /* DDEBUG */
180194

195+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
196+
"postgres: busy while connecting, rc:%d", (int) pgrc);
197+
181198
dd("returning NGX_AGAIN");
182199
return NGX_AGAIN;
183200
}
@@ -199,6 +216,8 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc,
199216
}
200217

201218
dd("connected successfully");
219+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
220+
"postgres: connected successfully");
202221

203222
pgxc->log->action = "sending query to PostgreSQL database";
204223
pgdt->state = state_db_send_query;
@@ -228,6 +247,8 @@ ngx_postgres_upstream_send_query(ngx_http_request_t *r, ngx_connection_t *pgxc,
228247
(void) ngx_cpystrn(query, pgdt->query.data, pgdt->query.len + 1);
229248

230249
dd("sending query: %s", query);
250+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
251+
"postgres: sending query: \"%s\"", query);
231252

232253
if (pglcf->output_binary) {
233254
pgrc = PQsendQueryParams(pgdt->pgconn, (const char *) query,
@@ -250,6 +271,8 @@ ngx_postgres_upstream_send_query(ngx_http_request_t *r, ngx_connection_t *pgxc,
250271
ngx_add_timer(pgxc->read, r->upstream->conf->read_timeout);
251272

252273
dd("query sent successfully");
274+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
275+
"postgres: query sent successfully");
253276

254277
pgxc->log->action = "waiting for result from PostgreSQL database";
255278
pgdt->state = state_db_get_result;
@@ -274,11 +297,18 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc,
274297
}
275298

276299
if (!PQconsumeInput(pgdt->pgconn)) {
300+
ngx_log_error(NGX_LOG_ERR, pgxc->log, 0,
301+
"postgres: failed to consume input: %s",
302+
PQerrorMessage(pgdt->pgconn));
303+
277304
dd("returning NGX_ERROR");
278305
return NGX_ERROR;
279306
}
280307

281308
if (PQisBusy(pgdt->pgconn)) {
309+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
310+
"postgres: busy while receiving result");
311+
282312
dd("returning NGX_AGAIN");
283313
return NGX_AGAIN;
284314
}
@@ -289,7 +319,7 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc,
289319
if (res == NULL) {
290320
dd("receiving result failed");
291321
ngx_log_error(NGX_LOG_ERR, pgxc->log, 0,
292-
"postgres: receiving result failed: %s",
322+
"postgres: failed to receive result: %s",
293323
PQerrorMessage(pgdt->pgconn));
294324

295325
dd("returning NGX_ERROR");
@@ -300,7 +330,7 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc,
300330
if ((pgrc != PGRES_COMMAND_OK) && (pgrc != PGRES_TUPLES_OK)) {
301331
dd("receiving result failed");
302332
ngx_log_error(NGX_LOG_ERR, pgxc->log, 0,
303-
"postgres: receiving result failed: %s: %s",
333+
"postgres: failed to receive result: %s: %s",
304334
PQresStatus(pgrc),
305335
PQerrorMessage(pgdt->pgconn));
306336

@@ -313,6 +343,10 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc,
313343
dd("result received successfully, cols:%d rows:%d",
314344
PQnfields(res), PQntuples(res));
315345

346+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pgxc->log, 0,
347+
"postgres: result received successfully, cols:%d rows:%d",
348+
PQnfields(res), PQntuples(res));
349+
316350
pgxc->log->action = "processing result from PostgreSQL database";
317351
rc = ngx_postgres_process_response(r, res);
318352

‎src/ngx_postgres_upstream.c

+9
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data)
356356
* internal checks in PQsetnonblocking are taking care of any
357357
* PQconnectStart failures, so we don't need to check them here.
358358
*/
359+
360+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
361+
"postgres: connecting");
362+
359363
pgdt->pgconn = PQconnectStart((const char *)connstring);
360364
if (PQsetnonblocking(pgdt->pgconn, 1) == -1) {
361365
ngx_log_error(NGX_LOG_ERR, pc->log, 0,
@@ -378,6 +382,8 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data)
378382
PQtrace(pgdt->pgconn, stderr);
379383
#endif
380384

385+
dd("connection status:%d", (int) PQstatus(pgdt->pgconn));
386+
381387
/* take spot in keepalive connection pool */
382388
pgscf->active_conns++;
383389

@@ -391,6 +397,9 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data)
391397
goto invalid;
392398
}
393399

400+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
401+
"postgres: connection fd:%d", fd);
402+
394403
pgxc = pc->connection = ngx_get_connection(fd, pc->log);
395404

396405
if (pgxc == NULL) {

0 commit comments

Comments
 (0)
Please sign in to comment.