Skip to content

write: close a row group before its values pass 256 MiB (#2071) - #2138

Open
jdatcmd wants to merge 4 commits into
mainfrom
fix/2071-writer-byte-budget
Open

jdatcmd wants to merge 4 commits into
mainfrom
fix/2071-writer-byte-budget

Conversation

@jdatcmd

@jdatcmd jdatcmd commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2071.

Problem

An INSERT into a pgcolumnar table failed when one column held more than 1 GiB of values in one row group or one chunk group. With the default limits (150000 rows per row group, 10000 per chunk group), that is a column averaging more than about 7 KB per row, such as a jsonb document. The load failed with string buffer exceeds maximum allowed length (1073741823 bytes), even when the values compressed to almost nothing. Heap stores the same rows.

Cause

The writer closed row groups and chunk groups by row count alone. Every buffer it builds is a StringInfo, and PostgreSQL caps a StringInfo at 1 GiB. Two buffers overflowed:

  • the per-chunk-group value stream, while rows are buffered (the 120 KB case);
  • at flush, flush_one_column copies a column's chunk-group streams into one region (the 8 KB case: 13 chunk groups of 80040000 bytes fit, the 14th does not).

The row group's data buffer, which holds every column's chunk, has the same exposure for several wide columns.

The fix

PgColumnarWriteRow now measures each row before it buffers the row:

  • a fixed-width value counts its width;
  • a varlena counts toast_raw_datum_size plus a 4-byte header, read from its header without detoasting.

If the row would take the row group past 256 MiB, summed over its columns, the writer flushes the row group first. Every chunk group is part of its row group, so this bounds each chunk-group value stream too. A row alone in its row group is always buffered. The cap matches the export cap from #1811 and leaves room for an encoding or codec that makes data larger.

Wide rows now make row groups with fewer than stripe_row_limit rows. Checked on PG 18: 150000 rows of 8 KB make 5 row groups (15984 to 33504 rows each). The reader already accepts short row groups, and the unused row numbers are the gap docs/limitations.md already describes. docs/limitations.md gains "Write size limits", and docs/configuration.md points stripe_row_limit at it.

One existing test changed. The opt-in arm of large_values.sh and test_large_values.py (PGC_RUN_OVER_1GB=1) asserted that 1100 rows of 1 MB incompressible text, compression off, were refused with 54000. That refusal is the defect this PR removes: on this branch the rows commit. The arm now asserts the rows load at full length and the server stays up. It is renamed in both harnesses, in expected_unrunnable.txt and in the ledger. I looked for a shape that still reaches the 1 GiB limit (one row with two 550 MB values), but heap refuses that row too, before the table AM sees it (invalid memory alloc request size 1153433660), so no refusal arm is left to keep.

Red, on main's code

test/large_values.sh, PG 18.6:

PASS  premise: the row limits are the defaults, 150000 per row group and 10000 per chunk group
FAIL  150000 rows of 8 KB text, 1.2 GB for one row group, load and are stored exactly: got [ERROR:  string buffer exceeds maximum allowed length (1073741823 bytes)|] want [INSERT 0 150000|150000,11250075000,1200000000,0]
FAIL  10000 rows of 120 KB text, 1.2 GB for one chunk group, load and are stored exactly: got [ERROR:  string buffer exceeds maximum allowed length (1073741823 bytes)|] want [INSERT 0 10000|10000,50005000,1200000000,0]
accounting: 6 passed + 2 failed + 0 unrunnable + 1 skipped = 9

PG 17.11 gives the same two failures. PG 17's wording is ERROR: out of memory, and the server log has the same detail, Cannot enlarge string buffer containing 1040520000 bytes by 80040000 more bytes.

test_large_values.py -k rows_past, PG 18.6:

E           psycopg.errors.ProgramLimitExceeded: string buffer exceeds maximum allowed length (1073741823 bytes)
E           DETAIL:  Cannot enlarge string buffer containing 1040520000 bytes by 80040000 more bytes.
FAILED test_large_values.py::test_rows_past_1gb_per_row_group_or_chunk_group_load
1 failed, 2 deselected in 12.30s

Green

test/large_values.sh, PG 18.6, with PGC_RUN_OVER_1GB=1:

PASS  premise: the row limits are the defaults, 150000 per row group and 10000 per chunk group
PASS  150000 rows of 8 KB text, 1.2 GB for one row group, load and are stored exactly
PASS  10000 rows of 120 KB text, 1.2 GB for one chunk group, load and are stored exactly
PASS  1.1 GB of incompressible text in one chunk group's rows loads, and the server stays up
accounting: 9 passed + 0 failed + 0 unrunnable + 0 skipped = 9

Without the opt-in, on the other majors:

pg15 accounting: 8 passed + 0 failed + 0 unrunnable + 1 skipped = 9
pg16 accounting: 8 passed + 0 failed + 0 unrunnable + 1 skipped = 9
pg17 accounting: 8 passed + 0 failed + 0 unrunnable + 1 skipped = 9
pg19 accounting: 8 passed + 0 failed + 0 unrunnable + 1 skipped = 9

test_large_values.py: PG 18 with PGC_RUN_OVER_1GB=1, 3 passed (9 checks). PG 18 and PG 17 without it, 3 passed, 8 pass + 1 unrun (the opt-in arm, listed in expected_unrunnable.txt).

The backend's peak memory (VmHWM) for both loads in one session was 813264 kB on PG 18. Before the fix, the backend reached a full 1 GiB buffer before failing.

Builds on PG 17 and PG 18: no compiler warnings.

Suites run

Shell suites, PG 17.11 and PG 18.6, all exit 0, the same counts on both:

suite checks
large_values 8 passed, 1 skipped (opt-in)
native_writer 10
native_toasted_write 8
savepoint_flush 30
projections 78
parallel_flush_optin 11
native_roundtrip 9
smoke SMOKE TEST PASSED
native_dml 15
statement_self_visibility 9
native_rewrite 20
encode_post_codec 11

pytest twins of the same 12 suites, -n 4: PG 17, 100 passed. PG 18, first run 99 passed and 1 failed: test_parallel_flush_optin.py::test_double_cancel_the_cheap_workers_published, on its premise the cheap workers published their segments before the second cancel: got 'no (new segments=[0])'. The file passed alone (11 passed), and a second -n 4 run of all 12 files gave 100 passed. That test's row groups are far below 256 MiB, so the new flush never runs there. I count it as a timing flake under load, not proven.

Guard half (NO_CLUSTER, --pgc-expect-tests 894): 894 passed after one fix. The first run failed test_no_test_mints_its_own_failure_text on my first draft of _load, which turned an error into text; _load now lets the error raise.

compare_to_bash.py test/large_values.sh test/pytest/test_large_values.py: every bash property is covered (8 of 8).

Registries:

  • expected_tests.txt: cluster_tests 5376 to 5377, by collection (6271 collected, less guard_tests 894).
  • TESTS.md and expected_unrunnable.txt: the new test, and the renamed opt-in arm.
  • check_ledger.tsv: 4 rows added, 1 removed. The green logs from PG 15 to 19 were merged in one merge, then one red log each from PG 18 and PG 17 on main's writer. The two load checks are dated 2026-11-24.
  • check_ledger_budget.txt: checks_never_observed_red 4894 to 4895.
  • census-commits: 0 refused. gate on the PG 17 and PG 18 logs: exit 0.
  • CHANGELOG.md: a Fixed entry.

Not verified

  • The full suite matrix and the full pytest run. I ran only the 12 writer-related suites above.
  • PG 15, 16 and 19 for anything but large_values.sh. I did not run the opt-in arm on PG 17, 15, 16 or 19.
  • A row whose values pass 1 GiB in total cannot be stored. I did not find a way to reach the table AM with such a row: heap's own tuple forming refused it first.
  • Parallel flush (pgcolumnar.parallel_flush = on) with rows past 256 MiB. Its suite passed, but its data stays far below the cap.
  • The per-row cost of the new measurement. It adds one pass over the row's columns that reads only headers. I did not benchmark it.

🤖 Generated with Claude Code

An INSERT failed when one column held more than 1 GiB of values in one
row group or one chunk group. The writer closed both by row count
alone, and every buffer it builds is a StringInfo, which PostgreSQL
caps at 1 GiB. With the default limits, 150000 rows of 8 KB or 10000
rows of 120 KB failed with "string buffer exceeds maximum allowed
length", even though the values compress to almost nothing. Heap
stores the same rows.

PgColumnarWriteRow now measures each row before it buffers it: the
fixed width of each fixed-width value, and the detoasted size plus a
4-byte header of each varlena, read from its header without
detoasting. If the row would take the row group past 256 MiB summed
over its columns, the writer flushes the row group first. Every chunk
group is part of its row group, so this bounds each chunk group's
value stream too. A row alone in its row group is always buffered.

test/large_values.sh and test_large_values.py gain the two loads from
the issue. The opt-in over-1-GiB arm asserted that 1100 rows of 1 MB
incompressible text were refused with 54000. That is the defect this
change removes, so the arm now asserts the rows load. It is renamed in
both harnesses, in expected_unrunnable.txt and in the ledger.
docs/limitations.md gains "Write size limits".

Ledger: four rows added, one removed (the renamed arm), merged from
large_values.sh logs on PG 15, 16, 17, 18 and 19, then one red log
each from PG 18 and PG 17 on main's writer. 9660 + 4 - 1 = 9663 rows;
never 4894 + 2 - 1 = 4895; dated 4766 + 2 = 4768.

Ledger-census: rows=9663 never=4895 dated=4768
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jdatcmd and others added 3 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 refuse with "GITHUB_BASE_REF is unset"
since ubuntu-latest moved to 26.04. PR #2162 (845c83e) passes the
variable through sudo by name. It is not on main yet, so this branch
takes it directly; the merge will collapse once #2162 lands.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This branch closes a row group before its values pass 256 MiB (#2071).
cancel_decode loaded 4,000,000 eight-column rows into a table meant to
hold one row group. By the writer's own measure those rows come to
409,333,376 bytes, so the writer now splits them into two groups, and
"the one-group table has exactly one group" failed with got 2 want 1 in
both twins on PG 17 and PG 18.

The fixture was wrong under the new writer, not the check. Both twins
now load 2,000,000 rows: 201,333,376 bytes, 75% of the cap. The check
still asks for exactly one group, and every other check is unchanged.
The uncancelled scan of the one group still took 1066 ms locally,
against the 300 ms the premise check needs.

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

jdatcmd commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

CI fix-up: two commits pushed (head 65c2190)

What I merged, and why

c37f4703 merges fix/ci-sudo-base-ref (845c83e, PR #2162) into this branch. Since ubuntu-latest moved to 26.04, every PR's suites jobs have refused with ledger integrity failure: no trustworthy prior ceiling: GITHUB_BASE_REF is unset. #2162 fixes that, but it is not on main yet, so I merged the branch directly. The merge had no conflicts: CHANGELOG.md auto-merged and kept both entries. Once #2162 lands on main, this merge collapses into it.

CI failures on c40516a, and what I did

1. cancel_decode: "the one-group table has exactly one group" failed in all four jobs. It failed in pytest cluster tests on PG 17 and PG 18, and in suites on PG 17 and PG 18. Every job shows the same line:

FAIL  the one-group table has exactly one group: got [2] want [1]
FAILED test_cancel_decode.py::test_the_one_group_table_has_exactly_one_group - AssertionError: the one-group table has exactly one group: got '2' want '1'
1 failed, 5376 passed

Cause: this PR's own change. The fixture loads 4,000,000 eight-column rows into a table that should hold one row group. By row_value_bytes (4 bytes per int; each text value's raw size plus VARHDRSZ), those rows measure 409,333,376 bytes. That is 1.5 times the new 256 MiB cap, so the writer now correctly splits them into two groups.

The fixture was wrong, not the check. Commit 65c21908 loads 2,000,000 rows in both twins: 201,333,376 bytes, 75% of the cap. The check still asks for exactly one group, and no other check changed. I reproduced the red on PG 18 before the change, and the green after it:

before:  FAIL  the one-group table has exactly one group: got [2] want [1]
         accounting: 10 passed + 1 failed + 0 unrunnable + 0 skipped = 11
after:   PASS  the one-group table has exactly one group
         uncancelled scan 1066 ms; cancel latency: many groups 130 ms, one group 176 ms
         accounting: 11 passed + 0 failed + 0 unrunnable + 0 skipped = 11

The uncancelled scan of the single group still takes 1066 ms. That is above the 300 ms premise, and above the 3x + 50 ms ratio bound (440 ms here). So a cancel that waited for the whole group would still fail. I worked that out from these numbers; I did not remove an interrupt check to test it.

2. suites (PG 17/18): the ledger refusal. Both jobs also printed the GITHUB_BASE_REF is unset refusal. Apart from it, the ledger lines agreed: census stated 4895, ledger holds 4895, coverage ... not covered=19, ceiling=19. The merge above fixes the refusal. No other suite failed in either job.

Local checks on PG 18.6, head 65c2190

  • Build after make clean: 0 lines in the make -s log, so no warnings.
  • Guard half, run as test/pytest/README.md says: 894 passed, which matches guard_tests 894.
  • test_cancel_decode.py test_large_values.py: 14 passed. The one UNRUN is the opt-in test_incompressible_text_past_1gb_loads.
  • large_values.sh: 8 passed + 0 failed + 0 unrunnable + 1 skipped. The skip is the opt-in 1.1 GB arm.
  • Full PG 18 matrix with GITHUB_BASE_REF=main:
    • Ledger gate: ledger coverage: registered=312 | covered=293, not covered=19, ceiling=19 and census stated 4895, ledger holds 4895: they agree, with no integrity failure.
    • Ledger commits: census commits: 1 change test/check_ledger.tsv since d52f6e8c (merge base with refs/remotes/origin/main), 0 refused.
    • 13 suites failed in that local run. A parallel run on the same machine stopped their clusters partway through (received immediate shutdown request). I reran them one at a time: 12 passed. The 13th, logical_subscriber, fails locally only because I ran it as a non-root user: its own initdb creates no postgres role. This PR does not touch it, and it passed as root on CI on c40516a.

Not verified: the new CI run on 65c2190. It has not finished yet.

🤖 Generated with Claude Code

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.

write: an INSERT of rows averaging more than about 7 KB in one column fails with a string-buffer error under default settings

1 participant