pg_duckdb icon indicating copy to clipboard operation
pg_duckdb copied to clipboard

DuckdbExplainOneQueryHook is not called for "explain execute" queries

Open ggnmstr opened this issue 7 months ago • 0 comments

What happens?

With duckdb.force_execution set to true, this sequence of queries crashes server:

CREATE TABLE INT8_TBL(q1 int8, q2 int8);
explain (memory, analyze, format json) select * from int8_tbl i8;
prepare int8_query as select * from int8_tbl i8;
explain (memory) execute int8_query; 

This happens because first query sets duckdb_explain_format to duckdb::ExplainFormat::JSON in DuckdbExplainOneQueryHook, meanwhile EXPLAIN EXECUTE queries are considered Utility statements, so ExplainOneQuery_hook is not called and duckdb_explain_format is still set to duckdb::ExplainFormat::JSON, which it should not be.

For instance, this sequence of queries works just well:

explain (memory, analyze, format json) select * from int8_tbl i8;
prepare int8_query as select * from int8_tbl i8;
explain (memory, format json) execute int8_query; 

I suggest moving code that sets value of duckdb_explain_format from DuckdbExplainOneQueryHook to Duckdb_ExplainCustomScan_Cpp, it seems its the only place where that value is actually needed. With that change, server no longer crashes.

To Reproduce

  1. Create fresh DB with pg_duckdb and duckdb.force_execution set to true
  2. Run this sequence of queries:
CREATE TABLE INT8_TBL(q1 int8, q2 int8);
explain (memory, analyze, format json) select * from int8_tbl i8;
prepare int8_query as select * from int8_tbl i8;
explain (memory) execute int8_query; 
  1. Get a server crash

OS:

Linux

pg_duckdb Version (if built from source use commit hash):

ea75539aa6bf4520fce537cffbe75fb12e4bcc65

Postgres Version (if built from source use commit hash):

17

Hardware:

No response

Full Name:

Jora Babayan

Affiliation:

Postgres Professional

What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a source build

Did you include all relevant data sets for reproducing the issue?

Not applicable - the reproduction does not require a data set

Did you include all code required to reproduce the issue?

  • [x] Yes, I have

Did you include all relevant configuration (e.g., CPU architecture, Linux distribution) to reproduce the issue?

  • [x] Yes, I have

ggnmstr avatar Apr 30 '25 08:04 ggnmstr