Table Function Scan can report incorrect "Maximum Storage" in EXPLAIN
Table Function Scan can report incorrect "Maximum Storage" in EXPLAIN
Hi,
40708acd65 added memory/disk usage to EXPLAIN ANALYZE for Table Function
Scan nodes. However, when the node is rescanned with changed
parameters, ExecReScanTableFuncScan() calls tuplestore_end(), so the
statistics of the earlier rescans are lost and only those of the final
rescan are reported.
This is the same problem that David reported for Material nodes in [1],
as also mentioned in [2].
For example:
EXPLAIN ANALYZE
SELECT count(*)
FROM (VALUES ((SELECT jsonb_agg(g) FROM generate_series(1, 100000) g)),
('[1]'::jsonb)) v(j),
LATERAL JSON_TABLE(v.j, '$[*]' COLUMNS (x int PATH '$')) jt;
The Table Function Scan node reports the following:
larger document first: master Memory 17kB patched Disk 4096kB
smaller document first: master Disk 4096kB patched Disk 4096kB
Regards,
Tatsuya Kawata
[1] https://www.postgresql.org/message-id/flat/CAApHDvoa55vcRth05Ozu5be4FawgTH-aCsZ5%3DZ%2B_UXTUzUxdQg%40mail.gmail.com
[2] https://www.postgresql.org/message-id/CAApHDvoFPyT10sRDY%2BbH1MOwHwWNnBQKR7xgEePDke15-EkMkQ%40mail.gmail.com
40708acd65 added memory/disk usage to EXPLAIN ANALYZE for Table Function
Scan nodes. However, when the node is rescanned with changed
parameters, ExecReScanTableFuncScan() calls tuplestore_end(), so the
statistics of the earlier rescans are lost and only those of the final
rescan are reported.
This is the same problem that David reported for Material nodes in [1],
as also mentioned in [2].
For example:
EXPLAIN ANALYZE
SELECT count(*)
FROM (VALUES ((SELECT jsonb_agg(g) FROM generate_series(1, 100000) g)),
('[1]'::jsonb)) v(j),
LATERAL JSON_TABLE(v.j, '$[*]' COLUMNS (x int PATH '$')) jt;
The Table Function Scan node reports the following:
larger document first: master Memory 17kB patched Disk 4096kB
smaller document first: master Disk 4096kB patched Disk 4096kB
Regards,
Tatsuya Kawata
[1] https://www.postgresql.org/message-id/flat/CAApHDvoa55vcRth05Ozu5be4FawgTH-aCsZ5%3DZ%2B_UXTUzUxdQg%40mail.gmail.com
[2] https://www.postgresql.org/message-id/CAApHDvoFPyT10sRDY%2BbH1MOwHwWNnBQKR7xgEePDke15-EkMkQ%40mail.gmail.com
Re: Table Function Scan can report incorrect "Maximum Storage" in EXPLAIN
От:
David Rowley <dgrowleyml@gmail.com>
Дата:
On Mon, 5 Oct 2026 at 03:52, Tatsuya Kawata wrote: > 40708acd65 added memory/disk usage to EXPLAIN ANALYZE for Table Function > Scan nodes. However, when the node is rescanned with changed > parameters, ExecReScanTableFuncScan() calls tuplestore_end(), so the > statistics of the earlier rescans are lost and only those of the final > rescan are reported. I made a very small change so we only call tuplestore_rescan() when the parameters don't change. The call to tuplestore_clear() rewinds the read pointers, so there's no point in calling tuplestore_rescan() after tuplestore_clear(). Pushed. Thanks. David
Re: Table Function Scan can report incorrect "Maximum Storage" in EXPLAIN
От:
Tatsuya Kawata <kawatatatsuya0913@gmail.com>
Дата:
Hi David,
> I made a very small change so we only call tuplestore_rescan() when
> the parameters don't change. The call to tuplestore_clear() rewinds
> the read pointers, so there's no point in calling tuplestore_rescan()
> after tuplestore_clear().
>
> Pushed. Thanks.
Thanks for the fix and for pushing it!
Regards,
Tatsuya Kawata
> I made a very small change so we only call tuplestore_rescan() when
> the parameters don't change. The call to tuplestore_clear() rewinds
> the read pointers, so there's no point in calling tuplestore_rescan()
> after tuplestore_clear().
>
> Pushed. Thanks.
Thanks for the fix and for pushing it!
Regards,
Tatsuya Kawata