Skip to content

parallel_copy: refuse 2+ workers for a table with a unique index (#2080) - #2147

Open
jdatcmd wants to merge 4 commits into
mainfrom
fix/2080-parallel-copy-unique
Open

jdatcmd wants to merge 4 commits into
mainfrom
fix/2080-parallel-copy-unique

Conversation

@jdatcmd

@jdatcmd jdatcmd commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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 or statement_timeout ended it, or failed with 40P01 deadlock 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 runs PREPARE TRANSACTION, which keeps the locks. The coordinator runs COMMIT PREPARED only 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_copy now 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:

ERROR:  0A000: pgcolumnar.parallel_copy cannot load "t_uk" with more than one worker, because it has a unique or exclusion index
HINT:  Load it with workers => 1, or with COPY.
  • One worker still loads such a table.
  • A partitioned target is not refused. Each worker loads its own partitions, so their indexes and lock tags differ. On PG 18, unfixed code, a 4-partition table with a primary key loaded 4000 rows with 4 workers in 22 ms.
  • Exclusion indexes are included because their check waits on a conflicting row's xid, which would be a sibling's prepared transaction in the same way.

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 a statement_timeout so the old hang shows as an error:

  1. a primary-key table with 2 workers is refused with 0A000 and the hint
  2. a CREATE UNIQUE INDEX table with 4 workers is refused the same way
  3. both refused tables stay empty
  4. control: 1 worker still loads 5000 rows into a primary-key table
  5. control: 2 workers still load 5000 rows into a table with a non-unique index
  6. a unique index created after the first check, while a second session holds SHARE on the table, is refused by the loaders with 0A000 (added after review)

Red, on main's code

pytest, PG 18.6 (2 failed, 3 passed):

E           AssertionError: parallel_copy refuses a single table with a primary key and 2 workers with 0A000, not a hang (#2080): got '57014 canceling statement due to statement timeout | no hint' want '0A000 pgcolumnar.parallel_copy cannot load "t_uk" with more than one worker, because it has a unique or exclusion index | Load it with workers => 1, or with COPY.'
E           AssertionError: parallel_copy refuses a single table with a unique index and 4 workers with 0A000, not a hang (#2080): got '57014 canceling statement due to statement timeout | no hint' want '0A000 pgcolumnar.parallel_copy cannot load "t_ui" with more than one worker, because it has a unique or exclusion index | Load it with workers => 1, or with COPY.'
2 failed, 3 passed, 96 deselected in 24.00s

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:

FAIL  parallel_copy refuses a single table with a primary key and 2 workers with 0A000, not a hang (#2080): got [57014 canceling statement due to statement timeout | no hint] want [0A000 pgcolumnar.parallel_copy cannot load "t_uk" with more than one worker, because it has a unique or exclusion index | Load it with workers => 1, or with COPY.]
FAIL  parallel_copy refuses a single table with a unique index and 4 workers with 0A000, not a hang (#2080): got [40P01 pgcolumnar.parallel_copy worker 0 failed: deadlock detected | no hint] want [0A000 pgcolumnar.parallel_copy cannot load "t_ui" with more than one worker, because it has a unique or exclusion index | Load it with workers => 1, or with COPY.]
PASS  the unique-index refusal loads nothing (#2080)
PASS  control: parallel_copy with 1 worker still loads a single table with a primary key (#2080)
PASS  control: parallel_copy with 2 workers still loads a single table with a non-unique index (#2080)

test/parallel_copy.sh, PG 17.11:

FAIL  parallel_copy refuses a single table with a primary key and 2 workers with 0A000, not a hang (#2080): got [57014 canceling statement due to statement timeout | no hint] want [...]
FAIL  parallel_copy refuses a single table with a unique index and 4 workers with 0A000, not a hang (#2080): got [57014 canceling statement due to statement timeout | no hint] want [...]
accounting: 99 passed + 2 failed + 0 unrunnable + 0 skipped = 101

Green

pytest, PG 18.6:

PASSED test_parallel_copy.py::test_parallel_copy_refuses_a_primary_key_table_with_two_workers
PASSED test_parallel_copy.py::test_parallel_copy_refuses_a_unique_index_table_with_four_workers
PASSED test_parallel_copy.py::test_parallel_copy_unique_refusal_loads_nothing
PASSED test_parallel_copy.py::test_parallel_copy_still_loads_a_primary_key_table_with_one_worker
PASSED test_parallel_copy.py::test_parallel_copy_still_loads_a_non_unique_index_table_with_two_workers
5 passed, 96 deselected in 5.44s

make gives 0 warnings.

Suites run

On the fixed code.

Suite PG 15 PG 16 PG 17 PG 18 PG 19
test/parallel_copy.sh 101/101 101/101 101/101 101/101 101/101
parallel_copy_dedup.sh 17/17 17/17
advisory_lock_class.sh 18/18 18/18
catalog_scan_index.sh 61/61 61/61
column_extent.sh 42/42 42/42
entry_point_privilege.sh 42/42 42/42
local_open_race_free.sh 11/11 11/11
import_export_privilege.sh 26/26 26/26
nologin_workers.sh 15/15 15/15
sorted_pathkeys.sh 113/113 113/113
rls_direct_storage.sh 26/26 26/26
prepared_writes.sh 11/11 11/11
server_file_privilege.sh 68/68 68/68
docs_style.sh 54/54

pytest, -n 4, the 14 files that use parallel_copy (including test_parallel_copy.py, test_parallel_copy_dedup.py and test_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.py then gave 54 passed. I did not rerun the whole guard half after that fix.

Registries: cluster_tests 5376 -> 5381, collected file by file. TESTS.md section 297 lists the 5 tests. check_ledger.tsv has 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 are never. checks_never_observed_red 4894 -> 4897. census-commits --against origin/main: 0 refused.

Not verified

  • PG 15, 16 and 19: I ran only test/parallel_copy.sh there, not the other suites and not pytest.
  • The full pytest corpus: I did not run it, only the 14 files above and the guard half.
  • The partitioned case with a unique index has no regression test. I checked it by hand once, on PG 18 only.
  • A file with duplicate keys across ranges: not run. The refusal covers it, because it is decided before any row is read.
  • The PG 18 and PG 17 suite logs were taken before I shortened the sql-reference.md sentence. That edit changes no code or test.

🤖 Generated with Claude Code

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]>
@jdatcmd

jdatcmd commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

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 header

Fixed. pcopy_has_unique_or_exclusion_index() and its comment now sit above the pcopy_naive_offsets header, so each function has its own header. The move changes no code. make gives 0 warnings on PG 18.6.

2. "Waits forever" was only part of what happens

Fixed. The CHANGELOG, docs/limitations.md, docs/sql-reference.md, the code comments, the commit message and the PR description now say the load hangs or fails with a deadlock error. They no longer say the deadlock detector cannot see it. My own red runs agree with yours: the late-index check below got 40P01 on one run and 57014 on another.

3. A unique index created after the check

Real. 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 CREATE INDEX queues behind it. The coordinator's own ROW EXCLUSIVE request then queues behind the CREATE INDEX, and the caller waits on the coordinator. Nothing detects that wait. I reproduced it on PG 18.6 by holding ROW EXCLUSIVE in the caller's own transaction, then running a non-unique CREATE INDEX in a second session:

client backend|IPC|BgworkerShutdown|SELECT pgcolumnar.parallel_copy('demo'::
client backend|Lock|relation|SET lock_timeout='30s'; CREATE INDEX dem
pgcolumnar parallel_copy coordinator|Lock|relation|
A: ERROR:  canceling statement due to statement timeout
B: CREATE INDEX

Instead, 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. The first check stays, because it refuses with the hint before any worker starts.

New check in both harnesses: parallel_copy refuses 2 workers when a unique index appears after its check, not a hang (#2080). A second session holds SHARE on the table. That lets the first check pass and stops the coordinator at its lock. The session then creates a unique index and commits.

Red, loader check removed (if (false && ...)), everything else as in 6d38984:

PG 18.6 pytest:  E AssertionError: parallel_copy refuses 2 workers when a unique index appears after its check, not a hang (#2080): got '40P01 pgcolumnar.parallel_copy worker N failed: deadlock detected | no hint' want '0A000 pgcolumnar.parallel_copy worker N failed: pgcolumnar.parallel_copy cannot load "t_ur" with more than one worker, because it has a unique or exclusion index | no hint'
                 accounting: 5 pass + 1 fail + 0 unrun = 6
PG 18.6 shell:   FAIL  parallel_copy refuses 2 workers when a unique index appears after its check, not a hang (#2080): got [57014 canceling statement due to statement timeout | no hint] want [0A000 ...]
                 accounting: 101 passed + 1 failed + 0 unrunnable + 0 skipped = 102
PG 17.11 shell:  FAIL  ... got [40P01 pgcolumnar.parallel_copy worker N failed: deadlock detected | no hint] want [0A000 ...]
                 accounting: 101 passed + 1 failed + 0 unrunnable + 0 skipped = 102
PG 17.11 pytest: E AssertionError: ... got '57014 canceling statement due to statement timeout | no hint' want '0A000 ...'
                 accounting: 5 pass + 1 fail + 0 unrun = 6

Red, all production code restored to origin/main, PG 18.6: pytest 3 pass + 3 fail = 6, shell 99 passed + 3 failed = 102. The 3 failures are the two original refusals and the new check, each with 40P01.

Green:

Run Result
test/parallel_copy.sh, PG 15, 16, 17, 18, 19 102/102 each
pytest test_parallel_copy.py + test_parallel_copy_dedup.py, PG 17.11 119 passed
the same, PG 18.6 119 passed
pytest test_docs_style.py, test_mutation_ledger.py, test_docs_cover_the_corpus.py, test_suite_accounting.py, test_compare_to_bash.py, PG 18.6 200 passed after the fix below
test/docs_style.sh, PG 18.6 54/54

test_docs_style.py first failed on a 27-word sentence in my new docs/limitations.md text. I split the sentence, and it then gave 54 passed. That docs-only edit came after the suite runs above.

No existing test changed its expectation.

Registries: cluster_tests 5381 -> 5382 (test_parallel_copy.py collects 96 on main, 102 here). check_ledger.tsv has one new row, dated 2026-11-24 from the red PG 18 and PG 17 shell logs, with a named mutation. never stays 4897, and rows go from 9665 to 9666. census-commits --against origin/main: 0 refused.

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.

jdatcmd and others added 2 commits November 24, 2026 06:34
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]>
@jdatcmd

jdatcmd commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

I merged fix/ci-sudo-base-ref (#2162, 845c83e) into this branch as fab34ff. I made no other change.

Why I merged it

Both suites (PG 17) and suites (PG 18) failed on 6d38984 for one reason only. The ubuntu-latest runner moved to 26.04, and sudo -E there drops GITHUB_BASE_REF. The ledger gate then refused to run:

ledger integrity failure: no trustworthy prior ceiling: GITHUB_BASE_REF is unset and the local main has no configured upstream. ...
PG18 could not run the ledger gate at all, which is not a pass:

#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 found

I read both suites job logs (jobs 138261900304 and 138261900335). The only failure in each was the ledger refusal above. Every suite passed, including parallel_copy:

FAIL   PG17  (303 ran, 9 skipped, 0 incomplete)   <- no suite =FAIL in the list
FAIL   PG18  (306 ran, 6 skipped, 0 incomplete)   <- no suite =FAIL in the list

The "listed as debt but now accounts" lines are informational, not failures (run_all_versions.sh around line 1333). The other 13 checks were green. I found nothing else to fix.

What I checked locally on fab34ff (PG 18.6)

  • Build after make clean: 0 compiler warnings (the build log is empty).
  • Guard half, run the way CI runs it: 894 passed, which equals guard_tests 894.
  • test/parallel_copy.sh: accounting: 102 passed + 0 failed + 0 unrunnable + 0 skipped = 102, including all six interchange: parallel_copy into a columnar table with a unique index or primary key hangs until cancelled, even with distinct keys #2080 checks.
  • test_parallel_copy.py --pg-config <sandbox>: 102 passed.
  • census-commits --against origin/main: 2 change test/check_ledger.tsv since d52f6e8c ..., 0 refused.
  • Full run_all_versions.sh with GITHUB_BASE_REF=main under sudo env, as CI runs it:
    ledger coverage: registered=312 | covered=293, not covered=19, ceiling=19
      ceiling against refs/remotes/origin/main (4 commits behind HEAD): 19 -> 19, which does not rise
    census commits: 2 change test/check_ledger.tsv since d52f6e8c (merge base with refs/remotes/origin/main), 0 refused
    PASS   PG18  (306 ran, 6 skipped, 0 incomplete)
    ALL VERSIONS PASSED
    

I did not run PG 17 locally; CI will. When #2162 lands on main, this merge becomes a no-op.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

interchange: parallel_copy into a columnar table with a unique index or primary key hangs until cancelled, even with distinct keys

1 participant