Skip to content

Fix segment result handling in pg_lock_status() - #2424

Open
protectione055 wants to merge 1 commit into
apache:mainfrom
protectione055:fix/exec-location-error-message
Open

protectione055 wants to merge 1 commit into
apache:mainfrom
protectione055:fix/exec-location-error-message

Conversation

@protectione055

@protectione055 protectione055 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Problem

Querying pg_locks on the coordinator returns corrupted waitstart timestamps for waiting locks on segments.

For example, when two sessions concurrently insert the same primary key so session 2 blocks on a segment:

-- Setup: CREATE TABLE t(a int PRIMARY KEY) DISTRIBUTED BY (a);
-- Session 1: BEGIN; INSERT INTO t VALUES (1);
-- Session 2: INSERT INTO t VALUES (1);  -- blocks on segment 1

-- Session 3 (coordinator):
SELECT gp_segment_id, locktype, granted, waitstart
FROM pg_locks
WHERE NOT granted AND gp_segment_id >= 0;

Before :

 gp_segment_id |   locktype    | granted |           waitstart           
---------------+---------------+---------+-------------------------------
             1 | transactionid | f       | 2000-01-01 08:00:00.002026+08
(1 row)

Root Cause

In pg_lock_status() , the coordinator dispatches a query to segments and receives rows in text format, then parses them manually with atoi() / atoll(). For waitstart (timestamptz), atoll() stops at the year prefix ("2026") and treats it as 2026 microseconds since the PostgreSQL epoch (2000-01-01). Similarly, unsigned oid/xid columns are parsed with signed atoi().

Fix

  • Replace manual atoi() / atoll() parsing with BuildTupleFromCStrings() so all segment columns are decoded via their type input functions.
  • Also register a MemoryContextCallback (pg_lock_status_cleanup) and clear PGresult on column-count mismatch so libpq results are not leaked on early termination or error.
  • Minor fix in interpret_exec_location() (src/backend/commands/functioncmds.c): report "invalid exec location" instead of "invalid data access".

After :

 gp_segment_id |   locktype    | granted |           waitstart           
---------------+---------------+---------+-------------------------------
             1 | transactionid | f       | 2026-11-25 14:50:30.513338+08
(1 row)

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated (src/test/isolation2/sql/pg_locks_segment.sql)
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Verification details:

  • Ran pg_locks_segment on two independent 3-segment clusters comparing before vs after: waitstart_ok for the waiting lock on segment 0 changed from f to t.

Checklist

In coordinator mode, pg_lock_status() previously parsed text-format
segment rows manually with atoi()/atoll(), which corrupted timestamptz
values (waitstart) and could mishandle unsigned OID/XID values. Use
BuildTupleFromCStrings() to decode columns via their type input
functions instead.

Also fix two edge cases in segment result handling:
- Skip empty segment result sets before reading the next row to avoid
  out-of-bounds access.
- Register a MemoryContextCallback on multi_call_memory_ctx and clear
  results on column-count mismatch so libpq PGresults are not leaked on
  early termination or error.

Also fix the error message in interpret_exec_location() to report
"invalid exec location" instead of "invalid data access".
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