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 1l2NjI-0006ee-Vn for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Jan 2021 00:21:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l2NjG-0003Oz-Pn for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Jan 2021 00:21:38 +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 1l2NjG-0003Os-El for pgsql-hackers@lists.postgresql.org; Thu, 21 Jan 2021 00:21:38 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l2NjC-00024F-WD for pgsql-hackers@postgresql.org; Thu, 21 Jan 2021 00:21:36 +0000 Received: by mail-ed1-x52a.google.com with SMTP id s11so434774edd.5 for ; Wed, 20 Jan 2021 16:21:34 -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=hmJXBcUO2YjJbjlstQJF5CPL7ibnp3iAsy2Hpqg2KY0=; b=gEzJYSMmoBJrDZxfzbwfeH5MEbaDXBXdi5MjBiWctsv8/TVBQiCOsczMbCeyinWjU/ BX612l0/IrZV7F/olCFo4kMk59ph5qT4845by2mU/OPKJkf5hJDlm1uv/+azwgQ6tJce EoZY3z0FQszc65pMdyTbzDYnS1N2GkRFzp3+iityXQjJnt9l5hfKqFHAgBdZmwc9XHF+ TVTglYi1bzelGhwnvniM49q8Mxu2mR8VqOgcu3dKKkrgF/TXkCQ4iFEq87vu6FbNAQec Y6f1F0hjQ1avjrcm88yXqVY6JirK6XKMz0uRu7/BgqHmz1aIlQhFm+Sr9sF1b8VT5E45 6sPQ== 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=hmJXBcUO2YjJbjlstQJF5CPL7ibnp3iAsy2Hpqg2KY0=; b=YDyDC24Vq4FHrFLW89Fa64lpd9GFhlvXSnz3ha1wD2MpT+kTR2dtCCJVuBuoDlAoor KcPUOsDMosGbhbJub/SRSfjD1R3/wNog67JTzXHTCGhTYZQwW+TNrn8qqaZqNAMtdBuG C5z9fIfu8UqhzO5Iatitx7+XsCYA7VNh3nLfdNNAfhs4PKOFaSu8Mrkp9MDzCGtC86Sh hyZmWQdQO3SUB05lSiNSUtAGb8ewozYpFi3y54AuvOo1HZVEnmKZKU13zNOe2v7uukb2 eNnJ9zO50xmH6cBcXa4OFaZxgZdTP8o0bMUVu7Wndqr4GS6GDqzylGZo7clf00lNJ/Ji K7oA== X-Gm-Message-State: AOAM531J6H1G58AIPJieOldVdec1VtkFtaeBHi6Y8xwCj8m6MHvRS65N M+XViGealRyCF72c/TVs3SU5OWXma/EqdBWTxDSP/+cS5Q52ZxAjJqxDUQDHfKl43uh6r1YgF0e ogpEtKg42yK8POnopiFViXuQbm96ETeFZhbEeywxl2KBy7GaR6v9ksZiigu+hI4SsDgnkd4BLap wzWYP5MXZ13wleNCb/8UAAhivBF4Oi8flFHPlv7fB01l9p1v7a6zNeGr9NS7x9Umc/5kxcXsuAY vfJwwTQGKzN0LYnTVFlba2D49yVG+2kvFtipg== X-Google-Smtp-Source: ABdhPJyqHE7dpkgwlmZHt1eRGirvrV88327n1EfS4E8ILHpqKktIBsHUJrg1/6/VkV8LG7Vuvnwajw== X-Received: by 2002:a50:eb81:: with SMTP id y1mr2641103edr.176.1611188492821; Wed, 20 Jan 2021 16:21:32 -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 gt12sm1506391ejb.38.2021.01.20.16.21.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Jan 2021 16:21:31 -0800 (PST) Subject: Re: POC: postgres_fdw insert batching To: Amit Langote Cc: "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> From: Tomas Vondra Message-ID: <261fbedb-1ced-82f7-e978-60cfe17bce07@enterprisedb.com> Date: Thu, 21 Jan 2021 01:21:29 +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="------------36DD44A54A605C0C7B4EAA1A" 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. --------------36DD44A54A605C0C7B4EAA1A Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 1/21/21 12:52 AM, Tomas Vondra wrote: > Hmm, seems that florican doesn't like this :-( > > https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=florican&dt=2021-01-20%2023%3A08%3A15 > > > It's a i386 machine running FreeBSD, so not sure what exactly it's picky > about. But when I tried running this under valgrind, I get some strange > failures in the new chunk in ExecInitModifyTable: > >   /* >    * Determine if the FDW supports batch insert and determine the batch >    * 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; > >   Assert(resultRelInfo->ri_BatchSize >= 1); > > It seems as if the resultRelInfo is not initialized, or something like > that. I wouldn't be surprised if the 32-bit machine was pickier and > failing because of that. > > A sample of the valgrind log is attached. It's pretty much just > repetitions of these three reports. > OK, it's definitely accessing uninitialized memory, because the resultRelInfo (on line 2801, i.e. the "if" condition) looks like this: (gdb) p resultRelInfo $1 = (ResultRelInfo *) 0xe595988 (gdb) p *resultRelInfo $2 = {type = 2139062142, ri_RangeTableIndex = 2139062143, ri_RelationDesc = 0x7f7f7f7f7f7f7f7f, ri_NumIndices = 2139062143, ri_IndexRelationDescs = 0x7f7f7f7f7f7f7f7f, ri_IndexRelationInfo = 0x7f7f7f7f7f7f7f7f, ri_TrigDesc = 0x7f7f7f7f7f7f7f7f, ri_TrigFunctions = 0x7f7f7f7f7f7f7f7f, ri_TrigWhenExprs = 0x7f7f7f7f7f7f7f7f, ri_TrigInstrument = 0x7f7f7f7f7f7f7f7f, ri_ReturningSlot = 0x7f7f7f7f7f7f7f7f, ri_TrigOldSlot = 0x7f7f7f7f7f7f7f7f, ri_TrigNewSlot = 0x7f7f7f7f7f7f7f7f, ri_FdwRoutine = 0x7f7f7f7f7f7f7f7f, ri_FdwState = 0x7f7f7f7f7f7f7f7f, ri_usesFdwDirectModify = 127, ri_NumSlots = 2139062143, ri_BatchSize = 2139062143, ri_Slots = 0x7f7f7f7f7f7f7f7f, ri_PlanSlots = 0x7f7f7f7f7f7f7f7f, ri_WithCheckOptions = 0x7f7f7f7f7f7f7f7f, ri_WithCheckOptionExprs = 0x7f7f7f7f7f7f7f7f, ri_ConstraintExprs = 0x7f7f7f7f7f7f7f7f, ri_GeneratedExprs = 0x7f7f7f7f7f7f7f7f, ri_NumGeneratedNeeded = 2139062143, ri_junkFilter = 0x7f7f7f7f7f7f7f7f, ri_returningList = 0x7f7f7f7f7f7f7f7f, ri_projectReturning = 0x7f7f7f7f7f7f7f7f, ri_onConflictArbiterIndexes = 0x7f7f7f7f7f7f7f7f, ri_onConflict = 0x7f7f7f7f7f7f7f7f, ri_PartitionCheckExpr = 0x7f7f7f7f7f7f7f7f, ri_PartitionRoot = 0x7f7f7f7f7f7f7f7f, ri_RootToPartitionMap = 0x8, ri_PartitionTupleSlot = 0x8, ri_ChildToRootMap = 0xe5952b0, ri_CopyMultiInsertBuffer = 0xe596740} (gdb) I may be wrong, but the most likely explanation seems to be this is due to the junk filter initialization, which simply moves past the end of the mtstate->resultRelInfo array. It kinda seems the GetForeignModifyBatchSize call should happen before that block. The attached patch fixes this for me (i.e. regression tests pass with no valgrind reports. Or did I get that wrong? -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------36DD44A54A605C0C7B4EAA1A Content-Type: text/x-patch; charset=UTF-8; name="modifytable-fix.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="modifytable-fix.patch" diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 9c36860704..2ac4999dc8 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -2672,6 +2672,23 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) } } + /* + * Determine if the FDW supports batch insert and determine the batch + * 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; + + Assert(resultRelInfo->ri_BatchSize >= 1); + /* select first subplan */ mtstate->mt_whichplan = 0; subplan = (Plan *) linitial(node->plans); @@ -2793,23 +2810,6 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) } } - /* - * Determine if the FDW supports batch insert and determine the batch - * 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; - - Assert(resultRelInfo->ri_BatchSize >= 1); - /* * Lastly, if this is not the primary (canSetTag) ModifyTable node, add it * to estate->es_auxmodifytables so that it will be run to completion by --------------36DD44A54A605C0C7B4EAA1A--