Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSQ12-0007te-Qv for pgsql-hackers@arkaria.postgresql.org; Mon, 11 Jun 2018 16:50:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fSQ11-0007Sc-MH for pgsql-hackers@arkaria.postgresql.org; Mon, 11 Jun 2018 16:49:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSQ11-0007SL-DP for pgsql-hackers@lists.postgresql.org; Mon, 11 Jun 2018 16:49:59 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSQ0x-0007u2-Pl for pgsql-hackers@postgresql.org; Mon, 11 Jun 2018 16:49:58 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 0D17B21B81; Mon, 11 Jun 2018 12:49:54 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 11 Jun 2018 12:49:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=HWTqg/LkkYIMxQ8c5vQcc7HFaA3vlwLVXARgBulte3U=; b=YRyeHMy+ jyWn4GlYPeR7wMRZWv3oYJ/dTHRl+L7TQD+//lBC8jqLV5Nq910ImQqbIODZ/Hls rSUCTP+ajBv4YQ5J9bUZPc/e2+UVSl21Hmrczf+rZ9KryjiFhKZ4hcMz793NM0yL iG1sRgxeTpwPxHmLVA9bTId/SOzV0YvzJYWSMavH99SkuS4ScRSHXLZpzHu/6szf iTs5cq7gN5zMFITCvQ8Exqcr+0ImP6x/5ene/HeC21FlXLcx1QlXXBCDDhkinSlh zy6K4vVtXjGHzoEbzYfjAkj3pX+WBHS13AcjhUDXwwSSnv897sb+i9QVgFYMAoCk aa/tp6Ym4T5CRg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=HWTqg/LkkYIMxQ8c5vQcc7HFaA3vl wLVXARgBulte3U=; b=C7HqCvjkfNg+/xWGn5xnndZlFOnMThjB4OOHscPmwVvBk vYOxhX6oYdmjxwW9/DfqPyidlS85QBO+C82mWpzU6etiqcs6Hq1FzPPg8mhO9D85 qWV/PTLA/jdv3PvI2nJJbXy8o0f4YeU2YPNj5cbXAD16eyJ2SDxoFuty9WqvSjdY E7whgIpWMJDs7572lUp8Qr+UvQ1ca/H51dbRGK8UpdMdxCGb+VJiuE7yc0J5J/l1 FqvYxIsT2qWAkcHnU8EOsKiiHA7R0JsbNr+vqUxmEtwiyCRhjygBPVz18jpp4nNn Ugl/xp6s2LksUPd9j1VgZzVm3nEmKbmn2Ey4Zmyzw== X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from intern.anarazel.de (c-98-210-140-171.hsd1.ca.comcast.net [98.210.140.171]) by mail.messagingengine.com (Postfix) with ESMTPA id 9077E10261; Mon, 11 Jun 2018 12:49:53 -0400 (EDT) Date: Mon, 11 Jun 2018 09:49:52 -0700 From: Andres Freund To: Michael Paquier , pgsql-hackers@postgresql.org Cc: pgsql-committers@lists.postgresql.org Subject: Re: pgsql: Fix and document lock handling for in-memory replication slot da Message-ID: <20180611164952.vmxdpdpirdtkdsz6@alap3.anarazel.de> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On 2018-06-10 10:45:04 +0000, Michael Paquier wrote: > Fix and document lock handling for in-memory replication slot data > > While debugging issues on HEAD for the new slot forwarding feature of > Postgres 11, some monitoring of the code surrounding in-memory slot data > has proved that the lock handling may cause inconsistent data to be read > by read-only callers of slot functions, particularly > pg_get_replication_slots() which fetches data for the system view > pg_replication_slots, or modules looking directly at slot information. > > The code paths involved in those problems concern logical decoding > initialization (down to 9.4) and WAL reservation for slots (new as of > 10). > > A set of comments documenting all the lock handlings, particularly the > dependency with LW locks for slots and the in_use flag as well as the > internal mutex lock is added, based on a suggested by Simon Riggs. > > Some of the fixed code exists down to 9.4 where WAL decoding has been > introduced, but as those race conditions are really unlikely going to > happen as those concern code paths for slot and decoding creation, just > fix the problem on HEAD. You can't do things like: /* start at current insert position */ + SpinLockAcquire(&slot->mutex); slot->data.restart_lsn = GetXLogInsertRecPtr(); + SpinLockRelease(&slot->mutex); a) we don't call external functions with a spinlock held. As a rule. It's too hard to se what happens in that other function / too likely to change independently. b) we most certainly don't do it if the other function also acquires a spinlock: XLogRecPtr GetXLogInsertRecPtr(void) { XLogCtlInsert *Insert = &XLogCtl->Insert; uint64 current_bytepos; SpinLockAcquire(&Insert->insertpos_lck); current_bytepos = Insert->CurrBytePos; SpinLockRelease(&Insert->insertpos_lck); return XLogBytePosToRecPtr(current_bytepos); } Nesting spinlock means that you'd need to be very careful about whether all lockers use the same order. And be ok with the system stalled until the PANIC if it failed. Same is true for the codepaths calling GetRedoRecPtr(). I don't object to the general idea of adding locking - although the benefit are nearly guaranteed to be cosmetic - but this has the potential to make things worse. - Andres