pgpool2.git / summary / log / commit / refs

commit    ba68991798d27cfedef1b3102ee7826cfcc613d9
Author:   Muhammad Usama <m.usama@gmail.com>
Date:     Mon May 26 13:09:06 2014 +0000

    Fix some MemoryContext related problems found during testing.
    
    Remove debug prints and single line comments which were inherited
    from exception manager development branch


src/context/pool_query_context.c | 4 ++ src/include/pool.h | 2 + src/include/utils/elog.h | 1 - src/main/pgpool_main.c | 75 ++++++++++++--------------- src/parallel_query/pool_rewrite_query.c | 21 ++++---- src/parser/gram.y | 1 - src/parser/parser.c | 15 +++--- src/protocol/child.c | 39 ++++++++++++-- src/protocol/pool_process_query.c | 33 +++++------- src/protocol/pool_proto_modules.c | 3 -- src/streaming_replication/pool_worker_child.c | 15 +----- src/utils/error/elog.c | 5 +- src/utils/mmgr/aset.c | 2 - src/utils/pool_ip.c | 4 -- 14 files changed, 108 insertions(+), 112 deletions(-) diff --git a/src/context/pool_query_context.c b/src/context/pool_query_context.c index caa4d3aea..7a801150b 100644 --- a/src/context/pool_query_context.c +++ b/src/context/pool_query_context.c @@ -80,6 +80,7 @@ void pool_query_context_destroy(POOL_QUERY_CONTEXT *query_context) pool_unset_query_in_progress(); MemoryContextDelete(query_context->memory_context); pfree(query_context); + query_context->original_query = NULL; session_context->query_context = NULL; } } @@ -93,7 +94,9 @@ void pool_start_query(POOL_QUERY_CONTEXT *query_context, char *query, int len, N if (query_context) { + MemoryContext old_context; session_context = pool_get_session_context(false); + old_context = MemoryContextSwitchTo(query_context->memory_context); query_context->original_length = len; query_context->rewritten_length = -1; query_context->original_query = pstrdup(query); @@ -106,6 +109,7 @@ void pool_start_query(POOL_QUERY_CONTEXT *query_context, char *query, int len, N query_context->temp_cache = pool_create_temp_query_cache(query); pool_set_query_in_progress(); session_context->query_context = query_context; + MemoryContextSwitchTo(old_context); } } diff --git a/src/include/pool.h b/src/include/pool.h index 61d84471c..c60247749 100644 --- a/src/include/pool.h +++ b/src/include/pool.h @@ -595,6 +595,8 @@ extern POOL_STATUS OneNode_do_command(POOL_CONNECTION *frontend, POOL_CONNECTION /* child.c */ extern POOL_CONNECTION_POOL_SLOT *make_persistent_db_connection( char *hostname, int port, char *dbname, char *user, char *password, bool retry); +extern POOL_CONNECTION_POOL_SLOT *make_persistent_db_connection_noerror( + char *hostname, int port, char *dbname, char *user, char *password, bool retry); extern void discard_persistent_db_connection(POOL_CONNECTION_POOL_SLOT *cp); /* define pool_system.c */ diff --git a/src/include/utils/elog.h b/src/include/utils/elog.h index 0dc709f2c..a34164457 100644 --- a/src/include/utils/elog.h +++ b/src/include/utils/elog.h @@ -275,7 +275,6 @@ extern int errfunction(const char *funcname); extern int errposition(int cursorpos); #define pg_unreachable() exit(0) -//extern int err_generic_string(int field, const char *str); extern int geterrcode(void); extern int geterrposition(void); diff --git a/src/main/pgpool_main.c b/src/main/pgpool_main.c index bbe0099ed..c7e6aa4bd 100644 --- a/src/main/pgpool_main.c +++ b/src/main/pgpool_main.c @@ -304,6 +304,7 @@ int PgpoolMain(bool discard_status, bool clear_memcache_oidmaps) /* Since not using PG_TRY, must reset error stack by hand */ error_context_stack = NULL; EmitErrorReport(); + MemoryContextSwitchTo(TopMemoryContext); FlushErrorState(); POOL_SETMASK(&BlockSig); @@ -2121,56 +2122,46 @@ static int trigger_failover_command(int node, const char *command_line, static POOL_CONNECTION_POOL_SLOT* verify_backend_node_status(int backend_no, bool* is_standby) { - POOL_CONNECTION_POOL_SLOT *s = NULL; - POOL_CONNECTION *con; + POOL_CONNECTION_POOL_SLOT *s = NULL; + POOL_CONNECTION *con; POOL_SELECT_RESULT *res; + BackendInfo *bkinfo = pool_get_node_info(backend_no); - BackendInfo *bkinfo = pool_get_node_info(backend_no); - *is_standby = false; - PG_TRY(); - { - - s = make_persistent_db_connection(bkinfo->backend_hostname, + *is_standby = false; + + s = make_persistent_db_connection_noerror(bkinfo->backend_hostname, bkinfo->backend_port, "postgres", pool_config->sr_check_user, pool_config->sr_check_password, true); - if (!s) - { - return NULL; - } - con = s->con; - do_query(con, "SELECT pg_is_in_recovery()", + if (s) + { + con = s->con; + do_query(con, "SELECT pg_is_in_recovery()", &res, PROTO_MAJOR_V3); - if (res->numrows <= 0) - { - ereport(LOG, - (errmsg("verify_backend_node_status: do_query returns no rows"))); - } - if (res->data[0] == NULL) - { - ereport(LOG, - (errmsg("verify_backend_node_status: do_query returns no data"))); - } - if (res->nullflags[0] == -1) - { - ereport(LOG, - (errmsg("verify_backend_node_status: do_query returns NULL"))); - } - if (res->data[0] && !strcmp(res->data[0], "t")) + if (res->numrows <= 0) + { + ereport(LOG, + (errmsg("verify_backend_node_status: do_query returns no rows"))); + } + if (res->data[0] == NULL) { - *is_standby = true; - } - free_select_result(res); - discard_persistent_db_connection(s); - } - PG_CATCH(); - { - EmitErrorReport(); - FlushErrorState(); - } - PG_END_TRY(); - return s; + ereport(LOG, + (errmsg("verify_backend_node_status: do_query returns no data"))); + } + if (res->nullflags[0] == -1) + { + ereport(LOG, + (errmsg("verify_backend_node_status: do_query returns NULL"))); + } + if (res->data[0] && !strcmp(res->data[0], "t")) + { + *is_standby = true; + } + free_select_result(res); + discard_persistent_db_connection(s); + } + return s; } /* diff --git a/src/parallel_query/pool_rewrite_query.c b/src/parallel_query/pool_rewrite_query.c index 6adaa195d..8ea1c5e2a 100644 --- a/src/parallel_query/pool_rewrite_query.c +++ b/src/parallel_query/pool_rewrite_query.c @@ -348,10 +348,11 @@ int IsSelectpgcatalog(Node *node,POOL_CONNECTION_POOL *backend) */ RewriteQuery *rewrite_query_stmt(Node *node,POOL_CONNECTION *frontend,POOL_CONNECTION_POOL *backend,RewriteQuery *message) { - PG_TRY(); - { - switch(node->type) - { + MemoryContext oldContext = CurrentMemoryContext; + PG_TRY(); + { + switch(node->type) + { case T_SelectStmt: { SelectStmt *stmt = (SelectStmt *)node; @@ -468,13 +469,13 @@ RewriteQuery *rewrite_query_stmt(Node *node,POOL_CONNECTION *frontend,POOL_CONNE break; } } - PG_CATCH(); - { - message->status= POOL_END; - FlushErrorState(); - } + PG_CATCH(); + { + message->status= POOL_END; + MemoryContextSwitchTo(oldContext); + FlushErrorState(); + } PG_END_TRY(); - pool_debug("pool_rewrite_stmt: query rule %d",node->type); diff --git a/src/parser/gram.y b/src/parser/gram.y index e72ee72af..0147e0155 100644 --- a/src/parser/gram.y +++ b/src/parser/gram.y @@ -58,7 +58,6 @@ #include "nodes.h" #include "keywords.h" -//#include "pool_memory.h" #include "gramparse.h" #include "makefuncs.h" #include "pool_string.h" diff --git a/src/parser/parser.c b/src/parser/parser.c index 2528933b1..b6b10de98 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -50,10 +50,7 @@ raw_parser(const char *str) core_yyscan_t yyscanner; base_yy_extra_type yyextra; int yyresult; - -// Do we need a seperate memory context here? -// if (pool_memory == NULL) -// pool_memory = pool_memory_create(PARSER_BLOCK_SIZE); + MemoryContext oldContext = CurrentMemoryContext; parsetree = NIL; /* in case grammar forgets to set it */ @@ -72,23 +69,23 @@ raw_parser(const char *str) yyresult = base_yyparse(yyscanner); scanner_finish(yyscanner); in_parser_context = false; - if (yyresult) /* error */ - return NIL; - return yyextra.parsetree; } PG_CATCH(); { + MemoryContextSwitchTo(oldContext); scanner_finish(yyscanner); in_parser_context = false; - return NIL; /* error */ + yyresult = -1; + FlushErrorState(); } PG_END_TRY(); + if (yyresult) /* error */ + return NIL; return yyextra.parsetree; } void free_parser(void) { - //pool_memory_delete(pool_memory, 1); } /* diff --git a/src/protocol/child.c b/src/protocol/child.c index d7a53d8d8..563e2cd1c 100644 --- a/src/protocol/child.c +++ b/src/protocol/child.c @@ -217,6 +217,10 @@ void do_child(int unix_fd, int inet_fd) error_context_stack = NULL; EmitErrorReport(); + /* process the cleanup in ProcessLoopContext which will get reset + * during the next loop iteration + */ + MemoryContextSwitchTo(ProcessLoopContext); if (accepted) connection_count_down(); @@ -422,6 +426,7 @@ backend_cleanup(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL* volatile backen { if(frontend) { + MemoryContext oldContext = CurrentMemoryContext; PG_TRY(); { if(pool_process_query(frontend, backend, 1) == POOL_CONTINUE) @@ -433,6 +438,7 @@ backend_cleanup(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL* volatile backen PG_CATCH(); { /* ignore the error message */ + MemoryContextSwitchTo(oldContext); FlushErrorState(); } PG_END_TRY(); @@ -452,8 +458,8 @@ backend_cleanup(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL* volatile backen } /* -* perform accept() and return new fd -*/ + * perform accept() and return new fd + */ static POOL_CONNECTION *do_accept(int unix_fd, int inet_fd, struct timeval *timeout) { return NULL; @@ -1539,6 +1545,33 @@ POOL_CONNECTION_POOL_SLOT *make_persistent_db_connection( return cp; } +/* + * make_persistent_db_connection_noerror() is a wrapper over + * make_persistent_db_connection() which does not ereports in case of an error + */ +POOL_CONNECTION_POOL_SLOT *make_persistent_db_connection_noerror( + char *hostname, int port, char *dbname, char *user, char *password, bool retry) +{ + POOL_CONNECTION_POOL_SLOT *slot = NULL; + MemoryContext oldContext = CurrentMemoryContext; + PG_TRY(); + { + slot = make_persistent_db_connection(hostname, + port, + dbname, + user, + password, retry); + } + PG_CATCH(); + { + EmitErrorReport(); + MemoryContextSwitchTo(oldContext); + FlushErrorState(); + slot = NULL; + } + PG_END_TRY(); + return slot; +} /* * Free memory of POOL_CONNECTION_POOL_SLOT. Should only be used in * make_persistent_db_connection and discard_persistent_db_connection. @@ -2102,8 +2135,6 @@ wait_for_new_connections(int unix_fd, int inet_fd, struct timeval *timeout, Sock if (FD_ISSET(inet_fd, &rmask)) { fd = inet_fd; - //inet++; -// *inet = true; } /* * Note that some SysV systems do not work here. For those diff --git a/src/protocol/pool_process_query.c b/src/protocol/pool_process_query.c index f479626b9..411ffede3 100644 --- a/src/protocol/pool_process_query.c +++ b/src/protocol/pool_process_query.c @@ -232,11 +232,7 @@ POOL_STATUS pool_process_query(POOL_CONNECTION *frontend, } else { - write_stderr("[%d]%s():%d\n",getpid(),__FUNCTION__,__LINE__); - status = ProcessFrontendResponse(frontend, backend); - write_stderr("[%d]%s():%d\n",getpid(),__FUNCTION__,__LINE__); - if (status != POOL_CONTINUE) return status; } @@ -4365,9 +4361,11 @@ static int detect_error(POOL_CONNECTION *backend, char *error_code, int major, c int pool_extract_error_message(bool read_kind, POOL_CONNECTION *backend, int major, bool unread, char **message) { char kind; + bool ret = 1; int readlen = 0, len; - StringInfo str_buf; /* unread buffer */ + StringInfo str_buf; /* unread buffer */ StringInfo str_message_buf; /* message buffer */ + MemoryContext oldContext = CurrentMemoryContext; char *str; str_buf = makeStringInfo(); @@ -4391,7 +4389,9 @@ int pool_extract_error_message(bool read_kind, POOL_CONNECTION *backend, int maj pfree(str_message_buf); pfree(str_buf->data); pfree(str_buf); - return 0; + ereport(ERROR, + (errmsg("unable to extract error message"), + errdetail("invalid message kind \"%C\"",kind))); } } @@ -4446,16 +4446,15 @@ int pool_extract_error_message(bool read_kind, POOL_CONNECTION *backend, int maj pfree(str_message_buf); pfree(str_buf->data); pfree(str_buf); - } - PG_CATCH(); - { + } + PG_CATCH(); + { + MemoryContextSwitchTo(oldContext); FlushErrorState(); - return -1; - } - PG_END_TRY(); - - - return 1; + ret = -1; + } + PG_END_TRY(); + return ret; } /* @@ -4735,11 +4734,7 @@ SELECT_RETRY: else if (FD_ISSET(frontend->fd, &readmask)) { - write_stderr("[%d]%s():%d\n",getpid(),__FUNCTION__,__LINE__); - status = ProcessFrontendResponse(frontend, backend); - write_stderr("[%d]%s():%d\n",getpid(),__FUNCTION__,__LINE__); - if (status != POOL_CONTINUE) return status; } diff --git a/src/protocol/pool_proto_modules.c b/src/protocol/pool_proto_modules.c index 487f715df..24fc13750 100644 --- a/src/protocol/pool_proto_modules.c +++ b/src/protocol/pool_proto_modules.c @@ -2373,10 +2373,7 @@ POOL_STATUS ProcessFrontendResponse(POOL_CONNECTION *frontend, MemoryContext old_context; case 'X': /* Terminate */ - write_stderr("[%d]%s():%d\n",getpid(),__FUNCTION__,__LINE__); pfree(contents); - write_stderr("[%d]%s():%d\n",getpid(),__FUNCTION__,__LINE__); - ereport(LOG, (errmsg("Frontend terminated"), errdetail("received message kind 'X' from frontend"))); diff --git a/src/streaming_replication/pool_worker_child.c b/src/streaming_replication/pool_worker_child.c index 8ec09b456..d824d94e2 100644 --- a/src/streaming_replication/pool_worker_child.c +++ b/src/streaming_replication/pool_worker_child.c @@ -198,25 +198,12 @@ static void establish_persistent_connection(void) if (slots[i] == NULL) { - PG_TRY(); - { bkinfo = pool_get_node_info(i); - slots[i] = make_persistent_db_connection(bkinfo->backend_hostname, + slots[i] = make_persistent_db_connection_noerror(bkinfo->backend_hostname, bkinfo->backend_port, "postgres", pool_config->sr_check_user, pool_config->sr_check_password, true); - } - PG_CATCH(); - { - ErrorData *edata; - edata = CopyErrorData(); - write_stderr("%s",edata->message); - FlushErrorState(); - slots[i] = NULL; - } - PG_END_TRY(); - } } } diff --git a/src/utils/error/elog.c b/src/utils/error/elog.c index e9365715d..956a7ef63 100644 --- a/src/utils/error/elog.c +++ b/src/utils/error/elog.c @@ -514,7 +514,6 @@ errfinish(int dummy,...) * can stop a query emitting tons of notice or warning messages, even if * it's in a loop that otherwise fails to check for interrupts. */ - //CHECK_FOR_INTERRUPTS(); } @@ -1181,7 +1180,7 @@ CopyErrorData(void) newedata->internalquery = pstrdup(newedata->internalquery); /* Use the calling context for string allocation */ -// newedata->assoc_context = CurrentMemoryContext; + newedata->assoc_context = CurrentMemoryContext; return newedata; } @@ -1394,7 +1393,7 @@ GetErrorContextStack(void) * Set up assoc_context to be the caller's context, so any allocations * done (which will include edata->context) will use their context. */ -// edata->assoc_context = CurrentMemoryContext; + edata->assoc_context = CurrentMemoryContext; /* * Call any context callback functions to collect the context information diff --git a/src/utils/mmgr/aset.c b/src/utils/mmgr/aset.c index c0dba6593..37b604937 100644 --- a/src/utils/mmgr/aset.c +++ b/src/utils/mmgr/aset.c @@ -84,8 +84,6 @@ *------------------------------------------------------------------------- */ -//#include "postgres.h" - #include "pool_type.h" #include "utils/palloc.h" #include "utils/memdebug.h" diff --git a/src/utils/pool_ip.c b/src/utils/pool_ip.c index e14500556..b83824223 100644 --- a/src/utils/pool_ip.c +++ b/src/utils/pool_ip.c @@ -168,7 +168,6 @@ getnameinfo_all(const struct sockaddr_storage * addr, int salen, node, nodelen, service, servicelen, flags); - printf("***** getnameinfo_unix() AF_UNIX\n"); } else { @@ -176,12 +175,9 @@ getnameinfo_all(const struct sockaddr_storage * addr, int salen, node, nodelen, service, servicelen, flags); - printf("***** getnameinfo() OTHERS\n"); } - printf("***** addr->ss_family = %u\n",(unsigned )addr->ss_family); - if (rc != 0) { if (node) [parent: c7bb34b61a0f]