[BUG]: windowed live.query leaks its total-count prepared statement on unsubscribe
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 92/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- postgresql, typescript
Research direction
Start in packages/pglite/src/live/index.ts and run the windowed-query teardown test in packages/pglite/tests/live.test.ts, named "deallocates all prepared statements for a windowed query on unsubscribe." Done means unsubscribing removes both prepared statements and the test passes with no remaining live_query_* entries.
Written by the indexing model from the issue text.
Description
Describe the bug
A windowed live.query (one given both offset and limit) prepares two statements, live_query_<id>_get(int, int) and live_query_<id>_get_total_count, but unsubscribe only runs DEALLOCATE live_query_<id>_get. The count statement survives every teardown while its backing view live_query_<id>_view is dropped, so it is left permanently dangling and one more accumulates per subscribe/unsubscribe cycle for the lifetime of the database.
To Reproduce
import { PGlite } from '@electric-sql/pglite'
import { live } from '@electric-sql/pglite/live'
const pg = await PGlite.create({ extensions: { live } })
await pg.exec(`CREATE TABLE t (id serial PRIMARY KEY); INSERT INTO t DEFAULT VALUES;`)
const { unsubscribe } = await pg.live.query({
query: 'SELECT * FROM t',
offset: 0,
limit: 10,
callback: () => {},
})
const before = await pg.query(`SELECT name FROM pg_prepared_statements WHERE name LIKE 'live_query_%'`)
// before.rows: live_query_<id>_get and live_query_<id>_get_total_count
await unsubscribe()
const after = await pg.query(`SELECT name FROM pg_prepared_statements WHERE name LIKE 'live_query_%'`)
// expected: []
// actual: [{ name: 'live_query_<id>_get_total_count' }]
The survivor is dangling, not merely leaked: EXECUTE live_query_<id>_get_total_count after unsubscribe fails because the view it selects from is gone. A non-windowed query (no offset/limit) tears down cleanly, since it never prepares the count statement.
The executable form of this is the test added in #1108 (packages/pglite/tests/live.test.ts, "deallocates all prepared statements for a windowed query on unsubscribe"), which fails on main with expected [ Array(1) ] to deeply equal [] and passes with the fix.
Logs
error: relation "live_query_<id>_view" does not exist
(from EXECUTE live_query_<id>_get_total_count after unsubscribe)
Details
- PGlite version: 0.5.8 (
mainat the time of filing) - using any extensions? which ones?
live - OS version: Linux x64
- node, bun, deno or browser version: Node 24
Additional context
Cause is in packages/pglite/src/live/index.ts: the unsubscribe teardown deallocates live_query_<id>_get unconditionally but never the total-count statement, even though the prepare path guards its creation on the same isWindowed flag. Mirroring that guard in the teardown is the fix in #1108.
- Dominant language
- TypeScript
- Stars
- 16.1k
- Forks
- 447
- Avg merge
- 5d 14h
- Merged PRs (30d)
- 8
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from electric-sql/pglite
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
electric-sql/pglite#1134 ·
Maintainers usually reply within 2 days
-
Feature request: Nitro 3 support (`unwasm` export condition)Possibly taken @pi0x claimed this 6 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
electric-sql/pglite#1120 · 3 comments ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
electric-sql/pglite#1048 ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 3-5 days Newbie friendliness 56/100
electric-sql/pglite#1128 ·
Maintainers usually reply within 2 days
-
[BUG]: pglite-sync: after a `must-refetch` a synced table can stay empty; commit errors are swallowedPossibly taken @jeromerg claimed this 4 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 45/100
electric-sql/pglite#1125 ·
Maintainers usually reply within 2 days
All issues in electric-sql/pglite
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
area:cowork area:desktop documentation enhancement platform:macos
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
anthropics/claude-code#101720 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
apache/rocketmq-dashboard#6610 ·
Maintainers usually reply within 3 days
-
Add: MS NOWOpencheck:passed streams:add
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
Global update drops overrideAccess, so Local API updateGlobal fails filterOptions validation (3.90 regression)Possibly taken @PINYOPATTANAWASANPORN claimed this today. Openarea: core Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
payloadcms/payload#18674 · 1 comment ·
Maintainers usually reply within 1 day