Skip to content

Conversation

@R4hul04
Copy link
Contributor

@R4hul04 R4hul04 commented Dec 19, 2025

(cherry picked from commits e50b7f2 and 3a37c4c)

Description

We are dealing with a critical crash bug in Babelfish's EPQ (EvalPlanQual) handling during concurrent UPDATE operations with OUTPUT clauses. The crash occurs when two sessions simultaneously modify the same row, where the column is of type "varchar" causing tuple alignment problems during EPQ re-evaluation. This is a memory corruption issue where PostgreSQL's EPQ mechanism fails to properly handle tuple structure changes when Babelfish processes OUTPUT clauses, leading to segmentation faults or assertion failures.

The Problem

Babelfish was processing the OUTPUT clause (RETURNING clause) BEFORE EPQ evaluation, which created a race condition crash because:

  1. Original Design Rationale: OUTPUT clause was moved before EPQ to ensure it executed before triggers
  2. Race Condition: When EPQ re-evaluation occurred, the OUTPUT clause had already processed using stale tuple data
  3. Crash Point: EPQ tried to re-evaluate with changed tuple structure, but OUTPUT processing was already complete with wrong memory references

The Solution

Align Babelfish OUTPUT clause execution with PostgreSQL's native RETURNING clause flow:

Key Insights from Fix

  1. Trigger Execution Order: Analysis revealed that OUTPUT clause doesn't actually need to run before triggers in Babelfish context
  2. PostgreSQL Alignment: Following PostgreSQL's native RETURNING execution flow eliminates the race condition
  3. EPQ Compatibility: Processing OUTPUT after EPQ ensures it always works with the correct, final tuple data
  4. Memory Safety: Eliminates tuple alignment crashes by ensuring OUTPUT clause processes current data structure

Related PR

babelfish-for-postgresql/babelfish_extensions#4336

Issues Resolved

BABEL-4880, BABEL-1290

Check List

  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is under the terms of the PostgreSQL license, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Herambh Shah added 2 commits December 19, 2025 00:36
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.

1 participant