Skip to content

Temporary tables, table variables and plaintext on disk

Use all three T-SQL temporary objects alongside encrypted permanent tables and identify where rows and query spills can reach disk in plaintext.

A runtime login with heap and enforcement off can read and write encrypted permanent tables and use table variables, # tables and SELECT INTO # on the same TDS connection. The Mimus 19.0.1 candidate with the four accepted changes allows these temporary objects to reach disk in plaintext. Copying a row from an encrypted permanent table to a temporary table can put a plaintext copy in a file. Deploy encrypted permanent tables

Choose settings for the runtime login

The PG administrator can set these values for the runtime login in physical database mimus. Reconnect over TDS to logical database vault. A runtime login that already inherits heap/off needs no override. The deployment login can use tde_heap and enforcement independently.

ALTER ROLE lab_user IN DATABASE mimus SET default_table_access_method = heap;
ALTER ROLE lab_user IN DATABASE mimus SET pg_tde.enforce_encryption = off;

Check the effective values on the actual TDS connection, especially when using a pool.

SELECT pg_catalog.current_setting('default_table_access_method'),
       pg_catalog.current_setting('pg_tde.enforce_encryption');
Effective table AM at creationEnforcementAll three T-SQL temporary objects
heapoffCreation succeeds. Temporary relations use ordinary heap storage.
tde_heapoff or onCreation fails with Unexpected catalog OID 2601 referenced in ENR. The Babelfish private temporary catalog cannot record the dependency on pg_am. There is no automatic fallback to heap.
heaponpg_tde enforcement rejects creation of plaintext relations.

Under the mixed configuration a login with permanent DDL privileges can create plaintext application tables. To require encryption for every permanent table, reserve permanent DDL for the encrypted deployment login and grant the runtime login only its application DML privileges. Deployment and runtime privileges

What survives a rollback?

On a fresh heap/off connection, send the table-variable example below as one batch. Insert 7, add 4 inside a transaction, then roll back. The sum is 11. The equivalent operation on each of the two other temporary objects returns 7.

DECLARE @t TABLE (id int);
INSERT INTO @t VALUES (7);
BEGIN TRANSACTION;
INSERT INTO @t VALUES (4);
ROLLBACK TRANSACTION;
SELECT SUM(id) FROM @t;
ObjectOpening statements in a separate batchSum after rollback
Table variableDECLARE @t TABLE(id int); INSERT INTO @t VALUES (7);11. Both rows are visible for the rest of the batch.
Local temporary tableCREATE TABLE #t(id int); INSERT INTO #t VALUES (7);7. The second insert rolled back.
Temporary SELECT INTOSELECT CAST(7 AS int) AS id INTO #s;7. The second insert rolled back.

The release and assertion builds of the four-change candidate passed these cases. The same connection could execute the next statement after a constraint error handled with TRY/CATCH. A cursor still holding an inner # table could read its original rows after the procedure returned, while a new table with the same name held the caller’s own data. Scope and reference handling

Where temporary data reaches disk

DestinationWhat a file can contain
Heap relations for @t, #t and SELECT INTO #tAll three can contain plaintext row data. In a separate storage test, each object received 20,000 rows. Each of the three relation files on the mounted filesystem contained its own test value.
Query work filesA PG sort of an encrypted permanent table produced a 4,710,400-byte spill file containing the plaintext test value. The table’s pg_tde key does not cover this file.

The product accepts these plaintext writes. Supporting tde_heap for the temporary relations would require protecting their access method across rollback, scope exit and final file cleanup. Executor spills such as sorts have a separate write path. These costs and the limited coverage are why temporary objects use heap in this build. pg_tde encryption limits

If the deployment must protect offline media, a tested setup placed all of PGDATA on a LUKS2 volume, used the default temporary tablespace and disabled swap for the service container. The markers were absent from the underlying volume. Readers with filesystem access could see plaintext on the unlocked mount. Cover the actual volumes for temporary relations and query spills when extra tablespaces are in use, and account for swap. This build cannot satisfy a requirement that these files themselves remain encrypted on the mounted filesystem. SQL Server automatically encrypts tempdb when a user database uses TDE, so check that expectation when migrating. Microsoft’s TDE and tempdb reference