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 1lCScw-0003F3-DH for pgsql-hackers@arkaria.postgresql.org; Wed, 17 Feb 2021 19:36:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lCScv-0004Rm-78 for pgsql-hackers@arkaria.postgresql.org; Wed, 17 Feb 2021 19:36:45 +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 1lCScu-0004Rf-RY for pgsql-hackers@lists.postgresql.org; Wed, 17 Feb 2021 19:36:44 +0000 Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lCScr-000685-Fo for pgsql-hackers@postgresql.org; Wed, 17 Feb 2021 19:36:43 +0000 Received: by mail-ej1-x635.google.com with SMTP id jt13so24352913ejb.0 for ; Wed, 17 Feb 2021 11:36:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=ghohQIOztQWtBxlE2CoWABs6O+0UA58sgUNpgqA6rHA=; b=qMYidRFZmN35CU94QeHTx/3yWM+abGZerQbzgAk3r3BxQwaxtTQ/DTlyoGP3wJQ7AF Z+jSVPsRg8kBFujPbOq1OVGmClRSL6IUFtZQLji3yG57tnUUVDKVuowdtPVVHA3lSv+j i7me+9De5NXlFUiAhf7lTLdIGpnQVHCcebTXuD9Cp9F0Mc7k/0QqNpEL1v8VwMB/pKlK 5ZmmkdJgexdhH3WVu7uZSLDOKmEIm92gmpGl0UyrYedOBzgVs0rUHbW18RYUR/rteMyT 9H89uBGZ6M/+eMNp8rtjgS+OGMwINl7ljqjXlgpcV5t/wfSs7HNheupipSlDVDHcSlCQ AdLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ghohQIOztQWtBxlE2CoWABs6O+0UA58sgUNpgqA6rHA=; b=nbanbVV5nSCa40+4XUNXf723+Pen4Ssd6ULd9EcENlkZshOBUfSpAmTk8EKwaN+dKz Y7fcMBCBBghzdfq19MCh5MXDsAnDULHdtGtM/Sj4BbmJIQTBYlnIluxTlxr0Ie0w9lBM Tj8Yrdpdw5wEP2Kzq2zsA0LFBqYPMFwp+FwC/khj1ZORdxZYJGUm0a11HqB0TFOvCk3b XWEf6DjIzK6RM7v6GMlTs46eChwMmh1l38KCST/AfODMf5CF6msqdOGqnXcXWecpopUU Y/TlV/1sAX8KD6oi9s2xKkRuGO7S35fl+ZknyMgwebscQlg3qsCZVKqMjnYsr5t9RccJ 1pXQ== X-Gm-Message-State: AOAM532xZM7P3uSLjqR0j62e1y8J1LdWNCCuy1sBm7m9cGux5riZ//kZ LhAVMV6Ygyi32k2/ztccfYKkkmlzxtQatQRaNd//+2WFvqfJqUBeRCkcjfv6Ip5A/mdnQMGQFTg 35q231SYxS43/eDkT1Za5C11cBgOn4ZmtpF/ppm+U8AxqIrCB7saN6lSkd9bJdjZP++tyQrWlN0 /Pu7uKX9W0pM0NmRBw8CzNAsWN9BOKiV+IfHgaR1ZTWTBF X-Google-Smtp-Source: ABdhPJwRhHM3wEekkIoBoJEZ9JrV6Q29pHe6CSV4lrhMoDrtcU83iR1zdkFmgzH3rC9P47jgM+65Kw== X-Received: by 2002:a17:906:16c6:: with SMTP id t6mr551085ejd.102.1613590599444; Wed, 17 Feb 2021 11:36:39 -0800 (PST) Received: from [10.137.0.4] (ip-86-49-253-127.net.upcbroadband.cz. [86.49.253.127]) by smtp.gmail.com with ESMTPSA id kv24sm1384672ejc.117.2021.02.17.11.36.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Feb 2021 11:36:38 -0800 (PST) Subject: Re: POC: postgres_fdw insert batching To: Amit Langote , PostgreSQL-development , "Tsunakawa, Takayuki" References: <20200628151002.7x5laxwpgvkyiu3q@development> <0fc809f6-7c3c-6d06-7ea3-f10765dfdf08@enterprisedb.com> <067ddf09-225c-e3c5-ea47-4dd39b34ebb0@enterprisedb.com> <7ffcee1f-d55e-cbdf-5f3f-98f1c30537ef@enterprisedb.com> <803d5507-299d-f109-7b71-b6bbbb17a053@enterprisedb.com> From: Tomas Vondra Message-ID: <6929d485-2d2a-da46-3681-4a400a3d794f@enterprisedb.com> Date: Wed, 17 Feb 2021 20:36:36 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2/17/21 9:51 AM, Amit Langote wrote: > On Wed, Feb 17, 2021 at 5:46 PM Amit Langote wrote: >> On Wed, Feb 17, 2021 at 12:04 AM Tomas Vondra >> wrote: >>> On 2/16/21 10:25 AM, Amit Langote wrote: >>>> On Tue, Feb 16, 2021 at 1:36 AM Tomas Vondra >>>>> Thanks for the patch, it seems fine to me. >>>> >>>> Thanks for checking. >>>> >>>>> I wonder it the commit >>>>> message needs some tweaks, though. At the moment it says: >>>>> >>>>> Prevent FDW insert batching during cross-partition updates >>>>> >>>>> but what the patch seems to be doing is simply initializing the info >>>>> only for CMD_INSERT operations. Which does the trick, but it affects >>>>> everything, i.e. all updates, no? Not just cross-partition updates. >>>> >>>> You're right. Please check the message in the updated patch. >>> >>> Thanks. I'm not sure I understand what "FDW may not be able to handle >>> both the original update operation and the batched insert operation >>> being performed at the same time" means. I mean, if we translate the >>> UPDATE into DELETE+INSERT, then we don't run both the update and insert >>> at the same time, right? What exactly is the problem with allowing >>> batching for inserts in cross-partition updates? >> >> Sorry, I hadn't shared enough details of my investigations when I >> originally ran into this. Such as that I had considered implementing >> the use of batching for these inserts too but had given up. >> >> Now that you mention it, I think I gave a less convincing reason for >> why we should avoid doing it at all. Maybe it would have been more >> right to say that it is the core code, not necessarily the FDWs, that >> currently fails to deal with the use of batching by the insert >> component of a cross-partition update. Those failures could be >> addressed as I'll describe below. >> >> For postgres_fdw, postgresGetForeignModifyBatchSize() could be taught >> to simply use the PgFdwModifyTable that is installed to handle the >> insert component of a cross-partition update (one can get that one via >> aux_fmstate field of the original PgFdwModifyState). However, even >> though that's fine for postgres_fdw to do, what worries (had worried) >> me is that it also results in scribbling on ri_BatchSize that the core >> code may see to determine what to do with a particular tuple, and I >> just have to hope that nodeModifyTable.c doesn't end up doing anything >> unwarranted with the original update based on seeing a non-zero >> ri_BatchSize. AFAICS, we are fine on that front. >> >> That said, there are some deficiencies in the code that have to be >> addressed before we can let postgres_fdw do as mentioned above. For >> example, the code in ExecModifyTable() that runs after breaking out of >> the loop to insert any remaining batched tuples appears to miss the >> tuples batched by such inserts. Apparently, that is because the >> ResultRelInfos used by those inserts are not present in >> es_tuple_routing_result_relations. Turns out I had forgotten that >> execPartition.c doesn't add the ResultRelInfos to that list if they >> are made by ExecInitModifyTable() for the original update operation >> and simply reused by ExecFindPartition() when tuples were routed to >> those partitions. It can be "fixed" by reverting to the original >> design in Tsunakawa-san's patch where the tuple routing result >> relations were obtained from the PartitionTupleRouting data structure, >> which fortunately stores all tuple routing result relations. (Sorry, >> I gave wrong advice in [1] in retrospect.) >> >>> On a closer look, it seems the problem actually lies in a small >>> inconsistency between create_foreign_modify and ExecInitRoutingInfo. The >>> former only set batch_size for CMD_INSERT while the latter called the >>> BatchSize() for all operations, expecting >= 1 result. So we may either >>> relax create_foreign_modify and set batch_size for all DML, or make >>> ExecInitRoutingInfo stricter (which is what the patches here do). >> >> I think we should be fine if we make >> postgresGetForeignModifyBatchSize() use the correct PgFdwModifyState >> as described above. We can be sure that we are not mixing the >> information used by the batched insert with that of the original >> unbatched update. >> >>> Is there a reason not to do the first thing, allowing batching of >>> inserts during cross-partition updates? I tried to do that, but it >>> dawned on me that we can't mix batched and un-batched operations, e.g. >>> DELETE + INSERT, because that'd break the order of execution, leading to >>> bogus results in case the same row is modified repeatedly, etc. >> >> Actually, postgres_fdw only supports moving a row into a partition (as >> part of a cross-partition update that is) if it has already finished >> performing any updates on it. So there is no worry of rows that are >> moved into a partition subsequently getting updated due to the >> original command. >> >> The attached patch implements the changes necessary to make these >> inserts use batching too. >> >> [1] https://www.postgresql.org/message-id/CA%2BHiwqEbnhwVJMsukTP-S9Kv1ynC7Da3yuqSPZC0Y7oWWOwoHQ%40mail.gmail.com > > Oops, I had mistakenly not hit "Reply All". Attaching the patch again. > Thanks. The patch seems reasonable, but it's a bit too large for a fix, so I'll go ahead and push one of the previous fixes restricting batching to plain INSERT commands. But this seems useful, so I suggest adding it to the next commitfest. One thing that surprised me is that we only move the rows *to* the foreign partition, not from it (even on pg13, i.e. before the batching etc.). I mean, using the example you posted earlier, with one foreign and one local partition, consider this: delete from p; insert into p values (2); test=# select * from p2; a --- 2 (1 row) test=# update p set a = 1; UPDATE 1 test=# select * from p1; a --- 1 (1 row) OK, so it was moved to the foreign partition, which is for rows with value in (1). So far so good. Let's do another update: test=# update p set a = 2; UPDATE 1 test=# select * from p1; a --- 2 (1 row) So now the foreign partition contains value (2), which is however wrong with respect to the partitioning rules - this should be in p2, the local partition. This however causes pretty annoying issue: test=# explain analyze select * from p where a = 2; QUERY PLAN --------------------------------------------------------------- Seq Scan on p2 p (cost=0.00..41.88 rows=13 width=4) (actual time=0.024..0.028 rows=0 loops=1) Filter: (a = 2) Planning Time: 0.355 ms Execution Time: 0.089 ms (4 rows) That is, we fail to find the row, because we eliminate the partition. Now, maybe this is expected, but it seems like a rather mind-boggling violation of POLA principle. I've checked if postgres_fdw mentions this somewhere, but all I see about row movement is this: Note also that postgres_fdw supports row movement invoked by UPDATE statements executed on partitioned tables, but it currently does not handle the case where a remote partition chosen to insert a moved row into is also an UPDATE target partition that will be updated later. and if I understand that correctly, that's about something different. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company