From 51822ec5b6cd549d97430e49c197b8ddd78a5fed Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Fri, 23 Dec 2011 17:23:02 +0000 Subject: [PATCH 01/32] Fix compatibility with poll, select and /dev/poll event models. Reported by Yichun Zhang (agentzh). Change-Id: I9411e72ccfdbb0d974a92b6cba569dc15254ed3d Signed-off-by: Piotr Sikora --- src/ngx_postgres_keepalive.c | 26 ++++++++++++++++---------- src/ngx_postgres_upstream.c | 35 +++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/ngx_postgres_keepalive.c b/src/ngx_postgres_keepalive.c index 82574a53..f5a8a022 100644 --- a/src/ngx_postgres_keepalive.c +++ b/src/ngx_postgres_keepalive.c @@ -186,6 +186,22 @@ ngx_postgres_keepalive_free_peer(ngx_peer_connection_t *pc, { c = pc->connection; + if (c->read->timer_set) { + ngx_del_timer(c->read); + } + + if (c->write->timer_set) { + ngx_del_timer(c->write); + } + + if (c->write->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) { + if (ngx_del_event(c->write, NGX_WRITE_EVENT, 0) != NGX_OK) { + return; + } + } + + pc->connection = NULL; + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, "postgres: free keepalive peer: saving connection %p", c); @@ -213,16 +229,6 @@ ngx_postgres_keepalive_free_peer(ngx_peer_connection_t *pc, item->connection = c; ngx_queue_insert_head(&pgscf->cache, q); - pc->connection = NULL; - - if (c->read->timer_set) { - ngx_del_timer(c->read); - } - - if (c->write->timer_set) { - ngx_del_timer(c->write); - } - c->write->handler = ngx_postgres_keepalive_dummy_handler; c->read->handler = ngx_postgres_keepalive_close_handler; diff --git a/src/ngx_postgres_upstream.c b/src/ngx_postgres_upstream.c index c8f17492..1c3c00aa 100644 --- a/src/ngx_postgres_upstream.c +++ b/src/ngx_postgres_upstream.c @@ -415,26 +415,29 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data) if (ngx_event_flags & NGX_USE_RTSIG_EVENT) { dd("NGX_USE_RTSIG_EVENT"); - rc = ngx_add_conn(pgxc); + if (ngx_add_conn(pgxc) != NGX_OK) { + goto bad_add; + } + } else if (ngx_event_flags & NGX_USE_CLEAR_EVENT) { dd("NGX_USE_CLEAR_EVENT"); - rc = ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT); - if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) != NGX_OK) { - ngx_log_error(NGX_LOG_ERR, pc->log, 0, - "postgres: failed to add nginx connection"); + if (ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT) != NGX_OK) { + goto bad_add; + } - goto invalid; + if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) != NGX_OK) { + goto bad_add; } - } else { - dd("NGX_LEVEL_EVENT"); - rc = ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT); - } - if (rc != NGX_OK) { - ngx_log_error(NGX_LOG_ERR, pc->log, 0, - "postgres: failed to add nginx connection"); + } else { + dd("NGX_USE_LEVEL_EVENT"); + if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT) != NGX_OK) { + goto bad_add; + } - goto invalid; + if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT) != NGX_OK) { + goto bad_add; + } } pgxc->log->action = "connecting to PostgreSQL database"; @@ -443,6 +446,10 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data) dd("returning NGX_AGAIN"); return NGX_AGAIN; +bad_add: + ngx_log_error(NGX_LOG_ERR, pc->log, 0, + "postgres: failed to add nginx connection"); + invalid: ngx_postgres_upstream_free_connection(pc->log, pc->connection, pgdt->pgconn, pgscf); From 06ca0dc735e7d13274aa7ab73537740b1ffea206 Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Fri, 23 Dec 2011 19:07:54 +0000 Subject: [PATCH 02/32] Make sure that the write event really occured before re-polling. The "re-polling" hack to work-around the case in which both: read and write events occured within the same event processing call was being used for any CONNECTION_MADE status, without checking if the write really occured. Based on patch from Yichun Zhang (agentzh). Change-Id: Ia8310a109baf639d1c5c3c766d2298c3610e6d47 Signed-off-by: Piotr Sikora --- src/ngx_postgres_processor.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ngx_postgres_processor.c b/src/ngx_postgres_processor.c index 0f431550..f91852b8 100644 --- a/src/ngx_postgres_processor.c +++ b/src/ngx_postgres_processor.c @@ -128,16 +128,19 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc, * writable event that has already appeared and will never appear * again :)" */ - if (PQstatus(pgdt->pgconn) == CONNECTION_MADE) { + if (PQstatus(pgdt->pgconn) == CONNECTION_MADE && pgxc->write->ready) { dd("re-polling on connection made"); pgrc = PQconnectPoll(pgdt->pgconn); + dd("re-polling rc:%d", (int) pgrc); if (pgrc == PGRES_POLLING_READING || pgrc == PGRES_POLLING_WRITING) { dd("returning NGX_AGAIN"); return NGX_AGAIN; } + + goto done; } #if defined(DDEBUG) && (DDEBUG) @@ -179,6 +182,7 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc, return NGX_AGAIN; } +done: /* remove connection timeout from new connection */ if (pgxc->write->timer_set) { ngx_del_timer(pgxc->write); From 77bef8e181716c8352ba672eedb126406c339350 Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Tue, 27 Dec 2011 20:50:02 +0000 Subject: [PATCH 03/32] Add nginx-style debug logging. Based on patch from Yichun Zhang (agentzh). Change-Id: If2094a3a300500b1a6710e59d148e83abf3b6da4 Signed-off-by: Piotr Sikora --- src/ngx_postgres_processor.c | 38 ++++++++++++++++++++++++++++++++++-- src/ngx_postgres_upstream.c | 9 +++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/ngx_postgres_processor.c b/src/ngx_postgres_processor.c index f91852b8..bf4a5eb7 100644 --- a/src/ngx_postgres_processor.c +++ b/src/ngx_postgres_processor.c @@ -59,6 +59,9 @@ ngx_postgres_process_events(ngx_http_request_t *r) goto failed; } + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: process events"); + switch (pgdt->state) { case state_db_connect: dd("state_db_connect"); @@ -115,6 +118,9 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc, pgrc = PQconnectPoll(pgdt->pgconn); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: polling while connecting, rc:%d", (int) pgrc); + if (pgrc == PGRES_POLLING_READING || pgrc == PGRES_POLLING_WRITING) { /* @@ -134,8 +140,16 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc, pgrc = PQconnectPoll(pgdt->pgconn); dd("re-polling rc:%d", (int) pgrc); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: re-polling while connecting, rc:%d", + (int) pgrc); + if (pgrc == PGRES_POLLING_READING || pgrc == PGRES_POLLING_WRITING) { + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: busy while connecting, rc:%d", + (int) pgrc); + dd("returning NGX_AGAIN"); return NGX_AGAIN; } @@ -178,6 +192,9 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc, } #endif /* DDEBUG */ + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: busy while connecting, rc:%d", (int) pgrc); + dd("returning NGX_AGAIN"); return NGX_AGAIN; } @@ -199,6 +216,8 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc, } dd("connected successfully"); + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: connected successfully"); pgxc->log->action = "sending query to PostgreSQL database"; pgdt->state = state_db_send_query; @@ -228,6 +247,8 @@ ngx_postgres_upstream_send_query(ngx_http_request_t *r, ngx_connection_t *pgxc, (void) ngx_cpystrn(query, pgdt->query.data, pgdt->query.len + 1); dd("sending query: %s", query); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: sending query: \"%s\"", query); if (pglcf->output_binary) { 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, ngx_add_timer(pgxc->read, r->upstream->conf->read_timeout); dd("query sent successfully"); + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: query sent successfully"); pgxc->log->action = "waiting for result from PostgreSQL database"; pgdt->state = state_db_get_result; @@ -274,11 +297,18 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc, } if (!PQconsumeInput(pgdt->pgconn)) { + ngx_log_error(NGX_LOG_ERR, pgxc->log, 0, + "postgres: failed to consume input: %s", + PQerrorMessage(pgdt->pgconn)); + dd("returning NGX_ERROR"); return NGX_ERROR; } if (PQisBusy(pgdt->pgconn)) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: busy while receiving result"); + dd("returning NGX_AGAIN"); return NGX_AGAIN; } @@ -289,7 +319,7 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc, if (res == NULL) { dd("receiving result failed"); ngx_log_error(NGX_LOG_ERR, pgxc->log, 0, - "postgres: receiving result failed: %s", + "postgres: failed to receive result: %s", PQerrorMessage(pgdt->pgconn)); dd("returning NGX_ERROR"); @@ -300,7 +330,7 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc, if ((pgrc != PGRES_COMMAND_OK) && (pgrc != PGRES_TUPLES_OK)) { dd("receiving result failed"); ngx_log_error(NGX_LOG_ERR, pgxc->log, 0, - "postgres: receiving result failed: %s: %s", + "postgres: failed to receive result: %s: %s", PQresStatus(pgrc), PQerrorMessage(pgdt->pgconn)); @@ -313,6 +343,10 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc, dd("result received successfully, cols:%d rows:%d", PQnfields(res), PQntuples(res)); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pgxc->log, 0, + "postgres: result received successfully, cols:%d rows:%d", + PQnfields(res), PQntuples(res)); + pgxc->log->action = "processing result from PostgreSQL database"; rc = ngx_postgres_process_response(r, res); diff --git a/src/ngx_postgres_upstream.c b/src/ngx_postgres_upstream.c index 1c3c00aa..6754d191 100644 --- a/src/ngx_postgres_upstream.c +++ b/src/ngx_postgres_upstream.c @@ -356,6 +356,10 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data) * internal checks in PQsetnonblocking are taking care of any * PQconnectStart failures, so we don't need to check them here. */ + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, + "postgres: connecting"); + pgdt->pgconn = PQconnectStart((const char *)connstring); if (PQsetnonblocking(pgdt->pgconn, 1) == -1) { 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) PQtrace(pgdt->pgconn, stderr); #endif + dd("connection status:%d", (int) PQstatus(pgdt->pgconn)); + /* take spot in keepalive connection pool */ pgscf->active_conns++; @@ -391,6 +397,9 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data) goto invalid; } + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, + "postgres: connection fd:%d", fd); + pgxc = pc->connection = ngx_get_connection(fd, pc->log); if (pgxc == NULL) { From 7420b8e01eb1bef4ab8845da7827edb663a94cfa Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Tue, 27 Dec 2011 20:54:46 +0000 Subject: [PATCH 04/32] Remove trailing whitespaces. Change-Id: I4c63b363cc922521eb2e48e93d1db0a4e4c0f105 Signed-off-by: Piotr Sikora --- TODO.md | 2 +- src/ngx_postgres_module.c | 4 ++-- src/ngx_postgres_processor.c | 2 +- src/ngx_postgres_rewrite.c | 2 +- src/ngx_postgres_upstream.c | 4 ++-- src/ngx_postgres_util.c | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/TODO.md b/TODO.md index 63e997d0..6548fcc1 100644 --- a/TODO.md +++ b/TODO.md @@ -15,7 +15,7 @@ Features that sooner or later will be added to `ngx_postgres`: * Use `PQescapeStringConn()` instead of `PQescapeString()`, this will require lazy-evaluation of the variables after we acquire connection, - but before we send query to the database. + but before we send query to the database. Notes: Don't break `$postgres_query`. * Cancel long-running queries using `PQcancel()`. diff --git a/src/ngx_postgres_module.c b/src/ngx_postgres_module.c index fed41647..176d828e 100644 --- a/src/ngx_postgres_module.c +++ b/src/ngx_postgres_module.c @@ -214,7 +214,7 @@ ngx_conf_enum_t ngx_postgres_requirement_options[] = { ngx_postgres_rewrite_enum_t ngx_postgres_rewrite_handlers[] = { { ngx_string("no_changes"), 0, ngx_postgres_rewrite_changes }, { ngx_string("changes"), 1, ngx_postgres_rewrite_changes }, - { ngx_string("no_rows"), 2, ngx_postgres_rewrite_rows }, + { ngx_string("no_rows"), 2, ngx_postgres_rewrite_rows }, { ngx_string("rows"), 3, ngx_postgres_rewrite_rows }, { ngx_null_string, 0, NULL } }; @@ -304,7 +304,7 @@ ngx_postgres_create_loc_conf(ngx_conf_t *cf) return NULL; } - /* + /* * set by ngx_pcalloc(): * * conf->upstream.* = 0 / NULL diff --git a/src/ngx_postgres_processor.c b/src/ngx_postgres_processor.c index bf4a5eb7..1b4dce1d 100644 --- a/src/ngx_postgres_processor.c +++ b/src/ngx_postgres_processor.c @@ -339,7 +339,7 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc, dd("returning NGX_HTTP_INTERNAL_SERVER_ERROR"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } - + dd("result received successfully, cols:%d rows:%d", PQnfields(res), PQntuples(res)); diff --git a/src/ngx_postgres_rewrite.c b/src/ngx_postgres_rewrite.c index beaef097..44f3e7ea 100644 --- a/src/ngx_postgres_rewrite.c +++ b/src/ngx_postgres_rewrite.c @@ -70,7 +70,7 @@ ngx_postgres_rewrite_changes(ngx_http_request_t *r, dd("entering"); pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module); - + if ((pgrcf->key % 2 == 0) && (pgctx->var_affected == 0)) { /* no_changes */ dd("returning"); diff --git a/src/ngx_postgres_upstream.c b/src/ngx_postgres_upstream.c index 6754d191..fd293d5b 100644 --- a/src/ngx_postgres_upstream.c +++ b/src/ngx_postgres_upstream.c @@ -211,7 +211,7 @@ ngx_postgres_upstream_init_peer(ngx_http_request_t *r, pgdt->query = query->sv; } - /* set $postgres_query */ + /* set $postgres_query */ pgctx->var_query = pgdt->query; dd("returning NGX_OK"); @@ -471,7 +471,7 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data) failed: /* a bit hack-ish way to return error response (setup part) */ pc->connection = ngx_get_connection(0, pc->log); - + dd("returning NGX_AGAIN (NGX_ERROR)"); return NGX_AGAIN; #endif diff --git a/src/ngx_postgres_util.c b/src/ngx_postgres_util.c index f2894c1a..cc254cdc 100644 --- a/src/ngx_postgres_util.c +++ b/src/ngx_postgres_util.c @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2010, FRiCKLE Piotr Sikora * Copyright (c) 2009-2010, Yichun Zhang * Copyright (C) 2002-2010, Igor Sysoev From 9b96044829bfe14b6b2f398091b91d30babfbf5f Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Wed, 28 Dec 2011 00:30:17 +0000 Subject: [PATCH 05/32] Update nginx compatibility list. Change-Id: I57ad737fc66ae7a7f76e678d1012fc8d0921a74e Signed-off-by: Piotr Sikora --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3d154d60..f21043d6 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,11 @@ Status This module is production-ready and it's compatible with following nginx releases: -- 0.7.x (tested with 0.7.60 to 0.7.68), -- 0.8.x (tested with 0.8.0 to 0.8.54). -- 1.0.x (tested with 1.0.0 to 1.0.2). +- 0.7.x (tested with 0.7.60 to 0.7.69), +- 0.8.x (tested with 0.8.0 to 0.8.55), +- 0.9.x (tested with 0.9.0 to 0.9.7), +- 1.0.x (tested with 1.0.0 to 1.0.11), +- 1.1.x (tested with 1.1.0 to 1.1.12). Configuration directives From 85b52b3cd87648c73778e442267e1543552eeddf Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Wed, 28 Dec 2011 17:51:31 +0000 Subject: [PATCH 06/32] Update CHANGES (releng for 0.9). Change-Id: I411c8bd5b24b8e5a4b7f26f84b8071ea02a16ccf Signed-off-by: Piotr Sikora --- CHANGES | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/CHANGES b/CHANGES index df52b7a8..18e6ea86 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,36 @@ +2011-12-27 VERSION 0.9 + * Improve debug logging. + From Yichun Zhang (agentzh). + +2011-12-23 + * Fix compatibility with poll, select and /dev/poll event models. + Reported by Yichun Zhang (agentzh). + +2011-11-10 + * Fix compatibility with PostgreSQL 9.x. + Reported by Yichun Zhang (agentzh). + + * Fix compatibility with nginx-1.1.4+. + From Yichun Zhang (agentzh). + +2011-06-21 + * Enforce writing of proper SQL queries by replacing "row " + output format with "text" and returning whole result-set with + values separated by newlines when using "postgres_output" + directive. + + * Enforce writing of proper SQL queries by requiring result-set + to contain exactly single value when using "binary_value" or + "value" output formats when using "postgres_output" directive. + +2011-06-17 + * Fix "duplicated last chunk" issue. + Reported by Silly Sad, diagnosed by Maxim Dounin. + + * Improve build-time PostgreSQL client library discovery process + by using PostgreSQL's pg_config. + Patch from Silly Sad. + 2010-12-23 VERSION 0.8 * Add option to return content in binary format using "binary_value" output format in "postgres_output" directive. From 7275ca4d49ad7ea1847fa701bf96346ab9836e55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?agentzh=20=28=E7=AB=A0=E4=BA=A6=E6=98=A5=29?= Date: Thu, 2 Feb 2012 12:31:44 +0800 Subject: [PATCH 07/32] fixed a source line exceeding 80 cols. --- src/ngx_postgres_output.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ngx_postgres_output.c b/src/ngx_postgres_output.c index 9fd96bc0..71e4d8dd 100644 --- a/src/ngx_postgres_output.c +++ b/src/ngx_postgres_output.c @@ -178,7 +178,8 @@ ngx_postgres_output_text(ngx_http_request_t *r, PGresult *res) } else { size = PQgetlength(res, row, col); if (size) { - b->last = ngx_copy(b->last, PQgetvalue(res, row, col), size); + b->last = ngx_copy(b->last, PQgetvalue(res, row, col), + size); } } From 4b0c753ee3593ab1512e761f986962210efa470b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?agentzh=20=28=E7=AB=A0=E4=BA=A6=E6=98=A5=29?= Date: Thu, 26 Apr 2012 22:27:39 +0800 Subject: [PATCH 08/32] updated valgrind.suppress for linux i386. --- valgrind.suppress | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/valgrind.suppress b/valgrind.suppress index dc47c735..ac1101e1 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -1,3 +1,25 @@ +{ + + Memcheck:Param + epoll_ctl(event) + fun:epoll_ctl +} +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_event_process_init + fun:ngx_single_process_cycle + fun:(below main) +} +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:(below main) +} { Memcheck:Leak From 948e9992e516c18ef3d93f2cd5f9b6005813f46e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?agentzh=20=28=E7=AB=A0=E4=BA=A6=E6=98=A5=29?= Date: Fri, 27 Apr 2012 12:48:47 +0800 Subject: [PATCH 09/32] updated valgrind.suppress for mockeagain (R). --- valgrind.suppress | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/valgrind.suppress b/valgrind.suppress index dc47c735..80d5c2c1 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -1,3 +1,12 @@ +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_calloc + fun:ngx_event_process_init + fun:ngx_single_process_cycle +} { Memcheck:Leak From e5d385c6e3c33b2ac38993226c417f58eabfe659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?agentzh=20=28=E7=AB=A0=E4=BA=A6=E6=98=A5=29?= Date: Fri, 1 Jun 2012 21:19:58 +0800 Subject: [PATCH 10/32] updated valgrind.suppress for the "hup reload" + valgrind/memcheck testing mode. --- valgrind.suppress | 50 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/valgrind.suppress b/valgrind.suppress index 762f9f4c..88fd8a9f 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -1,3 +1,40 @@ +{ + +Memcheck:Addr4 +fun:ngx_init_cycle +fun:ngx_master_process_cycle +fun:main +} +{ + +Memcheck:Param +socketcall.sendmsg(msg.msg_iov[i]) +fun:__sendmsg_nocancel +fun:ngx_write_channel +fun:ngx_signal_worker_processes +fun:ngx_master_process_cycle +fun:main +} +{ + +Memcheck:Param +socketcall.sendmsg(msg.msg_iov[i]) +fun:__sendmsg_nocancel +fun:ngx_write_channel +fun:ngx_master_process_cycle +fun:main +} +{ + +Memcheck:Param +socketcall.sendmsg(msg.msg_iov[i]) +fun:__sendmsg_nocancel +fun:ngx_write_channel +fun:ngx_pass_open_channel +fun:ngx_start_worker_processes +fun:ngx_master_process_cycle +fun:main +} { Memcheck:Param @@ -10,8 +47,6 @@ fun:malloc fun:ngx_alloc fun:ngx_event_process_init - fun:ngx_single_process_cycle - fun:(below main) } { @@ -27,7 +62,6 @@ fun:ngx_alloc fun:ngx_calloc fun:ngx_event_process_init - fun:ngx_single_process_cycle } { @@ -171,8 +205,6 @@ fun:malloc fun:ngx_alloc fun:ngx_event_process_init - fun:ngx_single_process_cycle - fun:main } { nginx-core-crc32-init @@ -193,8 +225,6 @@ fun:ngx_http_init_request fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { palloc_large_for_create_temp_buf @@ -207,8 +237,6 @@ fun:ngx_http_init_request fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { accept_create_pool @@ -220,8 +248,6 @@ fun:ngx_event_accept fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { create_pool_for_init_req @@ -233,8 +259,6 @@ fun:ngx_http_init_request fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { create_pool_posix_memalign From 5c37d2db14fdaa61661121073afee4a68be50815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?agentzh=20=28=E7=AB=A0=E4=BA=A6=E6=98=A5=29?= Date: Sat, 2 Jun 2012 10:34:19 +0800 Subject: [PATCH 11/32] updated valgrind.suppress further for linux i386. --- valgrind.suppress | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/valgrind.suppress b/valgrind.suppress index 88fd8a9f..5e687fe6 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -1,5 +1,15 @@ { +Memcheck:Param +socketcall.sendmsg(msg.msg_iov[i]) +fun:sendmsg +fun:ngx_write_channel +fun:ngx_signal_worker_processes +fun:ngx_master_process_cycle +fun:main +} +{ + Memcheck:Addr4 fun:ngx_init_cycle fun:ngx_master_process_cycle From 714081d7f726ea98e406bdf61fbd16d6e77e9994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?agentzh=20=28=E7=AB=A0=E4=BA=A6=E6=98=A5=29?= Date: Thu, 21 Jun 2012 23:05:22 +0800 Subject: [PATCH 12/32] bugfix: memory leak might happen if nginx 1.1.14+ is used *and* (at least) libpq fails to connect to the remote database. --- src/ngx_postgres_upstream.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ngx_postgres_upstream.c b/src/ngx_postgres_upstream.c index fd293d5b..b952cacd 100644 --- a/src/ngx_postgres_upstream.c +++ b/src/ngx_postgres_upstream.c @@ -30,6 +30,7 @@ #define DDEBUG 0 #endif +#include #include "ngx_postgres_ddebug.h" #include "ngx_postgres_module.h" #include "ngx_postgres_keepalive.h" @@ -564,6 +565,12 @@ ngx_postgres_upstream_free_connection(ngx_log_t *log, ngx_connection_t *c, rev->closed = 1; wev->closed = 1; +#if defined(nginx_version) && (nginx_version >= 1001004) + if (c->pool) { + ngx_destroy_pool(c->pool); + } +#endif + ngx_free_connection(c); } From c8aa3d390f80257bd6e9c63e5fb43be79820ad8c Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Fri, 17 Aug 2012 12:09:42 -0700 Subject: [PATCH 13/32] bugfix: the "open socket #n left in connection" alerts would appear in the nginx error log file when the pg connection pool was used and the worker process was shutting down. this issue could be captured by running t/sanity.t with the environment TEST_NGINX_USE_HUP=1. --- src/ngx_postgres_keepalive.c | 11 +++++++---- src/ngx_postgres_upstream.c | 2 ++ t/sanity.t | 24 +++++++++++++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/ngx_postgres_keepalive.c b/src/ngx_postgres_keepalive.c index f5a8a022..9f10749b 100644 --- a/src/ngx_postgres_keepalive.c +++ b/src/ngx_postgres_keepalive.c @@ -269,6 +269,10 @@ ngx_postgres_keepalive_close_handler(ngx_event_t *ev) c = ev->data; item = c->data; + if (c->close) { + goto close; + } + if (PQconsumeInput(item->pgconn) && !PQisBusy(item->pgconn)) { res = PQgetResult(item->pgconn); if (res == NULL) { @@ -283,13 +287,12 @@ ngx_postgres_keepalive_close_handler(ngx_event_t *ev) "postgres: received result on idle keepalive connection"); } +close: pgscf = item->srv_conf; - ngx_queue_remove(&item->queue); - - ngx_postgres_upstream_free_connection(ev->log, item->connection, - item->pgconn, pgscf); + ngx_postgres_upstream_free_connection(ev->log, c, item->pgconn, pgscf); + ngx_queue_remove(&item->queue); ngx_queue_insert_head(&pgscf->free, &item->queue); dd("returning"); diff --git a/src/ngx_postgres_upstream.c b/src/ngx_postgres_upstream.c index b952cacd..ceb24ded 100644 --- a/src/ngx_postgres_upstream.c +++ b/src/ngx_postgres_upstream.c @@ -572,6 +572,8 @@ ngx_postgres_upstream_free_connection(ngx_log_t *log, ngx_connection_t *c, #endif ngx_free_connection(c); + + c->fd = (ngx_socket_t) -1; } /* free spot in keepalive connection pool */ diff --git a/t/sanity.t b/t/sanity.t index 917d0edd..df9ce7b1 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket; repeat_each(2); -plan tests => repeat_each() * (blocks() * 3); +plan tests => repeat_each() * (blocks() * 5); $ENV{TEST_NGINX_POSTGRESQL_HOST} ||= '127.0.0.1'; $ENV{TEST_NGINX_POSTGRESQL_PORT} ||= 5432; @@ -69,6 +69,9 @@ Content-Type: application/x-resty-dbd-stream "bob". # field data "\x{00}" # row list terminator --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -115,6 +118,9 @@ Content-Type: application/x-resty-dbd-stream "bob". # field data "\x{00}" # row list terminator --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -142,6 +148,10 @@ Content-Type: application/x-resty-dbd-stream "\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}". # insert id "\x{00}\x{00}" # col count --- timeout: 10 +--- no_error_log +[alert] +[error] +--- LAST @@ -178,6 +188,9 @@ Content-Type: application/x-resty-dbd-stream "name". # col name data "\x{00}" # row list terminator --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -206,6 +219,9 @@ Content-Type: application/x-resty-dbd-stream "\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}". # insert id "\x{00}\x{00}" # col count --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -224,6 +240,9 @@ Content-Type: application/x-resty-dbd-stream --- response_body eval "" --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -275,3 +294,6 @@ Content-Type: application/x-resty-dbd-stream "bob". # field data "\x{00}" # row list terminator --- timeout: 10 +--- no_error_log +[alert] +[error] From 8d974d0a52ce3b43bc628b32ed94b5d7a8feeb3c Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Fri, 17 Aug 2012 12:11:43 -0700 Subject: [PATCH 14/32] removed the useless http-cache related code from ngx_postgres_upstream_finalize_request to suppress clang warnings. --- src/ngx_postgres_util.c | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/ngx_postgres_util.c b/src/ngx_postgres_util.c index cc254cdc..11b27979 100644 --- a/src/ngx_postgres_util.c +++ b/src/ngx_postgres_util.c @@ -120,34 +120,6 @@ ngx_postgres_upstream_finalize_request(ngx_http_request_t *r, u->pipe->temp_file->file.fd); } -#if (NGX_HTTP_CACHE) - - if (u->cacheable && r->cache) { - time_t valid; - - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, - "http upstream cache fd: %d", - r->cache->file.fd); - - if (rc == NGX_HTTP_BAD_GATEWAY || rc == NGX_HTTP_GATEWAY_TIME_OUT) { - - valid = ngx_http_file_cache_valid(u->conf->cache_valid, rc); - - if (valid) { - r->cache->valid_sec = ngx_time() + valid; - r->cache->error = rc; - } - } - -# if defined(nginx_version) && (nginx_version >= 8047) - ngx_http_file_cache_free(r->cache, u->pipe->temp_file); -# else - ngx_http_file_cache_free(r, u->pipe->temp_file); -# endif - } - -#endif - if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE)) { From b72f87b464ec4d37d37b9e8c71aff84e159dd22f Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Fri, 17 Aug 2012 12:12:39 -0700 Subject: [PATCH 15/32] docs: confirmed that we work with 1.2.3 and 1.3.4. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f21043d6..c7b34eba 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ releases: - 0.9.x (tested with 0.9.0 to 0.9.7), - 1.0.x (tested with 1.0.0 to 1.0.11), - 1.1.x (tested with 1.1.0 to 1.1.12). +- 1.2.x (tested with 1.2.3 to 1.2.3). +- 1.3.x (tested with 1.3.4 to 1.3.4). Configuration directives From 1e2a13a8b95e2359661b71ab56eadbb6be914831 Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Thu, 6 Sep 2012 14:57:59 -0700 Subject: [PATCH 16/32] suppressed new false positives from valgrind. --- valgrind.suppress | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/valgrind.suppress b/valgrind.suppress index 5e687fe6..e1942824 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -1,3 +1,43 @@ +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_create_pool + fun:ngx_event_accept + fun:ngx_epoll_process_events + fun:ngx_process_events_and_timers + fun:ngx_single_process_cycle + fun:main +} +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_create_pool + fun:ngx_http_init_request + fun:ngx_epoll_process_events + fun:ngx_process_events_and_timers + fun:ngx_single_process_cycle + fun:main +} +{ + +Memcheck:Leak +fun:malloc +fun:ngx_alloc +fun:ngx_malloc +fun:ngx_pnalloc +} +{ + +Memcheck:Leak +fun:malloc +fun:ngx_alloc +fun:ngx_malloc +fun:ngx_palloc +} { Memcheck:Param From 0ce9d14c02419d2a9ccaf34d1199a84fb93fba19 Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Thu, 6 Sep 2012 17:30:38 -0700 Subject: [PATCH 17/32] ignored one more false positive from valgrind memcheck. --- valgrind.suppress | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/valgrind.suppress b/valgrind.suppress index e1942824..9cb653bc 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -1,3 +1,15 @@ +{ + +Memcheck:Leak +fun:malloc +fun:ngx_alloc +fun:ngx_create_pool +fun:ngx_http_init_request +fun:ngx_event_process_posted +fun:ngx_process_events_and_timers +fun:ngx_single_process_cycle +fun:main +} { Memcheck:Leak From 6b024edbfe3b5dfe392872d17af8c73e2d783661 Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Thu, 6 Sep 2012 20:00:11 -0700 Subject: [PATCH 18/32] ignored another false positive from valgrind memcheck. --- valgrind.suppress | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/valgrind.suppress b/valgrind.suppress index 9cb653bc..224b1c4f 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -1,3 +1,15 @@ +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_create_pool + fun:ngx_event_accept + fun:ngx_event_process_posted + fun:ngx_process_events_and_timers + fun:ngx_single_process_cycle + fun:main +} { Memcheck:Leak From 3888057993ce94c82782a5d93fd8652e335e6690 Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Fri, 7 Sep 2012 14:36:08 -0700 Subject: [PATCH 19/32] suppressed more false positivies from valgrind memcheck for linux i386. --- valgrind.suppress | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/valgrind.suppress b/valgrind.suppress index 224b1c4f..644996c4 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -1,3 +1,10 @@ +{ + +Memcheck:Leak +fun:malloc +fun:ngx_alloc +obj:* +} { Memcheck:Leak @@ -7,8 +14,6 @@ fun:ngx_event_accept fun:ngx_event_process_posted fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { @@ -19,8 +24,6 @@ fun:ngx_create_pool fun:ngx_http_init_request fun:ngx_event_process_posted fun:ngx_process_events_and_timers -fun:ngx_single_process_cycle -fun:main } { @@ -31,8 +34,6 @@ fun:main fun:ngx_event_accept fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { @@ -43,8 +44,6 @@ fun:main fun:ngx_http_init_request fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { From d64b331529170d013d7c8692bea07dee4c0b4dfd Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Mon, 17 Sep 2012 11:58:04 -0700 Subject: [PATCH 20/32] removed the sendmsg/ngx_channel valgrind suppression rules. --- valgrind.suppress | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/valgrind.suppress b/valgrind.suppress index 644996c4..64fd861b 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -63,51 +63,11 @@ fun:ngx_palloc } { -Memcheck:Param -socketcall.sendmsg(msg.msg_iov[i]) -fun:sendmsg -fun:ngx_write_channel -fun:ngx_signal_worker_processes -fun:ngx_master_process_cycle -fun:main -} -{ - Memcheck:Addr4 fun:ngx_init_cycle fun:ngx_master_process_cycle fun:main } -{ - -Memcheck:Param -socketcall.sendmsg(msg.msg_iov[i]) -fun:__sendmsg_nocancel -fun:ngx_write_channel -fun:ngx_signal_worker_processes -fun:ngx_master_process_cycle -fun:main -} -{ - -Memcheck:Param -socketcall.sendmsg(msg.msg_iov[i]) -fun:__sendmsg_nocancel -fun:ngx_write_channel -fun:ngx_master_process_cycle -fun:main -} -{ - -Memcheck:Param -socketcall.sendmsg(msg.msg_iov[i]) -fun:__sendmsg_nocancel -fun:ngx_write_channel -fun:ngx_pass_open_channel -fun:ngx_start_worker_processes -fun:ngx_master_process_cycle -fun:main -} { Memcheck:Param From 4cfa7f7a25bdc8a60ab9610f0d83f718a7b48fd0 Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Wed, 5 Dec 2012 18:01:38 -0800 Subject: [PATCH 21/32] updated valgrind.suppress for linux i386. --- valgrind.suppress | 1 - 1 file changed, 1 deletion(-) diff --git a/valgrind.suppress b/valgrind.suppress index 64fd861b..c0626772 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -3,7 +3,6 @@ Memcheck:Leak fun:malloc fun:ngx_alloc -obj:* } { From 4ab3c76e91607a05bce6fbaf0b989de275109455 Mon Sep 17 00:00:00 2001 From: "agentzh (Yichun Zhang)" Date: Fri, 26 Apr 2013 11:54:03 -0700 Subject: [PATCH 22/32] added error log checks in the test file 000_init.t. --- t/000_init.t | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/t/000_init.t b/t/000_init.t index ef50138b..832244e4 100644 --- a/t/000_init.t +++ b/t/000_init.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket; repeat_each(1); -plan tests => repeat_each() * blocks(); +plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_POSTGRESQL_HOST} ||= '127.0.0.1'; $ENV{TEST_NGINX_POSTGRESQL_PORT} ||= 5432; @@ -36,6 +36,8 @@ __DATA__ GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -50,6 +52,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -64,6 +68,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -78,6 +84,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -95,6 +103,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -109,6 +119,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -126,6 +138,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -140,6 +154,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -154,3 +170,5 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] From a9b4dd6ba99cbdece46f2e2d79628cdef1795183 Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Wed, 31 Jul 2013 12:31:17 -0700 Subject: [PATCH 23/32] bugfix: compilation error happened with nginx 1.5.3+ because nginx changes the ngx_sock_ntop API. thanks an0ma1ia for the report in #19. --- src/ngx_postgres_upstream.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ngx_postgres_upstream.c b/src/ngx_postgres_upstream.c index ceb24ded..34583eb9 100644 --- a/src/ngx_postgres_upstream.c +++ b/src/ngx_postgres_upstream.c @@ -103,6 +103,9 @@ ngx_postgres_upstream_init(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *uscf) } peers->peer[n].host.len = ngx_sock_ntop(peers->peer[n].sockaddr, +#if defined(nginx_version) && (nginx_version >= 1005003) + peers->peer[n].socklen, +#endif peers->peer[n].host.data, NGX_SOCKADDR_STRLEN, 0); if (peers->peer[n].host.len == 0) { From c8c22c67dbbc167321f44fc4f65634a079accc81 Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Sat, 5 Apr 2014 21:35:00 -0700 Subject: [PATCH 24/32] suppressed a valgrind false positive in libdl. --- valgrind.suppress | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/valgrind.suppress b/valgrind.suppress index c0626772..e0b8e89d 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -318,4 +318,14 @@ fun:main fun:ngx_init_cycle fun:main } - +{ + + Memcheck:Cond + fun:index + fun:expand_dynamic_string_token + fun:_dl_map_object + fun:map_doit + fun:_dl_catch_error + fun:do_preload + fun:dl_main +} From 0e5ce4a430e3eb0b1754486b15da4e61e3d8d7ce Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Thu, 31 Jul 2014 13:40:24 -0700 Subject: [PATCH 25/32] tests: removed --- LAST from t/sanity.t which was added by accident. --- t/sanity.t | 1 - 1 file changed, 1 deletion(-) diff --git a/t/sanity.t b/t/sanity.t index df9ce7b1..25c3d7ed 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -151,7 +151,6 @@ Content-Type: application/x-resty-dbd-stream --- no_error_log [alert] [error] ---- LAST From 088ab19554808d7c5cfeec50f1cb61a8924b5b62 Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Fri, 1 Aug 2014 15:58:20 -0700 Subject: [PATCH 26/32] bugfix: segmentation fault might happen in ngx_destroy_pool when debug logging was enabled in the nginx build. thanks buddy-ekb for the report in #21. --- src/ngx_postgres_keepalive.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ngx_postgres_keepalive.c b/src/ngx_postgres_keepalive.c index 9f10749b..b6bac30f 100644 --- a/src/ngx_postgres_keepalive.c +++ b/src/ngx_postgres_keepalive.c @@ -85,6 +85,9 @@ ngx_postgres_keepalive_get_peer_single(ngx_peer_connection_t *pc, c->idle = 0; c->log = pc->log; +#if defined(nginx_version) && (nginx_version >= 1001004) + c->pool->log = pc->log; +#endif c->read->log = pc->log; c->write->log = pc->log; @@ -140,6 +143,9 @@ ngx_postgres_keepalive_get_peer_multi(ngx_peer_connection_t *pc, c->idle = 0; c->log = pc->log; +#if defined(nginx_version) && (nginx_version >= 1001004) + c->pool->log = pc->log; +#endif c->read->log = pc->log; c->write->log = pc->log; @@ -235,6 +241,9 @@ ngx_postgres_keepalive_free_peer(ngx_peer_connection_t *pc, c->data = item; c->idle = 1; c->log = ngx_cycle->log; +#if defined(nginx_version) && (nginx_version >= 1001004) + c->pool->log = ngx_cycle->log; +#endif c->read->log = ngx_cycle->log; c->write->log = ngx_cycle->log; From 56502a68fa08adced4e075c3bdc924e596a7b02c Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Wed, 6 Aug 2014 16:01:48 -0700 Subject: [PATCH 27/32] tests: fixed the test case indexes in output.t. --- t/output.t | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/t/output.t b/t/output.t index a564bf86..b4ba503a 100644 --- a/t/output.t +++ b/t/output.t @@ -82,7 +82,7 @@ test -=== TEST 5: value - NULL value +=== TEST 4: value - NULL value --- http_config eval: $::http_config --- config default_type text/plain; @@ -99,7 +99,7 @@ GET /postgres -=== TEST 6: value - empty value +=== TEST 5: value - empty value --- http_config eval: $::http_config --- config default_type text/plain; @@ -116,7 +116,7 @@ GET /postgres -=== TEST 7: text - sanity +=== TEST 6: text - sanity --- http_config eval: $::http_config --- config default_type text/plain; @@ -143,7 +143,7 @@ Content-Type: text/plain -=== TEST 8: rds - sanity (configured) +=== TEST 7: rds - sanity (configured) --- http_config eval: $::http_config --- config location /postgres { @@ -179,7 +179,7 @@ Content-Type: application/x-resty-dbd-stream -=== TEST 9: rds - sanity (default) +=== TEST 8: rds - sanity (default) --- http_config eval: $::http_config --- config location /postgres { @@ -214,7 +214,7 @@ Content-Type: application/x-resty-dbd-stream -=== TEST 10: inheritance +=== TEST 9: inheritance --- http_config eval: $::http_config --- config default_type text/plain; @@ -235,7 +235,7 @@ test -=== TEST 11: inheritance (mixed, don't inherit) +=== TEST 10: inheritance (mixed, don't inherit) --- http_config eval: $::http_config --- config postgres_output text; @@ -256,7 +256,7 @@ GET /postgres -=== TEST 14: value - sanity (request with known extension) +=== TEST 11: value - sanity (request with known extension) --- http_config eval: $::http_config --- config default_type text/plain; @@ -277,7 +277,7 @@ test -=== TEST 15: value - bytea returned in text format +=== TEST 12: value - bytea returned in text format --- http_config eval: $::http_config --- config default_type text/plain; @@ -298,7 +298,7 @@ Content-Type: text/plain -=== TEST 16: binary value - bytea returned in binary format +=== TEST 13: binary value - bytea returned in binary format --- http_config eval: $::http_config --- config default_type text/plain; @@ -319,7 +319,7 @@ Content-Type: text/plain -=== TEST 17: binary value - int2 returned in binary format +=== TEST 14: binary value - int2 returned in binary format --- http_config eval: $::http_config --- config default_type text/plain; @@ -340,7 +340,7 @@ Content-Type: text/plain -=== TEST 18: value - "if" pseudo-location +=== TEST 15: value - "if" pseudo-location --- http_config eval: $::http_config --- config default_type text/plain; @@ -366,7 +366,7 @@ Content-Type: text/plain -=== TEST 19: text - NULL value +=== TEST 16: text - NULL value --- http_config eval: $::http_config --- config default_type text/plain; @@ -393,7 +393,7 @@ Content-Type: text/plain -=== TEST 20: text - empty result +=== TEST 17: text - empty result --- http_config eval: $::http_config --- config default_type text/plain; @@ -414,7 +414,7 @@ Content-Type: text/plain -=== TEST 21: value - empty result +=== TEST 18: value - empty result --- http_config eval: $::http_config --- config default_type text/plain; @@ -431,7 +431,7 @@ GET /postgres -=== TEST 22: value - too many values +=== TEST 19: value - too many values --- http_config eval: $::http_config --- config default_type text/plain; From 1d8e00790d4c1563d4fe6db05614cdd51f19d3cc Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Mon, 18 Aug 2014 13:10:07 -0700 Subject: [PATCH 28/32] fixed coding style around labels. --- src/ngx_postgres_escape.c | 1 + src/ngx_postgres_keepalive.c | 1 + src/ngx_postgres_module.c | 1 + src/ngx_postgres_output.c | 1 + src/ngx_postgres_processor.c | 2 ++ src/ngx_postgres_upstream.c | 4 ++++ 6 files changed, 10 insertions(+) diff --git a/src/ngx_postgres_escape.c b/src/ngx_postgres_escape.c index 3b589741..dd781947 100644 --- a/src/ngx_postgres_escape.c +++ b/src/ngx_postgres_escape.c @@ -90,6 +90,7 @@ ngx_postgres_escape_string(ngx_http_script_engine_t *e) dd("returning"); done: + v->valid = 1; v->no_cacheable = 0; v->not_found = 0; diff --git a/src/ngx_postgres_keepalive.c b/src/ngx_postgres_keepalive.c index b6bac30f..6575338c 100644 --- a/src/ngx_postgres_keepalive.c +++ b/src/ngx_postgres_keepalive.c @@ -297,6 +297,7 @@ ngx_postgres_keepalive_close_handler(ngx_event_t *ev) } close: + pgscf = item->srv_conf; ngx_postgres_upstream_free_connection(ev->log, c, item->pgconn, pgscf); diff --git a/src/ngx_postgres_module.c b/src/ngx_postgres_module.c index 176d828e..365de007 100644 --- a/src/ngx_postgres_module.c +++ b/src/ngx_postgres_module.c @@ -872,6 +872,7 @@ ngx_postgres_conf_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) pgrcf->handler = e[i].handler; found: + if (cf->args->nelts == 3) { /* default rewrite */ dd("default rewrite"); diff --git a/src/ngx_postgres_output.c b/src/ngx_postgres_output.c index 71e4d8dd..81a0b275 100644 --- a/src/ngx_postgres_output.c +++ b/src/ngx_postgres_output.c @@ -260,6 +260,7 @@ ngx_postgres_output_rds(ngx_http_request_t *r, PGresult *res) } done: + last->next = NULL; /* set output response */ diff --git a/src/ngx_postgres_processor.c b/src/ngx_postgres_processor.c index 1b4dce1d..d25c0541 100644 --- a/src/ngx_postgres_processor.c +++ b/src/ngx_postgres_processor.c @@ -103,6 +103,7 @@ ngx_postgres_process_events(ngx_http_request_t *r) return; failed: + ngx_postgres_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); dd("returning"); @@ -200,6 +201,7 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc, } done: + /* remove connection timeout from new connection */ if (pgxc->write->timer_set) { ngx_del_timer(pgxc->write); diff --git a/src/ngx_postgres_upstream.c b/src/ngx_postgres_upstream.c index 34583eb9..adbc7a40 100644 --- a/src/ngx_postgres_upstream.c +++ b/src/ngx_postgres_upstream.c @@ -222,6 +222,7 @@ ngx_postgres_upstream_init_peer(ngx_http_request_t *r, return NGX_OK; failed: + #if defined(nginx_version) && (nginx_version >= 8017) dd("returning NGX_ERROR"); return NGX_ERROR; @@ -460,10 +461,12 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data) return NGX_AGAIN; bad_add: + ngx_log_error(NGX_LOG_ERR, pc->log, 0, "postgres: failed to add nginx connection"); invalid: + ngx_postgres_upstream_free_connection(pc->log, pc->connection, pgdt->pgconn, pgscf); @@ -473,6 +476,7 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data) #else failed: + /* a bit hack-ish way to return error response (setup part) */ pc->connection = ngx_get_connection(0, pc->log); From f9479fbebe09a7e24ed28833033a0406c4a6f000 Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Wed, 17 Sep 2014 12:50:37 -0700 Subject: [PATCH 29/32] bugfix: fixed compilation error with nginx 1.7.5+ because nginx 1.7.5+ changes the API in the events subsystem. --- src/ngx_postgres_upstream.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ngx_postgres_upstream.c b/src/ngx_postgres_upstream.c index adbc7a40..919029b0 100644 --- a/src/ngx_postgres_upstream.c +++ b/src/ngx_postgres_upstream.c @@ -561,11 +561,19 @@ ngx_postgres_upstream_free_connection(ngx_log_t *log, ngx_connection_t *c, } } +#if defined(nginx_version) && nginx_version >= 1007005 + if (rev->posted) { +#else if (rev->prev) { +#endif ngx_delete_posted_event(rev); } +#if defined(nginx_version) && nginx_version >= 1007005 + if (wev->posted) { +#else if (wev->prev) { +#endif ngx_delete_posted_event(wev); } From 49855a037607c89426b59f54ae2d3049dc5d1c74 Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Thu, 2 Apr 2015 12:34:16 -0700 Subject: [PATCH 30/32] bugfix: fixed compilation failures with nginx 1.7.11+ configured with --with-threads. --- src/ngx_postgres_ddebug.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ngx_postgres_ddebug.h b/src/ngx_postgres_ddebug.h index 7cc05ecd..38253fca 100644 --- a/src/ngx_postgres_ddebug.h +++ b/src/ngx_postgres_ddebug.h @@ -29,6 +29,7 @@ #ifndef _NGX_POSTGRES_DDEBUG_H_ #define _NGX_POSTGRES_DDEBUG_H_ +#include #include #if defined(DDEBUG) && (DDEBUG) From 04e33d179be93c4b7ecd0b1e7d461724262548da Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Tue, 16 Jun 2015 20:52:46 +0800 Subject: [PATCH 31/32] bugfix: use ngx_abs() instead of abs() to fix one clang warning (-Wabsolute-value). --- src/ngx_postgres_output.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_postgres_output.c b/src/ngx_postgres_output.c index 81a0b275..9fa5481c 100644 --- a/src/ngx_postgres_output.c +++ b/src/ngx_postgres_output.c @@ -549,7 +549,7 @@ ngx_postgres_output_chain(ngx_http_request_t *r, ngx_chain_t *cl) pglcf = ngx_http_get_module_loc_conf(r, ngx_postgres_module); pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module); - r->headers_out.status = pgctx->status ? abs(pgctx->status) + r->headers_out.status = pgctx->status ? ngx_abs(pgctx->status) : NGX_HTTP_OK; if (pglcf->output_handler == &ngx_postgres_output_rds) { From 7950a250e9bc99967d83c57fbbadb9d0d6d5c4bf Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Sat, 4 Jul 2015 15:24:05 +0800 Subject: [PATCH 32/32] feature: fixed compilation errors with nginx 1.9.1+. thanks Vadim A. Misbakh-Soloviov for the original patch in #38. --- src/ngx_postgres_util.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ngx_postgres_util.c b/src/ngx_postgres_util.c index 11b27979..ae7de38c 100644 --- a/src/ngx_postgres_util.c +++ b/src/ngx_postgres_util.c @@ -45,7 +45,9 @@ void ngx_postgres_upstream_finalize_request(ngx_http_request_t *r, ngx_http_upstream_t *u, ngx_int_t rc) { +#if defined(nginx_version) && (nginx_version < 1009001) ngx_time_t *tp; +#endif dd("entering"); @@ -61,10 +63,15 @@ ngx_postgres_upstream_finalize_request(ngx_http_request_t *r, u->resolved->ctx = NULL; } +#if defined(nginx_version) && (nginx_version >= 1009001) + if (u->state && u->state->response_time) { + u->state->response_time = ngx_current_msec - u->state->response_time; +#else if (u->state && u->state->response_sec) { tp = ngx_timeofday(); u->state->response_sec = tp->sec - u->state->response_sec; u->state->response_msec = tp->msec - u->state->response_msec; +#endif if (u->pipe) { u->state->response_length = u->pipe->read_length;