Registry Mirror
Skip to content

Conversation

@reshke

@reshke reshke commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

checkCanOptSelectLockingClause scans a raw WHERE tree before deciding whether to use the row-locking optimization. An EXISTS SubLink has a NULL testexpr, which the raw walker passes to queryNodeSearch. The callback dereferenced that NULL with IsA(node, Query), crashing during parse analysis. It also stopped on the first non-query child instead of recursing through the raw tree.

Handle NULL nodes, recurse through raw expressions, and stop once a SubLink or Query is found. Add an isolation case with GDD enabled and the Postgres optimizer selected.

Reproducer as the non-superuser role sqlscan:

SELECT * FROM pg_catalog.pg_class WHERE EXISTS (SELECT 1) FOR NO KEY UPDATE;

Verbatim client output before the fix:

server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
connection to server was lost

GDB used a preceding SELECT pg_sleep(15) on the same connection so it could attach before parsing the reproducer.

Verbatim backend log:
2026-12-01 22:48:37.317462 MSK,,,p3042112,th123456,,,2026-12-01 22:48:22 MSK,0,,cmd3,seg-1,,,,,"PANIC","XX000","Unexpected internal error: Segment process received signal SIGSEGV",,,,,,,0,,,,"1 0x55ce71184387 postgres StandardHandlerForSigillSigsegvSigbus_OnMainThread + 0x177
2 0x7f78c1bb9330 libc.so.6 + 0xc1bb9330
3 0x55ce70c57b14 postgres + 0x70c57b14
4 0x55ce70e12666 postgres raw_expression_tree_walker_impl + 0xc6
5 0x55ce70c58c9d postgres checkCanOptSelectLockingClause + 0x9d
6 0x55ce70c5c19b postgres transformTopLevelStmt + 0x7b
7 0x55ce70c5c2b1 postgres parse_analyze_fixedparams + 0x51
8 0x55ce70ff269e postgres pg_analyze_and_rewrite_fixedparams + 0x1e
9 0x55ce70ff3393 postgres exec_simple_query + 0x943
10 0x55ce70ff7934 postgres PostgresMain + 0x1ef4
11 0x55ce70f436f4 postgres + 0x70f436f4
12 0x55ce70f4495a postgres PostmasterMain + 0xf8a
13 0x55ce70b045e8 postgres main + 0x5d8
14 0x7f78c1b9e1ca libc.so.6 + 0xc1b9e1ca
15 0x7f78c1b9e28b libc.so.6 __libc_start_main + 0x8b
16 0x55ce70b06865 postgres _start + 0x25
"

GDB backtrace:
Breakpoint 1, 0x000055ce70c58c00 in checkCanOptSelectLockingClause () #0 0x000055ce70c58c00 in checkCanOptSelectLockingClause () #1 0x000055ce70c5c19b in transformTopLevelStmt () #2 0x000055ce70c5c2b1 in parse_analyze_fixedparams () #3 0x000055ce70ff269e in pg_analyze_and_rewrite_fixedparams () #4 0x000055ce70ff3393 in exec_simple_query ()
#5 0x000055ce70ff7934 in PostgresMain ()
#6 0x000055ce70f436f4 in ServerLoop.isra.0 ()
#7 0x000055ce70f4495a in PostmasterMain ()

Program received signal SIGSEGV, Segmentation fault. 0x000055ce70c57b14 in queryNodeSearch ()
#0 0x000055ce70c57b14 in queryNodeSearch ()
#1 0x000055ce70e12666 in raw_expression_tree_walker_impl () #2 0x000055ce70c58c9d in checkCanOptSelectLockingClause () #3 0x000055ce70c5c19b in transformTopLevelStmt () #4 0x000055ce70c5c2b1 in parse_analyze_fixedparams () #5 0x000055ce70ff269e in pg_analyze_and_rewrite_fixedparams () #6 0x000055ce70ff3393 in exec_simple_query ()
#7 0x000055ce70ff7934 in PostgresMain ()
#8 0x000055ce70f436f4 in ServerLoop.isra.0 ()
#9 0x000055ce70f4495a in PostmasterMain ()
#10 0x000055ce70b045e8 in main ()
[Inferior 1 (process 3042112) detached]

Fixes #ISSUE_Number

What does this PR do?

Type of Change

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

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context


checkCanOptSelectLockingClause scans a raw WHERE tree before deciding
whether to use the row-locking optimization. An EXISTS SubLink has a
NULL testexpr, which the raw walker passes to queryNodeSearch. The
callback dereferenced that NULL with IsA(node, Query), crashing during
parse analysis. It also stopped on the first non-query child instead of
recursing through the raw tree.

Handle NULL nodes, recurse through raw expressions, and stop once a
SubLink or Query is found. Add an isolation case with GDD enabled and
the Postgres optimizer selected.

Reproducer as the non-superuser role sqlscan:

    SELECT * FROM pg_catalog.pg_class WHERE EXISTS (SELECT 1) FOR NO KEY UPDATE;

Verbatim client output before the fix:

    server closed the connection unexpectedly
    	This probably means the server terminated abnormally
    	before or while processing the request.
    connection to server was lost

GDB used a preceding SELECT pg_sleep(15) on the same connection so it
could attach before parsing the reproducer.

Verbatim backend log:
2026-12-01 22:48:37.317462 MSK,,,p3042112,th123456,,,2026-12-01 22:48:22 MSK,0,,cmd3,seg-1,,,,,"PANIC","XX000","Unexpected internal error: Segment process received signal SIGSEGV",,,,,,,0,,,,"1    0x55ce71184387 postgres StandardHandlerForSigillSigsegvSigbus_OnMainThread + 0x177
2    0x7f78c1bb9330 libc.so.6 <symbol not found> + 0xc1bb9330
3    0x55ce70c57b14 postgres <symbol not found> + 0x70c57b14
4    0x55ce70e12666 postgres raw_expression_tree_walker_impl + 0xc6
5    0x55ce70c58c9d postgres checkCanOptSelectLockingClause + 0x9d
6    0x55ce70c5c19b postgres transformTopLevelStmt + 0x7b
7    0x55ce70c5c2b1 postgres parse_analyze_fixedparams + 0x51
8    0x55ce70ff269e postgres pg_analyze_and_rewrite_fixedparams + 0x1e
9    0x55ce70ff3393 postgres exec_simple_query + 0x943
10   0x55ce70ff7934 postgres PostgresMain + 0x1ef4
11   0x55ce70f436f4 postgres <symbol not found> + 0x70f436f4
12   0x55ce70f4495a postgres PostmasterMain + 0xf8a
13   0x55ce70b045e8 postgres main + 0x5d8
14   0x7f78c1b9e1ca libc.so.6 <symbol not found> + 0xc1b9e1ca
15   0x7f78c1b9e28b libc.so.6 __libc_start_main + 0x8b
16   0x55ce70b06865 postgres _start + 0x25
"

GDB backtrace:
Breakpoint 1, 0x000055ce70c58c00 in checkCanOptSelectLockingClause ()
#0  0x000055ce70c58c00 in checkCanOptSelectLockingClause ()
#1  0x000055ce70c5c19b in transformTopLevelStmt ()
#2  0x000055ce70c5c2b1 in parse_analyze_fixedparams ()
#3  0x000055ce70ff269e in pg_analyze_and_rewrite_fixedparams ()
#4  0x000055ce70ff3393 in exec_simple_query ()
#5  0x000055ce70ff7934 in PostgresMain ()
#6  0x000055ce70f436f4 in ServerLoop.isra.0 ()
apache#7  0x000055ce70f4495a in PostmasterMain ()

Program received signal SIGSEGV, Segmentation fault.
0x000055ce70c57b14 in queryNodeSearch ()
#0  0x000055ce70c57b14 in queryNodeSearch ()
#1  0x000055ce70e12666 in raw_expression_tree_walker_impl ()
#2  0x000055ce70c58c9d in checkCanOptSelectLockingClause ()
#3  0x000055ce70c5c19b in transformTopLevelStmt ()
#4  0x000055ce70c5c2b1 in parse_analyze_fixedparams ()
#5  0x000055ce70ff269e in pg_analyze_and_rewrite_fixedparams ()
#6  0x000055ce70ff3393 in exec_simple_query ()
apache#7  0x000055ce70ff7934 in PostgresMain ()
apache#8  0x000055ce70f436f4 in ServerLoop.isra.0 ()
apache#9  0x000055ce70f4495a in PostmasterMain ()
#10 0x000055ce70b045e8 in main ()
[Inferior 1 (process 3042112) detached]
@leborchuk

Copy link
Copy Markdown
Contributor

PTAL issues found by claude:

  • [P1] Start the search at the WHERE node. raw_expression_tree_walker() invokes the callback on the root’s children, so a top-level SubLink never reaches queryNodeSearch(). The walk can finish with ctx.found still false, leaving the row-locking optimization enabled. Call queryNodeSearch(stmt->whereClause, &ctx) so the callback inspects the root before descending. analyze.c:4322 (src/backend/parser/analyze.c:4322)
  • [P2] Assert the lock mode in the isolation test. The new LIMIT 0 case can return zero rows even when the optimization stays enabled. Run the statement inside a transaction and use the existing show_locks_lockmodes view to verify that the target table takes ExclusiveLock, the fallback lock. lockmodes.sql:402 (src/test/isolation2/sql/lockmodes.sql:402)

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.

2 participants