parallax-postgres 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.
- parallax_postgres-0.1.0/LICENSE +201 -0
- parallax_postgres-0.1.0/NOTICE +5 -0
- parallax_postgres-0.1.0/PKG-INFO +147 -0
- parallax_postgres-0.1.0/README.md +129 -0
- parallax_postgres-0.1.0/pyproject.toml +52 -0
- parallax_postgres-0.1.0/setup.cfg +4 -0
- parallax_postgres-0.1.0/setup.py +24 -0
- parallax_postgres-0.1.0/src/parallax/postgres/__init__.py +16 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_authorization.py +56 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_cloaders.pxi +103 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_cloaders_binary.pyi +4 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_cloaders_binary.pyx +7 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_cloaders_c.pyi +4 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_cloaders_c.pyx +7 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_compiled_loaders.py +67 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_connection.py +1495 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_context.py +749 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_endpoint.py +150 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_isolation.py +24 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_options.py +150 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_pool_metrics.py +173 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_psycopg_layout/cloader.pxi +10 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_psycopg_layout/psycopg_binary/__init__.pxd +0 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_psycopg_layout/psycopg_binary/_psycopg.pxd +1 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_psycopg_layout/psycopg_c/__init__.pxd +0 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_psycopg_layout/psycopg_c/_psycopg.pxd +1 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_route.py +346 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_route_plan.py +68 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_runtime.py +241 -0
- parallax_postgres-0.1.0/src/parallax/postgres/_startup.py +294 -0
- parallax_postgres-0.1.0/src/parallax/postgres/adapter.py +157 -0
- parallax_postgres-0.1.0/src/parallax/postgres/py.typed +0 -0
- parallax_postgres-0.1.0/src/parallax_postgres.egg-info/PKG-INFO +147 -0
- parallax_postgres-0.1.0/src/parallax_postgres.egg-info/SOURCES.txt +35 -0
- parallax_postgres-0.1.0/src/parallax_postgres.egg-info/dependency_links.txt +1 -0
- parallax_postgres-0.1.0/src/parallax_postgres.egg-info/requires.txt +3 -0
- parallax_postgres-0.1.0/src/parallax_postgres.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: parallax-postgres
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Parallax Postgres database adapter: the concrete database runtime over psycopg and its companion pool.
|
|
5
|
+
Author: Parallax
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Changelog, https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/releases.md
|
|
8
|
+
Project-URL: Documentation, https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/postgresql-lifecycle.md
|
|
9
|
+
Project-URL: Repository, https://github.com/davidsunglee/parallax
|
|
10
|
+
Requires-Python: >=3.14
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
License-File: NOTICE
|
|
14
|
+
Requires-Dist: parallax-core==0.1.0
|
|
15
|
+
Requires-Dist: psycopg[binary]<3.4,>=3.3.6
|
|
16
|
+
Requires-Dist: psycopg-pool>=3.3.1
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# parallax-postgres
|
|
20
|
+
|
|
21
|
+
Parallax's Postgres database adapter over `psycopg` and its companion
|
|
22
|
+
`psycopg-pool`. Install `parallax-postgres==0.1.0` with the application packages
|
|
23
|
+
shown in the [getting-started guide](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/guide/01-getting-started.md#install).
|
|
24
|
+
That guide links the authoritative Python, platform and PostgreSQL support
|
|
25
|
+
declarations and the C-driver and source-build installation paths. The normal
|
|
26
|
+
installation uses the binary driver and a prebuilt Parallax wheel. All installed
|
|
27
|
+
first-party packages must have matching release versions. See the
|
|
28
|
+
[release notes](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/releases.md)
|
|
29
|
+
for publication status. The distribution includes Apache-2.0 `LICENSE` and `NOTICE`.
|
|
30
|
+
|
|
31
|
+
## Connecting
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from parallax.core.db_port import Password
|
|
35
|
+
from parallax.postgres import OnDemandOptions, PoolOptions, PostgresAdapter, PostgresEndpoint
|
|
36
|
+
from parallax.snapshot import connect
|
|
37
|
+
|
|
38
|
+
writer = PostgresEndpoint("postgresql://app@localhost/app", credentials=Password("s3cret"))
|
|
39
|
+
adapter = PostgresAdapter(writer=writer)
|
|
40
|
+
with connect(adapter, model) as root:
|
|
41
|
+
db = root.using_database_login()
|
|
42
|
+
...
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`PostgresEndpoint` and `PostgresAdapter` are **configuration**. Constructing
|
|
46
|
+
either opens no connection, no pool, and no thread: an endpoint parses its
|
|
47
|
+
connection string and validates its credential declaration and retention
|
|
48
|
+
policy, and the adapter validates the acquisition and startup budgets its
|
|
49
|
+
runtime spends. That is what makes them safe to build at import time, hold as
|
|
50
|
+
module constants, share between threads, and — for a forking server — build
|
|
51
|
+
before the fork and open after it. The adapter's `writer` is the endpoint that
|
|
52
|
+
transactions and every read of a single-endpoint deployment use; nothing about
|
|
53
|
+
its role is inferred from its connection string.
|
|
54
|
+
|
|
55
|
+
## Where the password lives
|
|
56
|
+
|
|
57
|
+
The connection string says **where**, and `credentials` says **how**. A string
|
|
58
|
+
carrying a password is refused at construction, whatever `credentials` is, and
|
|
59
|
+
the refusal never quotes the string back:
|
|
60
|
+
|
|
61
|
+
```text
|
|
62
|
+
PostgresEndpoint("postgresql://app:<password>@db.internal/app", credentials=Password("<password>"))
|
|
63
|
+
ValueError: connection_string must not carry a password; supply it through credentials.
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
There are three spellings, all exported from `parallax.core.db_port`:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from parallax.core.db_port import DRIVER_MANAGED, Password
|
|
70
|
+
|
|
71
|
+
# a constant secret; `Password` is its own source and keeps it out of every repr
|
|
72
|
+
PostgresEndpoint("postgresql://app@db.internal:5432/app", credentials=Password("s3cret"))
|
|
73
|
+
|
|
74
|
+
# any object with `resolve() -> Password`, asked as connections are established
|
|
75
|
+
PostgresEndpoint("postgresql://app@db.internal/app", credentials=my_source)
|
|
76
|
+
|
|
77
|
+
# Parallax supplies none: peer, trust, a client certificate, Kerberos, or
|
|
78
|
+
# libpq's own PGPASSWORD, .pgpass and service files
|
|
79
|
+
PostgresEndpoint("host=/var/run/postgresql dbname=app", credentials=DRIVER_MANAGED)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
A source is asked where the driver establishes a **physical** connection, and
|
|
83
|
+
never when an acquisition reuses a retained one, which is what lets a
|
|
84
|
+
short-lived cloud token authenticate a pool that outlives it. It may block on a
|
|
85
|
+
network and must bound its own I/O. Parallax cannot see a password reachable
|
|
86
|
+
through a `service` file or `PGPASSWORD`, so neither is refused above; under an
|
|
87
|
+
explicit source the resolved password wins over both, by libpq's own
|
|
88
|
+
precedence.
|
|
89
|
+
|
|
90
|
+
`connect` is what opens a runtime, and the Database Root it returns is what owns
|
|
91
|
+
that runtime. Modeled work requires an explicitly selected `ScopedDatabase`, as
|
|
92
|
+
shown above. **Close the root**: `root.close()` and the context manager above are
|
|
93
|
+
equivalent, and both are idempotent. Every `connect` over one configuration
|
|
94
|
+
opens an independent runtime, so closing one root leaves another working.
|
|
95
|
+
|
|
96
|
+
Each operation acquires a connection and gives it back — an eager read for its
|
|
97
|
+
whole execution, each standalone stream page through its materialization, and a
|
|
98
|
+
transaction attempt for the attempt. A standalone page gives the connection back
|
|
99
|
+
before publishing roots, so caller work between pages can use even a one-slot
|
|
100
|
+
pool. A participating stream instead inherits its transaction attempt's
|
|
101
|
+
connection for every page.
|
|
102
|
+
|
|
103
|
+
A `reader=PostgresEndpoint(...)` beside the `writer` declares a reader route
|
|
104
|
+
with its own credentials and retention. New reads then default to it, writable
|
|
105
|
+
transactions stay on the writer, and both routes must authenticate the same
|
|
106
|
+
login; `reader_startup="required"` makes an unreachable reader fail `connect`
|
|
107
|
+
instead of leaving the reader pending. `with_routing(read_policy="reader_preferred")`
|
|
108
|
+
reads from the reader and moves to the writer, within the same
|
|
109
|
+
`acquire_timeout`, only where the reader cannot be reached or loses its
|
|
110
|
+
connection before any of your work runs; each route keeps its own `max_size`,
|
|
111
|
+
so a two-route root can hold both pools' connections in every process.
|
|
112
|
+
|
|
113
|
+
An endpoint's `pool=PoolOptions(...)` keeps connections between operations;
|
|
114
|
+
`pool=OnDemandOptions(...)` keeps none, closing each on release unless a caller
|
|
115
|
+
is already waiting for one. Omitting `pool` takes the retaining defaults. The
|
|
116
|
+
adapter's `acquire_timeout` and `startup_timeout` budget the whole runtime
|
|
117
|
+
rather than one pool.
|
|
118
|
+
|
|
119
|
+
## Everything else
|
|
120
|
+
|
|
121
|
+
[PostgreSQL connection lifecycle](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/postgresql-lifecycle.md)
|
|
122
|
+
is this adapter's operational guide, and the one place these facts are written
|
|
123
|
+
down:
|
|
124
|
+
|
|
125
|
+
- the full retention settings and their defaults, and what the driver's own
|
|
126
|
+
maintenance settings mean;
|
|
127
|
+
- what each operation holds, standalone per-page leases, and participating
|
|
128
|
+
connection affinity;
|
|
129
|
+
- the five distinct timeouts, and the one driver precedence that surprises
|
|
130
|
+
people;
|
|
131
|
+
- process and application-server lifetime — post-fork construction, the ASGI
|
|
132
|
+
lifespan, offloading complete operations, and why pool capacity is not HTTP
|
|
133
|
+
concurrency;
|
|
134
|
+
- writer and reader routes, deferred reader readiness, preferred reads and
|
|
135
|
+
their fallback, and sizing two routes' pools;
|
|
136
|
+
- connection budgeting across workers, replicas and runtimes;
|
|
137
|
+
- observing the pool, and what its measurements do and do not mean;
|
|
138
|
+
- which session settings are required, which are preserved;
|
|
139
|
+
- how fixed login identity, scoped role replacement/restoration, and required
|
|
140
|
+
session affinity behave, including unsupported transaction/statement pooling
|
|
141
|
+
proxies and the absence of runtime proxy detection;
|
|
142
|
+
- the three logging paths and their different disclosure policies;
|
|
143
|
+
- migrating from the pre-pooling surface.
|
|
144
|
+
|
|
145
|
+
Every application-shaped Python block in it is the source of an executable story
|
|
146
|
+
that runs against a real PostgreSQL server, so nothing there is a snippet that
|
|
147
|
+
has never executed.
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# parallax-postgres
|
|
2
|
+
|
|
3
|
+
Parallax's Postgres database adapter over `psycopg` and its companion
|
|
4
|
+
`psycopg-pool`. Install `parallax-postgres==0.1.0` with the application packages
|
|
5
|
+
shown in the [getting-started guide](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/guide/01-getting-started.md#install).
|
|
6
|
+
That guide links the authoritative Python, platform and PostgreSQL support
|
|
7
|
+
declarations and the C-driver and source-build installation paths. The normal
|
|
8
|
+
installation uses the binary driver and a prebuilt Parallax wheel. All installed
|
|
9
|
+
first-party packages must have matching release versions. See the
|
|
10
|
+
[release notes](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/releases.md)
|
|
11
|
+
for publication status. The distribution includes Apache-2.0 `LICENSE` and `NOTICE`.
|
|
12
|
+
|
|
13
|
+
## Connecting
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from parallax.core.db_port import Password
|
|
17
|
+
from parallax.postgres import OnDemandOptions, PoolOptions, PostgresAdapter, PostgresEndpoint
|
|
18
|
+
from parallax.snapshot import connect
|
|
19
|
+
|
|
20
|
+
writer = PostgresEndpoint("postgresql://app@localhost/app", credentials=Password("s3cret"))
|
|
21
|
+
adapter = PostgresAdapter(writer=writer)
|
|
22
|
+
with connect(adapter, model) as root:
|
|
23
|
+
db = root.using_database_login()
|
|
24
|
+
...
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`PostgresEndpoint` and `PostgresAdapter` are **configuration**. Constructing
|
|
28
|
+
either opens no connection, no pool, and no thread: an endpoint parses its
|
|
29
|
+
connection string and validates its credential declaration and retention
|
|
30
|
+
policy, and the adapter validates the acquisition and startup budgets its
|
|
31
|
+
runtime spends. That is what makes them safe to build at import time, hold as
|
|
32
|
+
module constants, share between threads, and — for a forking server — build
|
|
33
|
+
before the fork and open after it. The adapter's `writer` is the endpoint that
|
|
34
|
+
transactions and every read of a single-endpoint deployment use; nothing about
|
|
35
|
+
its role is inferred from its connection string.
|
|
36
|
+
|
|
37
|
+
## Where the password lives
|
|
38
|
+
|
|
39
|
+
The connection string says **where**, and `credentials` says **how**. A string
|
|
40
|
+
carrying a password is refused at construction, whatever `credentials` is, and
|
|
41
|
+
the refusal never quotes the string back:
|
|
42
|
+
|
|
43
|
+
```text
|
|
44
|
+
PostgresEndpoint("postgresql://app:<password>@db.internal/app", credentials=Password("<password>"))
|
|
45
|
+
ValueError: connection_string must not carry a password; supply it through credentials.
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
There are three spellings, all exported from `parallax.core.db_port`:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from parallax.core.db_port import DRIVER_MANAGED, Password
|
|
52
|
+
|
|
53
|
+
# a constant secret; `Password` is its own source and keeps it out of every repr
|
|
54
|
+
PostgresEndpoint("postgresql://app@db.internal:5432/app", credentials=Password("s3cret"))
|
|
55
|
+
|
|
56
|
+
# any object with `resolve() -> Password`, asked as connections are established
|
|
57
|
+
PostgresEndpoint("postgresql://app@db.internal/app", credentials=my_source)
|
|
58
|
+
|
|
59
|
+
# Parallax supplies none: peer, trust, a client certificate, Kerberos, or
|
|
60
|
+
# libpq's own PGPASSWORD, .pgpass and service files
|
|
61
|
+
PostgresEndpoint("host=/var/run/postgresql dbname=app", credentials=DRIVER_MANAGED)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
A source is asked where the driver establishes a **physical** connection, and
|
|
65
|
+
never when an acquisition reuses a retained one, which is what lets a
|
|
66
|
+
short-lived cloud token authenticate a pool that outlives it. It may block on a
|
|
67
|
+
network and must bound its own I/O. Parallax cannot see a password reachable
|
|
68
|
+
through a `service` file or `PGPASSWORD`, so neither is refused above; under an
|
|
69
|
+
explicit source the resolved password wins over both, by libpq's own
|
|
70
|
+
precedence.
|
|
71
|
+
|
|
72
|
+
`connect` is what opens a runtime, and the Database Root it returns is what owns
|
|
73
|
+
that runtime. Modeled work requires an explicitly selected `ScopedDatabase`, as
|
|
74
|
+
shown above. **Close the root**: `root.close()` and the context manager above are
|
|
75
|
+
equivalent, and both are idempotent. Every `connect` over one configuration
|
|
76
|
+
opens an independent runtime, so closing one root leaves another working.
|
|
77
|
+
|
|
78
|
+
Each operation acquires a connection and gives it back — an eager read for its
|
|
79
|
+
whole execution, each standalone stream page through its materialization, and a
|
|
80
|
+
transaction attempt for the attempt. A standalone page gives the connection back
|
|
81
|
+
before publishing roots, so caller work between pages can use even a one-slot
|
|
82
|
+
pool. A participating stream instead inherits its transaction attempt's
|
|
83
|
+
connection for every page.
|
|
84
|
+
|
|
85
|
+
A `reader=PostgresEndpoint(...)` beside the `writer` declares a reader route
|
|
86
|
+
with its own credentials and retention. New reads then default to it, writable
|
|
87
|
+
transactions stay on the writer, and both routes must authenticate the same
|
|
88
|
+
login; `reader_startup="required"` makes an unreachable reader fail `connect`
|
|
89
|
+
instead of leaving the reader pending. `with_routing(read_policy="reader_preferred")`
|
|
90
|
+
reads from the reader and moves to the writer, within the same
|
|
91
|
+
`acquire_timeout`, only where the reader cannot be reached or loses its
|
|
92
|
+
connection before any of your work runs; each route keeps its own `max_size`,
|
|
93
|
+
so a two-route root can hold both pools' connections in every process.
|
|
94
|
+
|
|
95
|
+
An endpoint's `pool=PoolOptions(...)` keeps connections between operations;
|
|
96
|
+
`pool=OnDemandOptions(...)` keeps none, closing each on release unless a caller
|
|
97
|
+
is already waiting for one. Omitting `pool` takes the retaining defaults. The
|
|
98
|
+
adapter's `acquire_timeout` and `startup_timeout` budget the whole runtime
|
|
99
|
+
rather than one pool.
|
|
100
|
+
|
|
101
|
+
## Everything else
|
|
102
|
+
|
|
103
|
+
[PostgreSQL connection lifecycle](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/postgresql-lifecycle.md)
|
|
104
|
+
is this adapter's operational guide, and the one place these facts are written
|
|
105
|
+
down:
|
|
106
|
+
|
|
107
|
+
- the full retention settings and their defaults, and what the driver's own
|
|
108
|
+
maintenance settings mean;
|
|
109
|
+
- what each operation holds, standalone per-page leases, and participating
|
|
110
|
+
connection affinity;
|
|
111
|
+
- the five distinct timeouts, and the one driver precedence that surprises
|
|
112
|
+
people;
|
|
113
|
+
- process and application-server lifetime — post-fork construction, the ASGI
|
|
114
|
+
lifespan, offloading complete operations, and why pool capacity is not HTTP
|
|
115
|
+
concurrency;
|
|
116
|
+
- writer and reader routes, deferred reader readiness, preferred reads and
|
|
117
|
+
their fallback, and sizing two routes' pools;
|
|
118
|
+
- connection budgeting across workers, replicas and runtimes;
|
|
119
|
+
- observing the pool, and what its measurements do and do not mean;
|
|
120
|
+
- which session settings are required, which are preserved;
|
|
121
|
+
- how fixed login identity, scoped role replacement/restoration, and required
|
|
122
|
+
session affinity behave, including unsupported transaction/statement pooling
|
|
123
|
+
proxies and the absence of runtime proxy detection;
|
|
124
|
+
- the three logging paths and their different disclosure policies;
|
|
125
|
+
- migrating from the pre-pooling surface.
|
|
126
|
+
|
|
127
|
+
Every application-shaped Python block in it is the source of an executable story
|
|
128
|
+
that runs against a real PostgreSQL server, so nothing there is a snippet that
|
|
129
|
+
has never executed.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "parallax-postgres"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Parallax Postgres database adapter: the concrete database runtime over psycopg and its companion pool."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.14"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE", "NOTICE"]
|
|
9
|
+
authors = [{ name = "Parallax" }]
|
|
10
|
+
dependencies = [
|
|
11
|
+
"parallax-core==0.1.0",
|
|
12
|
+
# The binary extra bundles libpq so the adapter installs self-contained (no
|
|
13
|
+
# system libpq required); the adapter owns that capability, so a sibling
|
|
14
|
+
# that imports psycopg declares it without the extra. The compiled loaders
|
|
15
|
+
# extend psycopg's private C loader layout, so a new minor release is a
|
|
16
|
+
# reviewed change rather than an automatic one.
|
|
17
|
+
"psycopg[binary]>=3.3.6,<3.4",
|
|
18
|
+
# The companion pool the runtime owns: the same project, released
|
|
19
|
+
# separately.
|
|
20
|
+
"psycopg-pool>=3.3.1",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Changelog = "https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/releases.md"
|
|
25
|
+
Documentation = "https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/postgresql-lifecycle.md"
|
|
26
|
+
Repository = "https://github.com/davidsunglee/parallax"
|
|
27
|
+
|
|
28
|
+
[build-system]
|
|
29
|
+
requires = ["setuptools==84.2.1", "Cython==3.3.0"]
|
|
30
|
+
build-backend = "setuptools.build_meta"
|
|
31
|
+
|
|
32
|
+
[tool.setuptools]
|
|
33
|
+
package-dir = { "" = "src" }
|
|
34
|
+
packages = ["parallax.postgres"]
|
|
35
|
+
include-package-data = false
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.package-data]
|
|
38
|
+
"parallax.postgres" = ["py.typed", "*.pyi"]
|
|
39
|
+
|
|
40
|
+
# The compiled modules are built from the Cython sources, so an edit to one must
|
|
41
|
+
# rebuild the installed package.
|
|
42
|
+
[tool.uv]
|
|
43
|
+
cache-keys = [
|
|
44
|
+
{ file = "pyproject.toml" },
|
|
45
|
+
{ file = "setup.py" },
|
|
46
|
+
{ file = "src/**/*.pyx" },
|
|
47
|
+
{ file = "src/**/*.pxd" },
|
|
48
|
+
{ file = "src/**/*.pxi" },
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
[tool.parallax.postgresql]
|
|
52
|
+
supported = ">=18.6,<19"
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""The compiled loader modules. Everything else about the build is declared in pyproject.toml."""
|
|
2
|
+
|
|
3
|
+
from setuptools import Extension, setup
|
|
4
|
+
|
|
5
|
+
_PACKAGE = "src/parallax/postgres"
|
|
6
|
+
_LAYOUT = f"{_PACKAGE}/_psycopg_layout"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _loaders(build: str) -> Extension:
|
|
10
|
+
"""The one loader body, compiled against one psycopg build's C adaptation module."""
|
|
11
|
+
return Extension(
|
|
12
|
+
f"parallax.postgres._cloaders_{build}",
|
|
13
|
+
sources=[f"{_PACKAGE}/_cloaders_{build}.pyx"],
|
|
14
|
+
include_dirs=[_LAYOUT],
|
|
15
|
+
depends=[
|
|
16
|
+
f"{_PACKAGE}/_cloaders.pxi",
|
|
17
|
+
f"{_LAYOUT}/cloader.pxi",
|
|
18
|
+
f"{_LAYOUT}/psycopg_{build}/__init__.pxd",
|
|
19
|
+
f"{_LAYOUT}/psycopg_{build}/_psycopg.pxd",
|
|
20
|
+
],
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
setup(ext_modules=[_loaders("binary"), _loaders("c")])
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from parallax.postgres._authorization import PostgresRole
|
|
4
|
+
from parallax.postgres._endpoint import PostgresEndpoint
|
|
5
|
+
from parallax.postgres._isolation import isolation_spelling
|
|
6
|
+
from parallax.postgres._options import OnDemandOptions, PoolOptions
|
|
7
|
+
from parallax.postgres.adapter import PostgresAdapter
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"OnDemandOptions",
|
|
11
|
+
"PoolOptions",
|
|
12
|
+
"PostgresAdapter",
|
|
13
|
+
"PostgresEndpoint",
|
|
14
|
+
"PostgresRole",
|
|
15
|
+
"isolation_spelling",
|
|
16
|
+
]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import TYPE_CHECKING, Any
|
|
5
|
+
|
|
6
|
+
from psycopg.sql import SQL, Composed, Identifier
|
|
7
|
+
|
|
8
|
+
from parallax.core.db_port import InvalidAuthorizationError
|
|
9
|
+
from parallax.postgres._connection import (
|
|
10
|
+
_complete_first_send, # pyright: ignore[reportPrivateUsage] - the lease's shared first-send markers
|
|
11
|
+
_start_first_send, # pyright: ignore[reportPrivateUsage] - the lease's shared first-send markers
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
import psycopg
|
|
16
|
+
from psycopg.rows import TupleRow
|
|
17
|
+
|
|
18
|
+
from parallax.postgres._context import PostgresConnectionContext
|
|
19
|
+
|
|
20
|
+
__all__ = ["PostgresRole", "install_role", "restore_role"]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True, slots=True)
|
|
24
|
+
class PostgresRole:
|
|
25
|
+
"""A non-empty PostgreSQL role name bound to principal execution."""
|
|
26
|
+
|
|
27
|
+
name: str
|
|
28
|
+
|
|
29
|
+
def __post_init__(self) -> None:
|
|
30
|
+
if not isinstance(self.name, str): # pyright: ignore[reportUnnecessaryIsInstance] - validates untyped callers at the public boundary
|
|
31
|
+
raise InvalidAuthorizationError("a PostgreSQL role name must be a string")
|
|
32
|
+
if not self.name:
|
|
33
|
+
raise InvalidAuthorizationError("a PostgreSQL role name must not be empty")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _role_statement(role: PostgresRole) -> Composed:
|
|
37
|
+
return SQL("SET ROLE {}").format(Identifier(role.name))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _install_role(
|
|
41
|
+
connection: psycopg.Connection[TupleRow],
|
|
42
|
+
context: PostgresConnectionContext | None,
|
|
43
|
+
statement: Composed,
|
|
44
|
+
) -> None:
|
|
45
|
+
with connection.cursor() as cursor:
|
|
46
|
+
_start_first_send(context)
|
|
47
|
+
cursor.execute(statement)
|
|
48
|
+
_complete_first_send(context)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def install_role(connection: Any, role: PostgresRole) -> None:
|
|
52
|
+
_install_role(connection, None, _role_statement(role))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def restore_role(connection: Any) -> None:
|
|
56
|
+
connection.execute("RESET ROLE")
|