pytest-pglite 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.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,221 @@
1
+ Metadata-Version: 2.4
2
+ Name: pytest-pglite
3
+ Version: 0.1.0
4
+ Summary: Native PGlite (PostgreSQL in WebAssembly) for Python test suites, with first-class pytest integration
5
+ License-Expression: Apache-2.0
6
+ License-File: LICENSE
7
+ Keywords: pytest,postgres,postgresql,pglite,wasm,wasmtime,testing
8
+ Author: Don Bowman
9
+ Author-email: don@agilicus.com
10
+ Requires-Python: >=3.14
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Framework :: Pytest
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.14
16
+ Classifier: Topic :: Database
17
+ Classifier: Topic :: Software Development :: Testing
18
+ Classifier: Typing :: Typed
19
+ Provides-Extra: async
20
+ Provides-Extra: django
21
+ Provides-Extra: examples
22
+ Provides-Extra: sqlalchemy
23
+ Requires-Dist: alembic (>=1.16,<2) ; extra == "examples"
24
+ Requires-Dist: asyncpg (>=0.31,<1) ; extra == "examples"
25
+ Requires-Dist: django (>=6.1,<7) ; extra == "django"
26
+ Requires-Dist: fastapi (>=0.142,<1) ; extra == "examples"
27
+ Requires-Dist: httpx (>=0.28,<1) ; extra == "examples"
28
+ Requires-Dist: platformdirs (>=4.3,<5)
29
+ Requires-Dist: psycopg[binary] (>=3.3,<4) ; extra == "async"
30
+ Requires-Dist: psycopg[binary] (>=3.3,<4) ; extra == "examples"
31
+ Requires-Dist: pydantic (>=2.13,<3)
32
+ Requires-Dist: pydantic-settings (>=2.15,<3)
33
+ Requires-Dist: pytest (>=9.1,<10)
34
+ Requires-Dist: pytest-asyncio (>=1.4,<2) ; extra == "examples"
35
+ Requires-Dist: pytest-django (>=4.14,<5) ; extra == "django"
36
+ Requires-Dist: sqlalchemy[asyncio] (>=2.1,<3) ; extra == "examples"
37
+ Requires-Dist: sqlalchemy[asyncio] (>=2.1,<3) ; extra == "sqlalchemy"
38
+ Requires-Dist: wasmtime (>=49.0,<50)
39
+ Project-URL: Homepage, https://github.com/donbowman/pytest-pglite
40
+ Project-URL: Issues, https://github.com/donbowman/pytest-pglite/issues
41
+ Project-URL: Repository, https://github.com/donbowman/pytest-pglite
42
+ Description-Content-Type: text/markdown
43
+
44
+ # pytest-pglite
45
+
46
+ Real PostgreSQL in your Python process, for tests. No Docker, no server, no
47
+ Node.js. `pytest-pglite` embeds PGlite, the WebAssembly build of PostgreSQL
48
+ 17.5, through [Wasmtime](https://wasmtime.dev/) and exposes it as a Postgres
49
+ wire-protocol server so every Python client just works.
50
+
51
+ ```python
52
+ def test_users(pglite_engine):
53
+ with pglite_engine.begin() as conn:
54
+ conn.execute(text("create table users (id serial primary key, name text)"))
55
+ conn.execute(text("insert into users (name) values ('Alice')"))
56
+ assert conn.execute(text("select count(*) from users")).scalar_one() == 1
57
+ ```
58
+
59
+ ## Features
60
+
61
+ - **Native WASM** - PostgreSQL runs inside the Python process via Wasmtime,
62
+ with only the standard WebAssembly System Interface as a dependency.
63
+ - **Real wire protocol** - psycopg (sync and async), asyncpg, SQLAlchemy,
64
+ Django and anything else that speaks Postgres connect to a local Unix socket
65
+ or TCP port.
66
+ - **pytest first** - session-scoped fast startup, function-scoped isolation,
67
+ first-class support for `pytest-xdist` (each worker gets its own database).
68
+ - **Multiple connections** - connections share one single-user Postgres
69
+ backend through a queue with transaction pinning, mirroring upstream
70
+ PGlite behaviour.
71
+ - **Async ready** - the server is asyncio based; async fixtures are provided
72
+ for FastAPI, httpx and other async test suites.
73
+ - **pgvector** - `CREATE EXTENSION vector` works out of the box.
74
+ - **Fast** - a warm worker starts in well under a second; per-test isolation
75
+ resets take milliseconds.
76
+
77
+ ## Install
78
+
79
+ ```bash
80
+ pip install pytest-pglite
81
+ # optional extras
82
+ pip install "pytest-pglite[sqlalchemy]"
83
+ pip install "pytest-pglite[async]"
84
+ pip install "pytest-pglite[django]"
85
+ pip install "pytest-pglite[examples]"
86
+ ```
87
+
88
+ The pytest plugin is registered automatically through the `pytest11` entry
89
+ point, so there is nothing to import or configure.
90
+
91
+ ## Fixtures
92
+
93
+ | Fixture | Scope | Description |
94
+ | --- | --- | --- |
95
+ | `pglite_server` | session | Running `PGliteServer` instance (one per xdist worker) |
96
+ | `pglite_dsn` | session | libpq connection string (Unix socket, or TCP) |
97
+ | `pglite_url` | session | SQLAlchemy URL string |
98
+ | `pglite_conn` | function | `psycopg.Connection` with a freshly reset schema |
99
+ | `pglite_async_conn` | function | `psycopg.AsyncConnection` with a freshly reset schema |
100
+ | `pglite_asyncpg` | function | `asyncpg.Connection` with a freshly reset schema |
101
+ | `pglite_engine` | function | SQLAlchemy `Engine` with a freshly reset schema |
102
+ | `pglite_execute` | function | Callable that runs SQL and returns rows |
103
+
104
+ ## Options
105
+
106
+ ```
107
+ --pglite-extensions=vector Comma separated extensions to create
108
+ --pglite-isolation=schema none | schema | transaction | database
109
+ --pglite-tcp Also listen on TCP (127.0.0.1, ephemeral port)
110
+ --pglite-tcp-address=host:port Explicit TCP bind address (default port 0 = OS assigned)
111
+ --pglite-work-dir=PATH Directory for database files (default: temp dir)
112
+ --pglite-wasm=PATH Use a custom pglite.wasi build
113
+ --pglite-keep-tmp Keep the temporary database directory
114
+ --pglite-log-level=INFO Engine log level
115
+ ```
116
+
117
+ All options can also be set with environment variables using the `PGLITE_`
118
+ prefix, for example `PGLITE_ISOLATION=transaction`.
119
+
120
+ ## Standalone server
121
+
122
+ The same engine can run outside pytest, for example for a manual psql session
123
+ or a development server:
124
+
125
+ ```bash
126
+ pytest-pglite --tcp-address 127.0.0.1:0 --extensions vector
127
+ # PGlite ready: postgresql://postgres:password@127.0.0.1:54321/template1?sslmode=disable
128
+ ```
129
+
130
+ ```python
131
+ from pytest_pglite import PGliteConfig, PGliteServer
132
+
133
+ with PGliteServer(PGliteConfig(tcp=True)) as server:
134
+ print(server.dsn)
135
+ ```
136
+
137
+ ## xdist
138
+
139
+ Each `pytest-xdist` worker process starts its own PGlite instance with its own
140
+ temporary directory, its own Unix socket (or its own OS-assigned TCP port).
141
+ There are no shared paths and no port collisions, so `pytest -n 4` just works.
142
+
143
+ ## How it works
144
+
145
+ The package embeds `pglite.wasi`, a WebAssembly build of PostgreSQL 17.5 with
146
+ pgvector statically linked. It is built from
147
+ [electric-sql/pglite-build](https://github.com/electric-sql/pglite-build) by
148
+ the pipeline in `build/` (see `build/README.md` for the patches and
149
+ provenance). A small engine wrapper drives the WASI module through Wasmtime.
150
+ On top of the
151
+ single-user backend, an asyncio Postgres wire-protocol proxy manages client
152
+ connections: each connection is a logical session, and protocol exchanges are
153
+ serialised through a query queue. Statements and portals are renamed per
154
+ connection so prepared-statement clients (psycopg3, asyncpg, SQLAlchemy) can
155
+ share one backend safely.
156
+
157
+ PGlite is single user, exactly like embedded SQLite. Concurrent connections
158
+ work, but work is serialised; a connection holding an open transaction holds
159
+ the backend until it commits or rolls back.
160
+
161
+ ## Performance
162
+
163
+ Measured on the development machine (Linux x86_64, CPython 3.14):
164
+
165
+ | Measurement | Result |
166
+ | --- | --- |
167
+ | Cold start (first ever, compiles WASM + initdb) | about 2.5 s |
168
+ | Warm start, new worker (AOT + template cache) | about 0.25 s |
169
+ | New connection | about 1 ms |
170
+ | Query round trip (`pytest-pglite` overhead) | about 1.3 ms |
171
+ | Per-test schema reset | about 1 ms |
172
+
173
+ Compiled-module files and an initialised database template are cached under
174
+ the platform cache directory; `pytest-xdist` workers share them.
175
+
176
+ ## Limitations
177
+
178
+ - Single database, single user (`postgres`). `CREATE DATABASE` and connected
179
+ database switching are not supported.
180
+ - Single backend: work from concurrent connections is queued, and a connection
181
+ holding an open transaction blocks others until it commits or rolls back.
182
+ Waiting connections time out with SQLSTATE 57014 after `queue_timeout`
183
+ seconds (default 60).
184
+ - Session state is shared: `SET search_path`, `SET timezone` and similar
185
+ run-time parameters affect every connection because they share one backend
186
+ session. Use `SET LOCAL` inside a transaction for per-test values.
187
+ - PostgreSQL wire-protocol server features are limited to what test suites
188
+ need: `LISTEN/NOTIFY` delivery is not implemented, and cancellation requests
189
+ are accepted but not acted on.
190
+ - `COPY TO STDOUT` works; `COPY FROM STDIN` is rejected with SQLSTATE 0A000
191
+ because libpglite's synchronous transport cannot stream `CopyData` during
192
+ `CopyFrom`.
193
+ - SQL errors kill the WebAssembly instance (the upstream WASI build has no
194
+ `setjmp`/`longjmp`). The Python host forwards the real ErrorResponse and
195
+ SQLSTATE, then restarts the backend over the same data directory when the
196
+ session cannot be recovered.
197
+ - Extensions are compiled into the WASM module at build time; only the
198
+ bundled set (`plpgsql`, `vector`) can be created.
199
+ - No Windows support for Unix sockets; use `--pglite-tcp` on Windows.
200
+
201
+ ## Development
202
+
203
+ ```bash
204
+ poetry install --all-extras
205
+ poetry run pytest
206
+ poetry run black .
207
+ poetry run flake8
208
+ poetry run mypy
209
+ ```
210
+
211
+ The WASM artifact lives in `src/pytest_pglite/_wasm/`; see `build/README.md`
212
+ for provenance and how to rebuild it. Rebuilding uses the upstream
213
+ pglite-build pipeline; the orchestrator pins compiler parallelism through
214
+ `PGLITE_JOBS` (default 8) because upstream defaults to `nproc` and can exhaust
215
+ memory on large machines.
216
+
217
+ ## License
218
+
219
+ Apache License 2.0. See [LICENSE](LICENSE) and
220
+ [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
221
+
@@ -0,0 +1,177 @@
1
+ # pytest-pglite
2
+
3
+ Real PostgreSQL in your Python process, for tests. No Docker, no server, no
4
+ Node.js. `pytest-pglite` embeds PGlite, the WebAssembly build of PostgreSQL
5
+ 17.5, through [Wasmtime](https://wasmtime.dev/) and exposes it as a Postgres
6
+ wire-protocol server so every Python client just works.
7
+
8
+ ```python
9
+ def test_users(pglite_engine):
10
+ with pglite_engine.begin() as conn:
11
+ conn.execute(text("create table users (id serial primary key, name text)"))
12
+ conn.execute(text("insert into users (name) values ('Alice')"))
13
+ assert conn.execute(text("select count(*) from users")).scalar_one() == 1
14
+ ```
15
+
16
+ ## Features
17
+
18
+ - **Native WASM** - PostgreSQL runs inside the Python process via Wasmtime,
19
+ with only the standard WebAssembly System Interface as a dependency.
20
+ - **Real wire protocol** - psycopg (sync and async), asyncpg, SQLAlchemy,
21
+ Django and anything else that speaks Postgres connect to a local Unix socket
22
+ or TCP port.
23
+ - **pytest first** - session-scoped fast startup, function-scoped isolation,
24
+ first-class support for `pytest-xdist` (each worker gets its own database).
25
+ - **Multiple connections** - connections share one single-user Postgres
26
+ backend through a queue with transaction pinning, mirroring upstream
27
+ PGlite behaviour.
28
+ - **Async ready** - the server is asyncio based; async fixtures are provided
29
+ for FastAPI, httpx and other async test suites.
30
+ - **pgvector** - `CREATE EXTENSION vector` works out of the box.
31
+ - **Fast** - a warm worker starts in well under a second; per-test isolation
32
+ resets take milliseconds.
33
+
34
+ ## Install
35
+
36
+ ```bash
37
+ pip install pytest-pglite
38
+ # optional extras
39
+ pip install "pytest-pglite[sqlalchemy]"
40
+ pip install "pytest-pglite[async]"
41
+ pip install "pytest-pglite[django]"
42
+ pip install "pytest-pglite[examples]"
43
+ ```
44
+
45
+ The pytest plugin is registered automatically through the `pytest11` entry
46
+ point, so there is nothing to import or configure.
47
+
48
+ ## Fixtures
49
+
50
+ | Fixture | Scope | Description |
51
+ | --- | --- | --- |
52
+ | `pglite_server` | session | Running `PGliteServer` instance (one per xdist worker) |
53
+ | `pglite_dsn` | session | libpq connection string (Unix socket, or TCP) |
54
+ | `pglite_url` | session | SQLAlchemy URL string |
55
+ | `pglite_conn` | function | `psycopg.Connection` with a freshly reset schema |
56
+ | `pglite_async_conn` | function | `psycopg.AsyncConnection` with a freshly reset schema |
57
+ | `pglite_asyncpg` | function | `asyncpg.Connection` with a freshly reset schema |
58
+ | `pglite_engine` | function | SQLAlchemy `Engine` with a freshly reset schema |
59
+ | `pglite_execute` | function | Callable that runs SQL and returns rows |
60
+
61
+ ## Options
62
+
63
+ ```
64
+ --pglite-extensions=vector Comma separated extensions to create
65
+ --pglite-isolation=schema none | schema | transaction | database
66
+ --pglite-tcp Also listen on TCP (127.0.0.1, ephemeral port)
67
+ --pglite-tcp-address=host:port Explicit TCP bind address (default port 0 = OS assigned)
68
+ --pglite-work-dir=PATH Directory for database files (default: temp dir)
69
+ --pglite-wasm=PATH Use a custom pglite.wasi build
70
+ --pglite-keep-tmp Keep the temporary database directory
71
+ --pglite-log-level=INFO Engine log level
72
+ ```
73
+
74
+ All options can also be set with environment variables using the `PGLITE_`
75
+ prefix, for example `PGLITE_ISOLATION=transaction`.
76
+
77
+ ## Standalone server
78
+
79
+ The same engine can run outside pytest, for example for a manual psql session
80
+ or a development server:
81
+
82
+ ```bash
83
+ pytest-pglite --tcp-address 127.0.0.1:0 --extensions vector
84
+ # PGlite ready: postgresql://postgres:password@127.0.0.1:54321/template1?sslmode=disable
85
+ ```
86
+
87
+ ```python
88
+ from pytest_pglite import PGliteConfig, PGliteServer
89
+
90
+ with PGliteServer(PGliteConfig(tcp=True)) as server:
91
+ print(server.dsn)
92
+ ```
93
+
94
+ ## xdist
95
+
96
+ Each `pytest-xdist` worker process starts its own PGlite instance with its own
97
+ temporary directory, its own Unix socket (or its own OS-assigned TCP port).
98
+ There are no shared paths and no port collisions, so `pytest -n 4` just works.
99
+
100
+ ## How it works
101
+
102
+ The package embeds `pglite.wasi`, a WebAssembly build of PostgreSQL 17.5 with
103
+ pgvector statically linked. It is built from
104
+ [electric-sql/pglite-build](https://github.com/electric-sql/pglite-build) by
105
+ the pipeline in `build/` (see `build/README.md` for the patches and
106
+ provenance). A small engine wrapper drives the WASI module through Wasmtime.
107
+ On top of the
108
+ single-user backend, an asyncio Postgres wire-protocol proxy manages client
109
+ connections: each connection is a logical session, and protocol exchanges are
110
+ serialised through a query queue. Statements and portals are renamed per
111
+ connection so prepared-statement clients (psycopg3, asyncpg, SQLAlchemy) can
112
+ share one backend safely.
113
+
114
+ PGlite is single user, exactly like embedded SQLite. Concurrent connections
115
+ work, but work is serialised; a connection holding an open transaction holds
116
+ the backend until it commits or rolls back.
117
+
118
+ ## Performance
119
+
120
+ Measured on the development machine (Linux x86_64, CPython 3.14):
121
+
122
+ | Measurement | Result |
123
+ | --- | --- |
124
+ | Cold start (first ever, compiles WASM + initdb) | about 2.5 s |
125
+ | Warm start, new worker (AOT + template cache) | about 0.25 s |
126
+ | New connection | about 1 ms |
127
+ | Query round trip (`pytest-pglite` overhead) | about 1.3 ms |
128
+ | Per-test schema reset | about 1 ms |
129
+
130
+ Compiled-module files and an initialised database template are cached under
131
+ the platform cache directory; `pytest-xdist` workers share them.
132
+
133
+ ## Limitations
134
+
135
+ - Single database, single user (`postgres`). `CREATE DATABASE` and connected
136
+ database switching are not supported.
137
+ - Single backend: work from concurrent connections is queued, and a connection
138
+ holding an open transaction blocks others until it commits or rolls back.
139
+ Waiting connections time out with SQLSTATE 57014 after `queue_timeout`
140
+ seconds (default 60).
141
+ - Session state is shared: `SET search_path`, `SET timezone` and similar
142
+ run-time parameters affect every connection because they share one backend
143
+ session. Use `SET LOCAL` inside a transaction for per-test values.
144
+ - PostgreSQL wire-protocol server features are limited to what test suites
145
+ need: `LISTEN/NOTIFY` delivery is not implemented, and cancellation requests
146
+ are accepted but not acted on.
147
+ - `COPY TO STDOUT` works; `COPY FROM STDIN` is rejected with SQLSTATE 0A000
148
+ because libpglite's synchronous transport cannot stream `CopyData` during
149
+ `CopyFrom`.
150
+ - SQL errors kill the WebAssembly instance (the upstream WASI build has no
151
+ `setjmp`/`longjmp`). The Python host forwards the real ErrorResponse and
152
+ SQLSTATE, then restarts the backend over the same data directory when the
153
+ session cannot be recovered.
154
+ - Extensions are compiled into the WASM module at build time; only the
155
+ bundled set (`plpgsql`, `vector`) can be created.
156
+ - No Windows support for Unix sockets; use `--pglite-tcp` on Windows.
157
+
158
+ ## Development
159
+
160
+ ```bash
161
+ poetry install --all-extras
162
+ poetry run pytest
163
+ poetry run black .
164
+ poetry run flake8
165
+ poetry run mypy
166
+ ```
167
+
168
+ The WASM artifact lives in `src/pytest_pglite/_wasm/`; see `build/README.md`
169
+ for provenance and how to rebuild it. Rebuilding uses the upstream
170
+ pglite-build pipeline; the orchestrator pins compiler parallelism through
171
+ `PGLITE_JOBS` (default 8) because upstream defaults to `nproc` and can exhaust
172
+ memory on large machines.
173
+
174
+ ## License
175
+
176
+ Apache License 2.0. See [LICENSE](LICENSE) and
177
+ [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).