For run-time partition pruning running during execution (rather than at executor startup), EvalPlanQualStart() reuses the es_part_prune_states from the parent EState. This could cause an issue when the EPQ recheck is finished, its EState is cleaned up and the es_query_cxt is deleted by FreeExecutorState(). That cleanup could free memory that was allocated again in InitExecPartitionPruneContexts() when it was called for the EPQ's EState, and naturally, those fields got allocated in the EPQ's es_query_cxt MemoryContext, which overwrote the ones for the parent EState. If pruning occurs again after the EPQ's EState was cleaned up, then we could have accessed free'd memory. This certainly caused issues in debug builds because we clobber freed memory, but may not have in production, depending on if the MemoryContext being deleted results in a free(). It may not if the allocation was on the context's init block and the context was pushed onto context_freelists[] for reuse. Here we fix this by adding an "initialized" field to PartitionPruneState and exit early in InitExecPartitionPruneContexts() when the state is already initialized. This was broken bybb3ec16e1, but the breakage there is slightly different as that commit neglected to do what was fixed in8741e48e5(which did get backpatched to v18 in9a82a64ed). Reported-by: Vladimir Savin <vladimir@encord.com> Author: David Rowley <dgrowleyml@gmail.com> Reviewed-by: Andrey Rachitskiy <pl0h0yp1@gmail.com> Discussion: https://postgr.es/m/CAP2G_gGpV=rKMLuET4RW-qO41GvBvi6Czsjsj16eT9hAwHxG6w@mail.gmail.com Backpatch-through: 18
PostgreSQL Database Management System
This directory contains the source code distribution of the PostgreSQL database management system.
PostgreSQL is an advanced object-relational database management system that supports an extended subset of the SQL standard, including transactions, foreign keys, subqueries, triggers, user-defined types and functions. This distribution also contains C language bindings.
Copyright and license information can be found in the file COPYRIGHT.
General documentation about this version of PostgreSQL can be found at https://www.postgresql.org/docs/devel/. In particular, information about building PostgreSQL from the source code can be found at https://www.postgresql.org/docs/devel/installation.html.
The latest version of this software, and related software, may be obtained at https://www.postgresql.org/download/. For more information look at our web site located at https://www.postgresql.org/.