From: Etsuro Fujita <fujita.etsuro@lab.ntt.co.jp>
To: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
To: Ildus Kurbangaliev <i.kurbangaliev@postgrespro.ru>
Cc: Dilip Kumar <dilipbalaut@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Cc: Robert Haas <robertmhaas@gmail.com>
Subject: Re: Bug in ExecModifyTable function and trigger issues for foreign tables
Date: Tue, 16 May 2017 21:36:11 +0900
Message-ID: <dbf4b9ff-b0be-2396-7c79-d5763ae8a034@lab.ntt.co.jp> (raw)
In-Reply-To: <CAFjFpReeA3G3-U-FsR79zx_xFBVZc73hhWcH4OVWMb+ZFViRQg@mail.gmail.com>
References: <20170514150525.0346ba72@postgrespro.ru>
<CAFiTN-v_WOycpycrh2c_TpzVU=yOjMNWwHnUYYxccK-fOaWjpw@mail.gmail.com>
<CAFiTN-vx1pYKbtkFdX9srL+dghCJdZ9-DgRb57oDs5GuCDCD2Q@mail.gmail.com>
<20170515121638.0c076bcc@wp.localdomain>
<CAFjFpRfQ1pkCjNQFVOP_BPJfc7OR3596nqVVFBgAiDEZqB4Azg@mail.gmail.com>
<20170515165455.545dd3d6@wp.localdomain>
<CAFjFpRcQudmKYU4PJRN1-wp5K4aksupSoVpcRVAzES_49T=Vng@mail.gmail.com>
<20170516150516.1b616230@wp.localdomain>
<CAFjFpReeA3G3-U-FsR79zx_xFBVZc73hhWcH4OVWMb+ZFViRQg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>
On 2017/05/16 21:11, Ashutosh Bapat wrote:
> On Tue, May 16, 2017 at 5:35 PM, Ildus Kurbangaliev
> <i.kurbangaliev@postgrespro.ru> wrote:
>> I agree. Maybe this issue should be added to Postgresql Open Items?
>> I think there should be some complex solution that fixes not only
>> triggers for foreign tables at table partitioning, but covers other
>> possible not working cases.
> I doubt if this is an open item, since DMLs on foreign tables are
> supported since 9.3 and support to add foreign tables to inheritance
> was added back in 9.5.
I think this issue was introduced by the latter, so that was my fault.
One approach I came up with to fix this issue is to rewrite the
targetList entries of an inherited UPDATE/DELETE properly using
rewriteTargetListUD, when generating a plan for each child table in
inheritance_planner. Attached is a WIP patch for that. Maybe I am
missing something, though.
Best regards,
Etsuro Fujita
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackersAttachments:inherited-foreign-modify-WIP.patch (application/x-patch, 5.0K) download
*** a/src/backend/executor/nodeModifyTable.c
--- b/src/backend/executor/nodeModifyTable.c
***************
*** 1469,1474 **** ExecModifyTable(ModifyTableState *node)
--- 1469,1475 ----
resultRelInfo++;
subplanstate = node->mt_plans[node->mt_whichplan];
junkfilter = resultRelInfo->ri_junkFilter;
+ tupleid = NULL;
estate->es_result_relation_info = resultRelInfo;
EvalPlanQualSetPlan(&node->mt_epqstate, subplanstate->plan,
node->mt_arowmarks[node->mt_whichplan]);
*** a/src/backend/optimizer/prep/prepunion.c
--- b/src/backend/optimizer/prep/prepunion.c
***************
*** 47,52 ****
--- 47,53 ----
#include "optimizer/tlist.h"
#include "parser/parse_coerce.h"
#include "parser/parsetree.h"
+ #include "rewrite/rewriteHandler.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/selfuncs.h"
***************
*** 110,115 **** static Node *adjust_appendrel_attrs_mutator(Node *node,
--- 111,117 ----
static Relids adjust_relid_set(Relids relids, Index oldrelid, Index newrelid);
static List *adjust_inherited_tlist(List *tlist,
AppendRelInfo *context);
+ static void rewrite_inherited_tlist(Query *parse, RangeTblEntry *target_rte);
/*
***************
*** 1806,1811 **** adjust_appendrel_attrs(PlannerInfo *root, Node *node, AppendRelInfo *appinfo)
--- 1808,1826 ----
newnode->targetList =
adjust_inherited_tlist(newnode->targetList,
appinfo);
+ /* And fix UPDATE/DELETE junk tlist entries too, if needed */+ if (newnode->commandType == CMD_UPDATE ||+ newnode->commandType == CMD_DELETE)+ {+ RangeTblEntry *childrte;+ RangeTblEntry *parentrte;+ + childrte = planner_rt_fetch(appinfo->child_relid, root);+ parentrte = planner_rt_fetch(appinfo->parent_relid, root);+ if (childrte->relkind == RELKIND_FOREIGN_TABLE ||+ parentrte->relkind == RELKIND_FOREIGN_TABLE)+ rewrite_inherited_tlist(newnode, childrte);+ }
}
result = (Node *) newnode;
}
***************
*** 2139,2144 **** adjust_inherited_tlist(List *tlist, AppendRelInfo *context)
--- 2154,2196 ----
}
/*
+ * Rewrite the targetlist entries of an inherited UPDATE/DELETE operation+ */+ static void+ rewrite_inherited_tlist(Query *parse, RangeTblEntry *target_rte)+ {+ ListCell *tl;+ List *new_tlist = NIL;+ Relation target_relation;+ + /*+ * Open the target relation; we need not lock it since it was already+ * locked by expand_inherited_rtentry().+ */+ target_relation = heap_open(target_rte->relid, NoLock);+ + /* Create a new tlist without junk tlist entries */+ foreach(tl, parse->targetList)+ {+ TargetEntry *tle = (TargetEntry *) lfirst(tl);+ + if (tle->resjunk)+ continue; /* don't need junk items */+ + new_tlist = lappend(new_tlist, tle);+ }+ + /* Replace the targetList with the new one */+ parse->targetList = new_tlist;+ + /* And add junk tlist entries needed for UPDATE/DELETE */+ rewriteTargetListUD(parse, target_rte, target_relation);+ + /* Close the target relation, but keep the lock */+ heap_close(target_relation, NoLock);+ }+ + /*
* adjust_appendrel_attrs_multilevel
* Apply Var translations from a toplevel appendrel parent down to a child.
*
*** a/src/backend/rewrite/rewriteHandler.c
--- b/src/backend/rewrite/rewriteHandler.c
***************
*** 72,79 **** static TargetEntry *process_matched_tle(TargetEntry *src_tle,
static Node *get_assignment_input(Node *node);
static void rewriteValuesRTE(RangeTblEntry *rte, Relation target_relation,
List *attrnos);
- static void rewriteTargetListUD(Query *parsetree, RangeTblEntry *target_rte,- Relation target_relation);
static void markQueryForLocking(Query *qry, Node *jtnode,
LockClauseStrength strength, LockWaitPolicy waitPolicy,
bool pushedDown);
--- 72,77 ----
***************
*** 1269,1275 **** rewriteValuesRTE(RangeTblEntry *rte, Relation target_relation, List *attrnos)
* For UPDATE queries, this is applied after rewriteTargetListIU. The
* ordering isn't actually critical at the moment.
*/
! static void
rewriteTargetListUD(Query *parsetree, RangeTblEntry *target_rte,
Relation target_relation)
{
--- 1267,1273 ----
* For UPDATE queries, this is applied after rewriteTargetListIU. The
* ordering isn't actually critical at the moment.
*/
! void
rewriteTargetListUD(Query *parsetree, RangeTblEntry *target_rte,
Relation target_relation)
{
*** a/src/include/rewrite/rewriteHandler.h
--- b/src/include/rewrite/rewriteHandler.h
***************
*** 23,28 **** extern void AcquireRewriteLocks(Query *parsetree,
--- 23,30 ----
bool forUpdatePushedDown);
extern Node *build_column_default(Relation rel, int attrno);
+ extern void rewriteTargetListUD(Query *parsetree, RangeTblEntry *target_rte,+ Relation target_relation);
extern Query *get_view_query(Relation view);
extern const char *view_query_is_auto_updatable(Query *viewquery,
bool check_cols);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: fujita.etsuro@lab.ntt.co.jp, ashutosh.bapat@enterprisedb.com, i.kurbangaliev@postgrespro.ru, dilipbalaut@gmail.com, robertmhaas@gmail.com
Subject: Re: Bug in ExecModifyTable function and trigger issues for foreign tables
In-Reply-To: <dbf4b9ff-b0be-2396-7c79-d5763ae8a034@lab.ntt.co.jp>
References: <20170514150525.0346ba72@postgrespro.ru> <CAFiTN-v_WOycpycrh2c_TpzVU=yOjMNWwHnUYYxccK-fOaWjpw@mail.gmail.com> <CAFiTN-vx1pYKbtkFdX9srL+dghCJdZ9-DgRb57oDs5GuCDCD2Q@mail.gmail.com> <20170515121638.0c076bcc@wp.localdomain> <CAFjFpRfQ1pkCjNQFVOP_BPJfc7OR3596nqVVFBgAiDEZqB4Azg@mail.gmail.com> <20170515165455.545dd3d6@wp.localdomain> <CAFjFpRcQudmKYU4PJRN1-wp5K4aksupSoVpcRVAzES_49T=Vng@mail.gmail.com> <20170516150516.1b616230@wp.localdomain> <CAFjFpReeA3G3-U-FsR79zx_xFBVZc73hhWcH4OVWMb+ZFViRQg@mail.gmail.com> <dbf4b9ff-b0be-2396-7c79-d5763ae8a034@lab.ntt.co.jp>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox