From 884ea16aa3f696ac57f8c46785c2a805b98129d3 Mon Sep 17 00:00:00 2001 From: openGaussDev Date: Mon, 7 Mar 2022 16:16:05 +0800 Subject: [PATCH] fix get_tuple Offering: openGaussDev More detail: fix get_tuple Match-id-aed00ca9fb0d67624e5de7d2448c57a3baf26eb0 --- .../storage/access/heap/tuptoaster.cpp | 30 +++++++++++++++++++ src/include/access/tuptoaster.h | 2 ++ 2 files changed, 32 insertions(+) diff --git a/src/gausskernel/storage/access/heap/tuptoaster.cpp b/src/gausskernel/storage/access/heap/tuptoaster.cpp index 4411f8af3..dc306fd61 100644 --- a/src/gausskernel/storage/access/heap/tuptoaster.cpp +++ b/src/gausskernel/storage/access/heap/tuptoaster.cpp @@ -44,6 +44,7 @@ #include "commands/vacuum.h" #include "utils/snapmgr.h" #include "mb/pg_wchar.h" +#include "access/tableam.h" #undef TOAST_DEBUG @@ -2900,3 +2901,32 @@ struct varlena * toast_pointer_fetch_data(TupleTableSlot* varSlot, Form_pg_attri return toast_pointer_lob; } +HeapTuple ctid_get_tuple(Relation relation, ItemPointer tid) +{ + Buffer user_buf = InvalidBuffer; + HeapTuple tuple = NULL; + HeapTuple new_tuple = NULL; + TM_Result result; + + /* alloc memory for old tuple and set tuple id */ + tuple = (HeapTupleData *)heaptup_alloc(BLCKSZ); + tuple->t_data = (HeapTupleHeader)((char *)tuple + HEAPTUPLESIZE); + Assert(tid != NULL); + tuple->t_self = *tid; + + if (tableam_tuple_fetch(relation, SnapshotAny, tuple, &user_buf, false, NULL)) { + result = HeapTupleSatisfiesUpdate(tuple, GetCurrentCommandId(true), user_buf, false); + if (result != TM_Ok) { + ereport(ERROR, (errcode(ERRCODE_SYSTEM_ERROR), errmsg("The tuple is updated, please use 'for update'."))); + } + + new_tuple = heapCopyTuple((HeapTuple)tuple, relation->rd_att, NULL); + ReleaseBuffer(user_buf); + } else { + heap_close(relation, NoLock); + ereport(ERROR, (errcode(ERRCODE_SYSTEM_ERROR), errmsg("The tuple is not found"))); + } + heap_freetuple(tuple); + + return new_tuple; +} diff --git a/src/include/access/tuptoaster.h b/src/include/access/tuptoaster.h index ddeb8ca07..09bd4cbdd 100644 --- a/src/include/access/tuptoaster.h +++ b/src/include/access/tuptoaster.h @@ -296,5 +296,7 @@ inline Datum fetch_real_lob_if_need(Datum toast_pointer) return ret; } +extern HeapTuple ctid_get_tuple(Relation relation, ItemPointer tid); + #endif /* TUPTOASTER_H */