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 1lqvTe-0007UA-MQ for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Jun 2021 10:30:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lqvTd-0002nS-Jh for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Jun 2021 10:30:25 +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 1lqvTd-0002nJ-7v for pgsql-hackers@lists.postgresql.org; Wed, 09 Jun 2021 10:30:25 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lqvTa-0002Pb-9i for pgsql-hackers@postgresql.org; Wed, 09 Jun 2021 10:30:23 +0000 Received: by mail-wr1-x42d.google.com with SMTP id c9so16200219wrt.5 for ; Wed, 09 Jun 2021 03:30:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=0R3OEPshliWcwYaGkDwXzMiFhadINODhMwdq81m3OJE=; b=mjQjr/HFLi6vixxMQw3ABIFYmyk2oVs1Oe71LCRaAPCytSQuledaWIzng+90rJ+fFM +GEX4cmuSFqUw8QmqdldZAjTegbf4vca5RL6SNJXQ+LoYq9rZ7ETLRQtVlYrkMCcmlyy pcSKvw4SrvxBG/KJr6Xy4HVZc3pMXBRzwCIrdQoPEBFvqKVT7xWgcy3+X7uehg17oCQQ J5Qkihgikod3Z+ozQtyGnxBjl5YvHbQbM6hlLtYEX6guKyuj3ZK0D0zkfojTYyKjl+1j vXcl/5PmmPPz3bhYXpCHANd4RojMayRj1xi/n1o93M0XycSc3OtA7Z/R6rRuko7XD810 lnJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=0R3OEPshliWcwYaGkDwXzMiFhadINODhMwdq81m3OJE=; b=fgwlI0uLEUACOUhGBVLdgUSfImTOQOrzRGulXLP44/eTWMFo7jMWfQ27fDT/XuM1tC HH8bmRfNW2GNXHsNsrTCQLW5xegBoM5zUzFtNtF9gOWiqlvpDQOfklgUXe0GnOv1NJdB /Aojh8pEw3BsP4+M2BlEuupQw/hZFWtyuxWdVl1naBuvYLYTYwAWYYvdYHFnKe3iK4ZE atufc9jSzHwmOsuCJq69EJ5D8wNntGOxumiNgpO3ZG3O+TLxu4bt3gfdUNSO9T73xLK+ wvSo/oSsJ6QHN3qfANfMELBifH0YbErJs7LBEYeet2WhqWuEUgodmjXe91z1434iM8aY aWYQ== X-Gm-Message-State: AOAM533zHQfStbBVTu7amYJxDXv31jYrT4qlfD/C9GF30JQ4maY/x3xX 1pjGJFcQ0Wqk/gw6r9G2+HaE7ujTTJMUa4gdjt6tFqTG23N2Uws29CST4x0slZQ7nTJ7vDQJWm2 TmWxr71FlSX325QBBH5+vKNVmI7XeJT3NvAwSg4djY/m9SOqwzAAqTpMMgYHfxeyJZfVEWaSNDW wCW+GQG0ad1o/BQlTv/UsHG0FoIJc1r8l5B2k5PugyF9LPAyVSg+EnxXjIA/lgEpvYfi0MRTp89 8L9xOgKT8aTak9tBqkwPMCk6IsoY9mpndqwSK5n X-Google-Smtp-Source: ABdhPJyA/GZjUXjyURQ/RLpqhiim+wX0Rc04kuK5aNmfPTDbxVMvbwAlpLxMhPYV22b3AmaIDw67DQ== X-Received: by 2002:adf:ea49:: with SMTP id j9mr24757596wrn.366.1623234620475; Wed, 09 Jun 2021 03:30:20 -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 n20sm20268073wmk.12.2021.06.09.03.30.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 09 Jun 2021 03:30:19 -0700 (PDT) Subject: Re: postgres_fdw batching vs. (re)creating the tuple slots From: Tomas Vondra To: Tom Lane , Andres Freund Cc: 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> Message-ID: <6dc1c07b-87e6-63e6-10b3-cca4698361fb@enterprisedb.com> Date: Wed, 9 Jun 2021 12:30:17 +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: <4e7ebff8-59ac-c90c-3ecd-8d9dfdfc75ad@enterprisedb.com> Content-Type: multipart/mixed; boundary="------------1B56B9ED3F1FEDA6967DA701" 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. --------------1B56B9ED3F1FEDA6967DA701 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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). regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------1B56B9ED3F1FEDA6967DA701 Content-Type: text/x-patch; charset=UTF-8; name="0001-create-copy-of-a-descriptor-for-batching-v2.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-create-copy-of-a-descriptor-for-batching-v2.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 --------------1B56B9ED3F1FEDA6967DA701 Content-Type: text/x-patch; charset=UTF-8; name="0002-initialize-slots-only-once-for-batching-v2.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0002-initialize-slots-only-once-for-batching-v2.patch" From 22a7a2c295ccce850da5f02d6239975057345b32 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..81b3b522c2 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[i]); + ExecDropSingleTupleTableSlot(resultRelInfo->ri_PlanSlots[i]); + } } /* 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 --------------1B56B9ED3F1FEDA6967DA701--