Skip to content

Commit

Permalink
Use options in pg_tde_insert for encryption
Browse files Browse the repository at this point in the history
...instead of bool

Also more comments.
  • Loading branch information
dAdAbird committed Jan 19, 2024
1 parent 01cc482 commit 5f98c73
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/access/pg_tdeam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ ReleaseBulkInsertStatePin(BulkInsertState bistate)
* reflected into *tup.
*/
void
pg_tde_insert(bool encrypt, Relation relation, HeapTuple tup, CommandId cid,
pg_tde_insert(Relation relation, HeapTuple tup, CommandId cid,
int options, BulkInsertState bistate)
{
TransactionId xid = GetCurrentTransactionId();
Expand Down Expand Up @@ -1885,7 +1885,8 @@ pg_tde_insert(bool encrypt, Relation relation, HeapTuple tup, CommandId cid,
/* NO EREPORT(ERROR) from here till changes are logged */
START_CRIT_SECTION();

pg_tde_RelationPutHeapTuple(relation, buffer, heaptup, encrypt,
pg_tde_RelationPutHeapTuple(relation, buffer, heaptup,
(options & HEAP_INSERT_TDE_NO_ENCRYPT) == 0,
(options & HEAP_INSERT_SPECULATIVE) != 0);

if (PageIsAllVisible(BufferGetPage(buffer)))
Expand Down Expand Up @@ -2478,7 +2479,7 @@ pg_tde_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples,
void
simple_pg_tde_insert(Relation relation, HeapTuple tup)
{
pg_tde_insert(true, relation, tup, GetCurrentCommandId(true), 0, NULL);
pg_tde_insert(relation, tup, GetCurrentCommandId(true), 0, NULL);
}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/access/pg_tdeam_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ pg_tdeam_tuple_insert(Relation relation, TupleTableSlot *slot, CommandId cid,
tuple->t_tableOid = slot->tts_tableOid;

/* Perform the insertion, and copy the resulting ItemPointer */
pg_tde_insert(true, relation, tuple, cid, options, bistate);
pg_tde_insert(relation, tuple, cid, options, bistate);
ItemPointerCopy(&tuple->t_self, &slot->tts_tid);

if (shouldFree)
Expand All @@ -282,7 +282,7 @@ pg_tdeam_tuple_insert_speculative(Relation relation, TupleTableSlot *slot,
options |= HEAP_INSERT_SPECULATIVE;

/* Perform the insertion, and copy the resulting ItemPointer */
pg_tde_insert(true, relation, tuple, cid, options, bistate);
pg_tde_insert(relation, tuple, cid, options, bistate);
ItemPointerCopy(&tuple->t_self, &slot->tts_tid);

if (shouldFree)
Expand Down
20 changes: 17 additions & 3 deletions src/access/pg_tdetoast.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,12 @@ pg_tde_toast_encrypt(Pointer dval, Oid valueid, RelKeysData *keys)
char* data_p;
char* encrypted_data;

if (VARATT_IS_SHORT(dval))
/*
* Encryption specific data_p and data_size as we have to avoid
* encryption of the compression info.
* See https://github.com/Percona-Lab/postgres-tde-ext/commit/dee6e357ef05d217a4c4df131249a80e5e909163
*/
if (VARATT_IS_SHORT(dval))
{
data_p = VARDATA_SHORT(dval);
data_size = VARSIZE_SHORT(dval) - VARHDRSZ_SHORT;
Expand All @@ -870,6 +875,8 @@ pg_tde_toast_encrypt(Pointer dval, Oid valueid, RelKeysData *keys)

/*
* Move an attribute to external storage.
*
* copy from PG src/backend/access/table/toast_helper.c
*/
static void
pg_tde_toast_tuple_externalize(ToastTupleContext *ttc, int attribute, int options)
Expand All @@ -888,7 +895,7 @@ pg_tde_toast_tuple_externalize(ToastTupleContext *ttc, int attribute, int option
}

/* ----------
* toast_save_datum -
* pg_tde_toast_save_datum -
*
* Save one single datum into the secondary relation and return
* a Datum reference for it.
Expand All @@ -898,6 +905,8 @@ pg_tde_toast_tuple_externalize(ToastTupleContext *ttc, int attribute, int option
* value: datum to be pushed to toast storage
* oldexternal: if not NULL, toast pointer previously representing the datum
* options: options to be passed to heap_insert() for toast rows
*
* based on toast_save_datum from PG src/backend/access/common/toast_internals.c
* ----------
*/
static Datum
Expand Down Expand Up @@ -1115,7 +1124,8 @@ pg_tde_toast_save_datum(Relation rel, Datum value,
* The tuple should be insterted not encrypted.
* TOAST data already encrypted.
*/
pg_tde_insert(false, toastrel, toasttup, mycid, options, NULL);
options |= HEAP_INSERT_TDE_NO_ENCRYPT;
pg_tde_insert(toastrel, toasttup, mycid, options, NULL);

/*
* Create the index entry. We cheat a little here by not using
Expand Down Expand Up @@ -1176,6 +1186,8 @@ pg_tde_toast_save_datum(Relation rel, Datum value,
* Test whether a toast value with the given ID exists in the toast relation.
* For safety, we consider a value to exist if there are either live or dead
* toast rows with that ID; see notes for GetNewOidWithIndex().
*
* copy from PG src/backend/access/common/toast_internals.c
* ----------
*/
static bool
Expand Down Expand Up @@ -1224,6 +1236,8 @@ toastrel_valueid_exists(Relation toastrel, Oid valueid)
* toastid_valueid_exists -
*
* As above, but work from toast rel's OID not an open relation
*
* copy from PG src/backend/access/common/toast_internals.c
* ----------
*/
static bool
Expand Down
11 changes: 6 additions & 5 deletions src/include/access/pg_tdeam.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@


/* "options" flag bits for pg_tde_insert */
#define HEAP_INSERT_SKIP_FSM TABLE_INSERT_SKIP_FSM
#define HEAP_INSERT_FROZEN TABLE_INSERT_FROZEN
#define HEAP_INSERT_NO_LOGICAL TABLE_INSERT_NO_LOGICAL
#define HEAP_INSERT_SPECULATIVE 0x0010
#define HEAP_INSERT_SKIP_FSM TABLE_INSERT_SKIP_FSM
#define HEAP_INSERT_FROZEN TABLE_INSERT_FROZEN
#define HEAP_INSERT_NO_LOGICAL TABLE_INSERT_NO_LOGICAL
#define HEAP_INSERT_SPECULATIVE 0x0010
#define HEAP_INSERT_TDE_NO_ENCRYPT 0x2000 /* to specify rare cases when NO TDE enc */

typedef struct BulkInsertStateData *BulkInsertState;
struct TupleTableSlot;
Expand Down Expand Up @@ -236,7 +237,7 @@ extern BulkInsertState GetBulkInsertState(void);
extern void FreeBulkInsertState(BulkInsertState);
extern void ReleaseBulkInsertStatePin(BulkInsertState bistate);

extern void pg_tde_insert(bool encrypt, Relation relation, HeapTuple tup, CommandId cid,
extern void pg_tde_insert(Relation relation, HeapTuple tup, CommandId cid,
int options, BulkInsertState bistate);
extern void pg_tde_multi_insert(Relation relation, struct TupleTableSlot **slots,
int ntuples, CommandId cid, int options,
Expand Down

0 comments on commit 5f98c73

Please sign in to comment.