mizzle 0.2.2__tar.gz → 0.2.4__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.
Files changed (141) hide show
  1. {mizzle-0.2.2 → mizzle-0.2.4}/CHANGELOG.md +21 -0
  2. {mizzle-0.2.2 → mizzle-0.2.4}/PKG-INFO +45 -1
  3. {mizzle-0.2.2 → mizzle-0.2.4}/README.md +44 -0
  4. {mizzle-0.2.2 → mizzle-0.2.4}/pyproject.toml +1 -1
  5. mizzle-0.2.4/release/notes.md +5 -0
  6. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/__init__.py +1 -1
  7. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/compiler.py +20 -1
  8. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/query/base.py +33 -20
  9. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/sql/functions.py +1 -1
  10. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/sql/nodes.py +3 -0
  11. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/db.py +11 -9
  12. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/dialects/base.py +5 -4
  13. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/dialects/postgres/__init__.py +1 -1
  14. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/dialects/postgres/driver.py +1 -1
  15. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/studio/ui.json +2 -2
  16. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_query_gaps.py +18 -0
  17. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_replicas.py +7 -2
  18. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_query_gaps.py +26 -1
  19. {mizzle-0.2.2 → mizzle-0.2.4}/uv.lock +1 -1
  20. mizzle-0.2.2/release/notes.md +0 -10
  21. {mizzle-0.2.2 → mizzle-0.2.4}/.gitignore +0 -0
  22. {mizzle-0.2.2 → mizzle-0.2.4}/.readthedocs.yaml +0 -0
  23. {mizzle-0.2.2 → mizzle-0.2.4}/CONTRIBUTING.md +0 -0
  24. {mizzle-0.2.2 → mizzle-0.2.4}/LICENSE +0 -0
  25. {mizzle-0.2.2 → mizzle-0.2.4}/assets/icon-1024.png +0 -0
  26. {mizzle-0.2.2 → mizzle-0.2.4}/assets/icon-512.png +0 -0
  27. {mizzle-0.2.2 → mizzle-0.2.4}/assets/icon.svg +0 -0
  28. {mizzle-0.2.2 → mizzle-0.2.4}/assets/logo-dark.png +0 -0
  29. {mizzle-0.2.2 → mizzle-0.2.4}/assets/logo-dark.svg +0 -0
  30. {mizzle-0.2.2 → mizzle-0.2.4}/assets/logo.png +0 -0
  31. {mizzle-0.2.2 → mizzle-0.2.4}/assets/logo.svg +0 -0
  32. {mizzle-0.2.2 → mizzle-0.2.4}/assets/mark-1024.png +0 -0
  33. {mizzle-0.2.2 → mizzle-0.2.4}/assets/mark-512.png +0 -0
  34. {mizzle-0.2.2 → mizzle-0.2.4}/assets/mark-dark-1024.png +0 -0
  35. {mizzle-0.2.2 → mizzle-0.2.4}/assets/mark-dark-512.png +0 -0
  36. {mizzle-0.2.2 → mizzle-0.2.4}/assets/mark-dark.svg +0 -0
  37. {mizzle-0.2.2 → mizzle-0.2.4}/assets/mark.svg +0 -0
  38. {mizzle-0.2.2 → mizzle-0.2.4}/assets/social-preview.png +0 -0
  39. {mizzle-0.2.2 → mizzle-0.2.4}/mkdocs.yml +0 -0
  40. /mizzle-0.2.2/release/mizzle-studio-0.2.2.zip → /mizzle-0.2.4/release/mizzle-studio-0.2.4.zip +0 -0
  41. {mizzle-0.2.2 → mizzle-0.2.4}/scripts/ci-local.sh +0 -0
  42. {mizzle-0.2.2 → mizzle-0.2.4}/scripts/llms_full.py +0 -0
  43. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/__main__.py +0 -0
  44. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/__init__.py +0 -0
  45. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/errors.py +0 -0
  46. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/naming.py +0 -0
  47. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/query/__init__.py +0 -0
  48. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/query/builder.py +0 -0
  49. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/query/insert.py +0 -0
  50. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/query/relational.py +0 -0
  51. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/query/returning.py +0 -0
  52. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/query/select.py +0 -0
  53. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/query/update.py +0 -0
  54. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/schema/__init__.py +0 -0
  55. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/schema/column.py +0 -0
  56. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/schema/constraints.py +0 -0
  57. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/schema/objects.py +0 -0
  58. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/schema/relations.py +0 -0
  59. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/schema/table.py +0 -0
  60. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/schema/types.py +0 -0
  61. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/schema/view.py +0 -0
  62. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/snapshot.py +0 -0
  63. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/sql/__init__.py +0 -0
  64. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/sql/alias.py +0 -0
  65. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/sql/operators.py +0 -0
  66. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/sql/raw.py +0 -0
  67. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/core/steps.py +0 -0
  68. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/dialects/__init__.py +0 -0
  69. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/dialects/postgres/compiler.py +0 -0
  70. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/dialects/postgres/ddl.py +0 -0
  71. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/dialects/postgres/introspect.py +0 -0
  72. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/dialects/postgres/types.py +0 -0
  73. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/kit/__init__.py +0 -0
  74. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/kit/cli.py +0 -0
  75. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/kit/codegen.py +0 -0
  76. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/kit/config.py +0 -0
  77. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/kit/diff.py +0 -0
  78. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/kit/init.py +0 -0
  79. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/kit/journal.py +0 -0
  80. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/kit/migrate.py +0 -0
  81. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/kit/snapshot.py +0 -0
  82. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/pg.py +0 -0
  83. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/py.typed +0 -0
  84. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/pydantic.py +0 -0
  85. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/studio/__init__.py +0 -0
  86. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/studio/assets.py +0 -0
  87. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/studio/server.py +0 -0
  88. {mizzle-0.2.2 → mizzle-0.2.4}/src/mizzle/supabase.py +0 -0
  89. {mizzle-0.2.2 → mizzle-0.2.4}/tests/__init__.py +0 -0
  90. {mizzle-0.2.2 → mizzle-0.2.4}/tests/conftest.py +0 -0
  91. {mizzle-0.2.2 → mizzle-0.2.4}/tests/infra/compose.yml +0 -0
  92. {mizzle-0.2.2 → mizzle-0.2.4}/tests/infra/odd-init.sh +0 -0
  93. {mizzle-0.2.2 → mizzle-0.2.4}/tests/infra/primary-init.sh +0 -0
  94. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/__init__.py +0 -0
  95. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/conftest.py +0 -0
  96. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_bulk.py +0 -0
  97. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_database.py +0 -0
  98. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_expressions.py +0 -0
  99. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_fuzz_expressions.py +0 -0
  100. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_fuzz_joins.py +0 -0
  101. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_fuzz_migrations.py +0 -0
  102. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_fuzz_queries.py +0 -0
  103. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_fuzz_types.py +0 -0
  104. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_generated_columns.py +0 -0
  105. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_infra.py +0 -0
  106. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_infra_io.py +0 -0
  107. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_kit_cli.py +0 -0
  108. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_listen.py +0 -0
  109. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_migration_gaps.py +0 -0
  110. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_production_env.py +0 -0
  111. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_relational.py +0 -0
  112. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_roles.py +0 -0
  113. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_roundtrip.py +0 -0
  114. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_schema_objects.py +0 -0
  115. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_stream.py +0 -0
  116. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_stress.py +0 -0
  117. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_studio.py +0 -0
  118. {mizzle-0.2.2 → mizzle-0.2.4}/tests/integration/test_vector.py +0 -0
  119. {mizzle-0.2.2 → mizzle-0.2.4}/tests/models.py +0 -0
  120. {mizzle-0.2.2 → mizzle-0.2.4}/tests/typing/__init__.py +0 -0
  121. {mizzle-0.2.2 → mizzle-0.2.4}/tests/typing/check_types.py +0 -0
  122. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/__init__.py +0 -0
  123. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/conftest.py +0 -0
  124. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_columns.py +0 -0
  125. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_compiler.py +0 -0
  126. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_errors.py +0 -0
  127. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_expressions.py +0 -0
  128. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_generated_columns.py +0 -0
  129. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_kit.py +0 -0
  130. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_migration_gaps.py +0 -0
  131. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_mutations.py +0 -0
  132. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_pull_relations.py +0 -0
  133. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_pydantic.py +0 -0
  134. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_raw_py314.py +0 -0
  135. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_runtime_types.py +0 -0
  136. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_schema_objects.py +0 -0
  137. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_select.py +0 -0
  138. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_session.py +0 -0
  139. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_studio_assets.py +0 -0
  140. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_supabase.py +0 -0
  141. {mizzle-0.2.2 → mizzle-0.2.4}/tests/unit/test_tables.py +0 -0
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.4
4
+
5
+ - Faster row building: each table's row decoder is generated once (fixed column positions
6
+ in a dict display) and cached. Selecting 1000 rows is about 1.75x faster, nested
7
+ relational queries about 1.25x.
8
+
9
+ ## 0.2.3
10
+
11
+ - `reset_connections="auto"` is the default: a connection's session state is reset when it
12
+ goes back to the pool only after a statement that may have changed it (raw `sql(...)`, a
13
+ function named with `func()`, `listen()`, direct connection use). This saves a round trip
14
+ per query: about 2x faster single-row work over a real network. Open transactions are
15
+ still always rolled back. Pass `reset_connections="always"` if triggers or database
16
+ functions change session settings themselves (`SET` or `set_config(..., false)` without
17
+ `LOCAL`, session advisory locks, temp tables).
18
+ - A function named with `func("...")` now counts as possibly changing the session, so the
19
+ connection is reset after it (before, `func("set_config", ..., False)` with `"auto"`
20
+ could leak a setting to the next user).
21
+ - Benchmarks against Tortoise, SQLAlchemy (ORM and Core), Piccolo and Django, with 50
22
+ concurrent clients and network latency (`BENCH_CLIENTS`, `BENCH_POOL`, Toxiproxy).
23
+
3
24
  ## 0.2.2
4
25
 
5
26
  - `mizzle pull --update`: follow a schema another app owns (microservices on one
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: mizzle
3
- Version: 0.2.2
3
+ Version: 0.2.4
4
4
  Summary: Fully typed async ORM for PostgreSQL: SQL-like queries, schema-driven migrations and a data studio
5
5
  Project-URL: Homepage, https://github.com/mizzlehq/mizzle
6
6
  Project-URL: Repository, https://github.com/mizzlehq/mizzle
@@ -120,6 +120,50 @@ Writes, transactions and `FOR UPDATE` always use the primary. Behind PgBouncer i
120
120
  transaction mode, pass `statement_cache_size=0`. CI tests a real streaming replica and
121
121
  PgBouncer on every change.
122
122
 
123
+ ## Benchmarks
124
+
125
+ Operations per second, higher is better: the same queries on the same PostgreSQL 18 database,
126
+ median of 5 rounds ([benchmarks/bench.py](https://github.com/mizzlehq/mizzle/blob/main/benchmarks/bench.py)).
127
+
128
+ **A busy app:** 50 concurrent clients, 8 connections per library, and 1 ms of network
129
+ latency each way (2.2 ms round trip, like a database on another server):
130
+
131
+ | | mizzle | Tortoise | SQLAlchemy ORM | SQLAlchemy Core | Piccolo | Django |
132
+ |---|---:|---:|---:|---:|---:|---:|
133
+ | insert 1 row | **3,040** | 1,587 | 935 | 921 | 1,577 | 427 |
134
+ | bulk insert 1000 rows | 448 | 180 | 223 | **462** | 76 | 111 |
135
+ | get by primary key | **3,078** | 1,566 | 942 | 952 | 1,565 | 413 |
136
+ | select 1000 rows | **728** | 362 | 268 | 680\* | 331 | 239 |
137
+ | update by primary key | **3,035** | 1,566 | 935 | 923 | 1,573 | 426 |
138
+ | 100 users with their posts | **1,358** | 392 | 245 | 549 | 369 | 110 |
139
+
140
+ **One client, database on the same machine** (0.04 ms round trip, 4 connections):
141
+
142
+ | | mizzle | Tortoise | SQLAlchemy ORM | SQLAlchemy Core | Piccolo | Django |
143
+ |---|---:|---:|---:|---:|---:|---:|
144
+ | insert 1 row | **9,831** | 6,207 | 2,226 | 2,827 | 5,651 | 4,588 |
145
+ | bulk insert 1000 rows | **215** | 120 | 156 | 194 | 66 | 132 |
146
+ | get by primary key | **10,112** | 4,516 | 2,623 | 3,182 | 4,706 | 3,467 |
147
+ | select 1000 rows | **898** | 356 | 300 | 846\* | 390 | 336 |
148
+ | update by primary key | **10,516** | 4,234 | 2,085 | 2,779 | 5,345 | 4,061 |
149
+ | 100 users with their posts | **655** | 336 | 234 | 472 | 320 | 218 |
150
+
151
+ \* SQLAlchemy Core returns plain tuples; the ORMs build full row objects.
152
+
153
+ mizzle loads nested rows in one statement, so it pays one round trip where the others pay
154
+ two. Raw asyncpg, the driver under mizzle, is the upper limit: on single-row work mizzle
155
+ reaches 97% of it over the network and 67–84% on the same machine. Python 3.14, mizzle 0.2.3, Tortoise 1.1.9,
156
+ SQLAlchemy 2.1.6, Piccolo 1.39.0, Django 6.1.3 (its async ORM runs queries in one thread, on
157
+ psycopg). The script's docstring explains each library's setup and the latency proxy. Run
158
+ it yourself:
159
+
160
+ ```bash
161
+ docker compose up -d
162
+ uv run --with tortoise-orm --with "sqlalchemy[asyncio]" --with piccolo --with django \
163
+ --with "psycopg[binary,pool]" python benchmarks/bench.py
164
+ BENCH_CLIENTS=50 BENCH_POOL=8 uv run ... # the concurrent run
165
+ ```
166
+
123
167
  ## What else is in the box
124
168
 
125
169
  Joins, subqueries, CTEs, window functions, `UNION`, `CASE` and typed SQL functions;
@@ -91,6 +91,50 @@ Writes, transactions and `FOR UPDATE` always use the primary. Behind PgBouncer i
91
91
  transaction mode, pass `statement_cache_size=0`. CI tests a real streaming replica and
92
92
  PgBouncer on every change.
93
93
 
94
+ ## Benchmarks
95
+
96
+ Operations per second, higher is better: the same queries on the same PostgreSQL 18 database,
97
+ median of 5 rounds ([benchmarks/bench.py](https://github.com/mizzlehq/mizzle/blob/main/benchmarks/bench.py)).
98
+
99
+ **A busy app:** 50 concurrent clients, 8 connections per library, and 1 ms of network
100
+ latency each way (2.2 ms round trip, like a database on another server):
101
+
102
+ | | mizzle | Tortoise | SQLAlchemy ORM | SQLAlchemy Core | Piccolo | Django |
103
+ |---|---:|---:|---:|---:|---:|---:|
104
+ | insert 1 row | **3,040** | 1,587 | 935 | 921 | 1,577 | 427 |
105
+ | bulk insert 1000 rows | 448 | 180 | 223 | **462** | 76 | 111 |
106
+ | get by primary key | **3,078** | 1,566 | 942 | 952 | 1,565 | 413 |
107
+ | select 1000 rows | **728** | 362 | 268 | 680\* | 331 | 239 |
108
+ | update by primary key | **3,035** | 1,566 | 935 | 923 | 1,573 | 426 |
109
+ | 100 users with their posts | **1,358** | 392 | 245 | 549 | 369 | 110 |
110
+
111
+ **One client, database on the same machine** (0.04 ms round trip, 4 connections):
112
+
113
+ | | mizzle | Tortoise | SQLAlchemy ORM | SQLAlchemy Core | Piccolo | Django |
114
+ |---|---:|---:|---:|---:|---:|---:|
115
+ | insert 1 row | **9,831** | 6,207 | 2,226 | 2,827 | 5,651 | 4,588 |
116
+ | bulk insert 1000 rows | **215** | 120 | 156 | 194 | 66 | 132 |
117
+ | get by primary key | **10,112** | 4,516 | 2,623 | 3,182 | 4,706 | 3,467 |
118
+ | select 1000 rows | **898** | 356 | 300 | 846\* | 390 | 336 |
119
+ | update by primary key | **10,516** | 4,234 | 2,085 | 2,779 | 5,345 | 4,061 |
120
+ | 100 users with their posts | **655** | 336 | 234 | 472 | 320 | 218 |
121
+
122
+ \* SQLAlchemy Core returns plain tuples; the ORMs build full row objects.
123
+
124
+ mizzle loads nested rows in one statement, so it pays one round trip where the others pay
125
+ two. Raw asyncpg, the driver under mizzle, is the upper limit: on single-row work mizzle
126
+ reaches 97% of it over the network and 67–84% on the same machine. Python 3.14, mizzle 0.2.3, Tortoise 1.1.9,
127
+ SQLAlchemy 2.1.6, Piccolo 1.39.0, Django 6.1.3 (its async ORM runs queries in one thread, on
128
+ psycopg). The script's docstring explains each library's setup and the latency proxy. Run
129
+ it yourself:
130
+
131
+ ```bash
132
+ docker compose up -d
133
+ uv run --with tortoise-orm --with "sqlalchemy[asyncio]" --with piccolo --with django \
134
+ --with "psycopg[binary,pool]" python benchmarks/bench.py
135
+ BENCH_CLIENTS=50 BENCH_POOL=8 uv run ... # the concurrent run
136
+ ```
137
+
94
138
  ## What else is in the box
95
139
 
96
140
  Joins, subqueries, CTEs, window functions, `UNION`, `CASE` and typed SQL functions;
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "mizzle"
3
- version = "0.2.2"
3
+ version = "0.2.4"
4
4
  description = "Fully typed async ORM for PostgreSQL: SQL-like queries, schema-driven migrations and a data studio"
5
5
  keywords = ["orm", "postgresql", "asyncpg", "async", "sql", "query-builder", "migrations", "typed"]
6
6
  classifiers = [
@@ -0,0 +1,5 @@
1
+
2
+ - Faster row building: each table's row decoder is generated once (fixed column positions
3
+ in a dict display) and cached. Selecting 1000 rows is about 1.75x faster, nested
4
+ relational queries about 1.25x.
5
+
@@ -121,7 +121,7 @@ from mizzle.core.sql.raw import sql
121
121
  from mizzle.db import Database, QueryEvent, Session
122
122
  from mizzle.dialects.base import Notification, Reconnected
123
123
 
124
- __version__ = "0.2.2"
124
+ __version__ = "0.2.4"
125
125
 
126
126
  __all__ = [
127
127
  "CheckViolation",
@@ -68,6 +68,14 @@ class CompiledQuery:
68
68
  # True when a Python .on_update() hook supplied a value: a prepared query must compile
69
69
  # again on each execution so the hook runs every time, not once at prepare().
70
70
  dynamic: bool = False
71
+ # True when it calls a function named with func(): mizzle cannot know it leaves the
72
+ # session alone (set_config, pg_advisory_lock...), so the connection is reset after it.
73
+ # Unlike raw, a read using one may still be retried after a lost connection.
74
+ opaque: bool = False
75
+
76
+ @property
77
+ def session_safe(self) -> bool:
78
+ return not (self.raw or self.opaque)
71
79
 
72
80
  @property
73
81
  def has_placeholders(self) -> bool:
@@ -87,7 +95,13 @@ class CompiledQuery:
87
95
  for value, p in zip(self.params, self.placeholders, strict=True)
88
96
  )
89
97
  return CompiledQuery(
90
- self.sql, params, (None,) * len(params), self.raw, self.locking, self.dynamic
98
+ self.sql,
99
+ params,
100
+ (None,) * len(params),
101
+ self.raw,
102
+ self.locking,
103
+ self.dynamic,
104
+ self.opaque,
91
105
  )
92
106
 
93
107
 
@@ -119,6 +133,7 @@ class Compiler:
119
133
  _raw: bool
120
134
  _locking: bool
121
135
  _dynamic: bool
136
+ _opaque: bool
122
137
  # (id(Param), SQL type it is compared with) -> its marker: a reused node reuses its
123
138
  # parameter, unless raw SQL holding it sits next to values of different types.
124
139
  _bound: dict[tuple[int, str | None], str]
@@ -144,6 +159,7 @@ class Compiler:
144
159
  self._raw = False
145
160
  self._locking = False
146
161
  self._dynamic = False
162
+ self._opaque = False
147
163
  self._bound = {}
148
164
  self._context = None
149
165
  self._scopes = []
@@ -152,6 +168,7 @@ class Compiler:
152
168
  def compile(self, node: Node) -> CompiledQuery:
153
169
  self._params, self._placeholders, self._raw, self._locking = [], [], False, False
154
170
  self._dynamic = False
171
+ self._opaque = False
155
172
  self._bound = {}
156
173
  self._context = None
157
174
  text = self.visit(node)
@@ -162,6 +179,7 @@ class Compiler:
162
179
  self._raw,
163
180
  self._locking,
164
181
  self._dynamic,
182
+ self._opaque,
165
183
  )
166
184
 
167
185
  def visit(self, node: Node) -> str:
@@ -314,6 +332,7 @@ class Compiler:
314
332
  return f"{'NOT ' if node.negate else ''}EXISTS ({self.visit(node.query)})"
315
333
 
316
334
  def visit_Func(self, node: Func) -> str:
335
+ self._opaque |= node.opaque
317
336
  args = ", ".join(self.visit(a) for a in node.args)
318
337
  return f"{node.name}({'DISTINCT ' if node.distinct else ''}{args})"
319
338
 
@@ -122,26 +122,39 @@ def row_decoder[T: Table](
122
122
 
123
123
  ``converter(column)`` gives the function applied to the column's non-NULL values, or
124
124
  None to keep them as they are."""
125
- names = tuple(name for name, _ in columns)
126
- converters = [
127
- (name, convert) for name, col in columns if (convert := converter(col)) is not None
128
- ]
129
- stop = start + len(names)
130
- whole = start == 0
131
- new = object.__new__
132
-
133
- def decode(values: Row) -> T:
134
- # zip() stops at the shorter side, so a full row needs no slice copy.
135
- data = dict(zip(names, values if whole else values[start:stop], strict=False))
136
- for name, convert in converters:
137
- value = data[name]
138
- if value is not None:
139
- data[name] = convert(value)
140
- obj = new(table)
141
- obj.__dict__ = data
142
- return obj
143
-
144
- return decode
125
+ key = (table, tuple(columns), start, converter)
126
+ if (cached := _ROW_DECODERS.get(key)) is not None:
127
+ return cast(Callable[[Row], T], cached)
128
+ # Generated once per table and column list: a dict display with fixed positions is ~2x
129
+ # faster per row than dict(zip(...)) plus a converter loop. Only repr()'d names,
130
+ # integers and our own converters go into the code.
131
+ env: dict[str, object] = {"new": object.__new__, "table": table}
132
+ lines: list[str] = []
133
+ items: list[str] = []
134
+ for i, (name, col) in enumerate(columns, start):
135
+ if (convert := converter(col)) is None:
136
+ items.append(f"{name!r}: v[{i}]")
137
+ else:
138
+ env[f"c{i}"] = convert
139
+ lines.append(f" x{i} = v[{i}]")
140
+ items.append(f"{name!r}: None if x{i} is None else c{i}(x{i})")
141
+ source = (
142
+ "def decode(v):\n"
143
+ + "".join(f"{line}\n" for line in lines)
144
+ + " obj = new(table)\n"
145
+ + f" obj.__dict__ = {{{', '.join(items)}}}\n"
146
+ + " return obj\n"
147
+ )
148
+ exec(source, env)
149
+ decode = _ROW_DECODERS[key] = cast(Callable[[Row], Table], env["decode"])
150
+ return cast(Callable[[Row], T], decode)
151
+
152
+
153
+ # ponytail: never evicted; one entry per table and selected column list (a few per query
154
+ # shape), fine unless queries are built from unbounded column lists.
155
+ _ROW_DECODERS: dict[
156
+ tuple[type[Table], tuple[tuple[str, ColumnBase], ...], int, object], Callable[[Row], Table]
157
+ ] = {}
145
158
 
146
159
 
147
160
  def table_columns(table: type[Table]) -> tuple[tuple[str, ColumnBase], ...]:
@@ -455,7 +455,7 @@ class func[T](TypedFunc[T]):
455
455
 
456
456
  def __init__(self, name: str, /, *args: object) -> None:
457
457
  nodes = tuple(a if isinstance(a, Node) else Param(a) for a in args)
458
- super().__init__(_identifier("function", name), nodes)
458
+ super().__init__(_identifier("function", name), nodes, opaque=True)
459
459
 
460
460
 
461
461
  # -- window functions -----------------------------------------------------------------
@@ -379,6 +379,9 @@ class Func(ExprBase):
379
379
  # The result has the type of its arguments (min, coalesce, lag...): a column among them
380
380
  # decodes it (e.g. back into an enum).
381
381
  keeps_type: bool = False
382
+ # Named by the user (func[T]("name")): mizzle cannot know it leaves the session alone
383
+ # (set_config, pg_advisory_lock...), so reset_connections="auto" resets after it.
384
+ opaque: bool = False
382
385
 
383
386
 
384
387
  @dataclass(frozen=True)
@@ -136,7 +136,7 @@ class Session(QueryBuilder):
136
136
  ) -> AsyncGenerator[list[Row]]:
137
137
  if batch_size < 1:
138
138
  raise QueryError("stream() needs batch_size >= 1")
139
- safe = not query.raw
139
+ safe = query.session_safe
140
140
  with self._logged(query):
141
141
  if self._conn is not None:
142
142
  # Locked per batch, not for the whole loop: its body may run other
@@ -253,7 +253,7 @@ class Session(QueryBuilder):
253
253
  format,
254
254
  header,
255
255
  self._timeout,
256
- session_safe=not compiled.raw,
256
+ session_safe=compiled.session_safe,
257
257
  )
258
258
  except DbConnectionError:
259
259
  await self._after_lost_connection(compiled, False)
@@ -611,7 +611,7 @@ class Session(QueryBuilder):
611
611
  self, conn: Connection, query: CompiledQuery, kind: _CallKind
612
612
  ) -> Awaitable[list[Row] | int | list[dict[str, object]]]:
613
613
  # A plain function returning the driver's coroutine: no extra await layer.
614
- safe = not query.raw
614
+ safe = query.session_safe
615
615
  if kind == "fetch":
616
616
  return conn.fetch(query.sql, query.params, self._timeout, session_safe=safe)
617
617
  if kind == "execute":
@@ -814,7 +814,7 @@ class Database(Session):
814
814
  max_idle: float = 300.0,
815
815
  query_timeout: float | None = None,
816
816
  statement_cache_size: int = 100,
817
- reset_connections: ResetMode = "always",
817
+ reset_connections: ResetMode = "auto",
818
818
  casing: Casing | None = None,
819
819
  logger: QueryLogger | None = None,
820
820
  log_params: bool = False,
@@ -825,11 +825,13 @@ class Database(Session):
825
825
  cancels statements that run longer (seconds; None: no limit).
826
826
  ``statement_cache_size=0`` is needed behind PgBouncer in transaction mode.
827
827
 
828
- ``reset_connections="auto"`` makes small queries up to ~40% faster: the session reset
829
- that normally follows every query is skipped when mizzle built the statement itself
830
- (raw SQL still resets). Leave it at ``"always"`` if triggers or functions your
831
- queries call change session settings (``set_config(..., false)``, ``SET``), because
832
- mizzle cannot see those."""
828
+ ``reset_connections="auto"`` (the default) resets a connection's session state when
829
+ it goes back to the pool only after a statement that may have changed it: raw
830
+ ``sql(...)``, a function named with ``func()``, ``listen()``, direct connection use.
831
+ Skipping the reset after mizzle-built statements saves a round trip per query (2x on
832
+ a real network). Use ``"always"`` if triggers or database functions change session
833
+ settings themselves (``SET`` or ``set_config(..., false)`` without LOCAL, session
834
+ advisory locks, temp tables), because mizzle cannot see inside them."""
833
835
  dialect = dialect_for_url(url)
834
836
  for replica in replicas:
835
837
  if dialect_for_url(replica).name != dialect.name:
@@ -20,9 +20,10 @@ from mizzle.core.sql.nodes import Node
20
20
  from mizzle.core.steps import Statement, Step
21
21
 
22
22
  type IsolationLevel = Literal["read committed", "repeatable read", "serializable"]
23
- # "always": reset session state (SET, LISTEN, advisory locks...) whenever a connection goes
24
- # back to the pool. "auto": skip it after statements mizzle built itself (faster; see
25
- # Database.connect for the one case it cannot see).
23
+ # "auto" (default): reset session state (SET, LISTEN, advisory locks...) when a connection
24
+ # goes back to the pool only after statements that may have changed it: raw SQL, functions
25
+ # named with func(), direct connection use. "always": after every use (see Database.connect
26
+ # for the one case "auto" cannot see).
26
27
  type ResetMode = Literal["always", "auto"]
27
28
  type CopyFormat = Literal["csv", "text", "binary"]
28
29
  # What a listener does when ``max_queue`` notifications wait unread: "raise" ends the
@@ -157,7 +158,7 @@ class Dialect(Protocol):
157
158
  pool_min_size: int = 1,
158
159
  max_idle: float = 300.0,
159
160
  query_timeout: float | None = None,
160
- reset_connections: ResetMode = "always",
161
+ reset_connections: ResetMode = "auto",
161
162
  ) -> Driver: ...
162
163
 
163
164
  # -- schema snapshots (normalized so a schema and its database compare equal) -----
@@ -36,7 +36,7 @@ class PostgresDialect:
36
36
  pool_min_size: int = 1,
37
37
  max_idle: float = 300.0,
38
38
  query_timeout: float | None = None,
39
- reset_connections: ResetMode = "always",
39
+ reset_connections: ResetMode = "auto",
40
40
  ) -> PostgresDriver:
41
41
  return await PostgresDriver.connect(
42
42
  url,
@@ -681,7 +681,7 @@ class PostgresDriver:
681
681
  pool_min_size: int = 1,
682
682
  max_idle: float = 300.0,
683
683
  query_timeout: float | None = None,
684
- reset_connections: ResetMode = "always",
684
+ reset_connections: ResetMode = "auto",
685
685
  ) -> PostgresDriver:
686
686
  """``statement_cache_size=0`` is needed behind PgBouncer in transaction mode.
687
687
  ``max_idle`` closes connections unused for that many seconds (0 keeps them)."""
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.2",
3
- "url": "https://github.com/mizzlehq/mizzle/releases/download/v0.2.2/mizzle-studio-0.2.2.zip",
2
+ "version": "0.2.4",
3
+ "url": "https://github.com/mizzlehq/mizzle/releases/download/v0.2.4/mizzle-studio-0.2.4.zip",
4
4
  "sha256": "924e1bfd991ee660ccbffe28039f0b83bb7ce752bcd404b877468826459071e2"
5
5
  }
@@ -20,6 +20,7 @@ from mizzle import (
20
20
  desc,
21
21
  eq,
22
22
  first_value,
23
+ func,
23
24
  lag,
24
25
  lower,
25
26
  not_similar_to,
@@ -264,3 +265,20 @@ async def test_cross_join(db: Database) -> None:
264
265
  assert len(rows) == 9
265
266
  # A nullable column against a non-null one: types check, and the SQL runs.
266
267
  assert await db.select(Users.id).from_(Users).where(eq(Users.bio, Users.email)) == []
268
+
269
+
270
+ async def test_auto_reset_after_a_user_named_function(db: Database, pg_url: str) -> None:
271
+ # func("set_config", ..., false) changes the session; mizzle cannot know that of a
272
+ # function you name, so reset_connections="auto" resets the connection after it.
273
+ await seed(db)
274
+ async with await Database.connect(
275
+ pg_url, schema=[Users, Posts], pool_size=1, reset_connections="auto"
276
+ ) as auto:
277
+ leak = func[str]("set_config", "application_name", "leaked", False)
278
+ assert await auto.select(leak).from_(Users).limit(1) == [("leaked",)]
279
+ [(name,)] = (
280
+ await auto.select(func[str]("current_setting", "application_name"))
281
+ .from_(Users)
282
+ .limit(1)
283
+ )
284
+ assert name != "leaked"
@@ -100,7 +100,7 @@ async def test_auto_reset_only_after_raw_sql(
100
100
  assert resets == ["reset"]
101
101
 
102
102
 
103
- async def test_default_always_resets(
103
+ async def test_always_resets_and_default_is_auto(
104
104
  pg_url: str, driver: PostgresDriver, monkeypatch: pytest.MonkeyPatch
105
105
  ) -> None:
106
106
  resets: list[str] = []
@@ -111,9 +111,14 @@ async def test_default_always_resets(
111
111
  return original(self)
112
112
 
113
113
  monkeypatch.setattr(asyncpg.Connection, "get_reset_query", counting)
114
- async with await Database.connect(pg_url, schema=[Users, Posts], pool_size=1) as db:
114
+ async with await Database.connect(
115
+ pg_url, schema=[Users, Posts], pool_size=1, reset_connections="always"
116
+ ) as db:
115
117
  await db.select().from_(Users)
116
118
  assert resets == ["reset"]
119
+ async with await Database.connect(pg_url, schema=[Users, Posts], pool_size=1) as db:
120
+ await db.select().from_(Users) # mizzle built it: no reset
121
+ assert resets == ["reset"]
117
122
 
118
123
 
119
124
  async def test_unreachable_replica_falls_back_to_primary(
@@ -17,6 +17,7 @@ from mizzle import (
17
17
  desc,
18
18
  eq,
19
19
  exists,
20
+ func,
20
21
  lag,
21
22
  lower,
22
23
  not_similar_to,
@@ -31,9 +32,11 @@ from mizzle import (
31
32
  ts_rank_cd,
32
33
  )
33
34
  from mizzle.core.errors import QueryError
35
+ from mizzle.core.query.base import row_decoder
34
36
  from mizzle.core.query.builder import QueryBuilder
35
37
  from mizzle.core.schema.table import Table
36
- from mizzle.core.sql.nodes import ExprBase
38
+ from mizzle.core.sql.nodes import ExprBase, Node
39
+ from mizzle.dialects.postgres.compiler import PostgresCompiler
37
40
  from mizzle.pg import date, integer, serial, text
38
41
  from tests.models import Posts, Role, Users
39
42
  from tests.unit.conftest import FakeRunner
@@ -251,3 +254,25 @@ def test_raw_sql_reused_in_casts(qb: QueryBuilder) -> None:
251
254
  'SELECT CAST($1 AS integer), CAST($2 AS text), CAST($1 AS integer) FROM "users"',
252
255
  (None, None),
253
256
  )
257
+
258
+
259
+ def test_user_named_functions_are_not_session_safe(qb: QueryBuilder) -> None:
260
+ # func() may change the session (reset after it) but stays a plain read (retried after a
261
+ # lost connection); built-in functions are safe; raw SQL is neither.
262
+ def flags(node: Node) -> tuple[bool, bool]:
263
+ compiled = PostgresCompiler().compile(node)
264
+ return compiled.session_safe, compiled.raw
265
+
266
+ assert flags(qb.select(lower(Users.email)).from_(Users)) == (True, False)
267
+ assert flags(qb.select(func[bool]("pg_is_in_recovery")).from_(Users)) == (False, False)
268
+ assert flags(qb.select(sql("1")).from_(Users)) == (False, True)
269
+
270
+
271
+ def test_generated_row_decoder() -> None:
272
+ # Columns from an offset, a converter (the enum) and NULLs; built once and reused.
273
+ columns = [(n, Users.__table__.columns[n]) for n in ("id", "bio", "role")]
274
+ decode = row_decoder(Users, columns, 2)
275
+ assert row_decoder(Users, columns, 2) is decode
276
+ user = decode(("x", "y", 7, None, "admin"))
277
+ assert (user.id, user.bio, user.role) == (7, None, Role.ADMIN)
278
+ assert not hasattr(user, "email") # not selected: absent, not None
@@ -755,7 +755,7 @@ wheels = [
755
755
 
756
756
  [[package]]
757
757
  name = "mizzle"
758
- version = "0.2.2"
758
+ version = "0.2.4"
759
759
  source = { editable = "." }
760
760
  dependencies = [
761
761
  { name = "asyncpg" },
@@ -1,10 +0,0 @@
1
-
2
- - `mizzle pull --update`: follow a schema another app owns (microservices on one
3
- database). It rewrites your schema module from the database and records what changed
4
- as a migration that runs nothing.
5
- - `mizzle pull`: a column named like a type or helper the class body calls (`text`, `date`,
6
- `time`, `integer`, `sql`, `one`...) or like an enum class is written as `text_ =
7
- text(name='text')`. Before, `text = text()` shadowed `text()` for the rest of the class,
8
- and the module failed to import.
9
- - `mizzle pull` imports `sql` only when the module uses it.
10
-
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes