Incremental Analysis: v5 Patch Addressing All Review Feedback
Summary
Mohamed Ali responded to all five of Zsolt Parragi's review objections with a comprehensive v5 patch that fixes both logic bugs, reworks structured output, switches to start-time ordering, and adds significant new features (security model, high-volume warnings, option inheritance). The test suite grew from 41 to 55 cases.
Bug Fixes (Responding to Review)
-
Invalid structured output (Critical fix): JSON/XML/YAML now produce a single valid parseable document. Nested plans are structured sub-objects inside a "Nested Plans" array, with "Execution Time Percentage" as a typed field. This was the most serious architectural issue from the review.
-
Slowest Statement tracking (Logic bug fix): Now tracks across ALL nesting levels and types, not just level-1. The reviewer's reproduction case (trigger-only scenarios) is now covered.
-
Total Trigger Time calculation (Logic bug fix): Now sums ALL trigger statements regardless of nesting level, fixing the cascading-trigger-inside-function scenario.
-
Statement ordering switched to chronological: Parents now appear before children (start-time order). The author acknowledges completion order was an implementation convenience from hook firing order and agrees start-time is more intuitive.
-
Trigger label scope: Author explicitly defends the current
GetMyTriggerDepth() > 0approach — labels ALL statements in trigger context — arguing that nesting level already distinguishes direct vs. cascade. No change made here.
New Features in v5
SECURITY DEFINER Protection (New Security Model)
- Nested plan capture is skipped entirely when effective user ≠ session user (SECURITY DEFINER context)
- Exception: superusers and
pg_read_all_statsmembers always see nested plans - NOTICE emitted explaining the restriction
- Uses same privilege gate as
pg_stat_statementsfor query text visibility - This addresses the previously-unresolved security concern from the initial analysis
High-Volume Warning System
- NOTICE emitted when >1000 nested statements captured without explicit
SHOW_NESTED - Targets per-row SQL function calls (
SELECT func(id) FROM large_table) which generate one capture per invocation - NOTICE suppressed when
SHOW_NESTEDis explicitly set - Threshold is a compile-time constant
- Memory bounded by
SHOW_NESTED— beyond limit, only counters/timing accumulated
Option Inheritance
- IO option:
INSTRUMENT_IOnow propagates to nested plan capture - SETTINGS option: Nested plans show when functions internally change planner settings (e.g.,
SET enable_seqscan = offinside a function)
Reentrancy Logging
- Upgraded from silent degradation to
DEBUG1log message when nestedEXPLAIN NESTED_STATEMENTSis encountered
Parallel Query Behavior (Documented, Not Changed)
The author documents the parallel interaction model:
- Hooks are process-local (leader only) — workers don't inherit them
- PL/pgSQL functions (PARALLEL UNSAFE) always run in leader — fully captured
- SQL functions pushed below Gather into workers: only leader executions captured
- Explicitly noted as same limitation as
auto_explain - No crashes in 6 parallel scenarios tested
Multi-Language Confirmation
Tested PL/pgSQL, PL/Tcl, PL/Perl — all captured identically via SPI. Architecture diagram provided showing hook interception below SPI layer. PL/Python mentioned as also supported but not explicitly tested.
Edge Cases Documented
- Prepared statements: SPI plan caching (custom→generic transition) doesn't affect capture
- Cursors: FOR loop = one capture per cursor query; explicit OPEN/FETCH/CLOSE captured at CLOSE time
- Cross-database (dblink): Not captured (separate backend) — same as auto_explain
- DDL inside functions: ProcessUtility path not captured; DML on created tables IS captured; no crash on mixed DDL+DML
Assessment
This is a substantial v5 that addresses all critical review feedback. The structured output rework and bug fixes resolve the blocking issues. The security model is a significant new design element that wasn't previously discussed. The patch is now considerably more production-ready, though it still awaits response from the reviewer (Zsolt) and hasn't had committer attention yet. The lack of auto_explain code sharing remains unaddressed.