Skip to content

Commit

Permalink
Test and other fixes for the smgr branch
Browse files Browse the repository at this point in the history
With this commit, old pg_tde code should work as before in the
same branch, and CI tests also should pass.
  • Loading branch information
dutow committed Jun 16, 2024
1 parent 8935125 commit a912c7f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/access/pg_tdeam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,10 +1111,10 @@ pg_tde_getnext(TableScanDesc sscan, ScanDirection direction)
* rather than the AM oid, is that this allows to write regression tests
* that create another AM reusing the heap handler.
*/
if (unlikely(sscan->rs_rd->rd_tableam != GetHeapamTableAmRoutine()))
if (unlikely(sscan->rs_rd->rd_tableam != GetPGTdeamTableAmRoutine()))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg_internal("only heap AM is supported")));
errmsg_internal("only pg_tde AM is supported")));

/*
* We don't expect direct calls to pg_tde_getnext with valid CheckXidAlive
Expand Down
6 changes: 6 additions & 0 deletions src/access/pg_tdeam_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2623,6 +2623,12 @@ static const TableAmRoutine pg_tdeam_methods = {
.scan_sample_next_tuple = pg_tdeam_scan_sample_next_tuple
};

const TableAmRoutine *
GetPGTdeamTableAmRoutine(void)
{
return &pg_tdeam_methods;
}

Datum
pg_tdeam_handler(PG_FUNCTION_ARGS)
{
Expand Down
26 changes: 25 additions & 1 deletion src/catalog/tde_master_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,26 @@ GetMasterKey(Oid dbOid)
LWLock *lock_files = tde_lwlock_mk_files();
LWLock *lock_cache = tde_lwlock_mk_cache();

// TODO: This recursion counter is a dirty hack until the metadata is in the catalog
// As otherwise we would call GetMasterKey recursively and deadlock
static int recursion = 0;

if(recursion > 0)
{
return NULL;
}

recursion++;

LWLockAcquire(lock_cache, LW_SHARED);
masterKey = get_master_key_from_cache(dbOid);
LWLockRelease(lock_cache);

if (masterKey)
{
recursion--;
return masterKey;
}

/*
* We should hold an exclusive lock here to ensure that a valid master key, if found, is added
Expand All @@ -244,6 +258,7 @@ GetMasterKey(Oid dbOid)
{
LWLockRelease(lock_cache);
LWLockRelease(lock_files);
recursion--;
return masterKey;
}

Expand All @@ -254,6 +269,7 @@ GetMasterKey(Oid dbOid)
LWLockRelease(lock_cache);
LWLockRelease(lock_files);

recursion--;
return NULL;
}

Expand All @@ -264,6 +280,7 @@ GetMasterKey(Oid dbOid)
LWLockRelease(lock_cache);
LWLockRelease(lock_files);

recursion--;
return NULL;
}

Expand All @@ -273,6 +290,7 @@ GetMasterKey(Oid dbOid)
LWLockRelease(lock_cache);
LWLockRelease(lock_files);

recursion--;
return NULL;
}

Expand All @@ -292,6 +310,7 @@ GetMasterKey(Oid dbOid)
if (masterKeyInfo)
pfree(masterKeyInfo);

recursion--;
return masterKey;
}

Expand Down Expand Up @@ -731,7 +750,12 @@ Datum pg_tde_master_key_info(PG_FUNCTION_ARGS)

master_key = GetMasterKey(MyDatabaseId);
if (master_key == NULL)
PG_RETURN_NULL();
{
ereport(ERROR,
(errmsg("Master key does not exists for the database"),
errhint("Use set_master_key interface to set the master key")));
PG_RETURN_NULL();
}

keyring = GetKeyProviderByID(master_key->keyInfo.keyringId);

Expand Down
3 changes: 3 additions & 0 deletions src/include/access/pg_tdeam.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,7 @@ extern void HeapCheckForSerializableConflictOut(bool visible, Relation relation,
/* Defined in pg_tdeam_handler.c */
extern bool is_pg_tde_rel(Relation rel);

const TableAmRoutine *
GetPGTdeamTableAmRoutine(void);

#endif /* PG_TDEAM_H */
2 changes: 1 addition & 1 deletion src/include/smgr/pg_tde_smgr.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

#pragma once

extern void RegisterStorageMgr();
extern void RegisterStorageMgr();
5 changes: 0 additions & 5 deletions src/pg_tde_event_capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,10 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
if (rel->rd_rel->relam == get_tde_table_am_oid())
{
/* We are creating the index on encrypted table */
ereport(NOTICE,
(errmsg("Creating index on **ENCRYPTED** relation:%s with Oid:%d", stmt->relation->relname, relationId)));
/* set the global state */
tdeCurrentCreateEvent.encryptMode = true;
}
else
ereport(DEBUG1,
(errmsg("Creating index on relation:%s with Oid:%d", stmt->relation->relname, relationId)));
table_close(rel, lockmode);
}
else
Expand All @@ -104,7 +100,6 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
tdeCurrentCreateEvent.eventType = TDE_TABLE_CREATE_EVENT;
tdeCurrentCreateEvent.relation = stmt->relation;

elog(DEBUG1, "CREATING TABLE %s Using Access Method %s", stmt->relation->relname, stmt->accessMethod);
if (stmt->accessMethod && !strcmp(stmt->accessMethod, "pg_tde2"))
{
tdeCurrentCreateEvent.encryptMode = true;
Expand Down
8 changes: 2 additions & 6 deletions src/smgr/pg_tde_smgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "access/pg_tde_tdemap.h"
#include "pg_tde_event_capture.h"

#if PG_VERSION_NUM >= 170000
#ifndef PERCONA_FORK

// TODO: implement proper IV
// iv should be based on blocknum + relfile, available in the API
Expand All @@ -21,10 +21,6 @@ tde_smgr_get_key(SMgrRelation reln)
// As otherwise we would call GetMasterKey recursively and deadlock
static int recursion = 0;

ereport(NOTICE,
(errmsg("Trying to decide if table is encrypted: %u", reln->smgr_rlocator.locator.relNumber)));


if(IsCatalogRelationOid(reln->smgr_rlocator.locator.relNumber))
{
// do not try to encrypt/decrypt catalog tables
Expand Down Expand Up @@ -214,4 +210,4 @@ void RegisterStorageMgr()
void RegisterStorageMgr()
{
}
#endif
#endif /* PERCONA_FORK */

0 comments on commit a912c7f

Please sign in to comment.