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 1l02CN-0001ht-N0 for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Jan 2021 12:57:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l02CM-0000qS-Ip for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Jan 2021 12:57:58 +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 1l02CM-0000qL-Ah for pgsql-hackers@lists.postgresql.org; Thu, 14 Jan 2021 12:57:58 +0000 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l02CE-0006UW-OF for pgsql-hackers@postgresql.org; Thu, 14 Jan 2021 12:57:57 +0000 Received: by mail-ej1-x62f.google.com with SMTP id f4so8002067ejx.7 for ; Thu, 14 Jan 2021 04:57:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=bfM/UR1+AR4uAZ2DOxlP52W66NmnJqfJzZEy3lxHj7k=; b=sRFcK0qhl8jLPRiFadOAMnW7maXZuQfesPCZv0MeYWNuu7IajsBpaRr245XFMjoTCf zVoSr5fdy4spJeitISWLrvDVTFiXblZ/nAtwGNtIPkLx8RL56nvebGSUyd1KQpAnMOjH o+WZdE//mfkSH41mONLLb7YYA7FBB2kAXQ6OtiEXJYRoy0J5JC/GzQHoAbqU3Th0RPCL tHV9ZwGVLveH/zZggDzHb0MWL7XsGjr6zUJV5ZxqJkbRkoWTZ67ujBHKr3KC/OnfH3mO 1qL4i/gukCIgWgRbS92zU/7aBmBV0vf4mWgJmG5oCwy6Uwbavo4vxEhXsF1sKk/NEzb+ SWVA== 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:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=bfM/UR1+AR4uAZ2DOxlP52W66NmnJqfJzZEy3lxHj7k=; b=BV0qVHNIKyZdAoe7TrKiv5OEAONDl1Djyw/r8U8FRASb19L/JMtbVkrGGKNsTLvYdy 8qqX5D+CWsseTGTGltWOxjIiF9BeLZdf9dA2Qa/dVaTv8/ebmwrka2jchSdkWP1mdE79 hDodxtDP+JRN6AN/E7ECJEH/49+OeP8F54yKkCN72edmlu4WibfmIIdLvPLZeC+0hoDd MUo4TdmhPk044imiamXXfadWEOL+0JZLCKDoAVCyncNzmP5gbsXbLpFiv/U9XZGT9oFF Qc6814m79hHtkGF0HsU6YXZunuhCOZ3V692laYdjQUfSgOCyPvI4QIhFr1bUPbeRqIfu 05hg== X-Gm-Message-State: AOAM530RW9a0oaOrnCoc2ApJOcckFsTWNfMLfNy+H667PvbfVWGYrO22 L7jMkNYDtPfGKmV/d5VeAr/gNGiYsLJPbqqvblkCB9DovPNY6vC7xqzboxwxQppD6loUz1Dnqk3 GG8bY08hXQXJrfStgG9lQW3TB2lxFK0xjKXJkPkQYqzTdJymFR1Fqay45F+1dLXhle7bu9RbvEF y37kRS1VY5aP1728B1Ak6HiNHl9b1Wv3cpxLWwzwjZabKpDvk0Z1GTpNS1hcVGNPNrSXKIYpXAx UOd2n9RBbNzqYkLweA5jsNd8sQrQtE4VlUkoA== X-Google-Smtp-Source: ABdhPJzR1k8hWAWocqM1VosM+vSmI7CMNhFLNtae7qzsiKJulBCp6rP8cfrcVGom2vgB3H3rh73m4g== X-Received: by 2002:a17:906:d101:: with SMTP id b1mr5241415ejz.80.1610629068506; Thu, 14 Jan 2021 04:57:48 -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 b7sm1975026ejz.4.2021.01.14.04.57.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 14 Jan 2021 04:57:47 -0800 (PST) Subject: Re: POC: postgres_fdw insert batching To: Amit Langote Cc: "tsunakawa.takay@fujitsu.com" , Tomas Vondra , "Andrey V. Lepikhov" , "pgsql-hackers@postgresql.org" References: <20200628151002.7x5laxwpgvkyiu3q@development> <71113ab1-ed31-3dbc-ce07-e081e17473b6@enterprisedb.com> <43b5d643-da69-70a5-16d2-d04d0851b723@enterprisedb.com> <0ab26876-9e89-ef24-094d-35b991672309@enterprisedb.com> <9808f343-410e-4441-9a10-42e14a1c6436@enterprisedb.com> <131a695b-9010-6751-9ea7-3dabe3555f8c@enterprisedb.com> From: Tomas Vondra Message-ID: <53187d73-05ad-90c8-b404-db5f250d3bb0@enterprisedb.com> Date: Thu, 14 Jan 2021 13:57:44 +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 1/14/21 9:58 AM, Amit Langote wrote: > Hi, > > On Thu, Jan 14, 2021 at 2:41 AM Tomas Vondra > wrote: >> On 1/13/21 3:43 PM, Tomas Vondra wrote: >>> Thanks for the report. Yeah, I think there's a missing check in >>> ExecInsert. Adding >>> >>> (!resultRelInfo->ri_TrigDesc->trig_insert_after_row) >>> >>> solves this. But now I'm wondering if this is the wrong place to make >>> this decision. I mean, why should we make the decision here, when the >>> decision whether to have a RETURNING clause is made in postgres_fdw in >>> deparseReturningList? We don't really know what the other FDWs will do, >>> for example. >>> >>> So I think we should just move all of this into GetModifyBatchSize. We >>> can start with ri_BatchSize = 0. And then do >>> >>> if (resultRelInfo->ri_BatchSize == 0) >>> resultRelInfo->ri_BatchSize = >>> resultRelInfo->ri_FdwRoutine->GetModifyBatchSize(resultRelInfo); >>> >>> if (resultRelInfo->ri_BatchSize > 1) >>> { >>> ... do batching ... >>> } >>> >>> The GetModifyBatchSize would always return value > 0, so either 1 (no >>> batching) or >1 (batching). >>> >> >> FWIW the attached v8 patch does this - most of the conditions are moved >> to the GetModifyBatchSize() callback. > > Thanks. A few comments: > > * I agree with leaving it up to an FDW to look at the properties of > the table and of the operation being performed to decide whether or > not to use batching, although maybe BeginForeignModify() is a better > place for putting that logic instead of GetModifyBatchSize()? So, in > create_foreign_modify(), instead of PgFdwModifyState.batch_size simply > being set to match the table's or the server's value for the > batch_size option, make it also consider the things that prevent > batching and set the execution state's batch_size based on that. > GetModifyBatchSize() simply returns that value. > > * Regarding the timing of calling GetModifyBatchSize() to set > ri_BatchSize, I wonder if it wouldn't be better to call it just once, > say from ExecInitModifyTable(), right after BeginForeignModify() > returns? I don't quite understand why it is being called from > ExecInsert(). Can the batch size change once the execution starts? > But it should be called just once. The idea is that initially we have batch_size=0, and the fist call returns value that is >= 1. So we never call it again. But maybe it could be called from BeginForeignModify, in which case we'd not need this logic with first setting it to 0 etc. > * Lastly, how about calling it GetForeignModifyBatchSize() to be > consistent with other nearby callbacks? > Yeah, good point. >> I've removed the check for the >> BatchInsert callback, though - the FDW knows whether it supports that, >> and it seems a bit pointless at the moment as there are no other batch >> callbacks. Maybe we should add an Assert somewhere, though? > > Hmm, not checking whether BatchInsert() exists may not be good idea, > because if an FDW's GetModifyBatchSize() returns a value > 1 but > there's no BatchInsert() function to call, ExecBatchInsert() would > trip. I don't see the newly added documentation telling FDW authors > to either define both or none. > Hmm. The BatchInsert check seemed somewhat unnecessary to me, but OTOH it can't hurt, I guess. I'll ad it back. > Regarding how this plays with partitions, I don't think we need > ExecGetTouchedPartitions(), because you can get the routed-to > partitions using es_tuple_routing_result_relations. Also, perhaps I'm not very familiar with es_tuple_routing_result_relations, but that doesn't seem to work. I've replaced the flushing code at the end of ExecModifyTable with a loop over es_tuple_routing_result_relations, but then some of the rows are missing (i.e. not flushed). > it's a good idea to put the "finishing" ExecBatchInsert() calls into a > function ExecFinishBatchInsert(). Maybe the logic to choose the > relations to perform the finishing calls on will get complicated in > the future as batching is added for updates/deletes too and it seems > better to encapsulate that in the separate function than have it out > in the open in ExecModifyTable(). > IMO that'd be an over-engineering at this point. We don't need such separate function yet, so why complicate the API? If we need it in the future, we can add it. > (Sorry about being so late reviewing this.) thanks -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company