diff --git a/src/access/pg_tdeam.c b/src/access/pg_tdeam.c index f47da2df..9d676eef 100644 --- a/src/access/pg_tdeam.c +++ b/src/access/pg_tdeam.c @@ -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(); @@ -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))) @@ -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); } /* diff --git a/src/access/pg_tdeam_handler.c b/src/access/pg_tdeam_handler.c index d5c229dd..3cd7e5ab 100644 --- a/src/access/pg_tdeam_handler.c +++ b/src/access/pg_tdeam_handler.c @@ -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) @@ -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) diff --git a/src/access/pg_tdetoast.c b/src/access/pg_tdetoast.c index 359340d5..e947b5b7 100644 --- a/src/access/pg_tdetoast.c +++ b/src/access/pg_tdetoast.c @@ -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; @@ -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) @@ -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. @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/include/access/pg_tdeam.h b/src/include/access/pg_tdeam.h index c25f285c..df3de661 100644 --- a/src/include/access/pg_tdeam.h +++ b/src/include/access/pg_tdeam.h @@ -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; @@ -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,