Which Greenplum Version your are using?
Did you tried this - Set Optimizer Parameters Explicitly:
**You can try enabling GPORCA explicitly in your session or function using:
SET optimizer = on;
Also consider setting other relevant parameters like:
check these parameters also(it should be ON)
optimizer_force_multistage_agg
optimizer_force_three_stage_scalar_dqa
FYI, As per vendor recommendations to Force GPORCA Usage
Avoid Dynamic SQL When Possible:
**If you can, avoid using EXECUTE with dynamic SQL inside functions. Instead, use static SQL so GPORCA can analyze it at compile time.
Use SQL Functions Instead of PL/pgSQL:
**SQL functions (as opposed to PL/pgSQL) are more likely to be optimized by GPORCA.
Use External Scripts for Complex Queries:
**If the query is very large and complex, consider storing it in a table or file and executing it from a client (e.g., Python, Bash) where GPORCA can fully optimize it.