Report
EXPLAIN ANALYZE initializes a SubPlan shared by Parallel Append children in the DSM once per child: assert builds fail Assert(key != key) in shm_toc_insert, and other builds report inflated loop counts and lose worker Sort statistics
- Severity
- 30
- Blamed commit
- bf11e7ee2e3607bb67d25aec73aa53b2d7e9961b
On 10 branches
REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Reported on pgsql-bugs as BUG #19766 (against 19rc1 and master).
When the children of a Parallel Append under Gather share one SubPlan, EXPLAIN ANALYZE initializes the SubPlan's parallel-instrumented nodes in the DSM segment once per child. ExecParallelInitializeDSM() uses planstate_tree_walker(), which walks each child's subPlan list, so the same PlanState is visited twice and shm_toc_insert() gets the same plan_node_id key twice. On assert-enabled builds of master and REL_19 this fails
TRAP: failed Assert("toc->toc_entry[i].key != key"), File: "shm_toc.c"
On non-assert builds and on REL_18 and older there is no crash and query results are correct, but EXPLAIN ANALYZE output is wrong.
Reproduction (assert-enabled build, default settings, empty tables):
CREATE TABLE ip (a int);
CREATE TABLE ic () INHERITS (ip);
CREATE TABLE s (b int);
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM ip WHERE a NOT IN (SELECT 1 FROM s, s s2);
-- master: trap in ExecMaterialInitializeDSM
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM ip WHERE a > ALL (SELECT b FROM s ORDER BY b, b + 1);
-- master and REL_19: trap in ExecSortInitializeDSM
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM ip WHERE a > ALL (SELECT b FROM s GROUP BY b);
-- REL_19: trap in ExecAggInitializeDSM
A list-partitioned table with two partitions gives the same plan and the same failures. With plans that put a Hash, Memoize or IncrementalSort node in the shared SubPlan, REL_19 traps in ExecHashInitializeDSM, ExecMemoizeInitializeDSM and ExecIncrementalSortInitializeDSM.
Without the assertion (non-assert master and REL_19, and REL_14 through REL_18), the duplicate entries corrupt the instrumentation. With two children sharing the SubPlan and 40000 outer rows, the shared Sort and its Seq Scan report loops=160000 instead of 40000, and the workers' "Sort Method" lines are missing. Workers look up the first toc entry and write their statistics there, while the leader reads the second, zero-filled allocation; each visit also adds the node to the instrumentation array again, so counts are aggregated once per visit. All allocations stay within the estimated DSM size, and no memory-safety problem was found.
Blame is bf11e7ee2e3607bb67d25aec73aa53b2d7e9961b ("Propagate sort instrumentation from workers back to leader", 2017-08-29), the first commit that inserts a toc entry keyed by plan_node_id for nodes that are not parallel-aware. Its parent 3452dc5240da43e833118484e1e9b4894d04431c logs no duplicate keys with a diagnostic in shm_toc_insert(); bf11e7ee2e36 logs three. 43222b8e53f49f2cf0849947a02c6fbd74899332 ("Assert no duplicate keys in shm_toc_insert()", master and REL_19) only adds the assertion that exposes it, and a241bc4fedefae3c0b0952c0a9d3a28d7747bfe3 ("Show Material node Maximum Storage for parallel workers", master only) adds the Material variant, which makes the report's first query trap with default settings. The repeated visit of a shared SubPlan under Gather is older still, and loop counts were already inflated on 3452dc5240da.
No fix is committed as of master ce10389d6378c98875fb5b3c8160085c83b64716. The thread has no patch.
Branches: assertion failure on master and REL_19; wrong EXPLAIN ANALYZE instrumentation on REL_14 through REL_18 (confirmed on their tips).