postgres.git / summary / log / commit / refs

commit    a20d50a00edc40ff11b08b1e3e3be6e4f9da51f6
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 | 13 +++++++++---- 2 files changed, 20 insertions(+), 4 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 b8d1d0ec751..f778075b9eb 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -3989,7 +3989,8 @@ show_indexscan_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; } @@ -3997,8 +3998,11 @@ show_indexscan_info(PlanState *planstate, ExplainState *es) { IndexOnlyScanState *indexstate = ((IndexOnlyScanState *) planstate); - nsearches = indexstate->ioss_Instrument->nsearches; - ntabletuplefetches = indexstate->ioss_Instrument->ntabletuplefetches; + if (indexstate->ioss_Instrument) + { + nsearches = indexstate->ioss_Instrument->nsearches; + ntabletuplefetches = indexstate->ioss_Instrument->ntabletuplefetches; + } SharedInfo = indexstate->ioss_SharedInfo; break; } @@ -4006,7 +4010,8 @@ show_indexscan_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: c266e204a7ab]