Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsPyB-0004YJ-2y for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Jul 2019 11:07:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hsPy8-0007ql-Pw for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Jul 2019 11:07:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsPy7-0007q0-Ln for pgsql-hackers@lists.postgresql.org; Tue, 30 Jul 2019 11:06:59 +0000 Received: from cyclops.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1hsPy4-00053c-08 for pgsql-hackers@postgresql.org; Tue, 30 Jul 2019 11:06:58 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 9975C21C4034; Tue, 30 Jul 2019 14:06:43 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.200] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 4850021C3EFC; Tue, 30 Jul 2019 14:06:43 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1564484803; bh=aWkOmCqWhXZKHXh2XpIP5NZN227MyKg4DsEiDE7eDDI=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=QYRKkUQhfXQXpqo630GQm4bwjYEagwtI2u0rRhMMHfwNdG1p0tGTaGxFbU8rsz26w KwUr6WA94pLkd0izxlznb1z5LWhnSdnJgUWVIca+9yEJjpZgeEK2X2xyCZ5SKk/KNK nDTB5kFvs8srOvQjZEwXqCM0+L1GaMGVaEu07DXY= Subject: Re: Built-in connection pooler To: Tomas Vondra Cc: Ryan Lambert , Thomas Munro , PostgreSQL Hackers , Bruce Momjian , Dimitri Fontaine References: <139e59f5-15bc-13ff-58e2-7a2229041236@postgrespro.ru> <78d43611-668e-a4e2-4796-dbffccc2512d@postgrespro.ru> <42c78b0e-e2ad-7920-3b31-37a81c9ba83b@postgrespro.ru> <20190726202437.2svtvcisiujqaoky@development> From: Konstantin Knizhnik Message-ID: Date: Tue, 30 Jul 2019 14:06:38 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <20190726202437.2svtvcisiujqaoky@development> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 26.07.2019 23:24, Tomas Vondra wrote: > Secondly, when trying this >  pgbench -p 5432 -U x -i -s 1 test >  pgbench -p 6543 -U x -c 24 -C -T 10 test > > it very quickly locks up, with plenty of non-granted locks in pg_locks, > but I don't see any interventions by deadlock detector so I presume > the issue is somewhere else. I don't see any such issues whe running > without the connection pool or without the -C option: > >  pgbench -p 5432 -U x -c 24 -C -T 10 test >  pgbench -p 6543 -U x -c 24 -T 10 test > > This is with default postgresql.conf, except for > >  connection_proxies = 4 > After some investigation I tend to think that it is problem of pgbench. It synchronously establishes new connection: #0  0x00007f022edb7730 in __poll_nocancel () at ../sysdeps/unix/syscall-template.S:84 #1  0x00007f022f7ceb77 in pqSocketPoll (sock=4, forRead=1, forWrite=0, end_time=-1) at fe-misc.c:1164 #2  0x00007f022f7cea32 in pqSocketCheck (conn=0x1273bf0, forRead=1, forWrite=0, end_time=-1) at fe-misc.c:1106 #3  0x00007f022f7ce8f2 in pqWaitTimed (forRead=1, forWrite=0, conn=0x1273bf0, finish_time=-1) at fe-misc.c:1038 #4  0x00007f022f7c0cdb in connectDBComplete (conn=0x1273bf0) at fe-connect.c:2029 #5  0x00007f022f7be71f in PQconnectdbParams (keywords=0x7ffc1add5640, values=0x7ffc1add5680, expand_dbname=1) at fe-connect.c:619 #6  0x0000000000403a4e in doConnect () at pgbench.c:1185 #7  0x0000000000407715 in advanceConnectionState (thread=0x1268570, st=0x1261778, agg=0x7ffc1add5880) at pgbench.c:2919 #8  0x000000000040f1b1 in threadRun (arg=0x1268570) at pgbench.c:6121 #9  0x000000000040e59d in main (argc=10, argv=0x7ffc1add5f98) at pgbench.c:5848 I.e. is starts normal transaction in one connection (few select/update/insert statement which are part of pgbench standard transaction) and at the same time tries to establish new connection. As far as built-in connection pooler performs transaction level scheduling, first session is grabbing backend until end of transaction. So until this transaction is completed backend will not be able to process some other transaction or accept new connection. But pgbench is completing this transaction because it is blocked in waiting response for new connection. The problem can be easily reproduced with just two connections if connection_proxies=1 and session_pool_size=1:  pgbench -p 6543 -n -c 2 -C -T 10 postgres knizhnik@knizhnik:~/postgrespro.ee11$ ps aux | fgrep postgres knizhnik 14425  0.0  0.1 172220 17540 ?        Ss   09:48   0:00 /home/knizhnik/postgresql.builtin_pool/dist/bin/postgres -D pgsql.proxy knizhnik 14427  0.0  0.0 183440  5052 ?        Ss   09:48   0:00 postgres: connection proxy knizhnik 14428  0.0  0.0 172328  4580 ?        Ss   09:48   0:00 postgres: checkpointer knizhnik 14429  0.0  0.0 172220  4892 ?        Ss   09:48   0:00 postgres: background writer knizhnik 14430  0.0  0.0 172220  7692 ?        Ss   09:48   0:00 postgres: walwriter knizhnik 14431  0.0  0.0 172760  5640 ?        Ss   09:48   0:00 postgres: autovacuum launcher knizhnik 14432  0.0  0.0  26772  2292 ?        Ss   09:48   0:00 postgres: stats collector knizhnik 14433  0.0  0.0 172764  5884 ?        Ss   09:48   0:00 postgres: logical replication launcher knizhnik 14434  0.0  0.0  22740  3084 pts/21   S+   09:48   0:00 pgbench -p 6543 -n -c 2 -C -T 10 postgres knizhnik 14435  0.0  0.0 173828 13400 ?        Ss   09:48   0:00 postgres: knizhnik postgres [local] idle in transaction knizhnik 21927  0.0  0.0  11280   936 pts/19   S+   11:58   0:00 grep -F --color=auto postgres But if you run each connection in separate thread, then this test is normally completed: nizhnik@knizhnik:~/postgresql.builtin_pool$ pgbench -p 6543 -n -j 2 -c 2 -C -T 10 postgres transaction type: scaling factor: 1 query mode: simple number of clients: 2 number of threads: 2 duration: 10 s number of transactions actually processed: 9036 latency average = 2.214 ms tps = 903.466234 (including connections establishing) tps = 1809.317395 (excluding connections establishing) -- Konstantin Knizhnik Postgres Professional: http://www.postgrespro.com The Russian Postgres Company