@@ -1824,14 +1824,12 @@ ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot,
18241824 * the partition constraint, if any.
18251825 *
18261826 * Note: 'slot' contains the tuple to check the constraints of, which may
1827- * have been converted from the original input tuple after tuple routing,
1828- * while 'orig_slot' contains the original tuple to be shown in the message,
1829- * if an error occurs.
1827+ * have been converted from the original input tuple after tuple routing.
1828+ * 'resultRelInfo' is the original result relation, before tuple routing.
18301829 */
18311830void
18321831ExecConstraints (ResultRelInfo * resultRelInfo ,
1833- TupleTableSlot * slot , TupleTableSlot * orig_slot ,
1834- EState * estate )
1832+ TupleTableSlot * slot , EState * estate )
18351833{
18361834 Relation rel = resultRelInfo -> ri_RelationDesc ;
18371835 TupleDesc tupdesc = RelationGetDescr (rel );
@@ -1854,23 +1852,37 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
18541852 {
18551853 char * val_desc ;
18561854 Relation orig_rel = rel ;
1857- TupleDesc orig_tupdesc = tupdesc ;
1855+ TupleDesc orig_tupdesc = RelationGetDescr ( rel ) ;
18581856
18591857 /*
1860- * choose the correct relation to build val_desc from the
1861- * tuple contained in orig_slot
1858+ * If the tuple has been routed, it's been converted to the
1859+ * partition's rowtype, which might differ from the root
1860+ * table's. We must convert it back to the root table's
1861+ * rowtype so that val_desc shown error message matches the
1862+ * input tuple.
18621863 */
18631864 if (resultRelInfo -> ri_PartitionRoot )
18641865 {
1866+ HeapTuple tuple = ExecFetchSlotTuple (slot );
1867+ TupleConversionMap * map ;
1868+
18651869 rel = resultRelInfo -> ri_PartitionRoot ;
18661870 tupdesc = RelationGetDescr (rel );
1871+ /* a reverse map */
1872+ map = convert_tuples_by_name (orig_tupdesc , tupdesc ,
1873+ gettext_noop ("could not convert row type" ));
1874+ if (map != NULL )
1875+ {
1876+ tuple = do_convert_tuple (tuple , map );
1877+ ExecStoreTuple (tuple , slot , InvalidBuffer , false);
1878+ }
18671879 }
18681880
18691881 insertedCols = GetInsertedColumns (resultRelInfo , estate );
18701882 updatedCols = GetUpdatedColumns (resultRelInfo , estate );
18711883 modifiedCols = bms_union (insertedCols , updatedCols );
18721884 val_desc = ExecBuildSlotValueDescription (RelationGetRelid (rel ),
1873- orig_slot ,
1885+ slot ,
18741886 tupdesc ,
18751887 modifiedCols ,
18761888 64 );
@@ -1897,15 +1909,27 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
18971909 /* See the comment above. */
18981910 if (resultRelInfo -> ri_PartitionRoot )
18991911 {
1912+ HeapTuple tuple = ExecFetchSlotTuple (slot );
1913+ TupleDesc old_tupdesc = RelationGetDescr (rel );
1914+ TupleConversionMap * map ;
1915+
19001916 rel = resultRelInfo -> ri_PartitionRoot ;
19011917 tupdesc = RelationGetDescr (rel );
1918+ /* a reverse map */
1919+ map = convert_tuples_by_name (old_tupdesc , tupdesc ,
1920+ gettext_noop ("could not convert row type" ));
1921+ if (map != NULL )
1922+ {
1923+ tuple = do_convert_tuple (tuple , map );
1924+ ExecStoreTuple (tuple , slot , InvalidBuffer , false);
1925+ }
19021926 }
19031927
19041928 insertedCols = GetInsertedColumns (resultRelInfo , estate );
19051929 updatedCols = GetUpdatedColumns (resultRelInfo , estate );
19061930 modifiedCols = bms_union (insertedCols , updatedCols );
19071931 val_desc = ExecBuildSlotValueDescription (RelationGetRelid (rel ),
1908- orig_slot ,
1932+ slot ,
19091933 tupdesc ,
19101934 modifiedCols ,
19111935 64 );
@@ -1927,15 +1951,27 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
19271951 /* See the comment above. */
19281952 if (resultRelInfo -> ri_PartitionRoot )
19291953 {
1954+ HeapTuple tuple = ExecFetchSlotTuple (slot );
1955+ TupleDesc old_tupdesc = RelationGetDescr (rel );
1956+ TupleConversionMap * map ;
1957+
19301958 rel = resultRelInfo -> ri_PartitionRoot ;
19311959 tupdesc = RelationGetDescr (rel );
1960+ /* a reverse map */
1961+ map = convert_tuples_by_name (old_tupdesc , tupdesc ,
1962+ gettext_noop ("could not convert row type" ));
1963+ if (map != NULL )
1964+ {
1965+ tuple = do_convert_tuple (tuple , map );
1966+ ExecStoreTuple (tuple , slot , InvalidBuffer , false);
1967+ }
19321968 }
19331969
19341970 insertedCols = GetInsertedColumns (resultRelInfo , estate );
19351971 updatedCols = GetUpdatedColumns (resultRelInfo , estate );
19361972 modifiedCols = bms_union (insertedCols , updatedCols );
19371973 val_desc = ExecBuildSlotValueDescription (RelationGetRelid (rel ),
1938- orig_slot ,
1974+ slot ,
19391975 tupdesc ,
19401976 modifiedCols ,
19411977 64 );
0 commit comments