• Andres Freund's avatar
    Fix transient memory leak for SRFs in FROM. · 299298bc
    Andres Freund authored
    In a9c35cf8 I changed ExecMakeTableFunctionResult() to dynamically
    allocate the FunctionCallInfo used to call the SRF. Unfortunately I
    did not account for the fact that the surrounding memory context has
    query lifetime, leading to a leak till the end of the query.
    
    In most cases the leak is fairly inconsequential, but if the
    FunctionScan is done many times in the query, the leak can add
    up. This happens e.g. if the function scan is on the inner side of a
    nested loop, due to a lateral join.
    EXPLAIN SELECT sum(f) FROM generate_series(1, 100000000) g(i), generate_series(i, i+1) f;
    quickly shows the leak.
    
    Instead of explicitly freeing the FunctionCallInfo it seems better to
    make sure all the per-set temporary state in
    ExecMakeTableFunctionResult() is cleaned up wholesale. Currently
    that's probably just the FunctionCallInfo allocation, but since
    there's some initialization work, and since there's already an
    appropriate context, this seems like a more robust approach.
    
    Bug: #16112
    Reported-By: Ben Cornett
    Author: Andres Freund
    Reviewed-By: Tom Lane
    Discussion: https://postgr.es/m/16112-4448bbf55a404189%40postgresql.org
    Backpatch: 12, a9c35cf8
    299298bc
execSRF.c 27.7 KB