Skip to content

Read the postgis lookup without asking for a transaction id - #297

Merged
aykut-bozkurt merged 1 commit into
CrunchyData:mainfrom
sfc-gh-abozkurt:read-the-postgis-lookup-read-only
Nov 4, 2026
Merged

aykut-bozkurt merged 1 commit into
CrunchyData:mainfrom
sfc-gh-abozkurt:read-the-postgis-lookup-read-only

Conversation

@sfc-gh-abozkurt

Copy link
Copy Markdown
Contributor

Problem

A parallel COPY TO fails outright when PostGIS is installed:

-- before
SET pg_parquet.max_parallel_workers TO 4;
COPY a_table TO '/tmp/out.parquet';
-- ERROR:  cannot assign transaction IDs during a parallel operation

No destination is written. It is not specific to a destination, an option or a column type: a
plain COPY <table> TO '<file>' in a fresh transaction is enough, on a table large enough for
the copy to be split into ctid ranges.

The startup of every copy looks the PostGIS extension up, and those lookups went through pgrx's
Spi::get_one, which takes the mutable path: it tells PostgreSQL the transaction is about to
write by asking it for a transaction id. A parallel worker cannot be given one, so the worker
that ran the lookup failed the copy.

Two things hid it:

  • A transaction that has already written holds a transaction id, and the lookup is answered
    out of the one it holds rather than assigning a new one. pgrx runs every test inside a
    transaction that created and filled the table it copies, so no test in parallel_copy_to.rs
    could reach the failure. An ordinary COPY TO is the first thing its transaction does, holds
    no id, and was refused.
  • With no PostGIS installed the lookup reads nothing, so those copies were unaffected.

Solution

Read the lookups through Spi::connect, which is read only, the way the profile mapping in
src/object_store/profile.rs already is. A read_one helper holds the one call so the four
lookups cannot drift apart:

fn read_one<T: FromDatum + IntoDatum>(query: &str) -> Result<Option<T>, pgrx::spi::SpiError> {
    Spi::connect(|client| client.select(query, Some(1), &[])?.first().get_one::<T>())
}
-- after
SET pg_parquet.max_parallel_workers TO 4;
COPY a_table TO '/tmp/out.parquet';
-- COPY 200000

Test plan

test_parallel_copy_to_without_a_transaction_id runs the copy in a database and a session of
its own, so that PostGIS is installed, as it has to be for the lookup to read anything, and the
transaction the copy runs in has written nothing. It then reads the file back and counts the
rows, so a copy that succeeds at nothing does not pass.

It fails on this branch's parent with the production error and passes with the fix:

# without the fix
psql failed: ERROR:  cannot assign transaction IDs during a parallel operation
test result: FAILED. 0 passed; 1 failed

# with the fix
test result: ok. 1 passed; 0 failed

The test leaves its database behind rather than dropping it. DROP DATABASE waits for every
backend to take a procsignal barrier, and the backend running the test is blocked on psql, so it
would wait for itself. The test instance is thrown away with its data directory.

Also ran copy_geospatial (37), copy_type_roundtrip (108), copy_options (61) and
create_table_from green, and checked by hand that a geometry and a geography column still
round-trip and that a parallel copy of a geometry table in a fresh transaction now succeeds.

🤖 Generated with Claude Code

A parallel COPY TO failed outright when postgis is installed:

  SET pg_parquet.max_parallel_workers TO 4;
  COPY a_table TO '/tmp/out.parquet';
  ERROR:  cannot assign transaction IDs during a parallel operation

The startup of every copy looks the postgis extension up, and the lookups
went through pgrx's Spi::get_one, which takes the mutable path: it tells
postgres the transaction is about to write by asking it for a transaction
id. A parallel worker cannot be given one, so the worker that ran the
lookup failed the copy. The lookups only read, so they are read through
Spi::connect instead, the way the profile mapping already is.

A transaction that has already written holds an id, and the lookup is
answered out of the one it holds, which is why the tests did not catch
this: pgrx runs a test inside a transaction that created and filled the
table it copies. The copy is the first thing an ordinary transaction does,
so it holds no id, and every parallel COPY TO in such a transaction was
refused. A copy with no postgis installed was unaffected, since the lookup
reads nothing then.

The test runs the copy in a database and a session of its own, so that
postgis is installed and the transaction the copy runs in has written
nothing. It leaves the database behind rather than dropping it, since
DROP DATABASE waits for every backend to take a procsignal barrier and
the backend running the test is blocked on psql, so it would wait for
itself.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.30508% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.77%. Comparing base (965ef66) to head (699d745).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/pgrx_tests/parallel_copy_to.rs 97.91% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main     #297    +/-   ##
========================================
  Coverage   94.77%   94.77%            
========================================
  Files         123      124     +1     
  Lines       25842    26016   +174     
========================================
+ Hits        24491    24657   +166     
- Misses       1351     1359     +8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aykut-bozkurt
aykut-bozkurt merged commit 6123eee into CrunchyData:main Nov 4, 2026
16 checks passed
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.

2 participants