pgwhatif 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pgwhatif-0.1.0/.clang-format +8 -0
- pgwhatif-0.1.0/.clang-tidy +9 -0
- pgwhatif-0.1.0/.dockerignore +13 -0
- pgwhatif-0.1.0/Dockerfile.web +29 -0
- pgwhatif-0.1.0/LICENSE.md +28 -0
- pgwhatif-0.1.0/MANIFEST.in +11 -0
- pgwhatif-0.1.0/Makefile +70 -0
- pgwhatif-0.1.0/PKG-INFO +138 -0
- pgwhatif-0.1.0/README.md +126 -0
- pgwhatif-0.1.0/include/pgwhatif.h +14 -0
- pgwhatif-0.1.0/native/catalog_memory.c +380 -0
- pgwhatif-0.1.0/native/catalog_memory.h +27 -0
- pgwhatif-0.1.0/native/exports.map +4 -0
- pgwhatif-0.1.0/native/index_attributes.inc +31 -0
- pgwhatif-0.1.0/native/observed.c +337 -0
- pgwhatif-0.1.0/native/partitions.c +245 -0
- pgwhatif-0.1.0/native/pgwhatif.c +647 -0
- pgwhatif-0.1.0/native/pgwhatif.h +28 -0
- pgwhatif-0.1.0/native/plan_output.c +207 -0
- pgwhatif-0.1.0/native/relation_access.c +33 -0
- pgwhatif-0.1.0/native/runtime.c +168 -0
- pgwhatif-0.1.0/native/runtime.h +9 -0
- pgwhatif-0.1.0/native/settings.c +109 -0
- pgwhatif-0.1.0/native/snapshot.c +605 -0
- pgwhatif-0.1.0/native/snapshot.h +31 -0
- pgwhatif-0.1.0/native/source_map.c +153 -0
- pgwhatif-0.1.0/native/syscache_lookup.inc +62 -0
- pgwhatif-0.1.0/patches/14-15/index-statistics.patch +17 -0
- pgwhatif-0.1.0/patches/14-16/namespaces.patch +8 -0
- pgwhatif-0.1.0/patches/14-16/relations.patch +10 -0
- pgwhatif-0.1.0/patches/14-16/syscache.patch +10 -0
- pgwhatif-0.1.0/patches/14-17/collations.patch +38 -0
- pgwhatif-0.1.0/patches/14-18/syscache.patch +9 -0
- pgwhatif-0.1.0/patches/16-17/index-statistics.patch +17 -0
- pgwhatif-0.1.0/patches/16-19/syscache.patch +9 -0
- pgwhatif-0.1.0/patches/17-19/namespaces.patch +8 -0
- pgwhatif-0.1.0/patches/17-19/relations.patch +10 -0
- pgwhatif-0.1.0/patches/17-19/syscache.patch +10 -0
- pgwhatif-0.1.0/patches/18-19/index-statistics.patch +17 -0
- pgwhatif-0.1.0/patches/19/syscache.patch +9 -0
- pgwhatif-0.1.0/patches/backend-services.patch +83 -0
- pgwhatif-0.1.0/patches/catalog-scans.patch +102 -0
- pgwhatif-0.1.0/patches/collations.patch +27 -0
- pgwhatif-0.1.0/patches/index-statistics.patch +78 -0
- pgwhatif-0.1.0/patches/namespaces.patch +30 -0
- pgwhatif-0.1.0/patches/parallel-paths.patch +9 -0
- pgwhatif-0.1.0/patches/partition-cache.patch +28 -0
- pgwhatif-0.1.0/patches/path-comparisons.patch +16 -0
- pgwhatif-0.1.0/patches/planner.patch +45 -0
- pgwhatif-0.1.0/patches/relations.patch +38 -0
- pgwhatif-0.1.0/patches/statistics.patch +29 -0
- pgwhatif-0.1.0/patches/syscache.patch +43 -0
- pgwhatif-0.1.0/pgwhatif/__init__.py +190 -0
- pgwhatif-0.1.0/pgwhatif/catalog.py +92 -0
- pgwhatif-0.1.0/pgwhatif/py.typed +0 -0
- pgwhatif-0.1.0/pgwhatif/results.py +268 -0
- pgwhatif-0.1.0/pgwhatif/versions.json +8 -0
- pgwhatif-0.1.0/pgwhatif.egg-info/PKG-INFO +138 -0
- pgwhatif-0.1.0/pgwhatif.egg-info/SOURCES.txt +85 -0
- pgwhatif-0.1.0/pgwhatif.egg-info/dependency_links.txt +1 -0
- pgwhatif-0.1.0/pgwhatif.egg-info/top_level.txt +1 -0
- pgwhatif-0.1.0/pyproject.toml +70 -0
- pgwhatif-0.1.0/scripts/pack_npm.py +79 -0
- pgwhatif-0.1.0/scripts/wheel-engines.sh +17 -0
- pgwhatif-0.1.0/setup.cfg +4 -0
- pgwhatif-0.1.0/setup.py +67 -0
- pgwhatif-0.1.0/tests/conftest.py +54 -0
- pgwhatif-0.1.0/tests/reference_db.py +195 -0
- pgwhatif-0.1.0/tests/test_api.py +101 -0
- pgwhatif-0.1.0/tests/test_browser.py +106 -0
- pgwhatif-0.1.0/tests/test_planner.py +430 -0
- pgwhatif-0.1.0/tests/test_runtime.py +64 -0
- pgwhatif-0.1.0/tests/wasm.mjs +24 -0
- pgwhatif-0.1.0/tools/build-llvm.sh +30 -0
- pgwhatif-0.1.0/tools/build.py +399 -0
- pgwhatif-0.1.0/tools/catalog_export.c +57 -0
- pgwhatif-0.1.0/tools/catalog_image.py +98 -0
- pgwhatif-0.1.0/tools/extraction.py +588 -0
- pgwhatif-0.1.0/tools/fmgr.py +75 -0
- pgwhatif-0.1.0/tools/overlay.py +45 -0
- pgwhatif-0.1.0/tools/planner_builtins.py +723 -0
- pgwhatif-0.1.0/tools/planner_settings.py +82 -0
- pgwhatif-0.1.0/tools/provenance.py +37 -0
- pgwhatif-0.1.0/tools/web_assets.py +17 -0
- pgwhatif-0.1.0/uv.lock +553 -0
- pgwhatif-0.1.0/web/runtime.mjs +36 -0
- pgwhatif-0.1.0/web/worker.mjs +17 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
FROM emscripten/emsdk:7.0.0@sha256:6bd7aef93408b10bf7b36642cb7c91119b302f720b9ec2d20439f73cf07fe894 AS sdk
|
|
2
|
+
USER root
|
|
3
|
+
RUN apt-get update && apt-get install -y --no-install-recommends cmake git ninja-build \
|
|
4
|
+
&& rm -rf /var/lib/apt/lists/*
|
|
5
|
+
COPY tools/build-llvm.sh /usr/local/src/build-llvm.sh
|
|
6
|
+
RUN --mount=type=cache,target=/tmp/llvm-tools \
|
|
7
|
+
sh /usr/local/src/build-llvm.sh /planner-llvm /tmp/llvm-tools
|
|
8
|
+
|
|
9
|
+
FROM emscripten/emsdk:7.0.0@sha256:6bd7aef93408b10bf7b36642cb7c91119b302f720b9ec2d20439f73cf07fe894 AS builder
|
|
10
|
+
USER root
|
|
11
|
+
COPY --from=sdk /planner-llvm/ /emsdk/upstream/bin/
|
|
12
|
+
RUN apt-get update && apt-get install -y --no-install-recommends bison flex git build-essential python3-venv \
|
|
13
|
+
&& rm -rf /var/lib/apt/lists/* && useradd --uid 10001 --create-home planner \
|
|
14
|
+
&& install -d -o planner -g planner /home/planner/src /home/planner/src/.build /home/planner/assets
|
|
15
|
+
USER planner
|
|
16
|
+
ENV EM_CACHE=/home/planner/.cache/emscripten
|
|
17
|
+
WORKDIR /home/planner/src
|
|
18
|
+
COPY --chown=planner:planner . .
|
|
19
|
+
COPY --from=postgres --chown=planner:planner / .build/linux/
|
|
20
|
+
RUN make web WEB_DIR=/home/planner/assets \
|
|
21
|
+
&& rm -rf .build/sources .build/linux/*/*/wasm-ir .build/linux/*/*/stock
|
|
22
|
+
COPY --from=wheel --chown=planner:planner /pgwhatif-*-manylinux*x86_64.whl /home/planner/wheels/
|
|
23
|
+
RUN python3 -m venv /tmp/consumer \
|
|
24
|
+
&& /tmp/consumer/bin/pip install --no-deps --no-index /home/planner/wheels/*.whl \
|
|
25
|
+
&& /tmp/consumer/bin/pip install "pytest>=8.4,<10" \
|
|
26
|
+
&& /tmp/consumer/bin/python -I -m pytest tests/test_planner.py --web-dir /home/planner/assets
|
|
27
|
+
|
|
28
|
+
FROM scratch
|
|
29
|
+
COPY --from=builder /home/planner/assets/ /
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tyler Kennedy
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its contributors
|
|
16
|
+
may be used to endorse or promote products derived from this software
|
|
17
|
+
without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
include README.md Makefile uv.lock .clang-format .clang-tidy
|
|
2
|
+
recursive-include native *.c *.h *.inc *.map
|
|
3
|
+
recursive-include patches *.patch
|
|
4
|
+
recursive-include tools *.py *.c *.sh
|
|
5
|
+
recursive-include web *.mjs
|
|
6
|
+
recursive-exclude pgwhatif/engines *
|
|
7
|
+
recursive-include include *.h
|
|
8
|
+
recursive-include tests *.py *.mjs
|
|
9
|
+
include Dockerfile.web .dockerignore
|
|
10
|
+
include pgwhatif/py.typed pgwhatif/versions.json
|
|
11
|
+
recursive-include scripts *.py *.sh
|
pgwhatif-0.1.0/Makefile
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
.DEFAULT_GOAL := help
|
|
2
|
+
|
|
3
|
+
PYTHON ?= python3
|
|
4
|
+
WEB_DIR ?= .build/web
|
|
5
|
+
UV ?= uv
|
|
6
|
+
QUALITY = $(UV) run --locked --only-group lint --python "$(PYTHON)"
|
|
7
|
+
PYTEST = $(UV) run --locked --only-group test --python "$(PYTHON)" python -m pytest
|
|
8
|
+
C_SOURCES := $(wildcard native/*.c native/*.h native/*.inc include/*.h tools/*.c tools/*.h)
|
|
9
|
+
BUILD_FLAGS ?=
|
|
10
|
+
|
|
11
|
+
major_arg = $(if $(VERSION),--version "$(VERSION)")
|
|
12
|
+
cache_arg = $(if $(SOURCE_CACHE),--source-cache "$(SOURCE_CACHE)")
|
|
13
|
+
|
|
14
|
+
.PHONY: help native web extract check-patches test verify lint lint-style lint-c format wheel sdist release-wheel npm-pack
|
|
15
|
+
|
|
16
|
+
help:
|
|
17
|
+
@printf '%s\n' \
|
|
18
|
+
'make native Build the native engines for Python' \
|
|
19
|
+
'make web Build the WebAssembly engines and worker' \
|
|
20
|
+
'make extract Export the C header, shared library, and LLVM bitcode' \
|
|
21
|
+
'make check-patches Apply the patch series to each pinned release' \
|
|
22
|
+
'make test Compare native plans with stock PostgreSQL' \
|
|
23
|
+
'make verify Compare stock, native, and WebAssembly results' \
|
|
24
|
+
'make lint Check Python and C (latest PostgreSQL headers, or VERSION=16)' \
|
|
25
|
+
'make format Apply Ruff and clang-format to the project sources' \
|
|
26
|
+
'make wheel Package all native engines in a local wheel' \
|
|
27
|
+
'make sdist Package the standalone sources' \
|
|
28
|
+
'make release-wheel Build and test the wheel for this platform (pip install --group release)' \
|
|
29
|
+
'make npm-pack Package all built web engines from WEB_DIR into dist/' \
|
|
30
|
+
'' \
|
|
31
|
+
'Compare upstream dispatch targets: python tools/fmgr.py OLD/fmgrtab.c NEW/fmgrtab.c' \
|
|
32
|
+
'Builds and tests cover all majors by default. Example: make verify VERSION=16'
|
|
33
|
+
|
|
34
|
+
native extract check-patches:
|
|
35
|
+
"$(PYTHON)" tools/build.py $@ $(major_arg) $(cache_arg)
|
|
36
|
+
|
|
37
|
+
web:
|
|
38
|
+
"$(PYTHON)" tools/build.py web $(major_arg) $(cache_arg) --out "$(WEB_DIR)"
|
|
39
|
+
|
|
40
|
+
test: native
|
|
41
|
+
$(PYTEST) tests $(if $(VERSION),--pg-version "$(VERSION)") $(if $(filter 1,$(LARGE)),--large)
|
|
42
|
+
|
|
43
|
+
verify: native web
|
|
44
|
+
$(PYTEST) tests $(if $(VERSION),--pg-version "$(VERSION)") --web-dir "$(WEB_DIR)"
|
|
45
|
+
|
|
46
|
+
lint: lint-style lint-c
|
|
47
|
+
|
|
48
|
+
lint-style:
|
|
49
|
+
$(QUALITY) ruff check .
|
|
50
|
+
$(QUALITY) ruff format --check .
|
|
51
|
+
$(QUALITY) clang-format --dry-run --Werror $(C_SOURCES)
|
|
52
|
+
|
|
53
|
+
lint-c:
|
|
54
|
+
$(QUALITY) python tools/build.py lint-c $(major_arg) $(cache_arg)
|
|
55
|
+
|
|
56
|
+
format:
|
|
57
|
+
$(QUALITY) ruff check --fix .
|
|
58
|
+
$(QUALITY) ruff format .
|
|
59
|
+
$(QUALITY) clang-format -i $(C_SOURCES)
|
|
60
|
+
|
|
61
|
+
wheel sdist:
|
|
62
|
+
"$(PYTHON)" -m build --$@ $(BUILD_FLAGS)
|
|
63
|
+
|
|
64
|
+
release-wheel:
|
|
65
|
+
@set -eu; scratch=$$(mktemp -d); trap 'rm -rf "$$scratch"' EXIT; \
|
|
66
|
+
"$(PYTHON)" -m build --sdist $(BUILD_FLAGS) --outdir "$$scratch"; \
|
|
67
|
+
"$(PYTHON)" -m cibuildwheel --output-dir dist "$$scratch"/*.tar.gz
|
|
68
|
+
|
|
69
|
+
npm-pack:
|
|
70
|
+
"$(PYTHON)" scripts/pack_npm.py "$(WEB_DIR)" dist
|
pgwhatif-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pgwhatif
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Experiment with PostgreSQL query plans in a private catalog
|
|
5
|
+
License-Expression: BSD-3-Clause AND PostgreSQL
|
|
6
|
+
Project-URL: Repository, https://github.com/TkTech/pgwhatif
|
|
7
|
+
Project-URL: Issues, https://github.com/TkTech/pgwhatif/issues
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.md
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# pgwhatif
|
|
14
|
+
|
|
15
|
+
Run the real PostgreSQL 14–19 query planners against a schema and statistics you
|
|
16
|
+
supply. Change a row estimate, add a hypothetical index, or tweak table
|
|
17
|
+
statistics to see how the plan changes in real time.
|
|
18
|
+
|
|
19
|
+
Currently available as a C library, Python binding, and WASM. This is the
|
|
20
|
+
underlying engine that powers the Query Lab and index advisories on
|
|
21
|
+
https://pgeye.com / https://github.com/tktech/pgeye, check em out.
|
|
22
|
+
|
|
23
|
+
## Python
|
|
24
|
+
|
|
25
|
+
Install from PyPI with:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
python -m pip install pgwhatif
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
This example compares a generic plan before and after adding an index:
|
|
32
|
+
```python
|
|
33
|
+
from pgwhatif import Column, Relation, Snapshot, plan
|
|
34
|
+
|
|
35
|
+
snapshot = Snapshot(
|
|
36
|
+
version=16,
|
|
37
|
+
relations=[Relation("orders", [Column("id", "bigint", n_distinct=-1)], rows=10_000, pages=100)],
|
|
38
|
+
)
|
|
39
|
+
sql = "SELECT id FROM orders WHERE id = $1"
|
|
40
|
+
|
|
41
|
+
baseline = plan(sql, snapshot=snapshot)
|
|
42
|
+
scenario = plan(
|
|
43
|
+
sql, snapshot=snapshot, hypothetical_indexes=["CREATE INDEX by_id ON public.orders (id)"]
|
|
44
|
+
)
|
|
45
|
+
print(baseline.root.node_type, scenario.root.node_type) # Seq Scan, Index Only Scan
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Pass EXPLAIN text or JSON to inspect a captured plan. Supply the SQL and
|
|
49
|
+
snapshot to compare it with a simulation.
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from pathlib import Path
|
|
53
|
+
|
|
54
|
+
captured = Path("explain.json").read_text()
|
|
55
|
+
comparison = plan(captured, sql=sql, snapshot=snapshot)
|
|
56
|
+
if comparison.observed:
|
|
57
|
+
print(comparison.observed.plan.node_type, comparison.root.node_type)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Walk the tree with `node.children` and inspect planner decisions in
|
|
61
|
+
`comparison.trace`.
|
|
62
|
+
|
|
63
|
+
## Browser
|
|
64
|
+
|
|
65
|
+
Install from npm and copy the worker and its versioned engines to your site's
|
|
66
|
+
public directory. The worker loads the requested major on demand.
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
npm install pgwhatif
|
|
70
|
+
mkdir -p public/pgwhatif && cp -R node_modules/pgwhatif/. public/pgwhatif/
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
const worker = new Worker('/pgwhatif/worker.mjs', { type: 'module' });
|
|
75
|
+
worker.onmessage = ({ data }) => console.log(data.result ?? data.error);
|
|
76
|
+
worker.postMessage({ id: 1, version: 16, request: { sql: 'SELECT 1' } });
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The worker can also inspect SQL without a snapshot. Send `{ sql, references_only: true }`
|
|
80
|
+
to get relation names and any explicit schemas. Send `{ sql, source_only: true }` to get
|
|
81
|
+
source spans. Each span has a kind and UTF-8 byte offsets from `start` (inclusive)
|
|
82
|
+
to `end` (exclusive).
|
|
83
|
+
|
|
84
|
+
## Local development
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
make test VERSION=16
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Known Limitations
|
|
91
|
+
|
|
92
|
+
- pgwhatif plans one SQL statement at a time. Simulated plans report estimated costs
|
|
93
|
+
and row counts. For actual timings and buffer counts, use PostgreSQL's
|
|
94
|
+
`EXPLAIN (ANALYZE, BUFFERS)`.
|
|
95
|
+
- The snapshot omits most-common values and histogram bounds. For unevenly
|
|
96
|
+
distributed data, the planner can choose a different plan from the one on your
|
|
97
|
+
server. The primary user of this library (https://pgeye.com) never collects this
|
|
98
|
+
data because it can expose PII or other sensitive real values.
|
|
99
|
+
- The library supports a subset of PostgreSQL's built-in functions and types.
|
|
100
|
+
Queries that depend on extension code, such as a PostGIS operator, are
|
|
101
|
+
currently outside its scope (but PRs are welcome :))
|
|
102
|
+
- A captured EXPLAIN plan shows the paths PostgreSQL chose. A new what-if plan
|
|
103
|
+
or decision trace still requires the original SQL and a snapshot of the tables
|
|
104
|
+
it references.
|
|
105
|
+
- Each request has a 128 MiB input limit and a 512 MiB planner heap limit. Large
|
|
106
|
+
partition trees can exceed the memory or work limit.
|
|
107
|
+
|
|
108
|
+
## How does this work?
|
|
109
|
+
|
|
110
|
+
For each supported major version, pgwhatif builds from a pinned PostgreSQL release.
|
|
111
|
+
Clang compiles the parser, planner, and supporting code to LLVM bitcode. The build
|
|
112
|
+
redirects catalog lookups to an in-memory catalog, then uses LLVM to discard code
|
|
113
|
+
the library cannot reach. It also embeds catalog entries from a clean build of
|
|
114
|
+
that release, so the planner knows its own types, operators, and index methods.
|
|
115
|
+
|
|
116
|
+
When you call `plan()`, the library adds your tables, statistics, and hypothetical
|
|
117
|
+
indexes to a private catalog in memory. A hypothetical index needs only a catalog
|
|
118
|
+
entry, even for a table with millions of rows. PostgreSQL's planner uses that catalog
|
|
119
|
+
to compare paths and choose a plan. Small patches record the path comparisons
|
|
120
|
+
for the decision trace. The library returns the result and discards the request's
|
|
121
|
+
temporary catalog.
|
|
122
|
+
|
|
123
|
+
## Adding a new Postgres version
|
|
124
|
+
|
|
125
|
+
1. Append the full release number and the published SHA-256 checksum for its
|
|
126
|
+
`.tar.bz2` source archive to `pgwhatif/versions.json`. Both bindings use these pins.
|
|
127
|
+
2. Review the patches for the previous major. Extend each applicable directory
|
|
128
|
+
range under `patches/`, or add a patch for the new major where upstream code
|
|
129
|
+
differs. Run `make check-patches` to check every supported release.
|
|
130
|
+
3. Compare the two releases’ generated function tables with
|
|
131
|
+
`python tools/fmgr.py OLD/fmgrtab.c NEW/fmgrtab.c`. Update
|
|
132
|
+
`tools/planner_builtins.py` for any changed C symbols the planner needs.
|
|
133
|
+
4. Run `make verify VERSION=N`, where `N` is the new major. This builds both
|
|
134
|
+
engines and compares their plans with stock PostgreSQL.
|
|
135
|
+
5. Compare `pgwhatif/engines/N/cut-symbols.txt` with the previous major's report
|
|
136
|
+
from the same platform. Each name identifies an unavailable function that
|
|
137
|
+
the extracted planner still references. Web builds write the same report to
|
|
138
|
+
`.build/web/N/cut-symbols.txt`.
|
pgwhatif-0.1.0/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# pgwhatif
|
|
2
|
+
|
|
3
|
+
Run the real PostgreSQL 14–19 query planners against a schema and statistics you
|
|
4
|
+
supply. Change a row estimate, add a hypothetical index, or tweak table
|
|
5
|
+
statistics to see how the plan changes in real time.
|
|
6
|
+
|
|
7
|
+
Currently available as a C library, Python binding, and WASM. This is the
|
|
8
|
+
underlying engine that powers the Query Lab and index advisories on
|
|
9
|
+
https://pgeye.com / https://github.com/tktech/pgeye, check em out.
|
|
10
|
+
|
|
11
|
+
## Python
|
|
12
|
+
|
|
13
|
+
Install from PyPI with:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
python -m pip install pgwhatif
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
This example compares a generic plan before and after adding an index:
|
|
20
|
+
```python
|
|
21
|
+
from pgwhatif import Column, Relation, Snapshot, plan
|
|
22
|
+
|
|
23
|
+
snapshot = Snapshot(
|
|
24
|
+
version=16,
|
|
25
|
+
relations=[Relation("orders", [Column("id", "bigint", n_distinct=-1)], rows=10_000, pages=100)],
|
|
26
|
+
)
|
|
27
|
+
sql = "SELECT id FROM orders WHERE id = $1"
|
|
28
|
+
|
|
29
|
+
baseline = plan(sql, snapshot=snapshot)
|
|
30
|
+
scenario = plan(
|
|
31
|
+
sql, snapshot=snapshot, hypothetical_indexes=["CREATE INDEX by_id ON public.orders (id)"]
|
|
32
|
+
)
|
|
33
|
+
print(baseline.root.node_type, scenario.root.node_type) # Seq Scan, Index Only Scan
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Pass EXPLAIN text or JSON to inspect a captured plan. Supply the SQL and
|
|
37
|
+
snapshot to compare it with a simulation.
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from pathlib import Path
|
|
41
|
+
|
|
42
|
+
captured = Path("explain.json").read_text()
|
|
43
|
+
comparison = plan(captured, sql=sql, snapshot=snapshot)
|
|
44
|
+
if comparison.observed:
|
|
45
|
+
print(comparison.observed.plan.node_type, comparison.root.node_type)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Walk the tree with `node.children` and inspect planner decisions in
|
|
49
|
+
`comparison.trace`.
|
|
50
|
+
|
|
51
|
+
## Browser
|
|
52
|
+
|
|
53
|
+
Install from npm and copy the worker and its versioned engines to your site's
|
|
54
|
+
public directory. The worker loads the requested major on demand.
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
npm install pgwhatif
|
|
58
|
+
mkdir -p public/pgwhatif && cp -R node_modules/pgwhatif/. public/pgwhatif/
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const worker = new Worker('/pgwhatif/worker.mjs', { type: 'module' });
|
|
63
|
+
worker.onmessage = ({ data }) => console.log(data.result ?? data.error);
|
|
64
|
+
worker.postMessage({ id: 1, version: 16, request: { sql: 'SELECT 1' } });
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The worker can also inspect SQL without a snapshot. Send `{ sql, references_only: true }`
|
|
68
|
+
to get relation names and any explicit schemas. Send `{ sql, source_only: true }` to get
|
|
69
|
+
source spans. Each span has a kind and UTF-8 byte offsets from `start` (inclusive)
|
|
70
|
+
to `end` (exclusive).
|
|
71
|
+
|
|
72
|
+
## Local development
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
make test VERSION=16
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Known Limitations
|
|
79
|
+
|
|
80
|
+
- pgwhatif plans one SQL statement at a time. Simulated plans report estimated costs
|
|
81
|
+
and row counts. For actual timings and buffer counts, use PostgreSQL's
|
|
82
|
+
`EXPLAIN (ANALYZE, BUFFERS)`.
|
|
83
|
+
- The snapshot omits most-common values and histogram bounds. For unevenly
|
|
84
|
+
distributed data, the planner can choose a different plan from the one on your
|
|
85
|
+
server. The primary user of this library (https://pgeye.com) never collects this
|
|
86
|
+
data because it can expose PII or other sensitive real values.
|
|
87
|
+
- The library supports a subset of PostgreSQL's built-in functions and types.
|
|
88
|
+
Queries that depend on extension code, such as a PostGIS operator, are
|
|
89
|
+
currently outside its scope (but PRs are welcome :))
|
|
90
|
+
- A captured EXPLAIN plan shows the paths PostgreSQL chose. A new what-if plan
|
|
91
|
+
or decision trace still requires the original SQL and a snapshot of the tables
|
|
92
|
+
it references.
|
|
93
|
+
- Each request has a 128 MiB input limit and a 512 MiB planner heap limit. Large
|
|
94
|
+
partition trees can exceed the memory or work limit.
|
|
95
|
+
|
|
96
|
+
## How does this work?
|
|
97
|
+
|
|
98
|
+
For each supported major version, pgwhatif builds from a pinned PostgreSQL release.
|
|
99
|
+
Clang compiles the parser, planner, and supporting code to LLVM bitcode. The build
|
|
100
|
+
redirects catalog lookups to an in-memory catalog, then uses LLVM to discard code
|
|
101
|
+
the library cannot reach. It also embeds catalog entries from a clean build of
|
|
102
|
+
that release, so the planner knows its own types, operators, and index methods.
|
|
103
|
+
|
|
104
|
+
When you call `plan()`, the library adds your tables, statistics, and hypothetical
|
|
105
|
+
indexes to a private catalog in memory. A hypothetical index needs only a catalog
|
|
106
|
+
entry, even for a table with millions of rows. PostgreSQL's planner uses that catalog
|
|
107
|
+
to compare paths and choose a plan. Small patches record the path comparisons
|
|
108
|
+
for the decision trace. The library returns the result and discards the request's
|
|
109
|
+
temporary catalog.
|
|
110
|
+
|
|
111
|
+
## Adding a new Postgres version
|
|
112
|
+
|
|
113
|
+
1. Append the full release number and the published SHA-256 checksum for its
|
|
114
|
+
`.tar.bz2` source archive to `pgwhatif/versions.json`. Both bindings use these pins.
|
|
115
|
+
2. Review the patches for the previous major. Extend each applicable directory
|
|
116
|
+
range under `patches/`, or add a patch for the new major where upstream code
|
|
117
|
+
differs. Run `make check-patches` to check every supported release.
|
|
118
|
+
3. Compare the two releases’ generated function tables with
|
|
119
|
+
`python tools/fmgr.py OLD/fmgrtab.c NEW/fmgrtab.c`. Update
|
|
120
|
+
`tools/planner_builtins.py` for any changed C symbols the planner needs.
|
|
121
|
+
4. Run `make verify VERSION=N`, where `N` is the new major. This builds both
|
|
122
|
+
engines and compares their plans with stock PostgreSQL.
|
|
123
|
+
5. Compare `pgwhatif/engines/N/cut-symbols.txt` with the previous major's report
|
|
124
|
+
from the same platform. Each name identifies an unavailable function that
|
|
125
|
+
the extracted planner still references. Web builds write the same report to
|
|
126
|
+
`.build/web/N/cut-symbols.txt`.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#ifndef PGWHATIF_H
|
|
2
|
+
#define PGWHATIF_H
|
|
3
|
+
|
|
4
|
+
#ifdef __cplusplus
|
|
5
|
+
extern "C" {
|
|
6
|
+
#endif
|
|
7
|
+
unsigned pgwhatif_abi_version(void);
|
|
8
|
+
/* Each call owns its snapshot. Release the returned UTF-8 JSON with pgwhatif_free. */
|
|
9
|
+
char *pgwhatif_request(const char *request);
|
|
10
|
+
void pgwhatif_free(char *result);
|
|
11
|
+
#ifdef __cplusplus
|
|
12
|
+
}
|
|
13
|
+
#endif
|
|
14
|
+
#endif
|