@@ -96,7 +96,9 @@ typedef struct
9696typedef struct
9797{
9898 Index child_varno ;
99- Oid parent_relid ;
99+ Oid parent_relid ,
100+ parent_reltype ,
101+ child_reltype ;
100102 List * translated_vars ;
101103} adjust_appendrel_varnos_cxt ;
102104
@@ -483,9 +485,11 @@ handle_modification_query(Query *parse, transform_query_cxt *context)
483485 if (!inh_translation_list_is_trivial (translated_vars ))
484486 {
485487 /* Translate varnos for this child */
486- aav_cxt .child_varno = result_rti ;
487- aav_cxt .parent_relid = parent ;
488- aav_cxt .translated_vars = translated_vars ;
488+ aav_cxt .child_varno = result_rti ;
489+ aav_cxt .parent_relid = parent ;
490+ aav_cxt .parent_reltype = RelationGetDescr (parent_rel )-> tdtypeid ;
491+ aav_cxt .child_reltype = RelationGetDescr (child_rel )-> tdtypeid ;
492+ aav_cxt .translated_vars = translated_vars ;
489493 adjust_appendrel_varnos ((Node * ) parse , & aav_cxt );
490494
491495 /* Translate column privileges for this child */
@@ -612,24 +616,43 @@ adjust_appendrel_varnos(Node *node, adjust_appendrel_varnos_cxt *context)
612616 {
613617 Var * var = (Var * ) node ;
614618
615- /* Don't transform system columns & other relations' Vars */
616- if (var -> varattno > 0 && var -> varno == context -> child_varno )
619+ /* See adjust_appendrel_attrs_mutator() */
620+ if (var -> varno == context -> child_varno )
617621 {
618- Var * child_var ;
622+ if (var -> varattno > 0 )
623+ {
624+ Var * child_var ;
619625
620- var = copyObject (var );
626+ var = copyObject (var );
621627
622- if (var -> varattno > list_length (context -> translated_vars ))
623- elog (ERROR , "attribute %d of relation \"%s\" does not exist" ,
624- var -> varattno , get_rel_name (context -> parent_relid ));
628+ if (var -> varattno > list_length (context -> translated_vars ))
629+ elog (ERROR , "attribute %d of relation \"%s\" does not exist" ,
630+ var -> varattno , get_rel_name (context -> parent_relid ));
625631
626- child_var = list_nth (context -> translated_vars , var -> varattno - 1 );
627- if (!child_var )
628- elog (ERROR , "attribute %d of relation \"%s\" does not exist" ,
629- var -> varattno , get_rel_name (context -> parent_relid ));
632+ child_var = list_nth (context -> translated_vars , var -> varattno - 1 );
633+ if (!child_var )
634+ elog (ERROR , "attribute %d of relation \"%s\" does not exist" ,
635+ var -> varattno , get_rel_name (context -> parent_relid ));
630636
631- /* Transform attribute number */
632- var -> varattno = child_var -> varattno ;
637+ /* Transform attribute number */
638+ var -> varattno = child_var -> varattno ;
639+ }
640+ else if (var -> varattno == 0 )
641+ {
642+ ConvertRowtypeExpr * r = makeNode (ConvertRowtypeExpr );
643+
644+ Assert (var -> vartype = context -> parent_reltype );
645+
646+ r -> arg = (Expr * ) var ;
647+ r -> resulttype = context -> parent_reltype ;
648+ r -> convertformat = COERCE_IMPLICIT_CAST ;
649+ r -> location = -1 ;
650+
651+ /* Make sure the Var node has the right type ID, too */
652+ var -> vartype = context -> child_reltype ;
653+
654+ return (Node * ) r ;
655+ }
633656 }
634657
635658 return (Node * ) var ;
0 commit comments