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 1lqw4k-0000go-Vo for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Jun 2021 11:08:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lqw4j-0003pn-SY for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Jun 2021 11:08: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 1lqw4j-0003nx-GU for pgsql-hackers@lists.postgresql.org; Wed, 09 Jun 2021 11:08:45 +0000 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lqw4g-0002hG-QP for pgsql-hackers@postgresql.org; Wed, 09 Jun 2021 11:08:44 +0000 Received: by mail-wr1-x42a.google.com with SMTP id o3so6631174wri.8 for ; Wed, 09 Jun 2021 04:08:41 -0700 (PDT) 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=7O8EivMCuIpwab1j7cSCBm3RL7t9uR3RPeABr5fZw44=; b=xBCwLHy1muOMpRqDxLI/HdpnkOOqJxM9B//wyfFJqbX79J/LWdfSxIXp5tL8jXdnQH m/YHR0qyXTAVdttyloqQtbfPV6gTGfcM/Gxli+7yzQSVOrOrDYNTx5oJmjl66vLUpEAs 2Rkp1w7xmmkXbYSfWrq4F0h71BzQfzV9lSHeIMIlOA6+1xckN/Ri+0YcGLwZGSrx0YUL vR858HwYGaNAC90LtJW2lora30fVkqBkGnmERD6096lhe1QWgiDjej5w4kLKDIQnBGKb OIcUNN3xB9V/lD8WxfB/XSGqM3qVTrcS3IPZ7XUG0GiuiTX4/ytRjxMf/H4Fe21GlO2Z I9UQ== 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=7O8EivMCuIpwab1j7cSCBm3RL7t9uR3RPeABr5fZw44=; b=qFnZ73tVUmYUlAamfykqJRKppcSSPKDWGFylNwTImISKx14bMcAvTSsV6gWEq617AS LGOiwINaoqlKUkdSqYX8NKuMzoEQkFvZJROe5cmYcCXdSCSCl9GgOTsYMTj0DTs/n1eV l44Z+hK4EVEDjIXllyTzBoeI+3pP0f4w8PkUJ7+YrJPsSi1P8LXSLihwU1LU00yB1S8g nLms9hCme2OK8OkUgUKzmbJvveMwOAcG8JkE+d2VTM2fYB0v4I5k6gCWOh2HAcir6JTD /rVdsThffEfxgzwtkSf24OfVbYk3ozsdvzkKhHC1eo6vVJwU/dLSDEDC0joAV6Cxk9x1 q73Q== X-Gm-Message-State: AOAM532KYrZRg0zZFDpQdMbIDhWoncHOPC2Jnms7+JLEZlil4u84p+cw HoRbecXmDMuRGKjblDDDlg430ci/p106uDsD5BZh1CdYE1gS+SIElLs5XiQPg4FipTQGqOfRHwZ Mzmln+5PBLNLB0ygl3DbDJdoNqxVbmKpQ/HLSSzZkEtb801FgL2P5NbyK2aYr6ryAICQPjOTz0Z 3b5hWMrOFGyxzq/MsPksb8o1os3z4zVXYBrlI+ID3MYYzvHslhUCIFldAXSywgGyGk0SDHYP0H9 qXX+rF5EtvC2oxL9hCzrdXYNv1AknWLTM/NBXu4 X-Google-Smtp-Source: ABdhPJy2TIqovi4wXTFmNk8PPK77fBcKpUk0j32V21ZrPXseAh2htNoLwitmndcWIVBYzSzxzqDt3A== X-Received: by 2002:a5d:4531:: with SMTP id j17mr27088069wra.303.1623236919775; Wed, 09 Jun 2021 04:08:39 -0700 (PDT) Received: from [10.137.0.18] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id t9sm16716066wmq.14.2021.06.09.04.08.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 09 Jun 2021 04:08:39 -0700 (PDT) Subject: Re: postgres_fdw batching vs. (re)creating the tuple slots To: Bharath Rupireddy Cc: Tom Lane , Andres Freund , PostgreSQL-development References: <20210530205831.vib4p7mt23cymhpc@alap3.anarazel.de> <2399204.1622409059@sss.pgh.pa.us> <20210530212616.5k5nxnbunwnwet2a@alap3.anarazel.de> <2433405.1622416631@sss.pgh.pa.us> <4e7ebff8-59ac-c90c-3ecd-8d9dfdfc75ad@enterprisedb.com> <6dc1c07b-87e6-63e6-10b3-cca4698361fb@enterprisedb.com> From: Tomas Vondra Message-ID: Date: Wed, 9 Jun 2021 13:08:36 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------0001BB5006AA166EDCBD5171" 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: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------0001BB5006AA166EDCBD5171 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 6/9/21 12:50 PM, Bharath Rupireddy wrote: > On Wed, Jun 9, 2021 at 4:00 PM Tomas Vondra > wrote: >> >> Hi, >> >> Here's a v2 fixing a silly bug with reusing the same variable in two >> nested loops (worked for simple postgres_fdw cases, but "make check" >> failed). > > I applied these patches and ran make check in postgres_fdw contrib > module, I saw a server crash. Is it the same failure you were saying > above? > Nope, that was causing infinite loop. This is jut a silly mistake on my side - I forgot to replace the i/j variable inside the loop. Here's v3. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------0001BB5006AA166EDCBD5171 Content-Type: text/x-patch; charset=UTF-8; name="0001-create-copy-of-a-descriptor-for-batching-v3.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-create-copy-of-a-descriptor-for-batching-v3.patch" From 494018fd3f2b983be474a85fc12fe3a4dbefa76b Mon Sep 17 00:00:00 2001 From: Tomas Vondra Date: Fri, 4 Jun 2021 12:45:18 +0200 Subject: [PATCH 1/2] create copy of a descriptor for batching --- src/backend/executor/nodeModifyTable.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 379b056310..c287a371a1 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -678,6 +678,8 @@ ExecInsert(ModifyTableState *mtstate, */ if (resultRelInfo->ri_BatchSize > 1) { + TupleDesc tdesc; + /* * If a certain number of tuples have already been accumulated, or * a tuple has come for a different relation than that for the @@ -703,13 +705,16 @@ ExecInsert(ModifyTableState *mtstate, resultRelInfo->ri_BatchSize); } + tdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor); + resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots] = - MakeSingleTupleTableSlot(slot->tts_tupleDescriptor, + MakeSingleTupleTableSlot(tdesc, slot->tts_ops); ExecCopySlot(resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots], slot); + resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots] = - MakeSingleTupleTableSlot(planSlot->tts_tupleDescriptor, + MakeSingleTupleTableSlot(tdesc, planSlot->tts_ops); ExecCopySlot(resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots], planSlot); -- 2.31.1 --------------0001BB5006AA166EDCBD5171 Content-Type: text/x-patch; charset=UTF-8; name="0002-initialize-slots-only-once-for-batching-v3.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0002-initialize-slots-only-once-for-batching-v3.patch" From 9290d7a90d9c93521ae531768055b5c567fcdc51 Mon Sep 17 00:00:00 2001 From: Tomas Vondra Date: Fri, 4 Jun 2021 12:59:47 +0200 Subject: [PATCH 2/2] initialize slots only once for batching --- src/backend/executor/nodeModifyTable.c | 43 ++++++++++++++------------ src/include/nodes/execnodes.h | 1 + 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index c287a371a1..91971ab041 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -678,8 +678,6 @@ ExecInsert(ModifyTableState *mtstate, */ if (resultRelInfo->ri_BatchSize > 1) { - TupleDesc tdesc; - /* * If a certain number of tuples have already been accumulated, or * a tuple has come for a different relation than that for the @@ -705,19 +703,25 @@ ExecInsert(ModifyTableState *mtstate, resultRelInfo->ri_BatchSize); } - tdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor); + /* initialize the slot, if not already done */ + if (resultRelInfo->ri_NumSlots >= resultRelInfo->ri_BatchInitialized) + { + TupleDesc tdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor); - resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots] = - MakeSingleTupleTableSlot(tdesc, - slot->tts_ops); - ExecCopySlot(resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots], - slot); + resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots] = + MakeSingleTupleTableSlot(tdesc, + slot->tts_ops); + ExecCopySlot(resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots], + slot); - resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots] = - MakeSingleTupleTableSlot(tdesc, - planSlot->tts_ops); - ExecCopySlot(resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots], - planSlot); + resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots] = + MakeSingleTupleTableSlot(tdesc, + planSlot->tts_ops); + ExecCopySlot(resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots], + planSlot); + + resultRelInfo->ri_BatchInitialized++; + } resultRelInfo->ri_NumSlots++; @@ -1039,12 +1043,6 @@ ExecBatchInsert(ModifyTableState *mtstate, if (canSetTag && numInserted > 0) estate->es_processed += numInserted; - - for (i = 0; i < numSlots; i++) - { - ExecDropSingleTupleTableSlot(slots[i]); - ExecDropSingleTupleTableSlot(planSlots[i]); - } } /* ---------------------------------------------------------------- @@ -3167,6 +3165,7 @@ ExecEndModifyTable(ModifyTableState *node) */ for (i = 0; i < node->mt_nrels; i++) { + int j; ResultRelInfo *resultRelInfo = node->resultRelInfo + i; if (!resultRelInfo->ri_usesFdwDirectModify && @@ -3174,6 +3173,12 @@ ExecEndModifyTable(ModifyTableState *node) resultRelInfo->ri_FdwRoutine->EndForeignModify != NULL) resultRelInfo->ri_FdwRoutine->EndForeignModify(node->ps.state, resultRelInfo); + + for (j = 0; j < resultRelInfo->ri_NumSlots; j++) + { + ExecDropSingleTupleTableSlot(resultRelInfo->ri_Slots[j]); + ExecDropSingleTupleTableSlot(resultRelInfo->ri_PlanSlots[j]); + } } /* diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 7795a69490..1062a44ee1 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -463,6 +463,7 @@ typedef struct ResultRelInfo /* batch insert stuff */ int ri_NumSlots; /* number of slots in the array */ int ri_BatchSize; /* max slots inserted in a single batch */ + int ri_BatchInitialized; /* number of slots initialized */ TupleTableSlot **ri_Slots; /* input tuples for batch insert */ TupleTableSlot **ri_PlanSlots; -- 2.31.1 --------------0001BB5006AA166EDCBD5171--