interconnect: prune cursor IC history table by local xid, not distributed xid - #1964
interconnect: prune cursor IC history table by local xid, not distributed xid#1964yjhjstz wants to merge 1 commit into
Conversation
…uted xid SetupUDPIFCInterconnect_Internal() records one CursorICHistoryEntry per interconnect instance on the QD and prunes old entries only when the statement runs in a different transaction than the previous one, using the distributed xid as the transaction identity. Distributed xids are assigned lazily (only for transactions that write or are dispatched two-phase), so autocommit read-only statements always see InvalidDistributedTransactionId on both sides of the comparison. The table is therefore never pruned on read-only sessions and grows by one 48-byte entry per Motion-containing statement for the life of the backend (2.8 MB after 60000 statements, plus the heap fragmentation the steady stream of small long-lived allocations induces). Use MyProc->lxid instead: it is assigned to every top-level transaction, including read-only ones, and stays constant within a transaction block, so entries belonging to cursors that are still open in the current transaction remain protected exactly as before. Verified on a single long-lived session running the queries from apache#1947: UdpInterconnectMemContext grew 0 -> 2814 KB over 60000 statements before the change and stays at 12 KB after it; RssAnon went 12.7 -> 24.6 MB before and is flat at 14.4 MB after. Pruning still does not happen inside an explicit transaction block or while a cursor is open, and FETCH from an open cursor after 300 intervening statements works. Related: apache#1947
leborchuk
left a comment
There was a problem hiding this comment.
LGTM,
Changing the pruning rule must not destroy is the history entry of an interconnect instance belonging to a still-open cursor.
And this is true:
- Non-holdable cursors can only exist inside an explicit transaction block (commands/portalcmds.c:77-78, RequireTransactionBlock), where lxid is constant → no prune.
- WITH HOLD cursors are drained into a tuplestore at commit (PersistHoldablePortal, portalcmds.c:424) and the executor is shut down, so no live interconnect crosses the boundary.
Also here we fixed other bug with cursors:
BEGIN;
DECLARE c CURSOR FOR ; -- dxid still Invalid
FETCH 1 FROM c;
INSERT ...; -- setupDtxTransaction() assigns gxid N
Do something with ; -- Invalid != N → prune fires, c still open, got race here
Maybe we should add here regression tests, but I'm not sure if they are valid for our case, we could add it and be sure that covered the issue. So in overall LGTM
I tested cursor case, It works fine. But hard to add to regression test. |
SetupUDPIFCInterconnect_Internal() records one CursorICHistoryEntry per interconnect instance on the QD and prunes old entries only when the statement runs in a different transaction than the previous one, using the distributed xid as the transaction identity. Distributed xids are assigned lazily (only for transactions that write or are dispatched two-phase), so autocommit read-only statements always see InvalidDistributedTransactionId on both sides of the comparison. The table is therefore never pruned on read-only sessions and grows by one 48-byte entry per Motion-containing statement for the life of the backend (2.8 MB after 60000 statements, plus the heap fragmentation the steady stream of small long-lived allocations induces).
Use MyProc->lxid instead: it is assigned to every top-level transaction, including read-only ones, and stays constant within a transaction block, so entries belonging to cursors that are still open in the current transaction remain protected exactly as before.
Verified on a single long-lived session running the queries from #1947: UdpInterconnectMemContext grew 0 -> 2814 KB over 60000 statements before the change and stays at 12 KB after it; RssAnon went 12.7 -> 24.6 MB before and is flat at 14.4 MB after. Pruning still does not happen inside an explicit transaction block or while a cursor is open, and FETCH from an open cursor after 300 intervening statements works.
Fixes #1947
What does this PR do?
Type of Change
Breaking Changes
Test Plan
make installcheckmake -C src/test installcheck-cbdb-parallelImpact
Performance:
User-facing changes:
Dependencies:
Checklist
Additional Context
CI Skip Instructions