pgsql-io / multicorn2

http://multicorn2.org
Other
83 stars 17 forks source link

Crash when using EXPLAIN and a non-constant join comparison against a path key #53

Closed mfenniak closed 4 months ago

mfenniak commented 4 months ago

Discovered a crash in multicorn2 when using dynamodb_fdw with a join between two tables, where the join contains conditions against a path key from the FDW. Originally I was trying to do a JSON operation on one field in a foreign table during a join (eg. a INNER JOIN b ON (b.pkey = a->>document['value']), but I think I've reproduced what is likely the same crash with a simpler test case:

explain select * from testmulticorn m1 left outer join testmulticorn m2 on upper(m1.test1) = m2.test1;

(https://github.com/pgsql-io/multicorn2/compare/main..mfenniak:non-const-crash) This is a case where m2.test1 is considered to be a path-key which can be used in a nested loop. Invoking the actual query seems to work fine, but the EXPLAIN query planning results in a SEGFAULT, crashing in execute:

#0  0x0000555555a42260 in pg_detoast_datum_packed ()
#1  0x00005555559bb082 in lower ()
#2  0x00005555557c37b2 in ExecInterpExpr ()
#3  0x00007ffff4cafb12 in execute ()
   from target:/nix/store/jbm1njb32xq2jji6szigz1lsqapcivff-postgresql-and-plugins-12.18/lib/multicorn.so
#4  0x00007ffff4cb20c9 in multicornExplainForeignScan ()
   from target:/nix/store/jbm1njb32xq2jji6szigz1lsqapcivff-postgresql-and-plugins-12.18/lib/multicorn.so
...snip...

In other cases where explain is being executed and the quals are being calculated, the uncalculable RHS value is replaced with "?", but in this case it seems to cause a crash, suggesting that multicorn thought it could be evaluated but it couldn't be.

I'll be trying to resolve this myself, but wanted to open the issue, just in case. :-)