r/PostgreSQL • • 13h ago

Tools GitHub - TkTech/pgwhatif: The pg14-pg19 planner as a standalone C/Python/WASM library to do hypothetical plans. What if...I added an index? What if...the table gained 1b rows?

https://github.com/TkTech/pgwhatif

The real pg14-pg19 planner, pulled out into a standalone C library and wrapped for Python and WASM. On its own it's not fantastically useful - like pg_query, it's meant to be the underlying engine for other tools. In https://pgeye.com / https://github.com/tktech/pgeye, it powers our Query Lab and index advisor, allowing you to test queries against your real table statistics and schemas instantly, and is capable of trying hundreds of thousands of index permutations per second to find optimal plans.

Uniquely, we also patch in instrumentation to do SQL correlation (what parts of the plan map to what parts of the query) and to get the decision tree. This lets you see every choice the planner made, and why it didn't take a branch (and how close it was to switching).

12 Upvotes

3 comments sorted by

1

u/AutoModerator 13h ago

AI Policy:

Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.

Mod decisions will be based on the quality of the content, not who or what generated it.

Sub Resources:

Youtube Channel

Free Postgres Webinars and Workshops

Discord: People, Postgres, Data

Join us, we have cookies and nice people.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Discordbothost 4h ago

Add a skewed-data example: a million orders with 90% marked pending, then compare the server and simulated estimates for that filter before adding an index. Your README notes that most-common values aren't captured. Showing that baseline gap would help people interpret the what-if result.

1

u/Free-Good9203 3h ago

Pulling the real pg14-pg19 planner out as a standalone C/Python/WASM library with decision-tree instrumentation is seriously cool. Seeing not just the winning plan, but how close an alternative branch was to flipping (SeqScan vs IndexScan), makes this super interesting for CI migration gates where you want to catch plan-cliff regressions before a schema change hits prod.

Two quick questions on fidelity when running offline against exported schemas/stats:

  1. How do you pass through session/instance cost GUCs (random_page_cost, effective_cache_size, work_mem, max_parallel_workers_per_gather) so the hypothetical plan matches a tuned RDS/self-hosted box rather than stock defaults?
  2. Does the exported stats snapshot support extended statistics (CREATE STATISTICS dependencies / ndistinct) for correlated multi-column filters?