Commit d3cc37f1d801a6b5cad9bf179274a8d767f1ee50 added this to ExecInitModifyTable:

+ /* The root table RT index is at the head of the partitioned_rels list */
+   if (node->partitioned_rels)
+   {
+       Index   root_rti;
+       Oid     root_oid;
+
+       root_rti = linitial_int(node->partitioned_rels);
+       root_oid = getrelid(root_rti, estate->es_range_table);
+       rel = heap_open(root_oid, NoLock);  /* locked by InitPlan */
+   }

but I noticed that that function doesn't use the relation descriptor at all. Since partitioned_rels is given in case of an UPDATE/DELETE on a partitioned table, the relation is opened in that case, but the relation descriptor isn't referenced at all in initializing WITH CHECK OPTION constraints and/or RETURNING projections. (The mtstate->resultRelinfo array is referenced in those initialization, instead.) So, I'd like to propose to remove this from that function. Attached is a patch for that.

Best regards,
Etsuro Fujita
*** a/src/backend/executor/nodeModifyTable.c
--- b/src/backend/executor/nodeModifyTable.c
***************
*** 45,51 ****
  #include "foreign/fdwapi.h"
  #include "miscadmin.h"
  #include "nodes/nodeFuncs.h"
- #include "parser/parsetree.h"
  #include "storage/bufmgr.h"
  #include "storage/lmgr.h"
  #include "utils/builtins.h"
--- 45,50 ----
***************
*** 1767,1786 **** ExecInitModifyTable(ModifyTable *node, EState *estate, int 
eflags)
  
        estate->es_result_relation_info = saved_resultRelInfo;
  
-       /* The root table RT index is at the head of the partitioned_rels list 
*/
-       if (node->partitioned_rels)
-       {
-               Index           root_rti;
-               Oid                     root_oid;
- 
-               root_rti = linitial_int(node->partitioned_rels);
-               root_oid = getrelid(root_rti, estate->es_range_table);
-               rel = heap_open(root_oid, NoLock);              /* locked by 
InitPlan */
-       }
-       else
-               rel = mtstate->resultRelInfo->ri_RelationDesc;
- 
        /* Build state for INSERT tuple routing */
        if (operation == CMD_INSERT &&
                rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
        {
--- 1766,1773 ----
  
        estate->es_result_relation_info = saved_resultRelInfo;
  
        /* Build state for INSERT tuple routing */
+       rel = mtstate->resultRelInfo->ri_RelationDesc;
        if (operation == CMD_INSERT &&
                rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
        {
***************
*** 1959,1968 **** ExecInitModifyTable(ModifyTable *node, EState *estate, int 
eflags)
                mtstate->ps.ps_ExprContext = NULL;
        }
  
-       /* Close the root partitioned rel if we opened it above. */
-       if (rel != mtstate->resultRelInfo->ri_RelationDesc)
-               heap_close(rel, NoLock);
- 
        /*
         * If needed, Initialize target list, projection and qual for ON 
CONFLICT
         * DO UPDATE.
--- 1946,1951 ----
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to