Skip to content

Commit

Permalink
Minor performance update
Browse files Browse the repository at this point in the history
Cache Relation Key in TupleTableSlot to Optimize Tuple Decryption.
This commit cache the relation key directly in the TupleTableSlot to avoid
repeated key lookups for each tuple decryption.
  • Loading branch information
codeforall committed Sep 2, 2024
1 parent 0586660 commit de51c98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/access/pg_tde_slot.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ static inline void tdeheap_tts_buffer_heap_store_tuple(TupleTableSlot *slot,
HeapTuple tuple,
Buffer buffer,
bool transfer_pin);

static inline RelKeyData *get_current_slot_relation_key(TDEBufferHeapTupleTableSlot *bslot, Relation rel);
static void
tdeheap_tts_buffer_heap_init(TupleTableSlot *slot)
{
TDEBufferHeapTupleTableSlot *bslot = (TDEBufferHeapTupleTableSlot *) slot;
bslot->cached_relation_key = NULL;
}

static void
Expand Down Expand Up @@ -509,7 +510,7 @@ PGTdeExecStoreBufferHeapTuple(Relation rel,

if (rel->rd_rel->relkind != RELKIND_TOASTVALUE)
{
RelKeyData *key = GetRelationKey(rel->rd_locator);
RelKeyData *key = get_current_slot_relation_key(bslot, rel);
slot_copytuple(bslot->decrypted_buffer, tuple);
PG_TDE_DECRYPT_TUPLE_EX(tuple, (HeapTuple)bslot->decrypted_buffer, key, "ExecStoreBuffer");
tuple->t_data = ((HeapTuple)bslot->decrypted_buffer)->t_data;
Expand Down Expand Up @@ -547,7 +548,7 @@ PGTdeExecStorePinnedBufferHeapTuple(Relation rel,

if (rel->rd_rel->relkind != RELKIND_TOASTVALUE)
{
RelKeyData *key = GetRelationKey(rel->rd_locator);
RelKeyData *key = get_current_slot_relation_key(bslot,rel);

slot_copytuple(bslot->decrypted_buffer, tuple);
PG_TDE_DECRYPT_TUPLE_EX(tuple, (HeapTuple)bslot->decrypted_buffer, key, "ExecStorePinnedBuffer");
Expand All @@ -561,3 +562,12 @@ PGTdeExecStorePinnedBufferHeapTuple(Relation rel,

return slot;
}

static inline RelKeyData*
get_current_slot_relation_key(TDEBufferHeapTupleTableSlot *bslot, Relation rel)
{
Assert(bslot != NULL);
if (bslot->cached_relation_key == NULL)
bslot->cached_relation_key = GetRelationKey(rel->rd_locator);
return bslot->cached_relation_key;
}
8 changes: 5 additions & 3 deletions src/include/access/pg_tde_slot.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
#define PG_TDE_SLOT_H


#include "postgres.h"
#include "executor/tuptable.h"
#include "utils/relcache.h"
#include "postgres.h"
#include "executor/tuptable.h"
#include "access/pg_tde_tdemap.h"
#include "utils/relcache.h"

/* heap tuple residing in a buffer */
typedef struct TDEBufferHeapTupleTableSlot
Expand All @@ -30,6 +31,7 @@ typedef struct TDEBufferHeapTupleTableSlot
*/
Buffer buffer; /* tuple's buffer, or InvalidBuffer */
char decrypted_buffer[BLCKSZ];
RelKeyData *cached_relation_key;
} TDEBufferHeapTupleTableSlot;

extern PGDLLIMPORT const TupleTableSlotOps TTSOpsTDEBufferHeapTuple;
Expand Down

0 comments on commit de51c98

Please sign in to comment.