Skip to content

supatype_mask

Planner-enforced column read masking and conditional write rejection

supatype_mask · Planner-enforced column read masking and conditional write rejection

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
102468supatype_masksupatype_mask1.0SECApache-2.0C

PostgreSQL · 17

Usage

Sources:

supatype_mask 1.0 adds planner-based column masking and conditional write rejection to PostgreSQL 17. Security labels identify SQL predicates; normal unreadable values become NULL, while some operations reject access. This complements ordinary grants and row-level security and does not replace them.

Enable and Label Columns

Append supatype_mask to shared_preload_libraries and restart PostgreSQL. A superuser then creates the extension in its fixed schema. Preloading registers hooks and the label provider; the SQL extension supplies the rejection function. Both are needed for the intended behavior.

CREATE EXTENSION supatype_mask;
CREATE TABLE public.staff (id bigint PRIMARY KEY, salary numeric);
CREATE FUNCTION public.may_read_salary(public.staff) RETURNS boolean
  LANGUAGE sql STABLE AS $$ SELECT current_user IN ('payroll_reader', 'payroll_editor') $$;
CREATE FUNCTION public.may_write_salary(public.staff) RETURNS boolean
  LANGUAGE sql STABLE AS $$ SELECT current_user = 'payroll_editor' $$;
SECURITY LABEL FOR supatype ON COLUMN public.staff.salary
  IS 'MASK READ public.may_read_salary WRITE public.may_write_salary';

Create and grant the example roles separately before testing as a nonexempt role. Predicates accept the table’s composite row and return boolean. A STABLE zero-argument predicate can express a session-only condition and be evaluated once per scan; the row-taking overload wins if both exist. IMMUTABLE predicates are rejected.

Read and Write Semantics

Normal reads, predicates, ordering and whole-row values are rewritten. An aggregate directly consuming an unreadable column fails instead of silently returning a partial result; a subquery’s own masked output remains masked. An UPDATE preserves the old value when the caller cannot read it, rejects a readable but unwritable value, and accepts an authorized write. INSERT evaluates the stored row and permits omitted/default values under the documented rules. COPY FROM and MERGE are refused for affected tables; COPY TO is rewritten.

Administration and Limits

supatype_mask.enabled defaults on and supatype_mask.exempt_roles defaults to service_role; both are reload-only settings. Superusers and listed effective roles bypass masking. Removing the SQL extension does not unload hooks or remove labels: reads still mask, while denied writes report a missing rejection function. Removing only preload leaves the SQL extension without enforcement.

Malformed or unavailable predicates fail closed with warnings. Logical replication is outside query rewriting, and error-based inference through predicate inputs remains a documented limit. Privileged users can change configuration, labels and code; this is not protection from the database administrator. The implementation depends on PostgreSQL 17 planner structures; do not infer support for other majors.

Table reader