Repository navigation
Conversation
pgcolumnar.parallel_copy with 2 or more workers hung until cancelled when the target was a single columnar table with a unique index or primary key. It hung even when every key in the file was distinct. Each loader takes a unique-key advisory lock per inserted key. The locks are hash buckets (128 by default), so distinct keys share them. PREPARE TRANSACTION keeps the locks, and the coordinator runs COMMIT PREPARED only after every loader has finished. A loader that waits on a sibling's prepared transaction therefore waits forever, and the deadlock detector cannot see it. parallel_copy now refuses that load up front, with SQLSTATE 0A000 and the hint "Load it with workers => 1, or with COPY." The check covers unique and exclusion indexes. One worker still loads such a table. A partitioned target is not refused: each worker loads its own partitions, so their indexes differ. docs/limitations.md and docs/sql-reference.md state the restriction. New checks in test/parallel_copy.sh and test_parallel_copy.py: two refusals (primary key with 2 workers, unique index with 4), the refused tables stay empty, and two controls (1 worker, and a non-unique index with 2 workers). Registries: expected_tests.txt cluster_tests 5376 -> 5381, collected file by file over every cluster file. check_ledger.tsv: 5 new rows from one green parallel_copy.sh log on each of PG 15, 16, 17, 18 and 19, then the red PG 18 and PG 17 logs of the unfixed code, merged with --reds-are-real and a named mutation. That dates the two refusal rows 2026-11-24; the other three stay never, so never 4894 -> 4897 and rows 9660 -> 9665. Ledger-census: rows=9665 never=4897 dated=4768 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This answers the three points of the review of #2147. 1. pcopy_has_unique_or_exclusion_index() sat between the header comment of pcopy_naive_offsets and that function. It now sits above that comment, so each function has its own header. The move changes no code. 2. Without the fix, the load does not only hang. It hangs until cancelled or fails with 40P01 "deadlock detected", and the review's runs and mine saw both. CHANGELOG.md, docs/limitations.md, docs/sql-reference.md and the code comments now say "hangs or fails with a deadlock error". They no longer say the deadlock detector cannot see it. 3. The calling backend checks the indexes under ACCESS SHARE, which CREATE INDEX does not wait for. A unique index created after that check, but before the loaders open the table, brought the old failure back. Each loader now checks again right after it takes ROW EXCLUSIVE on the table. From then on CREATE INDEX waits until the load ends, so the index list the loader reads holds for the whole load. I did not take the suggested fix, a stronger lock held by the calling backend. A run on PG 18.6 shows why: with ROW EXCLUSIVE held by the caller, a plain CREATE INDEX queues behind it, the coordinator's ROW EXCLUSIVE request queues behind the CREATE INDEX, and the caller waits on the coordinator. The load ended only at statement_timeout (57014). New check in test/parallel_copy.sh and test_parallel_copy.py: a second session holds SHARE on the table, so the first check passes and the coordinator stops at its lock. The session then creates a unique index and commits. With the loader's check removed it got 57014 or 40P01 on PG 18.6 and PG 17.11; with it, 0A000 from a loader. No existing test changed its expectation. Registries: expected_tests.txt cluster_tests 5381 -> 5382 (test_parallel_copy.py collects 96 on main, 102 here). check_ledger.tsv: one new row from a green parallel_copy.sh log on each of PG 15 to 19, dated 2026-11-24 from the red PG 18 and PG 17 logs, merged with --reds-are-real and the named mutation. never stays 4897; rows 9665 -> 9666. Ledger-census: rows=9666 never=4897 dated=4769 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the review. All three points were real. I fixed 1 and 2 as you suggested. For 3 I used a different fix, because the suggested one adds a new hang. Commit 6d38984. 1. The new function split pcopy_naive_offsets from its headerFixed. 2. "Waits forever" was only part of what happensFixed. The CHANGELOG, 3. A unique index created after the checkReal. I confirmed it by a run before fixing it (see the red output below). I did not take the suggested fix of holding ROW EXCLUSIVE in the calling backend. If the caller holds that lock, a plain Instead, each loader now checks again right after it takes ROW EXCLUSIVE on the table. From then on New check in both harnesses: Red, loader check removed ( Red, all production code restored to Green:
No existing test changed its expectation. Registries: Not verified: the full pytest corpus and the guard half, and pytest on PG 15, 16 and 19. Not fixed here, seen while testing point 3: a caller who already holds ROW EXCLUSIVE on the table in an open transaction can hit the same caller/coordinator queue described above. That is not new in this PR. |
ubuntu-latest moved from ubuntu-24.04 to ubuntu-26.04 between 2026-11-12 and 2026-11-22. Since then `sudo -E` no longer carries GITHUB_BASE_REF into run_all_versions.sh. The ledger gate then cannot find the PR's base and refuses: "no trustworthy prior ceiling: GITHUB_BASE_REF is unset". Every PR fails suites (PG 17) and suites (PG 18) although every suite passes (#2056, and every PR from #2127 to #2159). The step now names the variable in the env list, so it reaches the gate whatever sudo preserves. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The suites (PG 17/18) jobs fail on every PR with "ledger integrity failure: no trustworthy prior ceiling: GITHUB_BASE_REF is unset", because the ubuntu-latest runner moved to 26.04 and sudo there drops GITHUB_BASE_REF. PR #2162 (845c83e) passes the variable by name and is not on main yet. This merge brings that fix in so this PR's suite jobs can run the ledger gate. CHANGELOG.md merged without conflict. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
I merged Why I merged itBoth #2162 passes the variable by name. It is not on main yet, so I merged its branch. Git merged CHANGELOG.md with no conflict. CI failures I foundI read both suites job logs (jobs 138261900304 and 138261900335). The only failure in each was the ledger refusal above. Every suite passed, including The "listed as debt but now accounts" lines are informational, not failures ( What I checked locally on fab34ff (PG 18.6)
I did not run PG 17 locally; CI will. When #2162 lands on main, this merge becomes a no-op. |
Fixes #2080.
Problem
pgcolumnar.parallel_copy()with 2 or more workers failed when the target was a single columnar table with a unique index or primary key, even when every key in the file was distinct. The load either hung until a cancel orstatement_timeoutended it, or failed with 40P01deadlock detected. Runs saw both, with 2 workers and with 4.Cause
Each loader takes a unique-key advisory lock for every key it inserts (
src/columnar_unique.c,pgcolumnar_acquire_key_lock). The locks are hash buckets, 128 by default, so distinct keys share them. Each loader then runsPREPARE TRANSACTION, which keeps the locks. The coordinator runsCOMMIT PREPAREDonly after every loader has finished. A loader that waits on a sibling's prepared transaction therefore waits until cancelled. When the loaders wait on each other before they prepare, the deadlock detector sees the cycle and one fails with 40P01.The fix
parallel_copynow refuses that load before it starts any worker (src/columnar_parallel_copy.c). Each loader checks again once it holds ROW EXCLUSIVE on the table, so a unique index created after the first check is refused too (added after review; see the review comment). For a single columnar table with more than one worker, it checks the table's indexes. If any is unique or an exclusion index, it raises:docs/limitations.md and docs/sql-reference.md state the restriction. CHANGELOG.md has an entry under Fixed.
Tests, in both harnesses (
test/parallel_copy.sh,test_parallel_copy.py), each run under astatement_timeoutso the old hang shows as an error:CREATE UNIQUE INDEXtable with 4 workers is refused the same wayRed, on main's code
pytest, PG 18.6 (2 failed, 3 passed):
test/parallel_copy.sh, PG 18.6. The deadlock detector sometimes catches a cycle between loaders that have not prepared yet, so this run got 40P01 instead of a timeout. The review's runs saw 40P01 with 2 workers too. Either way the load is not refused:test/parallel_copy.sh, PG 17.11:Green
pytest, PG 18.6:
makegives 0 warnings.Suites run
On the fixed code.
test/parallel_copy.shparallel_copy_dedup.shadvisory_lock_class.shcatalog_scan_index.shcolumn_extent.shentry_point_privilege.shlocal_open_race_free.shimport_export_privilege.shnologin_workers.shsorted_pathkeys.shrls_direct_storage.shprepared_writes.shserver_file_privilege.shdocs_style.shpytest,
-n 4, the 14 files that useparallel_copy(includingtest_parallel_copy.py,test_parallel_copy_dedup.pyandtest_compare_to_bash.py): 324 passed, 1327 checks, on PG 17.11 and on PG 18.6.Guard half, as test/pytest/README.md shows: 893 passed and 1 failed (
test_docs_style.py, a 26-word sentence in my sql-reference.md text). I split the sentence;test_docs_style.pythen gave 54 passed. I did not rerun the whole guard half after that fix.Registries:
cluster_tests5376 -> 5381, collected file by file. TESTS.md section 297 lists the 5 tests.check_ledger.tsvhas 5 new rows from one green log per major (15 to 19); the two refusal rows are dated 2026-11-24 from the red PG 18 and PG 17 logs, the other three arenever.checks_never_observed_red4894 -> 4897.census-commits --against origin/main: 0 refused.Not verified
test/parallel_copy.shthere, not the other suites and not pytest.🤖 Generated with Claude Code