postgres.git / summary / log / commit / refs

commit    921c555eb2e32d246383d92183c06df4112e96b9
Author:   Peter Geoghegan <pg@bowt.ie>
Date:     Thu Nov 19 22:53:10 2026 +0000

    Fix auto_explain index scan NULL pointer dereference.
    
    Commit f026fbf059f, which made IndexScanInstrumentation into a pointer
    stored in various executor scan nodes, allocated the underlying
    instrumentation state at the point where the executor first initialized
    these nodes, though only when the plan was actually going to be run.
    This scheme assumed that EXPLAIN would only need to show the relevant
    counters for a plan that had actually run.
    
    That was correct for EXPLAIN, but failed to account for
    contrib/auto_explain's auto_explain.log_analyze option.  This option
    sets es->analyze when logging the plan of an EXPLAIN (BUFFERS) without
    ANALYZE.  The plan wouldn't run there, leading to a NULL pointer
    dereference crash.
    
    To fix, check that the instrumentation was allocated before using it.
    
    Author: Xiaoxuan Li <xioxuan@amazon.com>
    Discussion: https://postgr.es/m/CANuORAZumpvu2PJwGhVWihKfmUTNcWEbknBZG5-O3aq0uuKbhw@mail.gmail.com
    Backpatch-through: 19


contrib/auto_explain/t/001_auto_explain.pl | 11 +++++++++++ src/backend/commands/explain.c | 9 ++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/contrib/auto_explain/t/001_auto_explain.pl b/contrib/auto_explain/t/001_auto_explain.pl index f32a5e84f65..fccb6c58538 100644 --- a/contrib/auto_explain/t/001_auto_explain.pl +++ b/contrib/auto_explain/t/001_auto_explain.pl @@ -135,6 +135,17 @@ unlike( qr/Query Identifier:/, "query identifier not logged with compute_query_id=regress, text mode"); +# EXPLAIN (BUFFERS) without ANALYZE. The plan is started but never run, and +# log_analyze still logs it as if it had been analyzed. The index scan node +# must be logged with zero index searches. +$log_contents = query_log($node, + q{EXPLAIN (BUFFERS) SELECT * FROM pg_proc WHERE proname = 'int4pl';}); + +like( + $log_contents, + qr/Index Searches: 0/, + "index searches logged for EXPLAIN without ANALYZE, text mode"); + # JSON format. $log_contents = query_log( $node, diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index a40d03d35f3..35eba654813 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -3887,7 +3887,8 @@ show_indexsearches_info(PlanState *planstate, ExplainState *es) { IndexScanState *indexstate = ((IndexScanState *) planstate); - nsearches = indexstate->iss_Instrument->nsearches; + if (indexstate->iss_Instrument) + nsearches = indexstate->iss_Instrument->nsearches; SharedInfo = indexstate->iss_SharedInfo; break; } @@ -3895,7 +3896,8 @@ show_indexsearches_info(PlanState *planstate, ExplainState *es) { IndexOnlyScanState *indexstate = ((IndexOnlyScanState *) planstate); - nsearches = indexstate->ioss_Instrument->nsearches; + if (indexstate->ioss_Instrument) + nsearches = indexstate->ioss_Instrument->nsearches; SharedInfo = indexstate->ioss_SharedInfo; break; } @@ -3903,7 +3905,8 @@ show_indexsearches_info(PlanState *planstate, ExplainState *es) { BitmapIndexScanState *indexstate = ((BitmapIndexScanState *) planstate); - nsearches = indexstate->biss_Instrument->nsearches; + if (indexstate->biss_Instrument) + nsearches = indexstate->biss_Instrument->nsearches; SharedInfo = indexstate->biss_SharedInfo; break; } [parent: aa3e00984ea7]