• Tom Lane's avatar
    Fix incorrect handling of CTEs and ENRs as DML target relations. · 7421f4b8
    Tom Lane authored
    setTargetTable threw an error if the proposed target RangeVar's relname
    matched any visible CTE or ENR.  This breaks backwards compatibility in
    the CTE case, since pre-v10 we never looked for a CTE here at all, so that
    CTE names did not mask regular tables.  It does seem like a good idea to
    throw an error for the ENR case, though, thus causing ENRs to mask tables
    for this purpose; ENRs are new in v10 so we're not breaking existing code,
    and we may someday want to allow them to be the targets of DML.
    
    To fix that, replace use of getRTEForSpecialRelationTypes, which was
    overkill anyway, with use of scanNameSpaceForENR.
    
    A second problem was that the check neglected to verify null schemaname,
    so that a CTE or ENR could incorrectly be thought to match a qualified
    RangeVar.  That happened because getRTEForSpecialRelationTypes relied
    on its caller to have checked for null schemaname.  Even though the one
    remaining caller got it right, this is obviously bug-prone, so move
    the check inside getRTEForSpecialRelationTypes.
    
    Also, revert commit 18ce3a4a's extremely poorly thought out decision to
    add a NULL return case to parserOpenTable --- without either documenting
    that or adjusting any of the callers to check for it.  The current bug
    seems to have arisen in part due to working around that bad idea.
    
    In passing, remove the one-line shim functions transformCTEReference and
    transformENRReference --- they don't seem to be adding any clarity or
    functionality.
    
    Per report from Hugo Mercier (via Julien Rouhaud).  Back-patch to v10
    where the bug was introduced.
    
    Thomas Munro, with minor editing by me
    
    Discussion: https://postgr.es/m/CAOBaU_YdPVH+PTtiKSSLOiiW3mVDYsnNUekK+XPbHXiP=wrFLA@mail.gmail.com
    7421f4b8
plpgsql.sql 121 KB