Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into PG-1095-check-workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
artemgavrilov committed Oct 31, 2024
2 parents 71d6581 + c5f2fbb commit 8558225
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ trigger_on_view_basic \
change_access_method_basic \
insert_update_delete_basic \
keyprovider_dependency_basic \
vault_v2_test_basic
vault_v2_test_basic \
alter_index_basic
TAP_TESTS = 1

OBJS = src/encryption/enc_tde.o \
Expand Down
73 changes: 73 additions & 0 deletions expected/alter_index.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
\set tde_am tde_heap
\i sql/alter_index.inc
CREATE EXTENSION pg_tde;
SELECT pg_tde_add_key_provider_file('file-vault','/tmp/pg_tde_test_keyring.per');
pg_tde_add_key_provider_file
------------------------------
1
(1 row)

SELECT pg_tde_set_principal_key('test-db-principal-key','file-vault');
pg_tde_set_principal_key
--------------------------
t
(1 row)

SET default_table_access_method = :"tde_am";
CREATE TABLE concur_reindex_part (c1 int, c2 int) PARTITION BY RANGE (c1);
CREATE TABLE concur_reindex_part_0 PARTITION OF concur_reindex_part
FOR VALUES FROM (0) TO (10) PARTITION BY list (c2);
CREATE TABLE concur_reindex_part_0_1 PARTITION OF concur_reindex_part_0
FOR VALUES IN (1);
CREATE TABLE concur_reindex_part_0_2 PARTITION OF concur_reindex_part_0
FOR VALUES IN (2);
-- This partitioned table will have no partitions.
CREATE TABLE concur_reindex_part_10 PARTITION OF concur_reindex_part
FOR VALUES FROM (10) TO (20) PARTITION BY list (c2);
-- Create some partitioned indexes
CREATE INDEX concur_reindex_part_index ON ONLY concur_reindex_part (c1);
CREATE INDEX concur_reindex_part_index_0 ON ONLY concur_reindex_part_0 (c1);
ALTER INDEX concur_reindex_part_index ATTACH PARTITION concur_reindex_part_index_0;
-- This partitioned index will have no partitions.
CREATE INDEX concur_reindex_part_index_10 ON ONLY concur_reindex_part_10 (c1);
ALTER INDEX concur_reindex_part_index ATTACH PARTITION concur_reindex_part_index_10;
CREATE INDEX concur_reindex_part_index_0_1 ON ONLY concur_reindex_part_0_1 (c1);
ALTER INDEX concur_reindex_part_index_0 ATTACH PARTITION concur_reindex_part_index_0_1;
CREATE INDEX concur_reindex_part_index_0_2 ON ONLY concur_reindex_part_0_2 (c1);
ALTER INDEX concur_reindex_part_index_0 ATTACH PARTITION concur_reindex_part_index_0_2;
SELECT relid, parentrelid, level FROM pg_partition_tree('concur_reindex_part_index')
ORDER BY relid, level;
relid | parentrelid | level
-------------------------------+-----------------------------+-------
concur_reindex_part_index | | 0
concur_reindex_part_index_0 | concur_reindex_part_index | 1
concur_reindex_part_index_10 | concur_reindex_part_index | 1
concur_reindex_part_index_0_1 | concur_reindex_part_index_0 | 2
concur_reindex_part_index_0_2 | concur_reindex_part_index_0 | 2
(5 rows)

SELECT relid, parentrelid, level FROM pg_partition_tree('concur_reindex_part_index')
ORDER BY relid, level;
relid | parentrelid | level
-------------------------------+-----------------------------+-------
concur_reindex_part_index | | 0
concur_reindex_part_index_0 | concur_reindex_part_index | 1
concur_reindex_part_index_10 | concur_reindex_part_index | 1
concur_reindex_part_index_0_1 | concur_reindex_part_index_0 | 2
concur_reindex_part_index_0_2 | concur_reindex_part_index_0 | 2
(5 rows)

SELECT relid, parentrelid, level FROM pg_partition_tree('concur_reindex_part_index')
ORDER BY relid, level;
relid | parentrelid | level
-------------------------------+-----------------------------+-------
concur_reindex_part_index | | 0
concur_reindex_part_index_0 | concur_reindex_part_index | 1
concur_reindex_part_index_10 | concur_reindex_part_index | 1
concur_reindex_part_index_0_1 | concur_reindex_part_index_0 | 2
concur_reindex_part_index_0_2 | concur_reindex_part_index_0 | 2
(5 rows)

DROP TABLE concur_reindex_part;
DROP EXTENSION pg_tde;
RESET default_table_access_method;
73 changes: 73 additions & 0 deletions expected/alter_index_basic.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
\set tde_am tde_heap_basic
\i sql/alter_index.inc
CREATE EXTENSION pg_tde;
SELECT pg_tde_add_key_provider_file('file-vault','/tmp/pg_tde_test_keyring.per');
pg_tde_add_key_provider_file
------------------------------
1
(1 row)

SELECT pg_tde_set_principal_key('test-db-principal-key','file-vault');
pg_tde_set_principal_key
--------------------------
t
(1 row)

SET default_table_access_method = :"tde_am";
CREATE TABLE concur_reindex_part (c1 int, c2 int) PARTITION BY RANGE (c1);
CREATE TABLE concur_reindex_part_0 PARTITION OF concur_reindex_part
FOR VALUES FROM (0) TO (10) PARTITION BY list (c2);
CREATE TABLE concur_reindex_part_0_1 PARTITION OF concur_reindex_part_0
FOR VALUES IN (1);
CREATE TABLE concur_reindex_part_0_2 PARTITION OF concur_reindex_part_0
FOR VALUES IN (2);
-- This partitioned table will have no partitions.
CREATE TABLE concur_reindex_part_10 PARTITION OF concur_reindex_part
FOR VALUES FROM (10) TO (20) PARTITION BY list (c2);
-- Create some partitioned indexes
CREATE INDEX concur_reindex_part_index ON ONLY concur_reindex_part (c1);
CREATE INDEX concur_reindex_part_index_0 ON ONLY concur_reindex_part_0 (c1);
ALTER INDEX concur_reindex_part_index ATTACH PARTITION concur_reindex_part_index_0;
-- This partitioned index will have no partitions.
CREATE INDEX concur_reindex_part_index_10 ON ONLY concur_reindex_part_10 (c1);
ALTER INDEX concur_reindex_part_index ATTACH PARTITION concur_reindex_part_index_10;
CREATE INDEX concur_reindex_part_index_0_1 ON ONLY concur_reindex_part_0_1 (c1);
ALTER INDEX concur_reindex_part_index_0 ATTACH PARTITION concur_reindex_part_index_0_1;
CREATE INDEX concur_reindex_part_index_0_2 ON ONLY concur_reindex_part_0_2 (c1);
ALTER INDEX concur_reindex_part_index_0 ATTACH PARTITION concur_reindex_part_index_0_2;
SELECT relid, parentrelid, level FROM pg_partition_tree('concur_reindex_part_index')
ORDER BY relid, level;
relid | parentrelid | level
-------------------------------+-----------------------------+-------
concur_reindex_part_index | | 0
concur_reindex_part_index_0 | concur_reindex_part_index | 1
concur_reindex_part_index_10 | concur_reindex_part_index | 1
concur_reindex_part_index_0_1 | concur_reindex_part_index_0 | 2
concur_reindex_part_index_0_2 | concur_reindex_part_index_0 | 2
(5 rows)

SELECT relid, parentrelid, level FROM pg_partition_tree('concur_reindex_part_index')
ORDER BY relid, level;
relid | parentrelid | level
-------------------------------+-----------------------------+-------
concur_reindex_part_index | | 0
concur_reindex_part_index_0 | concur_reindex_part_index | 1
concur_reindex_part_index_10 | concur_reindex_part_index | 1
concur_reindex_part_index_0_1 | concur_reindex_part_index_0 | 2
concur_reindex_part_index_0_2 | concur_reindex_part_index_0 | 2
(5 rows)

SELECT relid, parentrelid, level FROM pg_partition_tree('concur_reindex_part_index')
ORDER BY relid, level;
relid | parentrelid | level
-------------------------------+-----------------------------+-------
concur_reindex_part_index | | 0
concur_reindex_part_index_0 | concur_reindex_part_index | 1
concur_reindex_part_index_10 | concur_reindex_part_index | 1
concur_reindex_part_index_0_1 | concur_reindex_part_index_0 | 2
concur_reindex_part_index_0_2 | concur_reindex_part_index_0 | 2
(5 rows)

DROP TABLE concur_reindex_part;
DROP EXTENSION pg_tde;
RESET default_table_access_method;
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ sql_tests = [
'change_access_method_basic',
'insert_update_delete_basic',
'vault_v2_test_basic',
'alter_index_basic',
]

tap_tests = [
Expand Down Expand Up @@ -125,6 +126,7 @@ if get_variable('percona_ext', false)
'change_access_method',
'insert_update_delete',
'vault_v2_test',
'alter_index',
]

tap_tests += [
Expand Down
37 changes: 37 additions & 0 deletions sql/alter_index.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
CREATE EXTENSION pg_tde;

SELECT pg_tde_add_key_provider_file('file-vault','/tmp/pg_tde_test_keyring.per');
SELECT pg_tde_set_principal_key('test-db-principal-key','file-vault');

SET default_table_access_method = :"tde_am";

CREATE TABLE concur_reindex_part (c1 int, c2 int) PARTITION BY RANGE (c1);
CREATE TABLE concur_reindex_part_0 PARTITION OF concur_reindex_part
FOR VALUES FROM (0) TO (10) PARTITION BY list (c2);
CREATE TABLE concur_reindex_part_0_1 PARTITION OF concur_reindex_part_0
FOR VALUES IN (1);
CREATE TABLE concur_reindex_part_0_2 PARTITION OF concur_reindex_part_0
FOR VALUES IN (2);
-- This partitioned table will have no partitions.
CREATE TABLE concur_reindex_part_10 PARTITION OF concur_reindex_part
FOR VALUES FROM (10) TO (20) PARTITION BY list (c2);
-- Create some partitioned indexes
CREATE INDEX concur_reindex_part_index ON ONLY concur_reindex_part (c1);
CREATE INDEX concur_reindex_part_index_0 ON ONLY concur_reindex_part_0 (c1);
ALTER INDEX concur_reindex_part_index ATTACH PARTITION concur_reindex_part_index_0;
-- This partitioned index will have no partitions.
CREATE INDEX concur_reindex_part_index_10 ON ONLY concur_reindex_part_10 (c1);
ALTER INDEX concur_reindex_part_index ATTACH PARTITION concur_reindex_part_index_10;
CREATE INDEX concur_reindex_part_index_0_1 ON ONLY concur_reindex_part_0_1 (c1);
ALTER INDEX concur_reindex_part_index_0 ATTACH PARTITION concur_reindex_part_index_0_1;
CREATE INDEX concur_reindex_part_index_0_2 ON ONLY concur_reindex_part_0_2 (c1);
ALTER INDEX concur_reindex_part_index_0 ATTACH PARTITION concur_reindex_part_index_0_2;
SELECT relid, parentrelid, level FROM pg_partition_tree('concur_reindex_part_index')
ORDER BY relid, level;
SELECT relid, parentrelid, level FROM pg_partition_tree('concur_reindex_part_index')
ORDER BY relid, level;
SELECT relid, parentrelid, level FROM pg_partition_tree('concur_reindex_part_index')
ORDER BY relid, level;
DROP TABLE concur_reindex_part;
DROP EXTENSION pg_tde;
RESET default_table_access_method;
2 changes: 2 additions & 0 deletions sql/alter_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
\set tde_am tde_heap
\i sql/alter_index.inc
2 changes: 2 additions & 0 deletions sql/alter_index_basic.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
\set tde_am tde_heap_basic
\i sql/alter_index.inc
18 changes: 10 additions & 8 deletions src/pg_tde_event_capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,15 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
{
LOCKMODE lockmode = AccessShareLock; /* TODO. Verify lock mode? */
AlterTableStmt *stmt = (AlterTableStmt *) parsetree;
TDEPrincipalKey *principal_key;
Oid relationId = RangeVarGetRelid(stmt->relation, NoLock, true);
Relation rel = table_open(relationId, lockmode);
Oid tablespace_oid = rel->rd_locator.spcOid;
ListCell *lcmd;

table_close(rel, lockmode);

foreach(lcmd, stmt->cmds)
{
AlterTableCmd *cmd = (AlterTableCmd *) lfirst(lcmd);

if (cmd->subtype == AT_SetAccessMethod && strcmp(cmd->name, "tde_heap") == 0)
if (cmd->subtype == AT_SetAccessMethod &&
((cmd->name != NULL && strcmp(cmd->name, "tde_heap")==0) ||
(cmd->name == NULL && strcmp(default_table_access_method, "tde_heap") == 0))
)
{
tdeCurrentCreateEvent.encryptMode = true;
tdeCurrentCreateEvent.eventType = TDE_TABLE_CREATE_EVENT;
Expand All @@ -163,6 +159,12 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)

if (tdeCurrentCreateEvent.encryptMode)
{
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);
LWLockRelease(tde_lwlock_enc_keys());
Expand Down
28 changes: 28 additions & 0 deletions t/008_tde_heap.pl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@
$stdout = $node->safe_psql('postgres', 'SELECT * FROM test_enc3 ORDER BY id ASC;', extra_params => ['-a']);
PGTDE::append_to_file($stdout);

$stdout = $node->safe_psql('postgres', 'CREATE TABLE test_enc4(id SERIAL,k VARCHAR(32),PRIMARY KEY (id)) USING heap;', extra_params => ['-a']);
PGTDE::append_to_file($stdout);

$stdout = $node->safe_psql('postgres', 'INSERT INTO test_enc4 (k) VALUES (\'foobar\'),(\'barfoo\');', extra_params => ['-a']);
PGTDE::append_to_file($stdout);

$stdout = $node->safe_psql('postgres', 'SET default_table_access_method = "tde_heap"; ALTER TABLE test_enc4 SET ACCESS METHOD DEFAULT;', extra_params => ['-a']);
PGTDE::append_to_file($stdout);

# Restart the server
PGTDE::append_to_file("-- server restart");
$rt_value = $node->stop();
Expand Down Expand Up @@ -140,6 +149,22 @@




# Verify that we can't see the data in the file
my $tablefile4 = $node->safe_psql('postgres', 'SHOW data_directory;');
$tablefile4 .= '/';
$tablefile4 .= $node->safe_psql('postgres', 'SELECT pg_relation_filepath(\'test_enc4\');');

$strings = 'TABLEFILE4 FOUND: ';
$strings .= `(ls $tablefile4 >/dev/null && echo yes) || echo no`;
PGTDE::append_to_file($strings);

$strings = 'CONTAINS FOO (should be empty): ';
$strings .= `strings $tablefile4 | grep foo`;
PGTDE::append_to_file($strings);



$stdout = $node->safe_psql('postgres', 'DROP TABLE test_enc;', extra_params => ['-a']);
PGTDE::append_to_file($stdout);

Expand All @@ -149,6 +174,9 @@
$stdout = $node->safe_psql('postgres', 'DROP TABLE test_enc3;', extra_params => ['-a']);
PGTDE::append_to_file($stdout);

$stdout = $node->safe_psql('postgres', 'DROP TABLE test_enc4;', extra_params => ['-a']);
PGTDE::append_to_file($stdout);

# DROP EXTENSION
$stdout = $node->safe_psql('postgres', 'DROP EXTENSION pg_tde;', extra_params => ['-a']);
ok($cmdret == 0, "DROP PGTDE EXTENSION");
Expand Down
7 changes: 7 additions & 0 deletions t/expected/008_tde_heap.out
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ INSERT INTO test_enc3 (k) VALUES ('foobar'),('barfoo');
SELECT * FROM test_enc3 ORDER BY id ASC;
1|foobar
2|barfoo
CREATE TABLE test_enc4(id SERIAL,k VARCHAR(32),PRIMARY KEY (id)) USING heap;
INSERT INTO test_enc4 (k) VALUES ('foobar'),('barfoo');
SET default_table_access_method = "tde_heap"; ALTER TABLE test_enc4 SET ACCESS METHOD DEFAULT;
-- server restart
SELECT * FROM test_enc ORDER BY id ASC;
1|foobar
Expand All @@ -29,8 +32,12 @@ TABLEFILE2 FOUND: yes
CONTAINS FOO (should be empty):
TABLEFILE3 FOUND: yes

CONTAINS FOO (should be empty):
TABLEFILE4 FOUND: yes

CONTAINS FOO (should be empty):
DROP TABLE test_enc;
DROP TABLE test_enc2;
DROP TABLE test_enc3;
DROP TABLE test_enc4;
DROP EXTENSION pg_tde;

0 comments on commit 8558225

Please sign in to comment.