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 1l3MXf-0005Zj-3Q for pgsql-hackers@arkaria.postgresql.org; Sat, 23 Jan 2021 17:17:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l3MX8-0002u3-5d for pgsql-hackers@arkaria.postgresql.org; Sat, 23 Jan 2021 17:17:10 +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 1l3MX7-0002tw-QD for pgsql-hackers@lists.postgresql.org; Sat, 23 Jan 2021 17:17:09 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l3MX0-0004ei-AJ for pgsql-hackers@postgresql.org; Sat, 23 Jan 2021 17:17:09 +0000 Received: by mail-ed1-x531.google.com with SMTP id b21so10250964edy.6 for ; Sat, 23 Jan 2021 09:17:01 -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=PZ8Y7Wd5W592DvoObA7I3tTrVGy167aThT083didHy0=; b=R/6KrjsmwzAmsyJWPL/u030b0whOxx33iy2zIX2sDyLqV6eLNC+qlFPVm0/MgZKcNm CfPWQ21u81BzC4LZa+3Y6EVF+2wvHbHCeWG0OdaT7SfMNE/gZUmmb/CBqQmLxj67oK8l qGS7/ThJl2O9YrOumxDJdCBed6KEU9//6aPcIuF1GHNTEynrrUSsAxS16B+gmgLDg61a ccLpLgxYXLDFs7sXuChXvSnIlZGcn6OBgjYVDnqemSCLb6oXqqmKcgX8G5xPND+oa+r3 08PYMfDDUI23MQb3Smgl5BNqwVLr4JrFtIE6yuH1DyYkW0SQpM5JWtXsmGMcMhcA3Tzi fQ/Q== 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=PZ8Y7Wd5W592DvoObA7I3tTrVGy167aThT083didHy0=; b=iGKlbguFpDizU1m6UY84Ut1IGPzkoZpHNT2jHru9NZI6c2qnHjNvHozixuzqFXTdNY T0Li5AoC0UhtD5EvHYmg9SDljtf0guQlP+ehjVDWhzBL6fXAtjVotIrKsSqUQb7x4HcE VsnpHiRDcMYt0u8D4yqW6Z1hrNX+CF5dGx2ORVS68Rbt7xhq0KFDHrlHaBp8sp5NAzoh IhkkzC8bb3bK64esxdtOjtvvL/2fFBBoEowx8ucR1uub9BKC3i2AKBMc54Vrt5xh8YbZ yShma2VigLSxG2N2T9gjiKei0gVmCOGhQOf/KaqMymMcDiRhCpD6k7uGpHQ2UnC7E2or BOjg== X-Gm-Message-State: AOAM533wSf9kE7Y+j/+oZKXe9JhOoNkiATDhtTOmL38J6P/rT+Zwi+XM qgtoz9UWQGmk3gFkpzE94vrvB1b6YeQakgWqfQdKW1tBUs+/Po30CIz4j0LDnZMjBM1iK+MRB4b Xpdq++WQAWmbR1hzrt6uWLmy2eN0Al8TYLZ8QJuHMKpNi+eVvjnurzfsRzUFi9j6GbwrDxZwu/d J8u3QLiyXg51/TWM5FiszjKj8I/0HyV920FbvZFPe58oggY0CpSfzGBX14Q4KuMXlJHaVj/zDKh UZ3niG5x2aSVyYIw6FAP+JwATyqTcP179SkbQ== X-Google-Smtp-Source: ABdhPJyBzONmMDsG+EXf+czyzNkWq2rp4Uz183k+XGYPNj3QfumdUoUECxpg7wHjFnebomaCpurirw== X-Received: by 2002:a50:998f:: with SMTP id m15mr819193edb.342.1611422219804; Sat, 23 Jan 2021 09:16:59 -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 v20sm946567edt.3.2021.01.23.09.16.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 23 Jan 2021 09:16:58 -0800 (PST) Subject: Re: POC: postgres_fdw insert batching To: Amit Langote Cc: "tsunakawa.takay@fujitsu.com" , Zhihong Yu , "Andrey V. Lepikhov" , Tomas Vondra , "pgsql-hackers@postgresql.org" References: <20200628151002.7x5laxwpgvkyiu3q@development> <9d94c863-ce7c-ea7f-6b84-834c98929e2f@enterprisedb.com> <7e70a11e-cf5d-2e5f-a802-274c5e0e236b@enterprisedb.com> <3ca9496f-0827-bf21-b709-593de63440a5@enterprisedb.com> <4faa3d09-0fc8-c4d6-7aec-87428fabd161@enterprisedb.com> <0fc809f6-7c3c-6d06-7ea3-f10765dfdf08@enterprisedb.com> From: Tomas Vondra Message-ID: <067ddf09-225c-e3c5-ea47-4dd39b34ebb0@enterprisedb.com> Date: Sat, 23 Jan 2021 18:16:54 +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; format=flowed 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/23/21 9:31 AM, Amit Langote wrote: > On Thu, Jan 21, 2021 at 11:36 AM Tomas Vondra > wrote: >> On 1/21/21 3:09 AM, tsunakawa.takay@fujitsu.com wrote: >>> From: Tomas Vondra >>>> Right, that's pretty much what I ended up doing (without the CMD_INSERT >>>> check it'd add batching info to explain for updates too, for example). >>>> I'll do a bit more testing on the attached patch, but I think that's the right fix to >>>> push. >>> >>> Thanks to the outer check for operation == CMD_INSERT, the inner one became unnecessary. >>> >> >> Right. I've pushed the fix, hopefully buildfarm will get happy again. > > I was looking at this and it looks like we've got a problematic case > where postgresGetForeignModifyBatchSize() is called from > ExecInitRoutingInfo(). > > That case is when the insert is performed as part of a cross-partition > update of a partitioned table containing postgres_fdw foreign table > partitions. Because we don't check the operation in > ExecInitRoutingInfo() when calling GetForeignModifyBatchSize(), such > inserts attempt to use batching. However the ResultRelInfo may be one > for the original update operation, so ri_FdwState contains a > PgFdwModifyState with batch_size set to 0, because updates don't > support batching. As things stand now, > postgresGetForeignModifyBatchSize() simply returns that, tripping the > following Assert in the caller. > > Assert(partRelInfo->ri_BatchSize >= 1); > > Use this example to see the crash: > > create table p (a int) partition by list (a); > create table p1 (like p); > create extension postgres_fdw; > create server lb foreign data wrapper postgres_fdw ; > create user mapping for current_user server lb; > create foreign table fp1 (a int) server lb options (table_name 'p1'); > alter table p attach partition fp1 for values in (1); > create or replace function report_trig_details() returns trigger as $$ > begin raise notice '% % on %', tg_when, tg_op, tg_relname; if tg_op = > 'DELETE' then return old; end if; return new; end; $$ language > plpgsql; > create trigger trig before update on fp1 for each row execute function > report_trig_details(); > create table p2 partition of p for values in (2); > insert into p values (2); > update p set a = 1; -- crashes > > So we let's check mtstate->operation == CMD_INSERT in > ExecInitRoutingInfo() to prevent calling GetForeignModifyBatchSize() > in cross-update situations where mtstate->operation would be > CMD_UPDATE. > > I've attached a patch. > Thanks for catching this. I think it'd be good if the fix included a regression test. The example seems like a good starting point, not sure if it can be simplified further. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company