Skip to content

Fix gzip external table reads - #2338

Merged
leborchuk merged 5 commits into
apache:mainfrom
aartchik:fix/gzip-reader-memory-context
Nov 19, 2026
Merged

leborchuk merged 5 commits into
apache:mainfrom
aartchik:fix/gzip-reader-memory-context

Conversation

@aartchik

Copy link
Copy Markdown
Contributor

What does this PR do?

Fix backend crashes when reading gzip files through file:// external tables.

Decoder memory can be allocated in the memory context of a single CSV record. Resetting that context frees memory that the decoder still references. On the tested revision, this caused SIGABRT with double free or corruption when finishing decompression.

The fix switches to the scan's copycontext while reading external data, so decoder allocations survive per-row context resets. PG_FINALLY restores the caller's memory context on both success and error paths.

Type of Change

  • Bug fix (non-breaking change)

Test Plan

Add gzip_reader to the existing gp_exttable_fdw regression suite. It generates a 1 MiB CSV containing 8,192 records and two gzip copies, then checks:

  • The uncompressed CSV.
  • A single gzip file.
  • Two gzip files matched by a glob.

The test verifies record counts, total message length, and decoded contents. It removes the generated files and tables afterward.

On the original revision:

  • The existing extension test passed.
  • The new test crashed the backend without the fix and passed with it.
  • Restoring the original library reproduced the failure.
  • Both extension tests passed in five consecutive runs with the fix.

After rebasing onto main at bb5d6931e00, the database was rebuilt and verified again:

  • Both extension tests passed.
  • All 13 selected core regression tests, including external-table tests, passed.

Validation used a local Linux ARM64 demo cluster with one coordinator and three primary segments, built without ORCA, gpcloud, PAX, or MDB locales. The new test reads files on the coordinator. No new failures attributable to the patch were identified.

Impact

User-facing changes: gzip external-table reads no longer crash in the reproduced scenarios.

Dependencies: No new dependencies.

AI assistance

  • This PR contains AI-assisted code generation.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @aartchik welcome!🎊 Thanks for taking the effort to make our project better! 🙌 Keep making such awesome contributions!

@Vlasdislav Vlasdislav left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR. We already review the changes in fork: open-gpdb/gpdb#447 so LGTM. But I suggest add to description stack trace about crash for clarity a very unobvious problem.

@MisterRaindrop

Copy link
Copy Markdown
Contributor

Thanks, the fix looks right to me. NextCopyFrom() runs in rowcontext, so when url_fread() moves to the next file of the glob, the zlib state that gfile allocates with palloc() lands in rowcontext, and the next MemoryContextReset() frees it. copycontext lives until the scan ends. gzip_reader passed in ic-gpcontrib.

One thing before it can be merged: rat-check fails because the two new files, input/gzip_reader.source and output/gzip_reader.source, have no license header. Could you add the Apache license header to both as a SQL comment, or add them to the excludes in pom.xml next to gp_exttable_fdw.source?

@leborchuk leborchuk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the contribution!

RAT should be fixed, it was already pointed out

@aartchik
aartchik requested a review from leborchuk November 17, 2026 11:51
Comment thread gpcontrib/gp_exttable_fdw/extaccess.c Resolved
@leborchuk
leborchuk enabled auto-merge (squash) November 19, 2026 20:50
@leborchuk
leborchuk disabled auto-merge November 19, 2026 20:51
@leborchuk
leborchuk enabled auto-merge (squash) November 19, 2026 20:51
@leborchuk
leborchuk merged commit 1c48a58 into apache:main Nov 19, 2026
134 checks passed
Vlasdislav pushed a commit to Vlasdislav/cloudberry that referenced this pull request Nov 22, 2026
Fix backend crashes when reading gzip files through file:// external tables.

Decoder memory can be allocated in the memory context of a single CSV record. Resetting that context frees memory that the decoder still references. On the tested revision, this caused SIGABRT with double free or corruption when finishing decompression.

The fix switches to the scan's copycontext while reading external data, so decoder allocations survive per-row context resets. PG_FINALLY restores the caller's memory context on both success and error paths.

---------

Co-authored-by: Artur Nasredinov <arturnasredin@yandex-team.ru>
Co-authored-by: Leonid <63977577+leborchuk@users.noreply.github.com>
(cherry picked from commit 1c48a58)
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.

5 participants