# 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.

---

LLMS index: [llms.txt](/llms.txt)

---

**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](/docs/mssql/encrypted-tables/)

## Choose settings for the runtime login {#settings}

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.

```sql
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.

```sql
SELECT pg_catalog.current_setting('default_table_access_method'),
       pg_catalog.current_setting('pg_tde.enforce_encryption');
```

| Effective table AM at creation | Enforcement | All three T-SQL temporary objects |
| --- | --- | --- |
| `heap` | `off` | Creation succeeds. Temporary relations use ordinary heap storage. |
| `tde_heap` | `off` or `on` | Creation 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. |
| `heap` | `on` | pg_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](/docs/mssql/encrypted-tables/#defaults)

## What survives a rollback? {#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**.

```sql
DECLARE @t TABLE (id int);
INSERT INTO @t VALUES (7);
BEGIN TRANSACTION;
INSERT INTO @t VALUES (4);
ROLLBACK TRANSACTION;
SELECT SUM(id) FROM @t;
```

| Object | Opening statements in a separate batch | Sum after rollback |
| --- | --- | --- |
| Table variable | `DECLARE @t TABLE(id int); INSERT INTO @t VALUES (7);` | 11. Both rows are visible for the rest of the batch. |
| Local temporary table | `CREATE TABLE #t(id int); INSERT INTO #t VALUES (7);` | 7. The second insert rolled back. |
| Temporary `SELECT INTO` | `SELECT 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](/docs/patches/mssql/#tde-increments)

## Where temporary data reaches disk {#disk}

| Destination | What a file can contain |
| --- | --- |
| Heap relations for `@t`, `#t` and `SELECT INTO #t` | All 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 files | A 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](/docs/tde/upstream/index/tde-limitations/)

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](https://learn.microsoft.com/en-us/sql/relational-databases/security/encryption/transparent-data-encryption?view=sql-server-ver17)

---

Backlinks:

- [SQL Server](/docs/mssql/)
- [T-SQL and client compatibility](/docs/mssql/compatibility/)
- [Create encrypted permanent tables over TDS](/docs/mssql/encrypted-tables/)
- [Known limits and troubleshooting](/docs/mssql/status/)
- [400 / 401 · SQL Server](/docs/patches/mssql/)
- [PG TDE](/docs/tde/)
- [Enable TDE and encrypt a table](/docs/tde/tables/)
