Skip to content

Import into a table with a projection no longer aborts the backend (#2092) - #2155

Open
jdatcmd wants to merge 4 commits into
mainfrom
fix/2092-import-projection-snapshot
Open

jdatcmd wants to merge 4 commits into
mainfrom
fix/2092-import-projection-snapshot

Conversation

@jdatcmd

@jdatcmd jdatcmd commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2092.

Problem

pgcolumnar.import_arrow() and pgcolumnar.import_parquet() abort the backend on a table that has a projection, on a PostgreSQL 18 or 19 build with assertions. PostgreSQL then restarts every session, and the import rolls back. docs/how-to.md adds a projection and later imports an Arrow file. A reader who runs the guide from top to bottom hits the crash once the projection example works (it fails today, #2093).

The crash needs a row in the options catalog, for any table. set_options on the table gives one. Without it, a projection alone crashes once another table has an options row. The tests give a neighbour table one first, so the case without set_options fails on main's code too.

Cause

The import buffers the projection's rows and writes them when the statement ends, in pgcolumnar_executor_end. An import is a SELECT, so its statement ends in portal cleanup, where no snapshot is active. The projection write (flush_ws_projections_ext) builds a write state, which reads the options catalog with GetTransactionSnapshot(). Nobody has registered that snapshot, so the first catalog tuple examined fails Assert(snapshot->regd_count > 0 || snapshot->active_count > 0) in HeapTupleSatisfiesMVCC. PostgreSQL 15, 16 and 17 have no such assertion.

The base table's write (pgcolumnar_flush_row_group) already pushes an active snapshot for this reason. It pops it before the projections are written, so the projection write runs without one. A plain INSERT ... SELECT does not reach this, because its ExecutorEnd runs with the statement's snapshot still active.

I confirmed this with temporary logging (removed): during the failing import, the projection flush ran with ActiveSnapshotSet() false, and the options read got a snapshot with regd_count=0 active_count=0.

The fix

flush_ws_projections_ext (src/columnar_write_state.c) pushes GetTransactionSnapshot() as the active snapshot when none is set, and pops it after the projection writers are flushed. This is the same pattern pgcolumnar_flush_row_group uses. It changes nothing when a snapshot is already active.

Update after review (commit 0aa1702)

A verifier found four problems. The review fix-up comment lists each with its output. In short:

  1. A new parallel_copy check (shell and pytest) loads into a table with a projection. parallel_copy does not share this crash. With 2 or more workers into an empty projection it hangs, for another reason: filed as interchange: parallel_copy into a table with an empty projection hangs until cancelled with 2 or more workers #2161.
  2. I ran every sql block of docs/how-to.md in order. On this branch: 0 backend crashes. On main's code, with the events columns that docs: eight SQL examples in the how-to, user guide and SQL reference fail when run as written #2093 says are missing: 1 crash, at "Import and export Arrow".
  3. The case without set_options could not fail on main's code. Each check now gives a neighbour table an options row first; on main's code that case now crashes in all four tests.
  4. The CHANGELOG, commit, comments and docstrings no longer use the sentence fragment.

The sections below describe the first commit. The red output there predates the neighbour fix; the comment has the new red output.

Red, on main's code (PG18.6 with assertions, d52f6e8)

Shell, test/arrow_import.sh and test/parquet_import.sh:

FAIL  import_arrow into a table with a projection, with or without set_options, keeps the server up and fills the projection (#2092): got [o0p0:1000/2000/- o0p1:1000/2000/2000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 1 backend crashes] want [o0p0:1000/2000/- o0p1:1000/2000/2000 o1p0:1000/2000/- o1p1:1000/2000/2000 | 0 backend crashes]
accounting: 100 passed + 1 failed + 0 unrunnable + 0 skipped = 101
TRAP: failed Assert("snapshot->regd_count > 0 || snapshot->active_count > 0"), File: "heapam_visibility.c", Line: 972, PID: 1839703

FAIL  import_parquet into a table with a projection, with or without set_options, keeps the server up and fills the projection (#2092): got [o0p0:1000/2000/- o0p1:1000/2000/2000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 1 backend crashes] want [o0p0:1000/2000/- o0p1:1000/2000/2000 o1p0:1000/2000/- o1p1:1000/2000/2000 | 0 backend crashes]
accounting: 25 passed + 1 failed + 0 unrunnable + 0 skipped = 26
TRAP: failed Assert("snapshot->regd_count > 0 || snapshot->active_count > 0"), File: "heapam_visibility.c", Line: 972, PID: 1841323

Each case reads o<set_options>p<projection>:<imported>/<rows in table>/<rows in projection>.

pytest (the tests had their first names then; they were renamed later so that no test name appears twice in the corpus):

got = 'o0p0:1000/2000/- o0p1:1000/2000/2000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 1 backend crashes'
E           AssertionError: import_arrow into a table with a projection, ... (#2092): got 'o0p0:1000/2000/- o0p1:1000/2000/2000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 1 backend crashes' want '... | 0 backend crashes'
got = 'o0p0:1000/2000/- o0p1:lost connection/1000/1000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 2 backend crashes'
E           AssertionError: import_parquet into a table with a projection, ... (#2092): got 'o0p0:1000/2000/- o0p1:lost connection/1000/1000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 2 backend crashes' want '... | 0 backend crashes'
accounting: 0 pass + 2 fail + 0 unrun = 2
2 failed, 2 errors in 6.79s

The 2 errors are the fixtures' teardown, whose connection the crash closed. In the parquet test, o0p1 (projection only) crashed too, because the arrow test had left an options row in the catalog.

Green

PASSED test_arrow_import.py::test_an_arrow_import_into_a_table_with_a_projection_keeps_the_server_up
PASSED test_parquet_import.py::test_a_parquet_import_into_a_table_with_a_projection_keeps_the_server_up
2 passed, 35 deselected in 23.05s

PASS  import_arrow into a table with a projection, with or without set_options, keeps the server up and fills the projection (#2092)
PASS  import_parquet into a table with a projection, with or without set_options, keeps the server up and fills the projection (#2092)

The build has no compiler warnings on PG15, 16, 17, 18 and 19 (grep -c warning: gave 0 on each).

Suites run

On the committed C code and shell suites. I renamed the two pytest functions after these runs, before committing; nothing else changed.

Suite PG15 PG16 PG17 PG18 PG19
arrow_import.sh 101/101 101/101 101/101 101/101 101/101
parquet_import.sh 19 passed, 7 skipped 19 passed, 7 skipped 26/26 26/26 26/26
projections.sh 78/78 78/78
projection_update.sh 9/9 9/9
projection_rewrite.sh 84/84 84/84
projection_parallel.sh 13/13 13/13
projection_drop_column.sh 11/11 11/11
projection_rename_restore.sh 8/8 8/8
projection_privilege.sh 28/28 28/28
sorted_projection.sh 29/29 29/29
native_projection.sh 8/8 8/8

The 7 skips on PG15 and 16 are the partitioned-parent checks, which need PG17, as on main.

pytest, with -n 4: test_arrow_import.py, test_parquet_import.py, test_arrow_import_encoding.py, test_import_deferred.py, test_import_exclusion.py, test_projections.py, test_projection_*.py (7 files), test_sorted_projection.py and test_native_projection.py:

PG18: accounting: 464 pass + 0 fail + 0 unrun = 464   222 passed in 52.19s
PG17: accounting: 464 pass + 0 fail + 0 unrun = 464   222 passed in 75.17s

After the rename, test_arrow_import.py and test_parquet_import.py again: 37 passed on PG18 and 37 passed on PG17.

Guard half, as the README shows: 894 passed, accounting: 2440 pass + 0 fail + 0 unrun = 2440. compare_to_bash.py gives missing: 0 for both pairs.

Registries

  • expected_tests.txt: cluster_tests 5376 -> 5378, collected over the files NO_CLUSTER does not name (5378 tests collected).
  • TESTS.md: both tests in the tables of sections 108 and 140, and the section text.
  • check_ledger.tsv: two rows. They come from one merge of green logs of both suites on PG15 to 19, then from each red PG18 log with --reds-are-real. Both rows are dated, so checks_never_observed_red stays 4894 and check_ledger_budget.txt does not change. census-commits: 0 refused.
  • expected_unrunnable.txt: no change. Neither test declines on any major.
  • CHANGELOG.md: a Fixed entry.

Not verified

  • Non-assert builds. I did not run one. There the unregistered snapshot is used without any check. I did not measure what that does.
  • PG19 red. PG19 has the same assertion, but I ran the red only on PG18. PG19 has green runs only.
  • The full pytest cluster suite. I ran the files listed above, not all of them.
  • Other suites. I did not run them on PG15, 16 or 19.

🤖 Generated with Claude Code

…2092)

import_arrow and import_parquet aborted the backend on a table with a
projection, on a PostgreSQL 18 or 19 build with assertions. PostgreSQL
then restarted every session, and the import rolled back.

The import buffers the projection's rows and writes them when the
statement ends. For a SELECT that is during portal cleanup, when no
snapshot is active. The projection write reads the options catalog with
GetTransactionSnapshot(), which nobody had registered, and the first
catalog tuple it examined failed the assertion in
HeapTupleSatisfiesMVCC. The base table's write already pushes an active
snapshot for this reason, but it pops it before the projections are
written. flush_ws_projections_ext now pushes its own.

Tests, written first and red on main's code on PG18: one check in
test/arrow_import.sh and one in test/parquet_import.sh, with their
pytest twins in test_arrow_import.py and test_parquet_import.py. Each
crosses projection with set_options on four tables and reads crashes
from the server log.

Ledger: two rows, merged from green runs of both suites on PG15, 16, 17,
18 and 19 in one merge, then from each red PG18 run with --reds-are-real
and the mutation "main's code before the #2092 fix". Both rows are
dated, so never stays 4894 and the budget does not move:
9660 + 2 = 9662 rows, 4766 + 2 = 4768 dated.

Ledger-census: rows=9662 never=4894 dated=4768
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A reviewer found that the arm without set_options (o0p1) of the four new
#2092 checks could not fail on main's code. The crash needs a row in the
options catalog for any table. The matrix runs o0p1 before any case
calls set_options, so in the shell suites and in the arrow pytest the
catalog was still empty. The parquet pytest went red on o0p1 only
because the arrow test had left a row behind. So these tests were wrong
for that arm: their names say "with or without set_options", but only
the arm with set_options proved the fix.

Each check now gives a neighbour table an options row before the matrix.
On main's code o0p1 now crashes in all four, each run on its own. The
names and wanted values do not change. The comments and docstrings are
rewritten in whole sentences.

A new check in test/parallel_copy.sh, and its twin in
test_parallel_copy.py, answers #2092's parallel_copy criterion. It loads
1 worker into an empty projection and 2 workers into a projection that
already holds rows, each table with an options row. Both fill the
projection and no backend dies, on main's code too: parallel_copy does
not share the import crash. It is a coverage check, not a regression
test of the fix. A mutation that skips every projection write turns it
red. With 2 or more workers into an empty projection, parallel_copy
hangs, for another reason. That is filed as #2161, and no case covers
it.

The CHANGELOG entry is rewritten in whole sentences.

Registries: cluster_tests 5378 -> 5379. TESTS.md gains the new test and
notes the neighbour row. check_ledger.tsv gains one row, merged from
green parallel_copy.sh runs on PG15 to 19, then from the red PG18 run
under the mutation "flush_ws_projections_ext returns before it writes
any projection row" with --reds-are-real. The row is dated, so never
stays 4894 and the budget does not change: 9662 + 1 = 9663 rows,
4768 + 1 = 4769 dated.

Ledger-census: rows=9663 never=4894 dated=4769
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jdatcmd

jdatcmd commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Review fix-up: what I did about each problem (commit 0aa1702)

All runs: sandbox, PostgreSQL 18.6 and 17.11 built with --enable-cassert. "Main's code" means src/columnar_write_state.c restored from origin/main and rebuilt; nothing else changed.

1. No parallel_copy case: added one, and filed the hang as #2161

What I did. I added one check to test/parallel_copy.sh, and its twin to test_parallel_copy.py:

parallel_copy into a table with a projection fills the projection, leaves no prepared transaction and keeps the server up (#2092)

It loads the 5000-row file twice. Each table has an options row and a projection:

  • 1 worker into a projection that holds no rows;
  • 2 workers into a projection that already holds 1000 rows.

Each load runs under statement_timeout = 60s, so a regression cannot hang the suite.

What it shows. parallel_copy does not share the import crash. The check passes on main's code too, so it is a coverage check, not a regression test of the fix. To show it can fail, I ran it under a mutation that skips every projection write. Only this check went red:

FAIL  parallel_copy into a table with a projection fills the projection, leaves no prepared transaction and keeps the server up (#2092): got [w1:5000/5000/0 w2:5000/6000/1000 | leak 0 | survived 1] want [w1:5000/5000/5000 w2:5000/6000/6000 | leak 0 | survived 1]
accounting: 96 passed + 1 failed + 0 unrunnable + 0 skipped = 97

The hang. I reproduced your hang. It needs 2 or more workers and a projection that holds no rows yet. A prepared loader holds the projection's storage-row lock (class 102, PGCOLUMNAR_LOCKCLASS_STORAGE_ROW), and the next loader waits for it:

    pid     | lockclass |     mode      | granted | storage_id
------------+-----------+---------------+---------+-------------
 (prepared) |       102 | ExclusiveLock | t       | 10000000050   <- the projection's storage
 (prepared) |       105 | ExclusiveLock | t       | 10000000049   <- the base table's storage
 3209006    |       102 | ExclusiveLock | f       | 10000000050
ERROR:  canceling statement due to statement timeout

It is a sibling of #2080 on a different lock, so #2080's fix would not cover it. I filed it as #2161. No check covers that case, and the comments in the new check say so.

2. The guide was not run: I ran every sql block in order

How. I ran all 48 sql blocks of docs/how-to.md in order, in one psql -v ON_ERROR_STOP=0 session, on a fresh database. Before the run I did CREATE EXTENSION pgcolumnar. I also mapped /data and /warehouse to a writable folder. The script counts backend crashes in the server log.

As written. The projection step at line 192 fails, because events has no customer_id (#2093, item 1). So the guide never reaches the crash, on main's code or on this branch:

blocks: 48  ERROR lines: 50  backend crashes during the run: 0

With the projection working. To reach the crash, I added customer_id and order_id to events before the run, and loaded 1000 rows. That is the fixture #2093 describes.

On main's code, the guide crashes at "Import and export Arrow":

==== block 36 (line 925)
 export_arrow
--------------
         2000
server closed the connection unexpectedly
blocks: 36  ERROR lines: 28  backend crashes during the run: 1
TRAP: failed Assert("snapshot->regd_count > 0 || snapshot->active_count > 0"), File: "heapam_visibility.c", Line: 972

On this branch, the same run completes:

==== block 36 (line 925)
 export_arrow
--------------
         2000
 import_arrow
--------------
         2000
blocks: 48  ERROR lines: 43  backend crashes during the run: 0

Afterwards events holds 4000 rows and events_by_customer holds 4000. The 43 errors are #2093's examples and steps that need outside resources:

  • 17 × missing relation;
  • 5 × missing file;
  • 6 × object storage not configured;
  • 3 × missing role.

3. The case without set_options could not fail: fixed

You are right. The crash needs an options row for any table, and o0p1 ran before any case had made one. Each of the four checks now gives a neighbour table an options row before the matrix. The check names and wanted values stay the same.

Red on main's code, PG18. Both shell suites:

FAIL  import_arrow into a table with a projection, ... (#2092): got [o0p0:1000/2000/- o0p1:lost connection/1000/1000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 2 backend crashes] want [... | 0 backend crashes]
accounting: 100 passed + 1 failed + 0 unrunnable + 0 skipped = 101
FAIL  import_parquet into a table with a projection, ... (#2092): got [o0p0:1000/2000/- o0p1:lost connection/1000/1000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 2 backend crashes] want [... | 0 backend crashes]
accounting: 25 passed + 1 failed + 0 unrunnable + 0 skipped = 26

The pytest twins, each run alone so that no other test can leave a row behind:

test_an_arrow_import_into_a_table_with_a_projection_keeps_the_server_up
got = 'o0p0:1000/2000/- o0p1:lost connection/1000/1000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 2 backend crashes'
1 failed, 1 error in 8.30s
test_a_parquet_import_into_a_table_with_a_projection_keeps_the_server_up
got = 'o0p0:1000/2000/- o0p1:lost connection/1000/1000 o1p0:1000/2000/- o1p1:lost connection/1000/1000 | 2 backend crashes'
1 failed, 1 error in 7.69s

The errors are the fixtures' teardown after the crash.

4. The sentence fragment: rewritten

The CHANGELOG entry, both .sh comments and both pytest docstrings now say it in whole sentences: "An import is a SELECT, so its statement ends in portal cleanup, where no snapshot is active." The CHANGELOG entry no longer ends in a list of file names. It says which checks were added, and points to #2093 and #2161.

The first commit's message still has the fragment. Force-push is denied here, so I cannot reword it. The new commit and the PR text use the whole sentence.

Green, on this branch

Suite PG15 PG16 PG17 PG18 PG19
arrow_import.sh 101/101 101/101 101/101 101/101 101/101
parquet_import.sh 19 passed, 7 skipped 19 passed, 7 skipped 26/26 26/26 26/26
parallel_copy.sh 97/97 97/97 97/97 97/97 97/97

These pytest files were run with -n 4: test_arrow_import.py, test_parquet_import.py and test_parallel_copy.py.

PG18: accounting: 225 pass + 0 fail + 0 unrun = 225   134 passed in 9.46s
PG17: accounting: 225 pass + 0 fail + 0 unrun = 225   134 passed in 9.97s

Guard half: 894 passed, accounting: 2440 pass + 0 fail + 0 unrun = 2440. compare_to_bash.py gives missing: 0 for all three pairs.

Registries

  • expected_tests.txt: cluster_tests 5378 -> 5379, by collection over the cluster files (5379 tests collected).
  • TESTS.md: the new test in section 297, and the neighbour row in sections 108 and 140.
  • check_ledger.tsv: one row. It comes from the green parallel_copy.sh logs on PG15 to 19, then from the mutation's red log with --reds-are-real. The row is dated, so never stays 4894 and the budget does not change.
  • census-commits: 2 change test/check_ledger.tsv since d52f6e8c (merge base with origin/main), 0 refused.

Still not verified

  • Non-assert builds.
  • The red on PG19.
  • The full pytest cluster suite.
  • parallel_copy into a partitioned target with a projection.

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]>
…pshot

Since ubuntu-latest moved to 26.04, the suites (PG 17/18) jobs fail with
"ledger integrity failure: no trustworthy prior ceiling: GITHUB_BASE_REF
is unset", although the suites pass. PR #2162 (845c83e) fixes the CI
step but is not in main yet, so this branch takes it to let CI go green.
CHANGELOG.md: both entries kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jdatcmd

jdatcmd commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Merged the CI fix from #2162 into this branch (ab858bd)

What I merged. origin/fix/ci-sudo-base-ref (845c83e, PR #2162). It is not in main yet, so I merged the branch instead of main. The only conflict-prone file, CHANGELOG.md, merged without a conflict and keeps both entries. There is no fix commit, because CI found nothing else to fix.

Why. suites (PG 17) and suites (PG 18) failed on 0aa1702 for one reason only, in both jobs:

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

No suite failed in either job: both logs hold 0 =FAIL entries, and population reconciliation: ... unaccounted=0. The other 13 checks passed.

Local checks on ab858bd (PG 18.6)

  • Build: a clean build compiled 38 files with 0 warnings.
  • Guard half (the README command, with -n 8): 894 passed, matching guard_tests 894 in expected_tests.txt. Accounting: 2440 pass + 0 fail + 0 unrun.
  • This PR's tests (test_arrow_import.py, test_parquet_import.py, test_parallel_copy.py): 134 passed. Accounting: 225 pass + 0 fail + 0 unrun.
  • Ledger gate, run as CI runs it (GITHUB_BASE_REF=main bash test/run_all_versions.sh <pg_config>):
    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
    
  • Suites that failed in that local run, and why. smoke, phase2 to phase6 and audit failed with createdb: error: connection to server on socket ".../.s.PGSQL.NNNN" failed. My sandbox exports PGHOST and PGPORT, and createdb followed them instead of the suite's own server. Rerun with those variables unset, all seven print PASSED.
  • logical_subscriber also failed locally: FATAL: role "postgres" does not exist. The script connects with a hard-coded -U postgres, and my local user has no role of that name. CI runs it as postgres, and it passed on both majors on 0aa1702. This PR does not touch it.

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: import_arrow and import_parquet abort the backend and restart the cluster on a table that has a projection

1 participant