Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f1xoH-0003Vh-4Q for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Mar 2018 17:27:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f1xoE-0005KK-Vu for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Mar 2018 17:27:26 +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 1f1xoE-0005K9-Ki for pgsql-hackers@lists.postgresql.org; Fri, 30 Mar 2018 17:27:26 +0000 Received: from mail-wr0-x241.google.com ([2a00:1450:400c:c0c::241]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f1xoA-0004Ij-2a for pgsql-hackers@postgresql.org; Fri, 30 Mar 2018 17:27:25 +0000 Received: by mail-wr0-x241.google.com with SMTP id p53so8538077wrc.10 for ; Fri, 30 Mar 2018 10:27:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=54I8P0Iqymv8A2dRy86imc+N8ogvp1NaEWf8GUg33tU=; b=CyjdRxRFnw/Vu5G/y68zN1CiyWy9i7ePfiNq0BcaU6Swl3QtgGpx+nyXxYDLm6XRBA PgAouKzxp2rR0E2YCi2Ma3jcyYC8Fowb4zeJh7qjMlfRVAz0TkGaxWl78KbgCILsL60b bOT4ezLzapQ+XNr4P5kgxN3IccFEt1gdhIJ8FD/TJnMENMHEkP+3WTxfgaz7Ewam1dvx AOiZUXjjrquzmVA/RJwYY3PQ2N1yvjZAa1IMmHzNcm2UpC4J4KdCpHMUzoebF1It/SGx dVNUfn85MNtj5CejphvbuCSnQPas79XnAckSigKrx5UI4h2IsK9LzExBBMWyqDZEt+1w 4XEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=54I8P0Iqymv8A2dRy86imc+N8ogvp1NaEWf8GUg33tU=; b=ahCH2TFwtgSAcMms305b19Gvm8wSBQvF/r/zH9MEk+NYBDI2DF/eh43VIWZaHg44hI wSTEhhbXMskB25vimfrL18/gOEbOP7ixjB4Rfry0rhMxqj6Ewzya68RXWFPXedgTvuLU ThwuORRVS2ZNV1XSAs5lHPeZOuoUBKaAHAinovF6R4bPkKZZdarBWPj8MOE8RFY5izf/ bGBxCQwLB8vs3tEweuyTEcI4t/tk/+Do9WKtf+ZgP+kaQo0Ht2AtpnSsPjeBF/WvCbmu r2pmvWLwRs/1H8v04ZpiqJ9lHy1Yx0ZUP9SQvMjyXs3uGzyUMzmosSGpM9iLv5Ic+Xlx o5Pg== X-Gm-Message-State: AElRT7FF1Gnu6a+6B/p1+yhTyFL/MQ9KUwJzyHg8qwbn5P3EVzkHItu2 O5JWBHn54hQsOqjYxg05n335Fw== X-Google-Smtp-Source: AIpwx4/FgeSQZP0xk3ZltPEA3Y7cUERLPYpTYTY63/NWVFJtNmm/o5rrHD1RB/bY50RJUxMexxS58A== X-Received: by 10.223.150.11 with SMTP id b11mr9602929wra.55.1522430840697; Fri, 30 Mar 2018 10:27:20 -0700 (PDT) Received: from [192.168.0.106] (150.123.broadband11.iol.cz. [90.178.123.150]) by smtp.gmail.com with ESMTPSA id m18sm10376696wri.65.2018.03.30.10.27.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 30 Mar 2018 10:27:19 -0700 (PDT) Subject: Re: [HACKERS] logical decoding of two-phase transactions To: Nikhil Sontakke Cc: Andres Freund , Tomas Vondra , Craig Ringer , Peter Eisentraut , Simon Riggs , Sokolov Yura , Stas Kelvich , Dmitry Dolgov <9erthalion6@gmail.com>, Tom Lane , Masahiko Sawada , PostgreSQL Hackers , Robert Haas References: <20180212162040.rsnlnysmf7n4yo7i@alap3.anarazel.de> <20180302005343.zv6nsqimcujzjrcd@alap3.anarazel.de> <20180329215815.36xxakgujx4luo4m@alap3.anarazel.de> From: Petr Jelinek Openpgp: preference=signencrypt Autocrypt: addr=petr.jelinek@2ndquadrant.com; keydata= xsFNBFawwSABEAC45Vua8rSggpM2kkMMmoOHz51a+fi3OXFfnrStVh80pM2/J9nSzjrIKQKf H5PtRnoYzSKnoUDoLOkLwDYitK3HzUUWXma+AWAqef2Y0GSZ7A3jJkD+JHOfkpS1CIpkEB/e WjA8Wqegwzc/OEE5111/l0Q1e6jxgZI86h8MMnm35Of1Y6Lz4kqWCyxHhaTJKTCkq66ICQ2C VJkbvuUoEE+Hr/I38dPHTAqIlCnycVnJPLHBQ/UYGHR/USF8qI/qJJx6tJcNAEqz69QZ/VlY 31UyJHdYe5NtBVz+rYZCOVWfpe3crbHuq9FjAPT+xcZefdxridmMKuW8eBmmXI9iZ3EG+X8J IsdLWL8QoqW0lnO1otYz1QDjdOvbEpq+u5/TAU8pgtaYH2J+6/GaQ6hMLyLBkaIAtnqlLCzq pxXcxMsFT14P5/rZA7EbBI8RS1ZIWJBlS2oO+dtVuENLJUC0SpptaRyn/uyOIopt7hSmBekL SzFnet7P9qofFJY+fQEz84v0G1gvwonHGHiCGgLpbMdPtnfPzda1aiTURvvOD7w/RmX1/ogk TqSGO4ejVNO/28QYouxmxzy8jbbIEiLCbHpLR/M/TvYHHPURxcx+MxVl3G+d/4Ihy4abxOdW pe9zLIcVMvqiUFQO28/79Gfhb7G4K+L21ORB82QYkzm3M7ebXQAR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yXvA5a RQQCRMXKbGaYhKkYEYiYOGqtTxN+PmyvNUBmFEcagBX2TXqEkgnI6zOnN13mB4SihJ2lne2r 9RNiPYs/hmRxYg== Message-ID: Date: Fri, 30 Mar 2018 19:27:18 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 30/03/18 09:56, Nikhil Sontakke wrote: > >> I as wondering how to hide this. Best idea I had so far would be to put >> it in heap_beginscan (and index_beginscan given that catalog scans use >> is as well) behind some condition. That would also improve performance >> because locking would not need to happen for syscache hits. The problem >> is however how to inform the heap_beginscan about the fact that we are >> in 2PC decoding. We definitely don't want to change all the scan apis >> for this. I wonder if we could add some kind of property to Snapshot >> which would indicate this fact - logical decoding is using it's own >> snapshots it could inject the information about being inside the 2PC >> decoding. >> > > The idea of adding that info in the Snapshot itself is interesting. We > could introduce a logicalxid field in SnapshotData to point to the XID > that the decoding backend is interested in. This could be added only > for the 2PC case. Support in the future for in-progress transactions > could use this field as well. If it's a valid XID, we could call > LogicalLockTransaction/LogicalUnlockTransaction on that XID from > heap_beginscan/head_endscan respectively. I can also look at what > other *_beginscan APIs would need this as well. > So I have spent some significant time today thinking about this (the issue in general not this specific idea). And I think this proposal does not work either. The problem is that we fundamentally want two things, not one. It's true we want to block ABORT from finishing while we are reading catalogs, but the other important part is that we want to bail gracefully when ABORT happened for the transaction being decoded. In other words,, if we do the locking transparently somewhere in the scan or catalog read or similar there is no way to let the plugin know that it should bail. So the locking code that's called from several layers deep would have only one option, to ERROR. I don't think we want to throw ERRORs when transaction which is being decoded has been aborted as that disrupts the replication. I think that we basically only have two options here that can satisfy both blocking ABORT and bailing gracefully in case ABORT has happened. Either the plugin has full control over locking (as in the patch), so that it can bail when the locking function reports that the transaction has aborted. Or we do the locking around the plugin calls, ie directly in logical decoding callback wrappers or similar. Both of these options have some disadvantages. Locking inside plugin make the plugin code much more complex if it wants to support this. For example if I as plugin author call any function that somewhere access syscache, I have to do the locking around that function call. Locking around plugin callbacks can hold he lock for longer periods of time since plugins usually end up writing to network. I think for most use-cases of 2PC decoding the latter is more useful as plugin should be connected to some kind transaction management solution. Also the time should be bounded by things like wal_sender_timeout (or statement_timeout for SQL variant of decoding). Note that I was initially advocating against locking around whole callbacks when Nikhil originally came up with the idea, but after we went over several other options here and given it a lot of thought I now think it's probably least bad way we have available. At least until somebody figures out how to solve all the issues around reading aborted catalog changes, but that does seem like rather large project on its own. And if we do locking around plugin callbacks now then we can easily switch to that solution if it ever happens without anybody having to rewrite the plugins. -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services