Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pg] Added support for postgres CTE Materialization options #3794

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gp27
Copy link

@gp27 gp27 commented Dec 17, 2024

This PR adds support for forcefully enabling or disabling materialized CTEs (postgres docs).
Materialized CTEs can impact query performance, and having explicit control allows for better optimization.

This feature was requested in issue #2318

Usage

I added an optional second parameter { materialized?: boolean } to $with, allowing queries to specify materialization behavior like this:

/* If the CTE is referenced more than once in the main query,
this will make sure that is executed only one time */
const sq = db.$with('sq', { materialized: true }).as(/*sub query*/);
/* When only a small part of the CTE output is needed and computing
the full subquery is expensive, it might be cheaper to force postgres
to duplicate the computation by inlining the CTE */
const sq = db.$with('sq', { materialized: false }).as(/*sub query*/);

Notes

If the PR is approved, this option will need to be documented. I’m unsure of the specific guidelines for documentation, so any pointers would be appreciated.

gp27 added 2 commits December 17, 2024 20:23
CTEs can now be used with an additional parameter: `db.$with('sq', { materialized: true }).as(...)`.
It can be used to force postgres to materialize/inline a subquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant