This is the multi-page printable view of this section.
Architecture
This is the Percona pg_tde 2.3.0 manual. For the paired build, backup tools and major upgrades, see PG TDE on Mimus.
1 - Architecture overview
This is the Percona pg_tde 2.3.0 manual. For the paired build, backup tools and major upgrades, see PG TDE on Mimus.
pg_tde is a customizable, complete, data at rest encryption extension. Extension means that pg_tde is delivered as a PostgreSQL extension.
pg_tde relies on extensibility changes in the PostgreSQL core. It currently runs only with Percona Server for PostgreSQL , which includes those changes.
The following sections break down the key architectural components of this design.
a. Customizable means that pg_tde supports many different use cases:
- Encrypting all tables in all databases, or only selected ones
- Storing encryption keys in different external key storage servers, for a list of these see Key management overview
- Using a single key for a clusters, or different keys for different clusters
- Centralizing all keys in one provider, or splitting them across providers
- Controlling permissions: who manages keys and who can create encrypted or unencrypted tables
b. Complete means that pg_tde aims to encrypt data at rest.
c. Data at rest means everything written to the disk. This includes the following:
- Table data files
- Indexes
- Sequences
- Temporary tables
- Write Ahead Log (WAL)
Main components
The main components of pg_tde are:
Core server changes focus on making the server more extensible, allowing the main logic of
pg_tdeto remain separate, as an extension. Core changes also add encryption-awareness to some command line tools that have to work directly with encrypted tables or encrypted WAL files.You can find the source code here .
The
pg_tdeextension implements the encryption code by hooking into the extension points introduced in the core changes, and the already existing extension points in the PostgreSQL server.Everything is controllable with GUC variables and SQL statements, similar to other extensions.
The keyring API and libraries implement the key storage logic with different key providers. The API is internal only, the keyring the libraries are currently part of the main codebase but could be separated into shared libraries in the future.
2 - Encryption architecture
This is the Percona pg_tde 2.3.0 manual. For the paired build, backup tools and major upgrades, see PG TDE on Mimus.
This section explains how pg_tde implements encryption inside PostgreSQL. It covers the key hierarchy, supported encryption algorithms, encryption workflow, and the core changes that make it possible to encrypt tables, WAL, and other database files.
Together, these components form the foundation of data-at-rest encryption in pg_tde.
Two-key hierarchy
pg_tde uses two kinds of keys for encryption:
- Internal keys to encrypt the data. They are stored in PostgreSQL’s data directory under
$PGDATA/pg_tde - Principal keys, which encrypt internal keys, are stored externally in a Key Management System (KMS) using the key provider API
pg_tde uses one principal key per database. Every internal key for the given database is encrypted using this principal key.
Internal keys are used for specific database files: each file with a different Object Identifier (OID) has a different internal key.
Example:
A table with 4 indexes will have at least 5 internal keys, one for the table and one for each index. Additional associated relations, such as sequences or a TOAST table, also have their own keys.
Encryption algorithm
pg_tde currently uses the following encryption algorithms:
AES-128-CBC,AES-256-CBCfor encrypting database files; encrypted with internal keysAES-128-CTR,AES-256-CTRfor WAL encryption; encrypted with internal keysAES-128-GCM,AES-256-GCMfor encrypting internal keys; encrypted with the principal key
Encryption workflow
You can use pg_tde to encrypt entire databases or only selected tables.
To support this without metadata changes, encrypted tables are labeled with the tde_heap access method marker.
The tde_heap access method is functionally identical to the heap access method. This allows pg_tde to distinguish between encrypted (tde_heap) and non-encrypted (heap) tables.
The initial decision about encryption is made using the postgres event trigger mechanism:
- When the
tde_heapclause is used forCREATE TABLEorALTER TABLEstatements, then the newly created data files are marked as encrypted - After this, the file operations encrypt or decrypt the data
Subsequent decisions are done using a slightly modified Storage Manager (SMGR) API:
- When a database file is re-created with a different ID as a result of a
TRUNCATEor aVACUUM FULLcommand, the newly created file inherits the encryption information - The file is then either encrypted or left unencrypted based on that inheritance
WAL encryption functionality
You can control WAL encryption globally via the pg_tde.wal_encrypt GUC variable, which requires a server restart.
WAL keys also contain the LSN of the first WAL write after key creation. This allows pg_tde to know which WAL ranges are encrypted or not and with which key.
See the Configure WAL encryption chapter for more information.
The setting only controls writes so that only WAL writes are encrypted when WAL encryption is enabled. This means that WAL files can contain both encrypted and unencrypted data, depending on what the status of this variable was when writing the data.
pg_tde keeps track of the encryption status of WAL records using internal keys. When the server is restarted it writes a new internal key if WAL encryption is enabled, or if it is disabled and was previously enabled it writes a dummy key signaling that WAL encryption ended.
With this information the WAL reader code can decide if a specific WAL record has to be decrypted or not and which key it should use to decrypt it.
Encrypting other access methods
Currently pg_tde only encrypts heap tables and other files such as indexes, TOAST tables, sequences that are related to the heap tables.
Indexes include any kind of index that goes through the SMGR API, not just the built-in indexes in PostgreSQL.
Other table access methods that use the SMGR API could also be encrypted. This requires adding a marker access method and extending the event triggers, using the same approach as with heap tables.
Storage Manager (SMGR) API
pg_tde relies on a slightly modified version of the SMGR API. These modifications include:
- Making the API generally extensible, where extensions can inject custom code into the storage manager
- Adding tracking information for files. When a new file is created for an existing relation, references to the existing file are also passed to the SMGR functions
With these modifications, pg_tde implements an additional layer on top of the normal Magnetic Disk SMGR API: if the related table is encrypted, pg_tde encrypts a file before writing it to the disk and, similarly, decrypts it after reading when needed.
WAL encryption
WAL encryption is implemented through a separate, server-wide mechanism that extends PostgreSQL WAL-related APIs. Similar to the SMGR integration, this relies on additional extensions to PostgreSQL core APIs to support encryption of WAL records.
For configuration details, see Configure WAL encryption.
3 - Key and key provider management
This is the Percona pg_tde 2.3.0 manual. For the paired build, backup tools and major upgrades, see PG TDE on Mimus.
pg_tde uses a hierarchy of principal and internal keys to encrypt database files. This chapter covers how to manage these keys, including rotation, regeneration, storage, and key provider configuration, so you can keep your encrypted data secure and accessible.
Principal key rotation
You can rotate principal keys to comply with common policies and to handle situations with potentially exposed principal keys.
Rotation means that pg_tde generates a new version of the principal key, and re-encrypts the associated internal keys with the new key. The old principal key is kept as is at the same location, because it may still be needed to decrypt backups or other databases.
Internal key regeneration
Internal keys for tables, indexes and other data files are generated once a file is created. There’s no way to re-encrypt a file.
There are workarounds for this, because operations that move the table data to a new file, such as VACUUM FULL or an ALTER TABLE that rewrites the file will create a new key for the new file, essentially rotating the internal key. This however means taking an exclusive lock on the table for the duration of the operation, which might not be desirable for huge tables.
WAL internal keys are fixed once created. Every time the server (re)starts, a new WAL key is generated. If WAL encryption is enabled (using pg_tde.wal_encrypt), all WAL writes following the creation of the new key are encrypted with it until another key is generated at the next restart. This ensures that each WAL segment uses a consistent encryption key, without requiring you to manage key rotation manually.
Internal key storage
Internal keys and pg_tde metadata in general are kept in a single $PGDATA/pg_tde directory. This directory stores separate files for each database, such as:
- Encrypted internal keys and internal key mapping to tables
- Information about the key providers
Also, the $PGDATA/pg_tde directory has a special global section marked with the OID 1664, which includes the global key providers and global internal keys.
The global section is used for WAL encryption. Specific databases can use the global section too, for scenarios where users configure individual principal keys for databases but use the same global key provider. For this purpose, you must enable the global provider inheritance.
The global default principal key uses the special OID 1663.
Key providers (principal key storage)
In pg_tde, a Key Management System (KMS) is treated as an external key provider. Key providers store and serve the principal keys that pg_tde uses for encryption and decryption.
When you configure a key provider, pg_tde:
- Uploads new principal keys when they are created
- Retrieves principal keys from the provider when needed for decryption (for example, at startup or restart)
- Caches retrieved keys to reduce repeated lookups
Each key provider requires a detailed configuration, including the service address and authentication information.
For a complete list of supported providers and their configuration steps, see the Key management overview.
Key provider management
Key provider configuration or location may change. For example, a service is moved to a new address or the principal key must be moved to a different key provider type. pg_tde supports both these scenarios enabling you to manage principal keys using simple SQL functions.
In certain cases you can’t use SQL functions to manage key providers. For example, if the key provider changed while the server wasn’t running and is therefore unaware of these changes. The startup can fail if it needs to access the encryption keys.
For such situations, pg_tde also provides command line tools to recover the database.
Sensitive key provider information
Authentication details for key providers are sensitive and must be protected.
Do not store these credentials in the $PGDATA directory alongside the database. Instead, ensure they are stored in a secure location with strict file system permissions to prevent unauthorized access.