• Andres Freund's avatar
    Fix several bugs related to ON CONFLICT's EXCLUDED pseudo relation. · ad227837
    Andres Freund authored
    Four related issues:
    
    1) attnos/varnos/resnos for EXCLUDED were out of sync when a column
       after one dropped in the underlying relation was referenced.
    2) References to whole-row variables (i.e. EXCLUDED.*) lead to errors.
    3) It was possible to reference system columns in the EXCLUDED pseudo
       relations, even though they would not have valid contents.
    4) References to EXCLUDED were rewritten by the RLS machinery, as
       EXCLUDED was treated as if it were the underlying relation.
    
    To fix the first two issues, generate the excluded targetlist with
    dropped columns in mind and add an entry for whole row
    variables. Instead of unconditionally adding a wholerow entry we could
    pull up the expression if needed, but doing it unconditionally seems
    simpler. The wholerow entry is only really needed for ruleutils/EXPLAIN
    support anyway.
    
    The remaining two issues are addressed by changing the EXCLUDED RTE to
    have relkind = composite. That fits with EXCLUDED not actually being a
    real relation, and allows to treat it differently in the relevant
    places. scanRTEForColumn now skips looking up system columns when the
    RTE has a composite relkind; fireRIRrules() already had a corresponding
    check, thereby preventing RLS expansion on EXCLUDED.
    
    Also add tests for these issues, and improve a few comments around
    excluded handling in setrefs.c.
    
    Reported-By: Peter Geoghegan, Geoff Winkless
    Author: Andres Freund, Amit Langote, Peter Geoghegan
    Discussion: CAEzk6fdzJ3xYQZGbcuYM2rBd2BuDkUksmK=mY9UYYDugg_GgZg@mail.gmail.com,
       CAM3SWZS+CauzbiCEcg-GdE6K6ycHE_Bz6Ksszy8AoixcMHOmsA@mail.gmail.com
    Backpatch: 9.5, where ON CONFLICT was introduced
    ad227837
insert_conflict.out 33.1 KB