Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fH7fD-0004dD-2B for pgsql-hackers@arkaria.postgresql.org; Fri, 11 May 2018 13:00:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fH7fB-0007fc-CE for pgsql-hackers@arkaria.postgresql.org; Fri, 11 May 2018 13:00:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fH7fB-0007fS-56 for pgsql-hackers@lists.postgresql.org; Fri, 11 May 2018 13:00:45 +0000 Received: from tama500.ecl.ntt.co.jp ([129.60.39.148]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fH7f6-00018o-Uw for pgsql-hackers@postgresql.org; Fri, 11 May 2018 13:00:44 +0000 Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama500.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id w4BD0bac026858; Fri, 11 May 2018 22:00:37 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id E14016391B9; Fri, 11 May 2018 22:00:37 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id D62476391B7; Fri, 11 May 2018 22:00:37 +0900 (JST) Received: from [IPv6:::1] (unknown [129.60.241.75]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id CFA4C4006AF; Fri, 11 May 2018 22:00:37 +0900 (JST) Message-ID: <5AF59415.10309@lab.ntt.co.jp> Date: Fri, 11 May 2018 22:01:09 +0900 From: Etsuro Fujita User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 MIME-Version: 1.0 Subject: Re: Expression errors with "FOR UPDATE" and postgres_fdw with partition wise join enabled. References: <5AD5B1E0.8020404@lab.ntt.co.jp> <5AD5C5A1.1070509@lab.ntt.co.jp> <5ADF12D1.1030406@lab.ntt.co.jp> <5AE1C0D3.5060009@lab.ntt.co.jp> <5AE84FBF.3040905@lab.ntt.co.jp> <5AF2E09F.9060208@lab.ntt.co.jp> <5AF440C2.3040805@lab.ntt.co.jp> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CC-Mail-RelayStamp: 1 To: Ashutosh Bapat Cc: Rajkumar Raghuwanshi , pgsql-hackers X-TM-AS-MML: disable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk (2018/05/11 16:17), Ashutosh Bapat wrote: > On Thu, May 10, 2018 at 6:23 PM, Etsuro Fujita > wrote: >> Yeah, but I think the IsA test would be sufficient to make things work >> correctly because we can assume that there aren't any other nodes than Var, >> PHV, and CRE translating a wholerow value of a child table into a wholerow >> value of its parent table's rowtype in the expression clause to which we >> apply pull_var_clause with PVC_INCLUDE_CONVERTROWTYPES. > > Not really. > Consider following case using the same tables fprt1 and fprt2 in test > sql/postgres_fdw.sql > create function row_as_is(a ftprt2_p1) returns ftprt2_p1 as $$begin > return a; end;$$ language plpgsql; > > With the change you propose i.e. > > diff --git a/src/backend/optimizer/util/var.c b/src/backend/optimizer/util/var.c > index f972712..088da14 100644 > --- a/src/backend/optimizer/util/var.c > +++ b/src/backend/optimizer/util/var.c > @@ -646,8 +646,9 @@ pull_var_clause_walker(Node *node, > pull_var_clause_context *context) > else > elog(ERROR, "PlaceHolderVar found where not expected"); > } > - else if (is_converted_whole_row_reference(node)) > + else if (IsA(node, ConvertRowtypeExpr)) > { > + Assert(is_converted_whole_row_reference(node)); > if (context->flags& PVC_INCLUDE_CONVERTROWTYPES) > { > context->varlist = lappend(context->varlist, node); > > > following query crashes at Assert(is_converted_whole_row_reference(node)); I guess you forgot to show the query. > If we remove that assert, it would end up pushing down row_as_is(), > which is a local user defined function, to the foreign server. That's > not desirable since the foreign server may not have that user defined > function. I don't understand the counter-example you tried to show, but I think that I was wrong here; the IsA test isn't sufficient. >> BTW, one thing I noticed about the new version of the patch is: there is yet >> another case where pull_var_clause produces an error. Here is an example: >> To produce this, apply the patch in [1] as well as the new version; without >> that patch in [1], the update query would crash the server (see [1] for >> details). To fix this, I think we need to pass PVC_RECURSE_CONVERTROWTYPES >> to pull_var_clause in build_remote_returning in postgres_fdw.c as well. > > I missed that call to PVC since it was added after I had created my > patches. That's a disadvantage of having changed PVC to use flags > instead of bools. We won't notice such changes. Thanks for pointing it > out. Changed as per your suggestion. Thanks for that change and the updated version! Yet yet another case where pull_var_clause produces an error: postgres=# create table list_pt (a int, b text) partition by list (a); CREATE TABLE postgres=# create table list_ptp1 partition of list_pt for values in (1); CREATE TABLE postgres=# alter table list_ptp1 add constraint list_ptp1_check check (list_ptp1::list_pt::text != NULL); ERROR: ConvertRowtypeExpr found where not expected The patch kept the flags passed to pull_var_clause in src/backend/catalog/heap.c as-is, but I think we need to modify that accordingly. Probably, the flags passed to pull_var_clause in CreateTrigger as well. Having said that, I started to think this approach would make code much complicated. Another idea I came up with to avoid that would be to use pull_var_clause as-is and then rewrite wholerows of partitions back to ConvertRowtypeExprs translating those wholerows to wholerows of their parents tables' rowtypes if necessary. That would be annoying and a little bit inefficient, but the places where we need to rewrite is limited: prepare_sort_from_pathkeys and build_tlist_to_deparse, IIUC. So we could really minimize the code change and avoid the additional overhead caused by the is_converted_whole_row_reference test added to pull_var_clause. I think the latter would be rather important, because PWJ is disabled by default. What do you think about that approach? Best regards, Etsuro Fujita