Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l9P5d-0000AG-8A for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Feb 2021 09:13:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l9P5c-0000qn-6R for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Feb 2021 09:13:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l9P5b-0000qg-Vd for pgsql-hackers@lists.postgresql.org; Tue, 09 Feb 2021 09:13:43 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l9P5Z-0003mb-JZ for pgsql-hackers@postgresql.org; Tue, 09 Feb 2021 09:13:42 +0000 Received: from hnk.local (p18067-ipngn2801funabasi.chiba.ocn.ne.jp [153.212.145.67]) by oss.nttdata.com (Postfix) with ESMTPSA id 8A69362883; Tue, 9 Feb 2021 18:13:38 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at oss.nttdata.com Subject: Re: adding wait_start column to pg_locks To: torikoshia Cc: Ian Lawrence Barwick , Robert Haas , Justin Pryzby , pgsql-hackers References: <20210101214930.GH25152@telsasoft.com> <9e958478d8bcea814e7ce16511a18912@oss.nttdata.com> <23d39ee9c31643fad8f9ba9c5cf3aaf4@oss.nttdata.com> <0fd375a53e306566cea7f451cc8cfcde@oss.nttdata.com> <7b1bb07a-73ec-1fcc-2f65-41101adf0080@oss.nttdata.com> <8002219d-b999-12fa-2327-49afe40e5fbb@oss.nttdata.com> <88c451c7-2729-4329-fbae-6ed7664adea1@oss.nttdata.com> <3db333a9-31f7-a225-8038-eeb835ba8f37@oss.nttdata.com> From: Fujii Masao Message-ID: <990e5d4b-075f-101f-aec5-bb44f9b30550@oss.nttdata.com> Date: Tue, 9 Feb 2021 18:13:37 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2021/02/09 17:48, torikoshia wrote: > On 2021-02-05 18:49, Fujii Masao wrote: >> On 2021/02/05 0:03, torikoshia wrote: >>> On 2021-02-03 11:23, Fujii Masao wrote: >>>>> 64-bit fetches are not atomic on some platforms. So spinlock is necessary when updating "waitStart" without holding the partition lock? Also GetLockStatusData() needs spinlock when reading "waitStart"? >>>> >>>> Also it might be worth thinking to use 64-bit atomic operations like >>>> pg_atomic_read_u64(), for that. >>> >>> Thanks for your suggestion and advice! >>> >>> In the attached patch I used pg_atomic_read_u64() and pg_atomic_write_u64(). >>> >>> waitStart is TimestampTz i.e., int64, but it seems pg_atomic_read_xxx and pg_atomic_write_xxx only supports unsigned int, so I cast the type. >>> >>> I may be using these functions not correctly, so if something is wrong, I would appreciate any comments. >>> >>> >>> About the documentation, since your suggestion seems better than v6, I used it as is. >> >> Thanks for updating the patch! >> >> +    if (pg_atomic_read_u64(&MyProc->waitStart) == 0) >> +        pg_atomic_write_u64(&MyProc->waitStart, >> +                            pg_atomic_read_u64((pg_atomic_uint64 *) &now)); >> >> pg_atomic_read_u64() is really necessary? I think that >> "pg_atomic_write_u64(&MyProc->waitStart, now)" is enough. >> >> +        deadlockStart = get_timeout_start_time(DEADLOCK_TIMEOUT); >> +        pg_atomic_write_u64(&MyProc->waitStart, >> +                    pg_atomic_read_u64((pg_atomic_uint64 *) &deadlockStart)); >> >> Same as above. >> >> +        /* >> +         * Record waitStart reusing the deadlock timeout timer. >> +         * >> +         * It would be ideal this can be synchronously done with updating >> +         * lock information. Howerver, since it gives performance impacts >> +         * to hold partitionLock longer time, we do it here asynchronously. >> +         */ >> >> IMO it's better to comment why we reuse the deadlock timeout timer. >> >>      proc->waitStatus = waitStatus; >> +    pg_atomic_init_u64(&MyProc->waitStart, 0); >> >> pg_atomic_write_u64() should be used instead? Because waitStart can be >> accessed concurrently there. >> >> I updated the patch and addressed the above review comments. Patch attached. >> Barring any objection, I will commit this version. > > Thanks for modifying the patch! > I agree with your comments. > > BTW, I ran pgbench several times before and after applying > this patch. > > The environment is virtual machine(CentOS 8), so this is > just for reference, but there were no significant difference > in latency or tps(both are below 1%). Thanks for the test! I pushed the patch. Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION