• Tom Lane's avatar
    Handle EPIPE more sanely when we close a pipe reading from a program. · ffa4cbd6
    Tom Lane authored
    Previously, any program launched by COPY TO/FROM PROGRAM inherited the
    server's setting of SIGPIPE handling, i.e. SIG_IGN.  Hence, if we were
    doing COPY FROM PROGRAM and closed the pipe early, the child process
    would see EPIPE on its output file and typically would treat that as
    a fatal error, in turn causing the COPY to report error.  Similarly,
    one could get a failure report from a query that didn't read all of
    the output from a contrib/file_fdw foreign table that uses file_fdw's
    PROGRAM option.
    
    To fix, ensure that child programs inherit SIG_DFL not SIG_IGN processing
    of SIGPIPE.  This seems like an all-around better situation since if
    the called program wants some non-default treatment of SIGPIPE, it would
    expect to have to set that up for itself.  Then in COPY, if it's COPY
    FROM PROGRAM and we stop reading short of detecting EOF, treat a SIGPIPE
    exit from the called program as a non-error condition.  This still allows
    us to report an error for any case where the called program gets SIGPIPE
    on some other file descriptor.
    
    As coded, we won't report a SIGPIPE if we stop reading as a result of
    seeing an in-band EOF marker (e.g. COPY BINARY EOF marker).  It's
    somewhat debatable whether we should complain if the called program
    continues to transmit data after an EOF marker.  However, it seems like
    we should avoid throwing error in any questionable cases, especially in a
    back-patched fix, and anyway it would take additional code to make such
    an error get reported consistently.
    
    Back-patch to v10.  We could go further back, since COPY FROM PROGRAM
    has been around awhile, but AFAICS the only way to reach this situation
    using core or contrib is via file_fdw, which has only supported PROGRAM
    sources since v10.  The COPY statement per se has no feature whereby
    it'd stop reading without having hit EOF or an error already.  Therefore,
    I don't see any upside to back-patching further that'd outweigh the
    risk of complaints about behavioral change.
    
    Per bug #15449 from Eric Cyr.
    
    Patch by me, review by Etsuro Fujita and Kyotaro Horiguchi
    
    Discussion: https://postgr.es/m/15449-1cf737dd5929450e@postgresql.org
    ffa4cbd6
copy.c 146 KB