[pgpool-hackers: 530] Re: penultimate patch for exception and memory manager ( EXCEPTION_MGR branch)

Muhammad Usama m.usama at gmail.com
Thu May 22 18:32:17 JST 2014


Sorry  gram.c mistakenly ended up in the diff file, it was not intended.
Please find the updated patch

Thanks
--Usama


On Wed, May 21, 2014 at 7:06 PM, Tatsuo Ishii <ishii at postgresql.org> wrote:

> > Oh I see it now. Thanks. Continue to look at rest of patches...
>
> I noticed that you patched to gram.c. Because gram.c is geneated from
> gram.y by bison, you should not patch to gram.c. Can you please patch
> to gram.y instead?
>
> Best regards,
> --
> Tatsuo Ishii
> SRA OSS, Inc. Japan
> English: http://www.sraoss.co.jp/index_en.php
> Japanese: http://www.sraoss.co.jp
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.sraoss.jp/pipermail/pgpool-hackers/attachments/20140522/f4192b1f/attachment.html>
-------------- next part --------------
diff --git a/src/context/pool_query_context.c b/src/context/pool_query_context.c
index caa4d3a..701a479 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,8 @@ 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 61d8447..f845f7e 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 0dc709f..a341644 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 bbe0099..08875f1 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);
 
@@ -2124,21 +2125,17 @@ static POOL_CONNECTION_POOL_SLOT*
     POOL_CONNECTION_POOL_SLOT   *s = NULL;
     POOL_CONNECTION *con;
 	POOL_SELECT_RESULT *res;
-
     BackendInfo *bkinfo = pool_get_node_info(backend_no);
+
     *is_standby = false;
-    PG_TRY();
-    {
-        
-        s = make_persistent_db_connection(bkinfo->backend_hostname,
+
+    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;
-        }
+    if (s)
+    {
         con = s->con;
         do_query(con, "SELECT pg_is_in_recovery()",
 						  &res, PROTO_MAJOR_V3);
@@ -2164,12 +2161,6 @@ static POOL_CONNECTION_POOL_SLOT*
         free_select_result(res);
         discard_persistent_db_connection(s);
     }
-    PG_CATCH();
-    {
-        EmitErrorReport();
-        FlushErrorState();
-    }
-    PG_END_TRY();
     return s;
 }
 
diff --git a/src/parallel_query/pool_rewrite_query.c b/src/parallel_query/pool_rewrite_query.c
index 6adaa19..d4d70dc 100644
--- a/src/parallel_query/pool_rewrite_query.c
+++ b/src/parallel_query/pool_rewrite_query.c
@@ -348,6 +348,7 @@ int IsSelectpgcatalog(Node *node,POOL_CONNECTION_POOL *backend)
  */
 RewriteQuery *rewrite_query_stmt(Node *node,POOL_CONNECTION *frontend,POOL_CONNECTION_POOL *backend,RewriteQuery *message)
 {
+    MemoryContext oldContext = CurrentMemoryContext;
     PG_TRY();
     {
         switch(node->type)
@@ -471,6 +472,7 @@ RewriteQuery *rewrite_query_stmt(Node *node,POOL_CONNECTION *frontend,POOL_CONNE
     PG_CATCH();
     {
         message->status= POOL_END;
+        MemoryContextSwitchTo(oldContext);
         FlushErrorState();
     }
     PG_END_TRY();
diff --git a/src/parser/gram.y b/src/parser/gram.y
index e72ee72..0147e01 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 2528933..ee6af25 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();
-	return yyextra.parsetree;
+    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 d7a53d8..563e2cd 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;
@@ -1540,6 +1546,33 @@ POOL_CONNECTION_POOL_SLOT *make_persistent_db_connection(
 }
 
 /*
+ * 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 f479626..9e65407 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)));
             }
         }
 
@@ -4449,13 +4449,13 @@ int pool_extract_error_message(bool read_kind, POOL_CONNECTION *backend, int maj
     }
     PG_CATCH();
     {
+        MemoryContextSwitchTo(oldContext);
 		FlushErrorState();
-        return -1;
+        ret = -1;
     }
     PG_END_TRY();
     
-
-	return 1;
+	return ret;
 }
 
 /*
@@ -4735,11 +4735,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;
 		}
@@ -4769,3 +4765,6 @@ void pool_dump_valid_backend(int backend_id)
                     RAW_MODE, REAL_MASTER_NODE_ID, pool_is_node_to_be_sent_in_current_query(backend_id),
                     *my_backend_status[backend_id])));
 }
+
+
+
diff --git a/src/protocol/pool_proto_modules.c b/src/protocol/pool_proto_modules.c
index 487f715..24fc137 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 8ec09b4..d824d94 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 e936571..956a7ef 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 c0dba65..37b6049 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 e145005..b838242 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)


More information about the pgpool-hackers mailing list