• Tom Lane's avatar
    Load relcache entries' partitioning data on-demand, not immediately. · 5b931237
    Tom Lane authored
    Formerly the rd_partkey and rd_partdesc data structures were always
    populated immediately when a relcache entry was built or rebuilt.
    This patch changes things so that they are populated only when they
    are first requested.  (Hence, callers *must* now always use
    RelationGetPartitionKey or RelationGetPartitionDesc; just fetching
    the pointer directly is no longer acceptable.)
    
    This seems to have some performance benefits, but the main reason to do
    it is that it eliminates a recursive-reload failure that occurs if the
    partkey or partdesc expressions contain any references to the relation's
    rowtype (as discovered by Amit Langote).  In retrospect, since loading
    these data structures might result in execution of nearly-arbitrary code
    via eval_const_expressions, it was a dumb idea to require that to happen
    during relcache entry rebuild.
    
    Also, fix things so that old copies of a relcache partition descriptor
    will be dropped when the cache entry's refcount goes to zero.  In the
    previous coding it was possible for such copies to survive for the
    lifetime of the session, as I'd complained of in a previous discussion.
    (This management technique still isn't perfect, but it's better than
    before.)  Improve the commentary explaining how that works and why
    it's safe to hand out direct pointers to these relcache substructures.
    
    In passing, improve RelationBuildPartitionDesc by using the same
    memory-context-parent-swap approach used by RelationBuildPartitionKey,
    thereby making it less dependent on strong assumptions about what
    partition_bounds_copy does.  Avoid doing get_rel_relkind in the
    critical section, too.
    
    Patch by Amit Langote and Tom Lane; Robert Haas deserves some credit
    for prior work in the area, too.  Although this is a pre-existing
    problem, no back-patch: the patch seems too invasive to be safe to
    back-patch, and the bug it fixes is a corner case that seems
    relatively unlikely to cause problems in the field.
    
    Discussion: https://postgr.es/m/CA+HiwqFUzjfj9HEsJtYWcr1SgQ_=iCAvQ=O2Sx6aQxoDu4OiHw@mail.gmail.com
    Discussion: https://postgr.es/m/CA+TgmoY3bRmGB6-DUnoVy5fJoreiBJ43rwMrQRCdPXuKt4Ykaw@mail.gmail.com
    5b931237
execPartition.c 64.9 KB