Skip to content

Commit

Permalink
Get rid off tablespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dAdAbird committed Nov 19, 2024
1 parent 74e6d08 commit a713cdc
Show file tree
Hide file tree
Showing 21 changed files with 41 additions and 58 deletions.
6 changes: 3 additions & 3 deletions src/access/pg_tde_tdemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pg_tde_create_key_map_entry(const RelFileLocator *newrlocator, uint32 entry_type
LWLock *lock_pk = tde_lwlock_enc_keys();

LWLockAcquire(lock_pk, LW_EXCLUSIVE);
principal_key = GetPrincipalKey(newrlocator->dbOid, newrlocator->spcOid, LW_EXCLUSIVE);
principal_key = GetPrincipalKey(newrlocator->dbOid, LW_EXCLUSIVE);
if (principal_key == NULL)
{
LWLockRelease(lock_pk);
Expand Down Expand Up @@ -875,7 +875,7 @@ pg_tde_move_rel_key(const RelFileLocator *newrlocator, const RelFileLocator *old

LWLockAcquire(tde_lwlock_enc_keys(), LW_EXCLUSIVE);

principal_key = GetPrincipalKey(oldrlocator->dbOid, oldrlocator->spcOid, LW_EXCLUSIVE);
principal_key = GetPrincipalKey(oldrlocator->dbOid, LW_EXCLUSIVE);
Assert(principal_key);

key_index = pg_tde_process_map_entry(oldrlocator, MAP_ENTRY_VALID, db_map_path, &offset, false);
Expand Down Expand Up @@ -946,7 +946,7 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type, bool n
* key.
*/
LWLockAcquire(lock_pk, LW_SHARED);
principal_key = GetPrincipalKey(rlocator->dbOid, rlocator->spcOid, LW_SHARED);
principal_key = GetPrincipalKey(rlocator->dbOid, LW_SHARED);
if (principal_key == NULL)
{
LWLockRelease(lock_pk);
Expand Down
2 changes: 1 addition & 1 deletion src/access/pg_tde_xlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ tdeheap_rmgr_desc(StringInfo buf, XLogReaderState *record)
{
XLogExtensionInstall *xlrec = (XLogExtensionInstall *) XLogRecGetData(record);

appendStringInfo(buf, "tde extension install for db %u/%u", xlrec->database_id, xlrec->tablespace_id);
appendStringInfo(buf, "tde extension install for db %u", xlrec->database_id);
}
if (info == XLOG_TDE_ROTATE_KEY)
{
Expand Down
2 changes: 1 addition & 1 deletion src/catalog/tde_global_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ init_default_keyring(void)
* TODO: should we remove it automaticaly on
* pg_tde_rotate_principal_key() ?
*/
save_new_key_provider_info(&provider, GLOBAL_DATA_TDE_OID, GLOBALTABLESPACE_OID, false);
save_new_key_provider_info(&provider, GLOBAL_DATA_TDE_OID, false);
elog(INFO,
"default keyring has been created for the global tablespace (WAL)."
" Change it with pg_tde_add_key_provider_* and run pg_tde_rotate_principal_key."
Expand Down
13 changes: 3 additions & 10 deletions src/catalog/tde_keyring.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ write_key_provider_info(KeyringProvideRecord *provider, Oid database_id,
* Save the key provider info to the file
*/
uint32
save_new_key_provider_info(KeyringProvideRecord* provider, Oid databaseId, Oid tablespaceId, bool write_xlog)
save_new_key_provider_info(KeyringProvideRecord* provider, Oid databaseId, bool write_xlog)
{
return write_key_provider_info(provider, databaseId, -1, true, write_xlog);
}
Expand Down Expand Up @@ -335,19 +335,12 @@ pg_tde_add_key_provider_internal(PG_FUNCTION_ARGS)
char *options = text_to_cstring(PG_GETARG_TEXT_PP(2));
bool is_global = PG_GETARG_BOOL(3);
KeyringProvideRecord provider;
Oid dbOid = MyDatabaseId;
Oid spcOid = MyDatabaseTableSpace;

if (is_global)
{
dbOid = GLOBAL_DATA_TDE_OID;
spcOid = GLOBALTABLESPACE_OID;
}
Oid dbOid = is_global ? GLOBAL_DATA_TDE_OID : MyDatabaseId;

strncpy(provider.options, options, sizeof(provider.options));
strncpy(provider.provider_name, provider_name, sizeof(provider.provider_name));
provider.provider_type = get_keyring_provider_from_typename(provider_type);
save_new_key_provider_info(&provider, dbOid, spcOid, true);
save_new_key_provider_info(&provider, dbOid, true);

PG_RETURN_INT32(provider.provider_id);
}
Expand Down
26 changes: 11 additions & 15 deletions src/catalog/tde_principal_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ static void shared_memory_shutdown(int code, Datum arg);
static void principal_key_startup_cleanup(int tde_tbl_count, XLogExtensionInstall *ext_info, bool redo, void *arg);
static void clear_principal_key_cache(Oid databaseId);
static inline dshash_table *get_principal_key_Hash(void);
static TDEPrincipalKey *get_principal_key_from_keyring(Oid dbOid, Oid spcOid);
static TDEPrincipalKey *get_principal_key_from_keyring(Oid dbOid);
static TDEPrincipalKey *get_principal_key_from_cache(Oid dbOid);
static void push_principal_key_to_cache(TDEPrincipalKey *principalKey);
static Datum pg_tde_get_key_info(PG_FUNCTION_ARGS, Oid dbOid, Oid spcOid);
static Datum pg_tde_get_key_info(PG_FUNCTION_ARGS, Oid dbOid);
static keyInfo *load_latest_versioned_key_name(TDEPrincipalKeyInfo *principal_key_info,
GenericKeyring *keyring,
bool ensure_new_key);
Expand Down Expand Up @@ -644,7 +644,6 @@ pg_tde_rotate_principal_key_internal(PG_FUNCTION_ARGS)
bool ret;
TDEPrincipalKey *current_key;
Oid dbOid = MyDatabaseId;
Oid spcOid = MyDatabaseTableSpace;

if (!PG_ARGISNULL(0))
new_principal_key_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
Expand All @@ -657,7 +656,6 @@ pg_tde_rotate_principal_key_internal(PG_FUNCTION_ARGS)
if (is_global)
{
dbOid = GLOBAL_DATA_TDE_OID;
spcOid = GLOBALTABLESPACE_OID;
}
#endif

Expand All @@ -667,7 +665,7 @@ pg_tde_rotate_principal_key_internal(PG_FUNCTION_ARGS)
is_global ? "cluster" : "database")));

LWLockAcquire(tde_lwlock_enc_keys(), LW_EXCLUSIVE);
current_key = GetPrincipalKey(dbOid, spcOid, LW_EXCLUSIVE);
current_key = GetPrincipalKey(dbOid, LW_EXCLUSIVE);
ret = RotatePrincipalKey(current_key, new_principal_key_name, new_provider_name, ensure_new_key);
LWLockRelease(tde_lwlock_enc_keys());

Expand All @@ -679,20 +677,18 @@ Datum
pg_tde_principal_key_info_internal(PG_FUNCTION_ARGS)
{
Oid dbOid = MyDatabaseId;
Oid spcOid = MyDatabaseTableSpace;
bool is_global = PG_GETARG_BOOL(0);

if (is_global)
{
dbOid = GLOBAL_DATA_TDE_OID;
spcOid = GLOBALTABLESPACE_OID;
}

return pg_tde_get_key_info(fcinfo, dbOid, spcOid);
return pg_tde_get_key_info(fcinfo, dbOid);
}

static Datum
pg_tde_get_key_info(PG_FUNCTION_ARGS, Oid dbOid, Oid spcOid)
pg_tde_get_key_info(PG_FUNCTION_ARGS, Oid dbOid)
{
TupleDesc tupdesc;
Datum values[6];
Expand All @@ -710,7 +706,7 @@ pg_tde_get_key_info(PG_FUNCTION_ARGS, Oid dbOid, Oid spcOid)
errmsg("function returning record called in context that cannot accept type record")));

LWLockAcquire(tde_lwlock_enc_keys(), LW_SHARED);
principal_key = GetPrincipalKey(dbOid, spcOid, LW_SHARED);
principal_key = GetPrincipalKey(dbOid, LW_SHARED);
LWLockRelease(tde_lwlock_enc_keys());
if (principal_key == NULL)
{
Expand Down Expand Up @@ -767,7 +763,7 @@ pg_tde_get_key_info(PG_FUNCTION_ARGS, Oid dbOid, Oid spcOid)
* Caller should hold an exclusive tde_lwlock_enc_keys lock
*/
TDEPrincipalKey *
get_principal_key_from_keyring(Oid dbOid, Oid spcOid)
get_principal_key_from_keyring(Oid dbOid)
{
GenericKeyring *keyring;
TDEPrincipalKey *principalKey = NULL;
Expand Down Expand Up @@ -806,7 +802,7 @@ get_principal_key_from_keyring(Oid dbOid, Oid spcOid)

#ifndef FRONTEND
/* We don't store global space key in cache */
if (spcOid != GLOBALTABLESPACE_OID)
if (!TDEisInGlobalSpace(dbOid))
{
push_principal_key_to_cache(principalKey);

Expand Down Expand Up @@ -840,14 +836,14 @@ get_principal_key_from_keyring(Oid dbOid, Oid spcOid)
* cache.
*/
TDEPrincipalKey *
GetPrincipalKey(Oid dbOid, Oid spcOid, LWLockMode lockMode)
GetPrincipalKey(Oid dbOid, LWLockMode lockMode)
{
#ifndef FRONTEND
TDEPrincipalKey *principalKey = NULL;

Assert(LWLockHeldByMeInMode(tde_lwlock_enc_keys(), lockMode));
/* We don't store global space key in cache */
if (spcOid != GLOBALTABLESPACE_OID)
if (!TDEisInGlobalSpace(dbOid))
{
principalKey = get_principal_key_from_cache(dbOid);
}
Expand All @@ -864,5 +860,5 @@ GetPrincipalKey(Oid dbOid, Oid spcOid, LWLockMode lockMode)
}
#endif

return get_principal_key_from_keyring(dbOid, spcOid);
return get_principal_key_from_keyring(dbOid);
}
1 change: 0 additions & 1 deletion src/encryption/enc_tde.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ pg_tde_crypt_tuple(HeapTuple tuple, HeapTuple out_tuple, RelKeyData *key, const

OffsetNumber
PGTdePageAddItemExtended(RelFileLocator rel,
Oid oid,
BlockNumber bn,
Page page,
Item item,
Expand Down
4 changes: 3 additions & 1 deletion src/include/catalog/tde_global_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* We take Oids of the sql operators, so there is no overlap with the "real"
* catalog objects possible.
*/
#define GLOBAL_DATA_TDE_OID InvalidOid
#define GLOBAL_DATA_TDE_OID 607
#define XLOG_TDE_OID 608

#define GLOBAL_SPACE_RLOCATOR(_obj_oid) (RelFileLocator) { \
Expand All @@ -30,6 +30,8 @@
_obj_oid \
}

#define TDEisInGlobalSpace(dbOid) (dbOid == GLOBAL_DATA_TDE_OID)

extern void TDEInitGlobalKeys(const char *dir);

#endif /* TDE_GLOBAL_CATALOG_H */
3 changes: 1 addition & 2 deletions src/include/catalog/tde_keyring.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ extern ProviderType get_keyring_provider_from_typename(char *provider_type);
extern void cleanup_key_provider_info(Oid databaseId);
extern void InitializeKeyProviderInfo(void);
extern uint32 save_new_key_provider_info(KeyringProvideRecord *provider,
Oid databaseId, Oid tablespaceId,
bool write_xlog);
Oid databaseId, bool write_xlog);
extern uint32 redo_key_provider_info(KeyringProviderXLRecord *xlrec);

extern bool ParseKeyringJSONOptions(ProviderType provider_type, void *out_opts,
Expand Down
4 changes: 2 additions & 2 deletions src/include/catalog/tde_principal_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ extern void cleanup_principal_key_info(Oid databaseId);

#ifndef FRONTEND
extern LWLock *tde_lwlock_enc_keys(void);
extern TDEPrincipalKey *GetPrincipalKey(Oid dbOid, Oid spcOid, LWLockMode lockMode);
extern TDEPrincipalKey *GetPrincipalKey(Oid dbOid, LWLockMode lockMode);
#else
extern TDEPrincipalKey *GetPrincipalKey(Oid dbOid, Oid spcOid, void *lockMode);
extern TDEPrincipalKey *GetPrincipalKey(Oid dbOid, void *lockMode);
#endif

extern bool save_principal_key_info(TDEPrincipalKeyInfo *principalKeyInfo);
Expand Down
2 changes: 1 addition & 1 deletion src/include/encryption/enc_tde.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern void

/* A wrapper to encrypt a tuple before adding it to the buffer */
extern OffsetNumber
PGTdePageAddItemExtended(RelFileLocator rel, Oid oid, BlockNumber bn, Page page,
PGTdePageAddItemExtended(RelFileLocator rel, BlockNumber bn, Page page,
Item item,
Size size,
OffsetNumber offsetNumber,
Expand Down
1 change: 0 additions & 1 deletion src/include/pg_tde.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
typedef struct XLogExtensionInstall
{
Oid database_id;
Oid tablespace_id;
} XLogExtensionInstall;

typedef void (*pg_tde_on_ext_install_callback) (int tde_tbl_count, XLogExtensionInstall *ext_info, bool redo, void *arg);
Expand Down
4 changes: 2 additions & 2 deletions src/include/pg_tde_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
#define pgstat_count_tdeheap_delete pgstat_count_heap_delete
#define pgstat_count_tdeheap_insert pgstat_count_heap_insert

#define TDE_PageAddItem(rel, oid, blkno, page, item, size, offsetNumber, overwrite, is_heap) \
PGTdePageAddItemExtended(rel, oid, blkno, page, item, size, offsetNumber, \
#define TDE_PageAddItem(rel, blkno, page, item, size, offsetNumber, overwrite, is_heap) \
PGTdePageAddItemExtended(rel, blkno, page, item, size, offsetNumber, \
((overwrite) ? PAI_OVERWRITE : 0) | \
((is_heap) ? PAI_IS_HEAP : 0))

Expand Down
1 change: 0 additions & 1 deletion src/pg_tde.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ pg_tde_extension_initialize(PG_FUNCTION_ARGS)
XLogExtensionInstall xlrec;

xlrec.database_id = MyDatabaseId;
xlrec.tablespace_id = MyDatabaseTableSpace;
run_extension_install_callbacks(&xlrec, false);

/*
Expand Down
8 changes: 2 additions & 6 deletions src/pg_tde_event_capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
{
CreateStmt *stmt = (CreateStmt *) parsetree;
TDEPrincipalKey *principal_key;
Oid tablespace_oid;

tdeCurrentCreateEvent.eventType = TDE_TABLE_CREATE_EVENT;
tdeCurrentCreateEvent.relation = stmt->relation;
Expand All @@ -118,10 +117,8 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)

if (tdeCurrentCreateEvent.encryptMode)
{
tablespace_oid = stmt->tablespacename != NULL ? get_tablespace_oid(stmt->tablespacename, false)
: MyDatabaseTableSpace;
LWLockAcquire(tde_lwlock_enc_keys(), LW_SHARED);
principal_key = GetPrincipalKey(MyDatabaseId, tablespace_oid, LW_SHARED);
principal_key = GetPrincipalKey(MyDatabaseId, LW_SHARED);
LWLockRelease(tde_lwlock_enc_keys());
if (principal_key == NULL)
{
Expand Down Expand Up @@ -162,11 +159,10 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
TDEPrincipalKey * principal_key;
Oid relationId = RangeVarGetRelid(stmt->relation, NoLock, true);
Relation rel = table_open(relationId, lockmode);
Oid tablespace_oid = rel->rd_locator.spcOid;
table_close(rel, lockmode);

LWLockAcquire(tde_lwlock_enc_keys(), LW_SHARED);
principal_key = GetPrincipalKey(MyDatabaseId, tablespace_oid, LW_SHARED);
principal_key = GetPrincipalKey(MyDatabaseId, LW_SHARED);
LWLockRelease(tde_lwlock_enc_keys());
if (principal_key == NULL)
{
Expand Down
2 changes: 1 addition & 1 deletion src/smgr/pg_tde_smgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ tde_smgr_get_key(SMgrRelation reln, RelFileLocator* old_locator)
}

LWLockAcquire(tde_lwlock_enc_keys(), LW_SHARED);
pk = GetPrincipalKey(reln->smgr_rlocator.locator.dbOid, reln->smgr_rlocator.locator.spcOid, LW_SHARED);
pk = GetPrincipalKey(reln->smgr_rlocator.locator.dbOid, LW_SHARED);
LWLockRelease(tde_lwlock_enc_keys());
if (pk == NULL)
{
Expand Down
2 changes: 1 addition & 1 deletion src16/access/pg_tde_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ tdeheap_RelationPutHeapTuple(Relation relation,
pageHeader = BufferGetPage(buffer);

if (encrypt)
offnum = TDE_PageAddItem(relation->rd_locator, tuple->t_tableOid, BufferGetBlockNumber(buffer), pageHeader, (Item) tuple->t_data,
offnum = TDE_PageAddItem(relation->rd_locator, BufferGetBlockNumber(buffer), pageHeader, (Item) tuple->t_data,
tuple->t_len, InvalidOffsetNumber, false, true);
else
offnum = PageAddItem(pageHeader, (Item) tuple->t_data,
Expand Down
2 changes: 1 addition & 1 deletion src16/access/pg_tde_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ raw_tdeheap_insert(RewriteState state, HeapTuple tup)
}

/* And now we can insert the tuple into the page */
newoff = TDE_PageAddItem(state->rs_new_rel->rd_locator, heaptup->t_tableOid, state->rs_blockno, page, (Item) heaptup->t_data, heaptup->t_len,
newoff = TDE_PageAddItem(state->rs_new_rel->rd_locator, state->rs_blockno, page, (Item) heaptup->t_data, heaptup->t_len,
InvalidOffsetNumber, false, true);
if (newoff == InvalidOffsetNumber)
elog(ERROR, "failed to add tuple");
Expand Down
6 changes: 3 additions & 3 deletions src16/access/pg_tdeam.c
Original file line number Diff line number Diff line change
Expand Up @@ -9382,7 +9382,7 @@ tdeheap_xlog_insert(XLogReaderState *record)
HeapTupleHeaderSetCmin(htup, FirstCommandId);
htup->t_ctid = target_tid;

if (TDE_PageAddItem(target_locator, target_locator.spcOid, blkno, page, (Item) htup, newlen, xlrec->offnum,
if (TDE_PageAddItem(target_locator, blkno, page, (Item) htup, newlen, xlrec->offnum,
true, true) == InvalidOffsetNumber)
elog(PANIC, "failed to add tuple");

Expand Down Expand Up @@ -9526,7 +9526,7 @@ tdeheap_xlog_multi_insert(XLogReaderState *record)
ItemPointerSetBlockNumber(&htup->t_ctid, blkno);
ItemPointerSetOffsetNumber(&htup->t_ctid, offnum);

offnum = TDE_PageAddItem(rlocator, rlocator.spcOid, blkno, page, (Item) htup, newlen, offnum, true, true);
offnum = TDE_PageAddItem(rlocator, blkno, page, (Item) htup, newlen, offnum, true, true);
if (offnum == InvalidOffsetNumber)
elog(PANIC, "failed to add tuple");
}
Expand Down Expand Up @@ -9800,7 +9800,7 @@ tdeheap_xlog_update(XLogReaderState *record, bool hot_update)
/* Make sure there is no forward chain link in t_ctid */
htup->t_ctid = newtid;

offnum = TDE_PageAddItem(rlocator, rlocator.spcOid, newblk, page, (Item) htup, newlen, offnum, true, true);
offnum = TDE_PageAddItem(rlocator, newblk, page, (Item) htup, newlen, offnum, true, true);
if (offnum == InvalidOffsetNumber)
elog(PANIC, "failed to add tuple");

Expand Down
2 changes: 1 addition & 1 deletion src17/access/pg_tde_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ tdeheap_RelationPutHeapTuple(Relation relation,
pageHeader = BufferGetPage(buffer);

if (encrypt)
offnum = TDE_PageAddItem(relation->rd_locator, tuple->t_tableOid, BufferGetBlockNumber(buffer), pageHeader, (Item) tuple->t_data,
offnum = TDE_PageAddItem(relation->rd_locator, BufferGetBlockNumber(buffer), pageHeader, (Item) tuple->t_data,
tuple->t_len, InvalidOffsetNumber, false, true);
else
offnum = PageAddItem(pageHeader, (Item) tuple->t_data,
Expand Down
2 changes: 1 addition & 1 deletion src17/access/pg_tde_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ raw_tdeheap_insert(RewriteState state, HeapTuple tup)
}

/* And now we can insert the tuple into the page */
newoff = TDE_PageAddItem(state->rs_new_rel->rd_locator, heaptup->t_tableOid, state->rs_blockno, page, (Item) heaptup->t_data, heaptup->t_len,
newoff = TDE_PageAddItem(state->rs_new_rel->rd_locator, state->rs_blockno, page, (Item) heaptup->t_data, heaptup->t_len,
InvalidOffsetNumber, false, true);
if (newoff == InvalidOffsetNumber)
elog(ERROR, "failed to add tuple");
Expand Down
Loading

0 comments on commit a713cdc

Please sign in to comment.