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 1kFThZ-0001jn-05 for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2020 02:49: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 1kFThX-0007yT-PA for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2020 02:49:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kFThX-0007yL-IX for pgsql-hackers@lists.postgresql.org; Tue, 08 Sep 2020 02:49:43 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kFThU-0001RF-Mf for pgsql-hackers@postgresql.org; Tue, 08 Sep 2020 02:49:43 +0000 Received: from hnk.local (sp49-98-212-203.msd.spmode.ne.jp [49.98.212.203]) by oss.nttdata.com (Postfix) with ESMTPSA id 60B2760343; Tue, 8 Sep 2020 11:49:37 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at oss.nttdata.com Subject: Re: Global snapshots To: Alexey Kondratov , "Andrey V. Lepikhov" Cc: tsunakawa.takay@fujitsu.com, movead.li@highgo.ca, 'Amit Kapila' , Masahiko Sawada , PostgreSQL-Dev References: <07b2c899-4ed0-4c87-1327-23c750311248@postgrespro.ru> <9964cf46-9294-34b9-4858-971e9029f5c7@postgrespro.ru> <806c3708-26ea-b94d-87b1-d48e41e93726@postgrespro.ru> <3ef7877bfed0582019eab3d462a43275@postgrespro.ru> From: Fujii Masao Message-ID: <057004b6-68bf-98ee-7c57-91c418f6e221@oss.nttdata.com> Date: Tue, 8 Sep 2020 11:49:36 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <3ef7877bfed0582019eab3d462a43275@postgrespro.ru> Content-Type: text/plain; charset=windows-1252; 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 2020/09/05 3:31, Alexey Kondratov wrote: > Hi, > > On 2020-07-27 09:44, Andrey V. Lepikhov wrote: >> On 7/27/20 11:22 AM, tsunakawa.takay@fujitsu.com wrote: >>> >>> US8356007B2 - Distributed transaction management for database systems with multiversioning - Google Patents >>> https://patents.google.com/patent/US8356007 >>> >>> >>> If it is, can we circumvent this patent? >>> >> >> Thank you for the research (and previous links too). >> I haven't seen this patent before. This should be carefully studied. > > I had a look on the patch set, although it is quite outdated, especially on 0003. > > Two thoughts about 0003: > > First, IIUC atomicity of the distributed transaction in the postgres_fdw is achieved by the usage of 2PC. I think that this postgres_fdw 2PC support should be separated from global snapshots. Agreed. > It could be useful to have such atomic distributed transactions even without a proper visibility, which is guaranteed by the global snapshot. Especially taking into account the doubts about Clock-SI and general questions about algorithm choosing criteria above in the thread. > > Thus, I propose to split 0003 into two parts and add a separate GUC 'postgres_fdw.use_twophase', which could be turned on independently from 'postgres_fdw.use_global_snapshots'. Of course if the latter is enabled, then 2PC should be forcedly turned on as well. > > Second, there are some problems with errors handling in the 0003 (thanks to Arseny Sher for review). > > +error: > +            if (!res) > +            { > +                sql = psprintf("ABORT PREPARED '%s'", fdwTransState->gid); > +                BroadcastCmd(sql); > +                elog(ERROR, "Failed to PREPARE transaction on remote node"); > +            } > > It seems that we should never reach this point, just because BroadcastStmt will throw an ERROR if it fails to prepare transaction on the foreign server: > > +            if (PQresultStatus(result) != expectedStatus || > +                (handler && !handler(result, arg))) > +            { > +                elog(WARNING, "Failed command %s: status=%d, expected status=%d", sql, PQresultStatus(result), expectedStatus); > +                pgfdw_report_error(ERROR, result, entry->conn, true, sql); > +                allOk = false; > +            } > > Moreover, It doesn't make much sense to try to abort prepared xacts, since if we failed to prepare it somewhere, then some foreign servers may become unavailable already and this doesn't provide us a 100% guarantee of clean up. > > +    /* COMMIT open transaction of we were doing 2PC */ > +    if (fdwTransState->two_phase_commit && > +        (event == XACT_EVENT_PARALLEL_COMMIT || event == XACT_EVENT_COMMIT)) > +    { > +        BroadcastCmd(psprintf("COMMIT PREPARED '%s'", fdwTransState->gid)); > +    } > > At this point, the host (local) transaction is already committed and there is no way to abort it gracefully. However, BroadcastCmd may rise an ERROR that will cause a PANIC, since it is non-recoverable state: > > PANIC:  cannot abort transaction 487, it was already committed > > Attached is a patch, which implements a plain 2PC in the postgres_fdw and adds a GUC 'postgres_fdw.use_twophase'. Also it solves these errors handling issues above and tries to add proper comments everywhere. I think, that 0003 should be rebased on the top of it, or it could be a first patch in the set, since it may be used independently. What do you think? Thanks for the patch! Sawada-san was proposing another 2PC patch at [1]. Do you have any thoughts about pros and cons between your patch and Sawada-san's? Regards, [1] https://www.postgresql.org/message-id/CA+fd4k4z6_B1ETEvQamwQhu4RX7XsrN5ORL7OhJ4B5B6sW-RgQ@mail.gmail.com -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION