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 1l2OzF-00019j-9B for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Jan 2021 01:42:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l2OzE-0004rA-8C for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Jan 2021 01:42:12 +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 1l2OzD-0004r2-Si for pgsql-hackers@lists.postgresql.org; Thu, 21 Jan 2021 01:42:12 +0000 Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l2OzB-0002k4-1J for pgsql-hackers@postgresql.org; Thu, 21 Jan 2021 01:42:10 +0000 Received: by mail-ed1-x535.google.com with SMTP id b2so602915edm.3 for ; Wed, 20 Jan 2021 17:42:08 -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; bh=nJycTTGrL18d83FHPNEGXZYA8McG157KZJeiTLlMtW8=; b=pldCrvwxT+8W9Hs0rLfd9FD35v5biDe5CnRkOh3JhXg4I+0wVEzkqfjoWsbh01IbYW JuQnBuS6S/iiFtLCMKoNSKB4rUP1OmBWxmNk15JsHpLVcMi56FHZ0vRjgKXRFNYR5Dxg 8FIk1zpuRIuIymBtn7sU4n4rU/4zdW2nSc/GS38QNKac87BEyFZcR2ROEsDamfNABSX3 lWiod1lAENnABWBiwPt7M6b0LVZWioiZWHfVyXoLTQzBhb1t1g14ufO7GUculnTlH46I BEvEg9s948aChNhaZluLrgf147L0MJq03wqxp4Jkr8iNOTfnFxOZpBwF2RtCtdI6BMIl e5rA== 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; bh=nJycTTGrL18d83FHPNEGXZYA8McG157KZJeiTLlMtW8=; b=cEBcQP1pEEV2my1Fj54UTPIfx+b6vxMb449zFwFf94hMBbTp0RDl2NYCnz6i2KUb8L qXDtOKS3/T4lK/wRo48kmerzv4QXenI548IhT87fe1A+u9oAXNDNvHV4qlL+LW1/DcWY d1h6Q8vfbes9Hn+3gry9mJsmEW1xqI9dbHy7JdaXTQBNyrFbbmZrVH/pwz3DNKi4vjVT VHZApA3LHvl36YuJ48N3hk+CNKtniombfjOXbMzLwW/HKg5xhgzA+VpHZSc4bkwIfSYO 0tSNO62KTP1evU1yw7OQjGxmFOvg/8wnmSsXkOUDnoX7TOxTC7XSilaFMj9IQ+9R18WW NOoQ== X-Gm-Message-State: AOAM531cJPWRt1MSX/aLP5HpK393k4axiTPDpCo29Xtvx4OvSKE7pKu/ NjXpBqIwTTi84l/yJ0BLq7UoDjyzHEuLXiVhZYMLbx1VQS0NJisHW4uiXr32tbnKaSVXPEtOX8n enwGCqKOTtgiUWBhzZCFyvDLA/ZRu1+j+bZAM3iaKIpyY05wK6IKGwrwxQcaSgQeQvfmAvldjZb E4dRmgQsWrnMRuiyXabQE3jZl8XpkVim0fcW+DMu4iHWDbX5kkPe9RA03Z2tVo3Ak+AsUbpaJ+U xk++9IRCv8lX8swQRt5iHO0io+JNQtD92QWXw== X-Google-Smtp-Source: ABdhPJwwD7poHn7YTdTBOEWT4K2Fv/GrAydDdsJy1Nu9YyweQayJ4yeIktPN9x/sdsHEAzi4LdR4TA== X-Received: by 2002:a05:6402:4310:: with SMTP id m16mr9450522edc.207.1611193326985; Wed, 20 Jan 2021 17:42:06 -0800 (PST) Received: from [10.137.0.20] (ip-86-49-253-127.net.upcbroadband.cz. [86.49.253.127]) by smtp.gmail.com with ESMTPSA id p26sm1985130edq.94.2021.01.20.17.42.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Jan 2021 17:42:05 -0800 (PST) Subject: Re: POC: postgres_fdw insert batching To: Amit Langote Cc: Zhihong Yu , "tsunakawa.takay@fujitsu.com" , "Andrey V. Lepikhov" , Tomas Vondra , "pgsql-hackers@postgresql.org" References: <20200628151002.7x5laxwpgvkyiu3q@development> <16fcfd4c-2d5e-67f7-fa69-9b5d119bac52@enterprisedb.com> <9d94c863-ce7c-ea7f-6b84-834c98929e2f@enterprisedb.com> <7e70a11e-cf5d-2e5f-a802-274c5e0e236b@enterprisedb.com> <3ca9496f-0827-bf21-b709-593de63440a5@enterprisedb.com> From: Tomas Vondra Message-ID: <4faa3d09-0fc8-c4d6-7aec-87428fabd161@enterprisedb.com> Date: Thu, 21 Jan 2021 02:42:03 +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: multipart/mixed; boundary="------------6D43F4FAD81726010FF254B9" Content-Language: en-US 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 This is a multi-part message in MIME format. --------------6D43F4FAD81726010FF254B9 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 1/21/21 2:24 AM, Amit Langote wrote: > On Thu, Jan 21, 2021 at 9:56 AM Tomas Vondra > wrote: >> On 1/21/21 1:17 AM, Zhihong Yu wrote: >>> Hi, >>> The assignment to resultRelInfo is done when junk_filter_needed is true: >>> >>> if (junk_filter_needed) >>> { >>> resultRelInfo = mtstate->resultRelInfo; >>> >>> Should the code for determining batch size access mtstate->resultRelInfo >>> directly ? >>> >> >> IMO the issue is that code iterates over all plans and moves to the next >> for each one: >> >> resultRelInfo++; >> >> so it ends up pointing past the last element, hence the failures. So >> yeah, either the code needs to move before the loop (per my patch), or >> we need to access mtstate->resultRelInfo directly. > > Accessing mtstate->resultRelInfo directly would do. The only > constraint on where this block should be placed is that > ri_projectReturning must be valid as of calling > GetForeignModifyBatchSize(), as Tsunakawa-san pointed out upthread. > So, after this block in ExecInitModifyTable: > > /* > * Initialize RETURNING projections if needed. > */ > if (node->returningLists) > { > .... > /* > * Build a projection for each result rel. > */ > resultRelInfo = mtstate->resultRelInfo; > foreach(l, node->returningLists) > { > List *rlist = (List *) lfirst(l); > > resultRelInfo->ri_returningList = rlist; > resultRelInfo->ri_projectReturning = > ExecBuildProjectionInfo(rlist, econtext, slot, &mtstate->ps, > resultRelInfo->ri_RelationDesc->rd_att); > resultRelInfo++; > } > } > Right. But I think Tom is right this should initialize ri_BatchSize for all the resultRelInfo elements, not just the first one. Per the attached patch, which resolves the issue both on x86_64 and armv7l for me. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------6D43F4FAD81726010FF254B9 Content-Type: text/x-patch; charset=UTF-8; name="modifytable-fix-2.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="modifytable-fix-2.patch" diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 9c36860704..10febcae8a 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -2798,17 +2798,23 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) * size (a FDW may support batching, but it may be disabled for the * server/table). */ - if (!resultRelInfo->ri_usesFdwDirectModify && - operation == CMD_INSERT && - resultRelInfo->ri_FdwRoutine != NULL && - resultRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize && - resultRelInfo->ri_FdwRoutine->ExecForeignBatchInsert) - resultRelInfo->ri_BatchSize = - resultRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize(resultRelInfo); - else - resultRelInfo->ri_BatchSize = 1; + resultRelInfo = mtstate->resultRelInfo; + for (i = 0; i < nplans; i++) + { + if (!resultRelInfo->ri_usesFdwDirectModify && + operation == CMD_INSERT && + resultRelInfo->ri_FdwRoutine != NULL && + resultRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize && + resultRelInfo->ri_FdwRoutine->ExecForeignBatchInsert) + resultRelInfo->ri_BatchSize = + resultRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize(resultRelInfo); + else + resultRelInfo->ri_BatchSize = 1; + + Assert(resultRelInfo->ri_BatchSize >= 1); - Assert(resultRelInfo->ri_BatchSize >= 1); + resultRelInfo++; + } /* * Lastly, if this is not the primary (canSetTag) ModifyTable node, add it --------------6D43F4FAD81726010FF254B9--