lockcast 0.11.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.
Files changed (116) hide show
  1. lockcast-0.11.0/.gitignore +12 -0
  2. lockcast-0.11.0/CHANGELOG.md +74 -0
  3. lockcast-0.11.0/LICENSE +21 -0
  4. lockcast-0.11.0/LICENSE-COMMERCIAL.md +108 -0
  5. lockcast-0.11.0/PKG-INFO +374 -0
  6. lockcast-0.11.0/README.md +358 -0
  7. lockcast-0.11.0/SECURITY.md +44 -0
  8. lockcast-0.11.0/action.yml +139 -0
  9. lockcast-0.11.0/docs/BENCHMARK.md +108 -0
  10. lockcast-0.11.0/docs/DESIGN.md +133 -0
  11. lockcast-0.11.0/docs/FAQ.md +80 -0
  12. lockcast-0.11.0/docs/LANDSCAPE.md +51 -0
  13. lockcast-0.11.0/docs/LICENSING.md +55 -0
  14. lockcast-0.11.0/docs/archive/BENCHMARK_88_cases.md +197 -0
  15. lockcast-0.11.0/docs/examples/README.md +14 -0
  16. lockcast-0.11.0/docs/examples/backfill_in_transaction.html +92 -0
  17. lockcast-0.11.0/docs/examples/backfill_in_transaction.json +301 -0
  18. lockcast-0.11.0/docs/examples/backfill_in_transaction.md +55 -0
  19. lockcast-0.11.0/docs/examples/backfill_in_transaction.txt +45 -0
  20. lockcast-0.11.0/docs/examples/queue_behind_traffic.html +72 -0
  21. lockcast-0.11.0/docs/examples/queue_behind_traffic.md +36 -0
  22. lockcast-0.11.0/docs/examples/queue_behind_traffic.txt +25 -0
  23. lockcast-0.11.0/docs/examples/shop.watch.profile.json +4373 -0
  24. lockcast-0.11.0/docs/examples/unique_orders.html +80 -0
  25. lockcast-0.11.0/docs/examples/unique_orders.json +223 -0
  26. lockcast-0.11.0/docs/examples/unique_orders.md +44 -0
  27. lockcast-0.11.0/docs/examples/unique_orders.sarif +93 -0
  28. lockcast-0.11.0/docs/examples/unique_orders.txt +34 -0
  29. lockcast-0.11.0/docs/github-actions.yml +30 -0
  30. lockcast-0.11.0/docs/guides/django.md +26 -0
  31. lockcast-0.11.0/docs/guides/drizzle.md +37 -0
  32. lockcast-0.11.0/docs/guides/flyway.md +33 -0
  33. lockcast-0.11.0/docs/guides/prisma.md +50 -0
  34. lockcast-0.11.0/docs/guides/rails.md +30 -0
  35. lockcast-0.11.0/examples/github-action/lockcast-check.yml +45 -0
  36. lockcast-0.11.0/pyproject.toml +58 -0
  37. lockcast-0.11.0/src/lockcast/__init__.py +3 -0
  38. lockcast-0.11.0/src/lockcast/__main__.py +5 -0
  39. lockcast-0.11.0/src/lockcast/a4band.py +32 -0
  40. lockcast-0.11.0/src/lockcast/amsub.py +47 -0
  41. lockcast-0.11.0/src/lockcast/calib.py +165 -0
  42. lockcast-0.11.0/src/lockcast/calib_v10.json +213 -0
  43. lockcast-0.11.0/src/lockcast/calib_v5.json +63 -0
  44. lockcast-0.11.0/src/lockcast/calib_v6.json +64 -0
  45. lockcast-0.11.0/src/lockcast/calib_v7.json +69 -0
  46. lockcast-0.11.0/src/lockcast/calib_v8.json +88 -0
  47. lockcast-0.11.0/src/lockcast/calib_v9.json +196 -0
  48. lockcast-0.11.0/src/lockcast/cli.py +625 -0
  49. lockcast-0.11.0/src/lockcast/clock.py +37 -0
  50. lockcast-0.11.0/src/lockcast/dupprobe.py +191 -0
  51. lockcast-0.11.0/src/lockcast/ed25519.py +89 -0
  52. lockcast-0.11.0/src/lockcast/engine.py +1123 -0
  53. lockcast-0.11.0/src/lockcast/enumundo.py +46 -0
  54. lockcast-0.11.0/src/lockcast/extract.py +161 -0
  55. lockcast-0.11.0/src/lockcast/fillfactor.py +113 -0
  56. lockcast-0.11.0/src/lockcast/hostbench.py +129 -0
  57. lockcast-0.11.0/src/lockcast/idxsplit.py +167 -0
  58. lockcast-0.11.0/src/lockcast/intent.py +182 -0
  59. lockcast-0.11.0/src/lockcast/lag.py +32 -0
  60. lockcast-0.11.0/src/lockcast/license_pubkey.txt +1 -0
  61. lockcast-0.11.0/src/lockcast/licensing.py +166 -0
  62. lockcast-0.11.0/src/lockcast/locks.py +110 -0
  63. lockcast-0.11.0/src/lockcast/model.py +844 -0
  64. lockcast-0.11.0/src/lockcast/notwin.py +220 -0
  65. lockcast-0.11.0/src/lockcast/pace.py +194 -0
  66. lockcast-0.11.0/src/lockcast/pgconn.py +84 -0
  67. lockcast-0.11.0/src/lockcast/policy.py +82 -0
  68. lockcast-0.11.0/src/lockcast/probe.py +261 -0
  69. lockcast-0.11.0/src/lockcast/profile.py +673 -0
  70. lockcast-0.11.0/src/lockcast/queueint.py +30 -0
  71. lockcast-0.11.0/src/lockcast/report/__init__.py +15 -0
  72. lockcast-0.11.0/src/lockcast/report/common.py +61 -0
  73. lockcast-0.11.0/src/lockcast/report/html.py +142 -0
  74. lockcast-0.11.0/src/lockcast/report/jsonout.py +7 -0
  75. lockcast-0.11.0/src/lockcast/report/markdown.py +90 -0
  76. lockcast-0.11.0/src/lockcast/report/sarif.py +47 -0
  77. lockcast-0.11.0/src/lockcast/report/text.py +76 -0
  78. lockcast-0.11.0/src/lockcast/rewrite.py +938 -0
  79. lockcast-0.11.0/src/lockcast/runner.py +462 -0
  80. lockcast-0.11.0/src/lockcast/sqlsplit.py +227 -0
  81. lockcast-0.11.0/src/lockcast/synth.py +650 -0
  82. lockcast-0.11.0/src/lockcast/tune.py +868 -0
  83. lockcast-0.11.0/src/lockcast/twin.py +890 -0
  84. lockcast-0.11.0/src/lockcast/verify.py +129 -0
  85. lockcast-0.11.0/tests/conftest.py +55 -0
  86. lockcast-0.11.0/tests/data/action_sim.profile.json +1515 -0
  87. lockcast-0.11.0/tests/data/realworld_chatwoot.profile.json.gz +0 -0
  88. lockcast-0.11.0/tests/data/realworld_gitlab.profile.json.gz +0 -0
  89. lockcast-0.11.0/tests/data/realworld_mastodon.profile.json.gz +0 -0
  90. lockcast-0.11.0/tests/test_b2b_full_sql.py +26 -0
  91. lockcast-0.11.0/tests/test_batch_v103.py +67 -0
  92. lockcast-0.11.0/tests/test_bughunt.py +295 -0
  93. lockcast-0.11.0/tests/test_cli_config.py +127 -0
  94. lockcast-0.11.0/tests/test_cli_ux.py +138 -0
  95. lockcast-0.11.0/tests/test_connection_loss.py +47 -0
  96. lockcast-0.11.0/tests/test_fillfactor.py +31 -0
  97. lockcast-0.11.0/tests/test_held_v101.py +86 -0
  98. lockcast-0.11.0/tests/test_integration.py +375 -0
  99. lockcast-0.11.0/tests/test_licensing.py +181 -0
  100. lockcast-0.11.0/tests/test_pace_v102.py +75 -0
  101. lockcast-0.11.0/tests/test_pause_v103.py +46 -0
  102. lockcast-0.11.0/tests/test_privileges_and_encoding.py +138 -0
  103. lockcast-0.11.0/tests/test_profile_encoding.py +29 -0
  104. lockcast-0.11.0/tests/test_rewrite.py +227 -0
  105. lockcast-0.11.0/tests/test_unique_order.py +66 -0
  106. lockcast-0.11.0/tests/test_units.py +248 -0
  107. lockcast-0.11.0/tests/test_v10.py +69 -0
  108. lockcast-0.11.0/tests/test_v105.py +99 -0
  109. lockcast-0.11.0/tests/test_v106.py +294 -0
  110. lockcast-0.11.0/tests/test_v3.py +170 -0
  111. lockcast-0.11.0/tests/test_v4_model.py +111 -0
  112. lockcast-0.11.0/tests/test_v5.py +134 -0
  113. lockcast-0.11.0/tests/test_v6.py +86 -0
  114. lockcast-0.11.0/tests/test_v7.py +69 -0
  115. lockcast-0.11.0/tests/test_v9.py +69 -0
  116. lockcast-0.11.0/tools/simulate_action.py +210 -0
@@ -0,0 +1,12 @@
1
+ __pycache__/
2
+ *.pyc
3
+ .pytest_cache/
4
+ .mypy_cache/
5
+ .ruff_cache/
6
+ *.egg-info/
7
+ dist/
8
+ build/
9
+ .venv/
10
+ .env
11
+ *.pem
12
+ *.key
@@ -0,0 +1,74 @@
1
+ # Changelog
2
+
3
+ All notable changes. Dates are from the development history; no version has been published yet.
4
+
5
+ ## 0.11.0 (2026-12-04) - public release candidate
6
+
7
+ - Release version. The forecast model is the Part D model from 0.10.6 (unique-order stays experimental and off).
8
+ - Verdict equivalence, frozen 0.10.6 vs this tree: Part A 54/54, Part D 87/87 (fast 65 + heavy 22), ORM 36, Part C 4 of 70.
9
+ - License signing public key embedded in `src/lockcast/license_pubkey.txt` (was `UNSET`). The private key is not in the repository.
10
+ - Seller name Yackov Mozgori. Pricing: Team $149/month, Founders $99/month, Audit $299. Purchase link is a placeholder ("coming soon — open an issue to join the founders list"). Contact is GitHub Issues and Discussions.
11
+ - Postgres 18.7: integration suite 37 passed; A/D sample 9/9 same verdicts as PG 17.
12
+
13
+ ## 0.10.6 (2026-12-03) - first candidate for a public release
14
+
15
+ - **Fix:** twin integrity. `enc()` in the synthesizer cast long unique strings to `numeric(40,0)` and overflowed, so some
16
+ twins came out empty and the forecast was too low. A new twin-integrity gate returns `unverified` instead of a verdict
17
+ when a touched table or a foreign-key parent holds less than 98% of the intended rows.
18
+ - Rehearsal budget scaled by the size of the touched tables (F2); no model override above 10x growth (F3); a strict
19
+ secondary verdict (F4).
20
+ - **Result:** preregistered Part D, 87 migrations from 12 open-source projects. See [docs/BENCHMARK.md](docs/BENCHMARK.md).
21
+ - README rewritten around Part D; the earlier 88-case benchmark moved to `docs/archive/`; the 98% figure from Part A is
22
+ withdrawn (inflated by the empty-twin bug).
23
+ - `pyproject.toml` version aligned with the code (was 0.10.5).
24
+ - **Fixes found while preparing the release**:
25
+ the package did not import on Python 3.10/3.11 and every twin check crashed on Python 3.12 and older; no-twin mode
26
+ under-estimated index builds, called syntax errors SAFE, and missed tables written in capitals; a usage error exited 2
27
+ (= "migration fails"), now 3; SIGTERM left scratch databases on the twin server, now cleaned up; concurrent runs on
28
+ one twin server broke each other; partitioned tables were loaded twice into the twin; files after a failing file were
29
+ reported SAFE; UTF-8 BOM, NUL bytes and unterminated strings are handled; the GitHub Action handles multi-line `files`, a failed base fetch,
30
+ renamed files and a failing comment step.
31
+ - **The forecast model is the one that passed Part D.** A change that made the twin's unique text keys follow production's
32
+ order (it raised unique-index forecasts by about 10% to 25%) was tried during release preparation and **removed from
33
+ the default path**: it was never validated against Part D and it flagged safe cases that the validated model did not.
34
+ It stays behind `--experimental-unique-order` (or `LOCKCAST_EXPERIMENTAL_UNIQUE_ORDER=1`), off by default, unvalidated.
35
+ Verdict equivalence with the frozen Part D build was re-checked: see docs/BENCHMARK.md.
36
+ - New: `lockcast cleanup` and automatic reaping of scratch databases left behind by a `kill -9`, a crash or a lost CI
37
+ runner. Every twin and working copy carries an owner mark (host, pid, process start time); `check` drops, on its own,
38
+ only those whose owner is dead on this host (or older than 24 h from another host). Kept twins (`--keep-twin`) and
39
+ databases without a mark are never removed unless you pass `--kept` / `--unmarked` to `cleanup --yes`.
40
+ - Ctrl-C and SIGTERM during `lockcast profile` (pg_dump) leave no partial profile file; Ctrl-C exits 130.
41
+ - The GitHub Action was run for real with `act` and Docker (5 scenarios). Found and fixed: a
42
+ misleading warning when the runner has no `gh`.
43
+ - A non-superuser `CREATEDB` role failed the whole check on Postgres 17+: `CHECKPOINT` now needs `pg_checkpoint`,
44
+ and the error was mis-explained as "needs CREATEDB". The checkpoint is skipped when the role cannot run it.
45
+ - `lockcast profile` on a `SQL_ASCII` database crashed (`TypeError: cannot use a string pattern on a bytes-like object`
46
+ and table keys like `b'public'.b'orders'`). Catalog bytes are now decoded.
47
+ - CLI: `--repeats 0`, `--twin-fraction 0` and `--twin-max-size 0` were treated as "not set" and silently became the
48
+ defaults (3, 2%, 2 GB). They now error (exit 3). A config file whose `[tables]` is not a table also errors. Help and
49
+ the README now say the default twin cap is 2% (it already was, since v10); the example `.lockcast.toml` no longer
50
+ pastes `speed_factor = 1.0` (that would disable the measured speed ratio).
51
+
52
+ - A dropped connection to the twin mid-check (network cut, server restart) was reported as verdict FAILS (exit 2,
53
+ "the migration will not complete"). It is now a tool error (exit 3) with a plain message. Scratch databases may
54
+ remain if `--twin-dsn` itself is unreachable; `lockcast cleanup` on a live DSN drops them.
55
+ - `explain_error` for a full disk on the twin is a plain hint (free space or lower `--twin-max-size`); filling the
56
+ filesystem can also stop Postgres itself.
57
+ - `CREATE DATABASE ... TEMPLATE` failed through pgbouncer ("template database is still in use") because the pool kept
58
+ a server session after the client closed. Before cloning, lockcast now terminates backends on the template (scratch
59
+ `lockcast_*` databases only). Session and transaction pooling then both completed a check with exit 0.
60
+
61
+ - New: offline license key check for Team features (`lockcast license`), warning when the live probe and the profile
62
+ benchmark disagree about production speed, warning when the twin server looks like production.
63
+
64
+ ## 0.10.0 to 0.10.5 (2026-11-28 to 2026-12-02)
65
+
66
+ 0.10.0: duplicate-key probe before unique builds, a speed factor measured at check time with a read-only CPU probe
67
+ (`--probe-dsn`), exact `pg_sleep` timing, twin budget 0.02, a DML factor, a full-size re-measure near the budget line.
68
+ 0.10.1 to 0.10.2: drop disturbed twin repeats; a pace probe around every timed twin run. 0.10.4: stall of a paused
69
+ loop is per-batch work. 0.10.5: raw-seconds clock, pricing and identity model fixes.
70
+
71
+ ## Earlier
72
+
73
+ 0.1.0 to 0.9.0 (2026-11-26 to 2026-11-27): the first twin-based forecaster and, per release, calibration and model
74
+ changes, each frozen and evaluated before use. History is in the git log (125 commits at the time of writing).
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yackov Mozgori
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,108 @@
1
+ # lockcast Team License (DRAFT)
2
+
3
+ > **DRAFT. NOT LEGAL ADVICE. NOT YET IN FORCE.** This text was drafted by the project's assistant to give a lawyer
4
+ > something to mark up. It has not been reviewed by a lawyer. Before any copy is sold or offered, a lawyer licensed
5
+ > where the seller is established, and one who knows software licensing and the tax treatment of the sale, must review and
6
+ > rewrite it as needed. Items for the lawyer are listed at the end.
7
+
8
+ Version 0.1 (draft), 2026-12-03.
9
+
10
+ ## 1. Parties and definitions
11
+
12
+ - **"Licensor"**: Yackov Mozgori (sole proprietor / עוסק). Postal address: to be filled in by the Licensor before this draft is signed.
13
+ - **"Customer"**: the organization named in the License Key (the "Licensed Organization").
14
+ - **"Software"**: the lockcast program, in source and binary form.
15
+ - **"Core"**: every part of the Software that is licensed under the MIT License in the file `LICENSE`. The Core is not
16
+ covered by this agreement and stays MIT.
17
+ - **"Team Features"**: the parts listed in Schedule A.
18
+ - **"License Key"**: the signed string issued by the Licensor to the Customer, naming the Licensed Organization, the
19
+ features and an expiry date.
20
+ - **"Affiliate"**: an entity that controls, is controlled by, or is under common control with the Customer.
21
+
22
+ ## 2. Grant
23
+
24
+ Subject to this agreement and payment of the fees, the Licensor grants the Customer a non-exclusive, non-transferable,
25
+ worldwide license, during the term shown on the License Key, to use, run and modify the Team Features for the internal
26
+ business purposes of the Licensed Organization and its Affiliates, with no limit on the number of developers or
27
+ machines.
28
+
29
+ ## 3. Restrictions
30
+
31
+ The Customer may not, and may not let others: (a) sell, sublicense, rent or distribute the Team Features, or a product
32
+ that is substantially them; (b) share the License Key outside the Licensed Organization and its Affiliates, or publish it;
33
+ (c) remove or bypass the license check, or alter the Team Features to run without a valid License Key, except where the
34
+ law does not allow this restriction; (d) use the Team Features to provide a competing hosted service to third parties.
35
+
36
+ Nothing here restricts what the MIT License gives the Customer for the Core.
37
+
38
+ ## 4. Fees, term, renewal
39
+
40
+ The fee, the billing period and the term are those of the order (monthly or annual). The term renews as the order says.
41
+ If it is not renewed, the License Key expires. After a **14-day grace period** the Team Features stop working; the Core
42
+ keeps working. No data is deleted by the Software, because the Software stores none and sends none.
43
+
44
+ ## 5. Where the money goes
45
+
46
+ Payments are taken by a merchant of record, *[name to be filled in]*, which is the seller of record for tax purposes
47
+ and issues the invoice. *(For the lawyer: confirm this arrangement and the Licensor's own tax and invoicing duties.)*
48
+
49
+ ## 6. Privacy
50
+
51
+ The Software contains no telemetry and does not connect to the Licensor. The License Key is verified offline. The
52
+ Licensor holds only what the order and the merchant of record record: the Customer's name, billing contact and the key
53
+ issued.
54
+
55
+ ## 7. Warranty, liability
56
+
57
+ The Software forecasts. A forecast is not a guarantee. The Software is provided **as is**; to the fullest extent the law
58
+ allows, the Licensor gives no warranty (including of fitness for a purpose or of the accuracy of a forecast) and is not
59
+ liable for indirect or consequential loss, including downtime, lost revenue or data loss caused by running a migration.
60
+ The Licensor's total liability is limited to the fees paid in the twelve months before the claim. *(For the lawyer: this
61
+ clause must be checked against the law that applies to the Customer and to the Licensor; liability limits are not valid
62
+ everywhere, for example for gross negligence or consumer sales.)*
63
+
64
+ ## 8. Support
65
+
66
+ Support is through GitHub Issues and GitHub Discussions on https://github.com/kobima6813/lockcast (no personal email). Business days, no service level, unless the order says so.
67
+
68
+ ## 9. Termination
69
+
70
+ Either party may end this agreement for a material breach that is not fixed within 30 days of notice. On termination the
71
+ License Key stops being valid and Section 3 and 7 continue.
72
+
73
+ ## 10. Law and courts
74
+
75
+ *[Governing law and court to be chosen by the Licensor with the lawyer.]*
76
+
77
+ ---
78
+
79
+ ## Schedule A: Team Features
80
+
81
+ 1. `lockcast fix`: a verified safe rewrite written to a file.
82
+ 2. `lockcast plan`: a check of an ordered multi-step deploy plan.
83
+ 3. `lockcast profile --watch`: sampling lock holders for the lock-queue forecast.
84
+ 4. The SARIF report and the HTML report.
85
+ 5. Use of the GitHub Action on **private** repositories.
86
+
87
+ Everything else, including `profile`, `check`, the verdict and forecast, text, JSON and Markdown reports, and the Action on public repositories,
88
+ is Core and MIT.
89
+
90
+ **Open point for the lawyer and the owner:** today the code of items 1 to 3 lives in files that are MIT-licensed (the
91
+ rewrite, profile and engine modules). A commercial license over code that is also MIT only protects what is in files that are *not*
92
+ MIT. Before the first public release, decide either (a) to move Team code into its own directory under this license
93
+ (`src/lockcast/team/`), or (b) to keep everything MIT and treat the License Key and this text as the terms of a paid
94
+ tier (support, the right to use the key, the Action on private repositories), without a code-level restriction.
95
+ Section 3(c) assumes (a).
96
+
97
+ ## Items for the lawyer
98
+
99
+ 1. Who is the Licensor (a sole proprietor or a company), and how the tax treatment of selling software internationally works.
100
+ 2. Whether a merchant of record (Polar, Paddle or similar) makes the buyer's invoice and VAT their duty, and what remains
101
+ with the Licensor, including Israeli income tax reporting.
102
+ 3. Whether Section 3(c) (no bypassing the check) is enforceable and wise for software whose source is public.
103
+ 4. Dual-licensing mechanics: a repository with MIT files and commercial files in one tree. Contributions: a Contributor
104
+ License Agreement or a Developer Certificate of Origin, so the Licensor can license contributions commercially.
105
+ 5. The liability limit and the "as is" clause, for business customers in the EU, the US and Israel.
106
+ 6. Whether a forecast that is wrong needs a stronger disclaimer in the product itself.
107
+ 7. Trademark: the name "lockcast".
108
+ 8. Privacy: whether the merchant-of-record and license records need a privacy notice.
@@ -0,0 +1,374 @@
1
+ Metadata-Version: 2.5
2
+ Name: lockcast
3
+ Version: 0.11.0
4
+ Summary: Forecast how long a Postgres migration will make production wait, before it runs.
5
+ Author: Yackov Mozgori
6
+ License: MIT
7
+ License-File: LICENSE
8
+ Requires-Python: >=3.10
9
+ Requires-Dist: pglast>=6.0
10
+ Requires-Dist: psycopg[binary]>=3.1
11
+ Requires-Dist: tomli>=2.0; python_version < '3.11'
12
+ Provides-Extra: test
13
+ Requires-Dist: cryptography>=41; extra == 'test'
14
+ Requires-Dist: pytest>=7; extra == 'test'
15
+ Description-Content-Type: text/markdown
16
+
17
+ # lockcast
18
+
19
+ Forecast how long a Postgres migration will make production wait, before it runs.
20
+
21
+ A linter reads the SQL and tells you a statement is "dangerous". lockcast runs the migration on
22
+ a small twin of your database and tells you what it measured: which tables stall, whether reads
23
+ or only writes wait, for how many seconds at production's size, and from which statement to
24
+ which. Then it writes the safe version of the migration and proves on the twin that it ends in
25
+ the same schema and data with the stall gone.
26
+
27
+ A run of an early build (0.1.0) on a 9.2 GB test database (`bench/cases/addcol_then_backfill_users_tx.sql`):
28
+
29
+ ```
30
+ $ lockcast check addcol_then_backfill_users_tx.sql --profile shop.profile.json --twin-dsn "$TWIN"
31
+ lockcast 0.1.0 twin 31.2 MB = 0.3% of production (9.2 GB), measured at 3 sizes, 24 s
32
+
33
+ bench/cases/addcol_then_backfill_users_tx.sql DANGEROUS
34
+ reads and writes on public.users wait about 24.1 s (range 18.1 s to 32.2 s)
35
+
36
+ # line statement on twin production locks
37
+ 1 1 ALTER TABLE users ADD COLUMN verified boolean 0 ms 0 ms ACCESS EXCLUSIVE on users
38
+ 2 2 UPDATE users SET verified = false 1.2 s 24.1 s (18.1 s-32.2 s) ROW EXCLUSIVE on users
39
+
40
+ Stalls in production
41
+ public.users reads and writes 24.1 s (18.1 s to 32.2 s) statements 1-2, ACCESS EXCLUSIVE
42
+ public.users writes to locked rows 21.7 s (16.3 s to 28.9 s) statement 2, 100% of rows locked
43
+
44
+ x line 1: public.users: reads and writes wait about 24.1 s (range 18.1 s to 32.2 s) under ACCESS EXCLUSIVE, held from statement 1 to 2. Budget 1.0 s.
45
+ x line 2: public.users: 100% of rows (2,000,000) stay row-locked for about 21.7 s (range 16.3 s to 28.9 s); UPDATE and DELETE on those rows wait. Budget 1.0 s.
46
+ ! line 1: no lock_timeout is set when ACCESS EXCLUSIVE is requested on public.users: if a long transaction holds the table, the migration waits and every query that conflicts queues behind it. Set lock_timeout and retry.
47
+ ! line 1: ACCESS EXCLUSIVE on public.users is taken by statement 1 and kept until the transaction ends at statement 2: 100% of the stall is later statements. Run them in separate transactions.
48
+ ! about 1.4 GB of WAL will be written: expect replication lag and extra checkpoints.
49
+
50
+ Safe rewrite, proven on the twin: longest stall 24.1 s -> 1 ms; run time 24.1 s -> 26.5 s; same schema and data
51
+ -- lockcast: no-transaction
52
+ -- Generated by lockcast. Each statement commits on its own; run it with your migration
53
+ -- tool's "no transaction" option. If a step fails, earlier steps stay applied.
54
+ SET lock_timeout = '500ms';
55
+
56
+ ALTER TABLE users ADD COLUMN verified boolean;
57
+
58
+ -- batches of 10,000 keys, each committed: row locks last milliseconds, and no lock is held across the whole backfill
59
+ DO $lockcast$
60
+ DECLARE
61
+ _lc_lo bigint;
62
+ _lc_hi bigint;
63
+ _lc_step bigint := 10000;
64
+ BEGIN
65
+ SELECT min(id), max(id) INTO _lc_lo, _lc_hi FROM users;
66
+ WHILE _lc_lo <= _lc_hi LOOP
67
+ UPDATE users SET verified = FALSE WHERE users.id >= _lc_lo AND users.id < _lc_lo + _lc_step;
68
+ COMMIT;
69
+ _lc_lo := _lc_lo + _lc_step;
70
+ END LOOP;
71
+ END
72
+ $lockcast$;
73
+ WAL written: about 1.4 GB
74
+
75
+ Result: DANGEROUS
76
+ ```
77
+
78
+ Measured on the full database, reads and writes on `users` waited 17.0 s. This run's forecast is 42%
79
+ too high and its range misses the measurement. Seven checks of this file gave 19.0 s to 24.1 s: the
80
+ twin's own time for the `UPDATE` varied between 1.0 s and 1.2 s from run to run, and the
81
+ extrapolation to production's 2 million rows multiplies it by 20. The verdict, and the rewrite where
82
+ one was generated, were the same every time. On the same file squawk 2.74 reported only the missing timeouts, MigrationPilot
83
+ 1.6.4 predicted "< 1s" for the `ALTER` and warned about an unbatched backfill, and pgfence 0.8.1
84
+ flagged the `UPDATE` as a policy issue. More examples, in every report format:
85
+ [`docs/examples/`](docs/examples/).
86
+
87
+ ## What you get
88
+
89
+ | | |
90
+ |---|---|
91
+ | A verdict per migration file | `SAFE`, `DANGEROUS` (with the forecast stall and its range), or `FAILS` (with the error Postgres will give) |
92
+ | What waits, and for how long | per table: reads and writes, writes only, or writes to row-locked rows; the lock; the statements it spans |
93
+ | A rewrite that is proven, not suggested | generated from the syntax tree, run on the twin, accepted only if schema and data match the original and the stall is inside the budget |
94
+ | Things a linter cannot see | a lock held to the end of a transaction while a backfill runs, DDL inside `DO` blocks and functions, row locks from `UPDATE`/`DELETE`, the difference between a 2 thousand row table and a 20 million row one |
95
+ | Reports | terminal, Markdown for a pull request comment and JSON (free); SARIF for code scanning and a self-contained HTML page ([Team](docs/LICENSING.md)) |
96
+
97
+ ## How it works
98
+
99
+ 1. **Profile** (`lockcast profile`, run where production is reachable). Catalog and statistics queries
100
+ only, in a read-only session: table sizes, `pg_stats` per column, the settings that decide how
101
+ DDL performs, the schema from `pg_dump --schema-only`. No row is read. Value samples that
102
+ Postgres already keeps in `pg_stats` are dropped for text and sensitive-looking columns. The
103
+ result is one JSON file you commit or store as an artifact.
104
+ 2. **Twin** (in CI, on a scratch Postgres). Every table gets its schema; only the tables the
105
+ migration touches get rows, generated from the statistics: row count, widths, null fractions,
106
+ distinct counts, physical ordering, foreign keys valid by construction. Bytes per row are
107
+ calibrated against production. The twin is capped at 2% of production's size (or 2 GB) and is
108
+ built at three sizes: F, F/2, F/4.
109
+ 3. **Measure**. The migration runs for real on a copy of each twin. After every statement lockcast
110
+ reads `pg_locks` for the locks it holds, the per-table tuple counters for what it read and
111
+ wrote, and `pg_class` for what it rewrote. A second connection samples the locks while it runs.
112
+ 4. **Forecast**. For each statement two straight lines are fitted across the three sizes: how many
113
+ tuples it touches as a function of table size, and seconds as a function of tuples touched.
114
+ Production's time is read off at production's sizes. A statement that touches no tuples is a
115
+ catalog change and stays constant; a predicate that selects a fixed number of rows is not
116
+ scaled up. A lock is held from the statement that takes it to the end of its transaction, so
117
+ the stall is the sum of the forecasts in between.
118
+ 5. **Rewrite and prove**. Dangerous statements are rewritten (list below), the rewrite is measured
119
+ the same way, and its end state is compared with the original's: schema catalog by catalog,
120
+ data by per-column digests.
121
+
122
+ ## Quick start
123
+
124
+ ```sh
125
+ pip install . # from this repository; lockcast is not on PyPI yet
126
+
127
+ # once, or nightly: where production (or a replica) is reachable
128
+ lockcast profile --dsn "$PROD_DSN" -o lockcast.profile.json
129
+
130
+ # in CI: a scratch Postgres of the same major version, never production
131
+ lockcast check migrations/0042_*.sql \
132
+ --profile lockcast.profile.json \
133
+ --twin-dsn postgresql://postgres:twin@localhost:5432/postgres \
134
+ --budget 1s
135
+ ```
136
+
137
+ Exit code 0 is safe, 1 is dangerous, 2 means the migration would fail, 3 is a tool error.
138
+
139
+ `lockcast modes` lists the lock modes. The check report already shows the proven rewrite; `lockcast fix FILE` writes it to a
140
+ file (Team, see [Licensing](#licensing)). `lockcast check ... --no-twin` gives a rough size-only estimate without a scratch
141
+ database: it assumes 160 MB/s, errs towards flagging, and is for a first look, not for a decision.
142
+
143
+ **Using an ORM or migration tool?** Guides: [Prisma](docs/guides/prisma.md), [Drizzle](docs/guides/drizzle.md),
144
+ [Flyway, dbmate, goose and other SQL-file tools](docs/guides/flyway.md), [Rails](docs/guides/rails.md),
145
+ [Django](docs/guides/django.md). Questions: [FAQ](docs/FAQ.md). Security and what touches your database: [SECURITY.md](SECURITY.md).
146
+
147
+ ### GitHub Actions
148
+
149
+ ```yaml
150
+ jobs:
151
+ lockcast:
152
+ runs-on: ubuntu-latest
153
+ services:
154
+ postgres:
155
+ image: postgres:16 # production's major version
156
+ env: { POSTGRES_PASSWORD: twin }
157
+ ports: ["5432:5432"]
158
+ steps:
159
+ - uses: actions/checkout@v4
160
+ with: { fetch-depth: 0 }
161
+ - uses: kobima6813/lockcast@v0.11.0 # the repository this code is pushed to
162
+ with:
163
+ twin-dsn: postgresql://postgres:twin@localhost:5432/postgres
164
+ paths: migrations
165
+ budget: 1s
166
+ ```
167
+
168
+ The action (`action.yml` in this repository) checks the SQL files the pull request adds or changes, posts one comment and keeps it
169
+ updated, and writes a JSON report (and SARIF with a Team license). On a **private** repository the Action needs a Team
170
+ license key (`license-key:`); without one it prints a warning, checks nothing and exits 0. Full example: [docs/github-actions.yml](docs/github-actions.yml).
171
+
172
+ ### How the file is run matters
173
+
174
+ Rails, Django, Flyway, Alembic, dbmate, goose, Atlas and Prisma wrap a migration file in one
175
+ transaction, so a lock taken by the first statement is held until the last one finishes. lockcast
176
+ assumes that (`--tx file`) unless the file has its own `BEGIN`/`COMMIT`, carries a "no transaction"
177
+ marker (goose, dbmate, sql-migrate, `-- lockcast: no-transaction`), or contains a statement that
178
+ cannot run in a transaction. `--tx statement` forces one transaction per statement.
179
+
180
+ ## Measured accuracy
181
+
182
+ The result below comes from a **preregistered test** ("Part D"). The cases, the scale, the definition of
183
+ "dangerous", the tool settings and the pass bars were written down and committed (`c518c77`) before any
184
+ stall was measured. The preregistration, the case manifest and the raw scores are internal research
185
+ files and are not in this repository. The numbers themselves are in [docs/BENCHMARK.md](docs/BENCHMARK.md).
186
+
187
+ **What was run.** 87 migrations from 12 open-source Rails applications (Forem, RubyGems, OpenStreetMap,
188
+ Loomio, Maybe, Consul, Open Food Network, Discourse, publishing-api, Feedbin, Mastodon, Chatwoot),
189
+ applied to PostgreSQL copies of their schemas filled with synthetic rows at **4 times** the base size
190
+ (tables of tens of thousands to several million rows), while a reader and a writer hit every touched
191
+ table. A case is "dangerous" when the measured stall was **1 second or more**: 45 were dangerous and 42
192
+ safe, and none was voided. 63 are real migrations taken from the projects' history; 24 are synthetic
193
+ patterns we built (9 dangerous, 15 safe).
194
+
195
+ lockcast saw only a statistical profile of each database and a scratch twin. Run 2026-12-03,
196
+ lockcast 0.10.6.
197
+
198
+ | Tool | Dangerous caught (of 45) | False alarms (of 42) | Balanced accuracy |
199
+ |---|---:|---:|---:|
200
+ | **lockcast 0.10.6** | **45** (95% CI for recall 92% to 100%) | **2 (4.8%**, CI 1.3% to 15.8%) | **0.976** |
201
+ | Antifailure (any lock, or write-blocking, held 1 s or more) | 45 | 14 (33%) | 0.833 |
202
+ | "Big table is dangerous" rule (3 million rows or more, blocking statement) | 43 | 11 (26%) | 0.847 |
203
+ | MigrationPilot 1.6.4 | 45 | 40 (95%) | 0.524 |
204
+ | squawk 2.74 | 45 | 42 (100%) | 0.500 |
205
+ | pgfence 0.8.1 | 45 | 42 (100%) | 0.500 |
206
+
207
+ Paired differences in balanced accuracy, with 95% intervals: lockcast over Antifailure +0.143
208
+ (0.07 to 0.22); over the size rule +0.129 (0.056 to 0.208). Both intervals exclude zero; the preregistered
209
+ claim was that lockcast would beat every competitor, and it did.
210
+
211
+ On the 63 real migrations alone lockcast made no error (36 of 36 dangerous caught, 0 of 27 safe flagged).
212
+ Its two false alarms are both synthetic patterns (`d2_ma_00_fk`, `d2_ma_07_index`).
213
+
214
+ Read the competitor rows with care. squawk, pgfence and MigrationPilot are linters: they read the SQL
215
+ and say a statement *can* lock a table, which is what they are built for. They were scored as "any
216
+ finding means dangerous", because the question here is whether a stall of a second or more will happen.
217
+ The comparison that means something is with the tools that measure or use table size
218
+ (Antifailure and the size rule).
219
+
220
+ **Limits of this result:**
221
+
222
+ - **Scaled copies, not production.** The databases were built at 4 times a base size with synthetic
223
+ rows, and on one machine. Absolute seconds are not production numbers; the question was whether the
224
+ verdict matched what happened on the same copy.
225
+ - **"Dangerous" means a stall of 1 second or more.** A different threshold gives a different answer.
226
+ Near the line (measured 0.7 s to 1.4 s, 15 cases) lockcast had 1 false alarm and no miss, and the
227
+ other tools were wrong on nearly all of them; this is where any tool is most fragile.
228
+ - **24 of the 87 cases are synthetic patterns**, not commits from the projects.
229
+ - **We built the tool and the test.** The harness, the case list and the raw results are in the
230
+ repository so the numbers can be rerun and the cases argued with.
231
+ - **A small sample.** 87 cases: the lower end of the recall interval is 92%, not 100%.
232
+ - **lockcast was changed after an earlier test.** Part C (70 cases, same method) found that lockcast
233
+ caught 6 of 10 dangerous migrations, because of a bug that left some twins empty. The fix (0.10.6) was
234
+ made, and Part D used cases it had never seen. The Part A and Part C numbers are development data
235
+ for that fix and are not independent evidence. An earlier README quoted a 98% figure from Part A;
236
+ that figure was inflated by the same bug (22 of its 54 cases ran on empty twins) and is withdrawn.
237
+ - **Other tools were run as shipped, with the settings in `PREREG_D.md`.** Antifailure's JSON output
238
+ could not be read in 2 of 24 Mastodon and Chatwoot cases; the scorer counted them as judged. Atlas,
239
+ Bytebase, Landsafe, pg-rehearse, Database Lab, eugene and others were **not** in this test. See
240
+ [docs/LANDSCAPE.md](docs/LANDSCAPE.md).
241
+ - **Hardware.** The twin and the database ran on the same machine. On different hardware set
242
+ `--speed-factor` (production seconds per twin-host second).
243
+ - **Postgres version.** Run on one Postgres major version only (17.12).
244
+
245
+ The test measured verdicts. How close the forecast *seconds* are to the measurement was not scored
246
+ in Part D; an earlier development benchmark on a different database found the measurement inside the
247
+ stated range only in 70% to 79% of cases, so treat the ranges as somewhat too narrow.
248
+
249
+ ## What the rewrites cover
250
+
251
+ | Original | Rewrite |
252
+ |---|---|
253
+ | `CREATE INDEX`, `DROP INDEX`, `REINDEX` | the `CONCURRENTLY` form |
254
+ | `ADD FOREIGN KEY`, `ADD CHECK` | `NOT VALID`, then `VALIDATE CONSTRAINT` |
255
+ | `SET NOT NULL` | a `CHECK (col IS NOT NULL)` added `NOT VALID` and validated, `SET NOT NULL`, drop the check |
256
+ | `ADD UNIQUE`, `ADD PRIMARY KEY` | `CREATE UNIQUE INDEX CONCURRENTLY`, then `USING INDEX` |
257
+ | `ADD COLUMN ... DEFAULT <volatile>` | add the column, set the default, backfill in committed batches, `NOT NULL` last |
258
+ | `UPDATE` / `DELETE` over much of a table | a loop over primary-key ranges that commits each batch |
259
+ | `REFRESH MATERIALIZED VIEW` | `CONCURRENTLY`, when the view has a unique index |
260
+ | one transaction around everything | one transaction per statement |
261
+ | a short lock that would wait behind running transactions (needs `--watch`) | the same statements under a short `lock_timeout`, run again if it times out |
262
+
263
+ Every rewrite runs under `lock_timeout` set to half the budget, and sets it back after a concurrent
264
+ index build, which has to wait for old transactions.
265
+
266
+ A type change that rewrites the table, a `GENERATED ... STORED` or identity column, `VACUUM FULL`
267
+ and `CLUSTER` have no single online form. lockcast says so and describes the expand-and-contract
268
+ route; it does not invent a rewrite it cannot prove.
269
+
270
+ ## Lock queue forecast
271
+
272
+ A migration that needs a strong lock first waits for every transaction already using the table,
273
+ and every later query that conflicts queues behind it. `lockcast profile --watch 60s` samples the
274
+ lock holders in production every 0.25 s for a minute and records, per table, how long a new request
275
+ would have waited. The check adds that wait (p90) to the stall, caps it at the migration's
276
+ `lock_timeout`, and reports the chance of hitting the timeout. When the queue alone breaks the
277
+ budget, the rewrite is the same statements with `lock_timeout` at half the budget, to be run again
278
+ if it times out. Without `--watch` you get the plain warning that no `lock_timeout` is set.
279
+
280
+ Checked against real `LOCK TABLE ... IN ACCESS EXCLUSIVE MODE` requests under emulated traffic
281
+ (scripts and results in [`bench/queue/`](bench/queue/) and `bench/results/queue/`):
282
+
283
+ | Traffic on the table | Sampled p90 / max | Measured p90 / max | Waits over 0.5 s, sampled / measured |
284
+ |---|---:|---:|---:|
285
+ | six threads of short transactions (0.05 to 0.6 s) | 0.63 s / 0.63 s | 0.53 s / 0.59 s (40 requests) | 12% / 12% |
286
+ | the same, plus a report holding the table 3 s of every 5, sampled during the same minutes | 2.38 s / 2.88 s | 2.44 s / 3.00 s (26 requests) | 54% / 62% |
287
+
288
+ When the sample and the requests came from different minutes, right after a server restart, the
289
+ p90 still agreed (2.38 s and 2.58 s) but the medians did not (0.9 s and 0.5 s): the forecast is only
290
+ as good as the assumption that traffic during the migration looks like the traffic sampled. Two
291
+ synthetic patterns are not production traffic.
292
+
293
+ ## What it does not do
294
+
295
+ - **It is not a guarantee.** The forecast is an extrapolation with a stated range. In an earlier
296
+ development benchmark the measured value fell inside the stated range in 70% to 79% of cases, so
297
+ the ranges are somewhat too narrow for the 80% they are meant to cover. Part D scored verdicts, not ranges.
298
+ - **Writes are assumed not to be bound by the disk.** A statement that writes several gigabytes can
299
+ run longer than forecast. Reads of tables that are large against the cache are costed at
300
+ production's own observed read latency when `track_io_timing` is on there.
301
+ - **Twin rows are synthetic.** A new foreign key or unique key is assumed to hold for existing rows
302
+ (the report says so), unless statistics contradict it. A `CHECK` on free text may fail on the twin
303
+ though it would pass in production.
304
+ - **Known miss: a unique index on a high-cardinality text column.** In the small ORM test, `CREATE UNIQUE INDEX` on
305
+ `users.email` (Prisma and Drizzle) measured 1.02 s and 1.04 s and was forecast at 0.51 s and 0.53 s: verdict safe, a
306
+ false negative just above the 1 s line. Synthetic keys sort faster than real ones. A change that raises such forecasts
307
+ exists as `--experimental-unique-order`, but it is **off by default and unvalidated** (it was not part of Part D and it
308
+ flags some safe cases), so the release ships the model that passed Part D. Treat unique-index builds on text keys as
309
+ "forecast may be too low by up to 2x"; keep the probe (`--probe-dsn`) and your own judgement for those.
310
+ - **Query plans can differ between the twin and production.** When the cost per row changes between
311
+ twin sizes, the slowest is assumed and the range is wide.
312
+ - **Scope.** Developed on Postgres 16 and 17; the preregistered test ran on 17.12 only. The code is written for 13 and later (it uses
313
+ `pg_stat_io` where it exists, 16 and later); the CI workflow runs the tests on 14, 16 and 17 but has
314
+ not been run yet. SQL migration files only. Foreign keys that reference a partitioned
315
+ table and exclusion constraints are not synthesized. Renames and drops that break running
316
+ application code are a different problem and are not flagged.
317
+
318
+ ## What is in the profile
319
+
320
+ Schema DDL; per table: row count, bytes, indexes, constraints; per column: type, null fraction,
321
+ average width, distinct count, correlation; settings; optionally lock-holder samples. For numeric,
322
+ date and low-cardinality columns the most common values and histogram bounds from `pg_stats` are
323
+ kept, because predicates in migrations depend on them. For text, JSON and columns whose names look
324
+ sensitive they are dropped and only their shape is kept. `--redact all` drops every value sample;
325
+ `--redact none` keeps them all.
326
+
327
+ ## Configuration
328
+
329
+ `.lockcast.toml` in the repository root:
330
+
331
+ ```toml
332
+ profile = "lockcast.profile.json"
333
+ budget = "1s" # longest acceptable stall per table
334
+ read_budget = "500ms" # optional, for stalls that block reads
335
+ tx = "auto" # auto | file | statement
336
+ twin_fraction = 0.02 # default; raise only if you need a larger twin
337
+ twin_max_size = "2GB"
338
+ # speed_factor = 1.0 # omit to use the measured production/twin speed ratio
339
+
340
+ [tables."public.audit_log"]
341
+ budget = "30s"
342
+ ```
343
+
344
+ ## Development
345
+
346
+ ```sh
347
+ pip install -e '.[test]'
348
+ pytest -q # unit tests, no database needed
349
+ LOCKCAST_TEST_DSN=postgresql://postgres:twin@localhost:5432/postgres pytest -q # plus end-to-end
350
+ ```
351
+
352
+ ## Licensing
353
+
354
+ The core (profile, check, verdict and forecast, text, JSON and Markdown reports, the Action on public repositories) is MIT and free,
355
+ with no limits. A few extras (`fix` as a file, `plan`, `profile --watch`, SARIF and HTML reports, the Action on private
356
+ repositories) are a paid Team tier with an offline-checked key. Details and honest limits: [docs/LICENSING.md](docs/LICENSING.md).
357
+ The Team license text is a draft, not yet reviewed by a lawyer ([LICENSE-COMMERCIAL.md](LICENSE-COMMERCIAL.md)). The code in this
358
+ repository is under the MIT [LICENSE](LICENSE).
359
+
360
+ Seller: **Yackov Mozgori**.
361
+
362
+ ### Pricing
363
+
364
+ | Plan | Price |
365
+ |---|---|
366
+ | Team | $149 / month |
367
+ | Founders | $99 / month |
368
+ | Audit | $299 |
369
+
370
+ Purchase link: **coming soon — open an issue to join the founders list**.
371
+
372
+ Contact: [GitHub Issues](https://github.com/kobima6813/lockcast/issues) and [GitHub Discussions](https://github.com/kobima6813/lockcast/discussions). No personal email.
373
+
374
+ [Changelog](CHANGELOG.md) · [Benchmark](docs/BENCHMARK.md) · [Landscape](docs/LANDSCAPE.md)