<div dir="ltr">Hi Ishii-San<div><br></div><div><div class="gmail_quote"><div dir="ltr">On Fri, Dec 7, 2018 at 6:30 PM Tatsuo Ishii &lt;<a href="mailto:ishii@sraoss.co.jp">ishii@sraoss.co.jp</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Usama,<br>
<br>
I think I have found the cause. The transaction in question basically<br>
does these:<br>
<br>
BEGIN<br>
:<br>
(lots of extended query messages)<br>
:<br>
parse(COMMIT)<br>
bind(COMMIT)<br>
execute(COMMIT)<br>
Sync<br>
Close prepared statement (S_14) [1]<br>
Close prepared statement (S_11) [2]<br>
BEGIN &lt;-- hang<br>
<br>
This message sequence seems to be unusual, because two close<br>
messages[1][2] are issued *after* a sync message (usually extended<br>
query protocol messages sequence including close messages are finished<br>
by a sync message). Pgpool-II expects a command complete message after<br>
the last BEGIN but unfortunately two close messages, which are<br>
responses of [1][2], arrived before it. While processing the close<br>
complete message, Pgpool-II turned off the query-in-progress flag as<br>
shown in the session_context. So read_kind_from_backend tried to read<br>
from node 1, then hangs.<br>
<br>
I was able to reproduce the hang by using attached pgproto data<br>
(pgproto.data).<br>
<br>
If the two close message were issued *before* the sync message,<br>
Pgpool-II does not hang. (see attached pgproto2.data).<br><br></blockquote><div><br></div><div>Thanks for looking into this. This one seems to be a tough one.</div><div>Apparently the application getting the hang is using the JDBC driver which is sending this</div><div>unusual message sequence.</div><div><br></div><div>Thanks</div><div>Kind Regards</div><div>Muhammad Usama</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
&gt; Hi Ishii-San<br>
&gt; <br>
&gt; Please find the attached the debug log and pgpool.conf<br>
&gt; <br>
&gt; The output of the stack trace, session context and query context is<br>
&gt; pasted below<br>
&gt; <br>
&gt; *--STACK-TRACE--*<br>
&gt; #0  0x00007f3d5d218c53 in __select_nocancel () at<br>
&gt; ../sysdeps/unix/syscall-template.S:81<br>
&gt; #1  0x000000000042e086 in pool_check_fd (cp=cp@entry=0x7f3d5efb43a0) at<br>
&gt; protocol/pool_process_query.c:696<br>
&gt; #2  0x000000000042e356 in pool_check_fd (cp=cp@entry=0x7f3d5efb43a0) at<br>
&gt; protocol/pool_process_query.c:728<br>
&gt; #3  0x000000000045fafc in pool_read (cp=0x7f3d5efb43a0,<br>
&gt; buf=buf@entry=0x7fffbc4830b5,<br>
&gt; len=len@entry=1) at utils/pool_stream.c:166<br>
&gt; #4  0x000000000043303b in read_kind_from_backend<br>
&gt; (frontend=frontend@entry=0x2314e98,<br>
&gt; backend=backend@entry=0x7f3d5efaf750,<br>
&gt; decided_kind=decided_kind@entry=0x7fffbc4834ba<br>
&gt; &quot;D&quot;)<br>
&gt;     at protocol/pool_process_query.c:3379<br>
&gt; #5  0x000000000043f402 in ProcessBackendResponse<br>
&gt; (frontend=frontend@entry=0x2314e98,<br>
&gt; backend=backend@entry=0x7f3d5efaf750, state=state@entry=0x7fffbc483524,<br>
&gt;     num_fields=num_fields@entry=0x7fffbc483522) at<br>
&gt; protocol/pool_proto_modules.c:2663<br>
&gt; #6  0x0000000000431ea7 in pool_process_query (frontend=0x2314e98,<br>
&gt; backend=0x7f3d5efaf750, reset_request=reset_request@entry=0) at<br>
&gt; protocol/pool_process_query.c:320<br>
&gt; #7  0x000000000042cd49 in do_child (fds=fds@entry=0x23134b0) at<br>
&gt; protocol/child.c:388<br>
&gt; #8  0x00000000004090a5 in fork_a_child (fds=0x23134b0, id=0) at<br>
&gt; main/pgpool_main.c:659<br>
&gt; #9  0x000000000040fb73 in PgpoolMain (discard_status=discard_status@entry=0<br>
&gt; &#39;\000&#39;, clear_memcache_oidmaps=clear_memcache_oidmaps@entry=0 &#39;\000&#39;) at<br>
&gt; main/pgpool_main.c:378<br>
&gt; #10 0x0000000000407777 in main (argc=&lt;optimized out&gt;, argv=&lt;optimized out&gt;)<br>
&gt; at main/main.c:349<br>
&gt; <br>
&gt; *--SESSION CONTEXT--*<br>
&gt; (gdb) print *session_context<br>
&gt; $4 = {process_context = 0x77c260 &lt;process_context_d&gt;, frontend = 0x2314e98,<br>
&gt; backend = 0x7f3d5efaf750, in_progress = 0 &#39;\000&#39;,<br>
&gt; doing_extended_query_message = 0 &#39;\000&#39;,<br>
&gt;   need_to_restore_where_to_send = 0 &#39;\000&#39;, where_to_send_save = &quot;\001&quot;,<br>
&gt; &#39;\000&#39; &lt;repeats 126 times&gt;, command_success = 1 &#39;\001&#39;, writing_transaction<br>
&gt; = 0 &#39;\000&#39;,<br>
&gt;   failed_transaction = 0 &#39;\000&#39;, skip_reading_from_backends = 0 &#39;\000&#39;,<br>
&gt; ignore_till_sync = 0 &#39;\000&#39;, transaction_isolation = POOL_UNKNOWN,<br>
&gt; query_context = 0x248d418,<br>
&gt;   memory_context = 0x22f2558, uncompleted_message = 0x231aeb8, message_list<br>
&gt; = {capacity = 64, size = 63, sent_messages = 0x2351f70},<br>
&gt; load_balance_node_id = 0,<br>
&gt;   mismatch_ntuples = 0 &#39;\000&#39;, ntuples = {0, -1, 0 &lt;repeats 126 times&gt;},<br>
&gt; reset_context = 0 &#39;\000&#39;, query_cache_array = 0x0, num_selects = 0,<br>
&gt; pending_messages = 0x231b020,<br>
&gt;   previous_message_exists = 0 &#39;\000&#39;, previous_message = {type =<br>
&gt; POOL_EXECUTE, contents = 0x24704f0 &quot;SS_13&quot;, contents_len = 5, query =<br>
&gt; &quot;COMMIT&quot;, &#39;\000&#39; &lt;repeats 1017 times&gt;,<br>
&gt;     statement = &quot;\000_46&quot;, &#39;\000&#39; &lt;repeats 123 times&gt;, portal = &#39;\000&#39;<br>
&gt; &lt;repeats 127 times&gt;, is_rows_returned = 0 &#39;\000&#39;, not_forward_to_frontend =<br>
&gt; 0 &#39;\000&#39;, node_ids = {0, -1},<br>
&gt;     query_context = 0x242ff68}, major = 3, minor = 0}<br>
&gt; <br>
&gt; *--QEURY-TRACE--*<br>
&gt; (gdb) print *session_context-&gt;query_context<br>
&gt; $5 = {original_query = 0x248e0e8 &quot;BEGIN&quot;, rewritten_query = 0x0,<br>
&gt; original_length = 6, rewritten_length = -1, parse_tree = 0x248e038,<br>
&gt; rewritten_parse_tree = 0x0,<br>
&gt;   where_to_send = &quot;\001&quot;, &#39;\000&#39; &lt;repeats 126 times&gt;,<br>
&gt; virtual_master_node_id = 0, query_state = {POOL_UNPARSED &lt;repeats 128<br>
&gt; times&gt;}, is_cache_safe = 0 &#39;\000&#39;, temp_cache = 0x0,<br>
&gt;   is_multi_statement = 0 &#39;\000&#39;, dboid = 0, query_w_hex = 0x0,<br>
&gt; is_parse_error = 0 &#39;\000&#39;, num_original_params = -1,<br>
&gt; pg_terminate_backend_conn = 0x0, skip_cache_commit = 0 &#39;\000&#39;,<br>
&gt;   memory_context = 0x7f3d5efbff20}<br>
&gt; <br>
&gt; Thanks<br>
&gt; Best Regards<br>
&gt; Muhammad Usama<br>
&gt; <br>
&gt; <br>
&gt; On Fri, Dec 7, 2018 at 3:49 AM Tatsuo Ishii &lt;<a href="mailto:ishii@sraoss.co.jp" target="_blank">ishii@sraoss.co.jp</a>&gt; wrote:<br>
&gt; <br>
&gt;&gt; Hi Usama,<br>
&gt;&gt;<br>
&gt;&gt; I need:<br>
&gt;&gt;<br>
&gt;&gt; - exact Pgpool-II version<br>
&gt;&gt; - pgpool.conf<br>
&gt;&gt; - gdb print command result of *pool_session_context when Pgpool-II hangs<br>
&gt;&gt;<br>
&gt;&gt; Also a full debug log will be helpful.<br>
&gt;&gt;<br>
&gt;&gt; Best regards,<br>
&gt;&gt; --<br>
&gt;&gt; Tatsuo Ishii<br>
&gt;&gt; SRA OSS, Inc. Japan<br>
&gt;&gt; English: <a href="http://www.sraoss.co.jp/index_en.php" rel="noreferrer" target="_blank">http://www.sraoss.co.jp/index_en.php</a><br>
&gt;&gt; Japanese:<a href="http://www.sraoss.co.jp" rel="noreferrer" target="_blank">http://www.sraoss.co.jp</a><br>
&gt;&gt;<br>
&gt;&gt; &gt; Hi Ishii San<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Thanks for looking into this, As discussed over the call please find the<br>
&gt;&gt; &gt; attached pgpool log and stack trace for the stuck issue.<br>
&gt;&gt; &gt; I am currently not able to trim down the test case so sorry for throwing<br>
&gt;&gt; a<br>
&gt;&gt; &gt; huge log file.<br>
&gt;&gt; &gt; There is no special configuration on PostgreSQL server side and this<br>
&gt;&gt; issue<br>
&gt;&gt; &gt; only happens when the load balance node and the primary node are same<br>
&gt;&gt; &gt; and as you can see from the stack trace the stuck happens in the<br>
&gt;&gt; &gt; read_kind_from_backend() function and the reason for that is<br>
&gt;&gt; &gt; the function is trying to read response from the backend node 1(standby)<br>
&gt;&gt; &gt; while it has only sent the query to the backend node 0 (primary)<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; --stack-trace--<br>
&gt;&gt; &gt; #0  0x00007f1775216c53 in __select_nocancel () at<br>
&gt;&gt; &gt; ../sysdeps/unix/syscall-template.S:81<br>
&gt;&gt; &gt; #1  0x000000000042def6 in pool_check_fd (cp=cp@entry=0x7f1776fb2328) at<br>
&gt;&gt; &gt; protocol/pool_process_query.c:696<br>
&gt;&gt; &gt; #2  0x000000000042e1c6 in pool_check_fd (cp=cp@entry=0x7f1776fb2328) at<br>
&gt;&gt; &gt; protocol/pool_process_query.c:728<br>
&gt;&gt; &gt; #3  0x000000000045f92c in pool_read (cp=0x7f1776fb2328,<br>
&gt;&gt; &gt; buf=buf@entry=0x7ffebb5f2345,<br>
&gt;&gt; &gt; len=len@entry=1) at utils/pool_stream.c:166<br>
&gt;&gt; &gt; #4  0x0000000000432eab in read_kind_from_backend<br>
&gt;&gt; &gt; (frontend=frontend@entry=0x2292e88,<br>
&gt;&gt; &gt; backend=backend@entry=0x7f1776fad6d8,<br>
&gt;&gt; &gt; decided_kind=decided_kind@entry=0x7ffebb5f274a<br>
&gt;&gt; &gt; &quot;D&quot;)<br>
&gt;&gt; &gt;     at protocol/pool_process_query.c:3379<br>
&gt;&gt; &gt; #5  0x000000000043f232 in ProcessBackendResponse<br>
&gt;&gt; &gt; (frontend=frontend@entry=0x2292e88,<br>
&gt;&gt; &gt; backend=backend@entry=0x7f1776fad6d8, state=state@entry=0x7ffebb5f27b4,<br>
&gt;&gt; &gt;     num_fields=num_fields@entry=0x7ffebb5f27b2) at<br>
&gt;&gt; &gt; protocol/pool_proto_modules.c:2653<br>
&gt;&gt; &gt; #6  0x0000000000431d17 in pool_process_query (frontend=0x2292e88,<br>
&gt;&gt; &gt; backend=0x7f1776fad6d8, reset_request=reset_request@entry=0) at<br>
&gt;&gt; &gt; protocol/pool_process_query.c:320<br>
&gt;&gt; &gt; #7  0x000000000042cbb9 in do_child (fds=fds@entry=0x2291370) at<br>
&gt;&gt; &gt; protocol/child.c:387<br>
&gt;&gt; &gt; #8  0x00000000004090a5 in fork_a_child (fds=0x2291370, id=2) at<br>
&gt;&gt; &gt; main/pgpool_main.c:659<br>
&gt;&gt; &gt; #9  0x000000000040fb73 in PgpoolMain (discard_status=discard_status@entry<br>
&gt;&gt; =0<br>
&gt;&gt; &gt; &#39;\000&#39;, clear_memcache_oidmaps=clear_memcache_oidmaps@entry=0 &#39;\000&#39;) at<br>
&gt;&gt; &gt; main/pgpool_main.c:378<br>
&gt;&gt; &gt; #10 0x0000000000407777 in main (argc=&lt;optimized out&gt;, argv=&lt;optimized<br>
&gt;&gt; out&gt;)<br>
&gt;&gt; &gt; at main/main.c:361<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Thanks<br>
&gt;&gt; &gt; Best Regards<br>
&gt;&gt; &gt; Muhammad Usama<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Thu, Dec 6, 2018 at 3:52 AM Tatsuo Ishii &lt;<a href="mailto:ishii@sraoss.co.jp" target="_blank">ishii@sraoss.co.jp</a>&gt; wrote:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; Hi Ishii San<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; I was investigating the issue faced by one of Pgpool-II user who<br>
&gt;&gt; reported<br>
&gt;&gt; &gt;&gt; &gt; that after executing a certain transaction (large amount of data<br>
&gt;&gt; DELETE /<br>
&gt;&gt; &gt;&gt; &gt; INSERT / UPDATE, COMMIT), the next execution of the BEGIN statement<br>
&gt;&gt; &gt;&gt; &gt; hangs(using the extended query protocol).<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; I think the problem is in pool_is_node_to_be_sent_in_current_query()<br>
&gt;&gt; &gt;&gt; &gt; function. The issue is in streaming replication mode, when<br>
&gt;&gt; &gt;&gt; &gt; pool_is_query_in_progress is not set, the function always returns<br>
&gt;&gt; true.<br>
&gt;&gt; &gt;&gt; Now<br>
&gt;&gt; &gt;&gt; &gt; consider that we have two backend servers 1 primary and one standby.<br>
&gt;&gt; And<br>
&gt;&gt; &gt;&gt; &gt; for a particular session the primary node gets selected as a load<br>
&gt;&gt; balance<br>
&gt;&gt; &gt;&gt; &gt; node. So effectively that session should only be sending queries and<br>
&gt;&gt; &gt;&gt; &gt; reading responses from primary server alone. But with the current<br>
&gt;&gt; &gt;&gt; &gt; implementation of pool_is_node_to_be_sent_in_current_query() the<br>
&gt;&gt; &gt;&gt; &gt; VALID_BACKEND macro will return true for standby node as well when the<br>
&gt;&gt; &gt;&gt; &gt; query in progress flag is not set(which is wrong when standby node is<br>
&gt;&gt; not<br>
&gt;&gt; &gt;&gt; &gt; selected for load balancing). and if this happens in let say<br>
&gt;&gt; &gt;&gt; &gt; read_kind_from_backend() function we will get a stuck, since the<br>
&gt;&gt; &gt;&gt; &gt; read_kind_from_backend() will be trying to read the response from<br>
&gt;&gt; standby<br>
&gt;&gt; &gt;&gt; &gt; node while we have never sent any query to that node.<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; Can you please take a look at the attached patch, I am not sure if it<br>
&gt;&gt; &gt;&gt; will<br>
&gt;&gt; &gt;&gt; &gt; have some side effects or not.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Unfortunately the patch would not help and probably would break many<br>
&gt;&gt; &gt;&gt; cases which are working today.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; For the scenario you mentioned is already handled by<br>
&gt;&gt; &gt;&gt; pool_pending_message_* functions defined in<br>
&gt;&gt; &gt;&gt; context/query_context.c. When a message, for example parse, is sent<br>
&gt;&gt; &gt;&gt; from frontend and forwarded to backends, the info is recorded in an<br>
&gt;&gt; &gt;&gt; FIFO queue by pool_pending_message_add() along with info which of<br>
&gt;&gt; &gt;&gt; backend the message is forwarded to. When the parse message will be<br>
&gt;&gt; &gt;&gt; replied back with &quot;parse complete message&quot; from the backend,<br>
&gt;&gt; &gt;&gt; read_kind_message_from_backend() can know from which of backend it<br>
&gt;&gt; &gt;&gt; should read a message by looking at the head of the queue.  Once the<br>
&gt;&gt; &gt;&gt; message is retrieved (in this example parse complete message), an<br>
&gt;&gt; &gt;&gt; entry in the queue (in this example parse message) is pulled out and<br>
&gt;&gt; &gt;&gt; removed.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; This mechanism usually works well unless backend sends unpredicted<br>
&gt;&gt; &gt;&gt; message. Recently one of such a case was reported:<br>
&gt;&gt; &gt;&gt; <a href="https://www.pgpool.net/mantisbt/view.php?id=448" rel="noreferrer" target="_blank">https://www.pgpool.net/mantisbt/view.php?id=448</a><br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; In the report PostgreSQL sends an error message caused by<br>
&gt;&gt; &gt;&gt; idle_in_transaction_timeout setting which Pgpool-II does not expect.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Does the user has any uncommon setting in postgresql.conf?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Best regards,<br>
&gt;&gt; &gt;&gt; --<br>
&gt;&gt; &gt;&gt; Tatsuo Ishii<br>
&gt;&gt; &gt;&gt; SRA OSS, Inc. Japan<br>
&gt;&gt; &gt;&gt; English: <a href="http://www.sraoss.co.jp/index_en.php" rel="noreferrer" target="_blank">http://www.sraoss.co.jp/index_en.php</a><br>
&gt;&gt; &gt;&gt; Japanese:<a href="http://www.sraoss.co.jp" rel="noreferrer" target="_blank">http://www.sraoss.co.jp</a><br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt;<br>
</blockquote></div></div></div>