Hide a column per row, not per table
supatype_mask masks reads and rejects writes on a single column, row by row. The rule is a SQL function you name in a security label, and the planner enforces it on every query.
-- Readable by the post's author. Called once per row.
CREATE FUNCTION public.can_read_posts__salary(posts posts) RETURNS boolean
LANGUAGE sql STABLE AS $$
SELECT author_id = auth.uid() FROM (SELECT ($1).*) AS posts;
$$;
-- Writable by HR. No arguments, so it runs once per scan.
CREATE FUNCTION public.can_write_posts__salary() RETURNS boolean
LANGUAGE sql STABLE AS $$ SELECT auth.role() = 'hr' $$;
SECURITY LABEL FOR supatype ON COLUMN public.posts.salary
IS 'MASK READ public.can_read_posts__salary WRITE public.can_write_posts__salary';
-- Every reference to posts.salary now plans as:
-- CASE WHEN public.can_read_posts__salary(posts)
-- THEN posts.salary ELSE NULL END
SELECT * FROM posts; -- other authors' salary is NULL
SELECT id FROM posts WHERE salary > 150; -- filters on the masked value
SELECT sum(salary) FROM posts; -- error if any row is unreadable Why supatype_mask
Column privileges are all or nothing. Revoke SELECT on salary and a PostgREST request with no select list asks for every column, so the whole table answers 403 instead of a row with one field hidden. A view can put a CASE where the column was, but Postgres stores the view's column list when you create it. A column added later is invisible until someone re-runs CREATE OR REPLACE VIEW.
supatype_mask puts the rule on the column. You label it with a read predicate and a write predicate, and the planner rewrites every reference to that column into a CASE that calls your read predicate. That covers the select list, WHERE, ORDER BY, joins, subqueries, views, COPY TO and whole-row references such as to_jsonb(posts). A filter cannot be used to guess the hidden value.
Writes are judged per row too. If a client reads a masked null and sends the whole object back, the real value is kept. A caller who can see the value but may not change it gets a 42501 error. The rules are plain SQL functions, so the C extension stays small and the logic stays where you can read it.
supatype_mask or column GRANTs or a view per role
| column GRANTs or a view per role | supatype_mask | |
|---|---|---|
| A request for every column | A column REVOKE makes PostgREST answer 403 for the whole table when no select list is given. | The column is returned, set to null on the rows the caller may not read. |
| Who can see the value | A GRANT decides per role, for every row in the table. | A predicate decides per row, for example author_id = auth.uid(). |
| Adding a column | A view stores a fixed column list, so a new column is missing until the view is recreated. | The label sits on the column and the table is queried directly. Nothing to recreate. |
| Clients the rule reaches | A view protects the queries that go through the view. | The rewrite happens in the planner, so psql, PostgREST and anything else on the database get the same answer. |
| Writing back a value you never saw | A view column built from CASE is read-only, so writes need a separate path and their own checks. | An unreadable value is kept. A readable value the caller may not change is rejected with an error. |
| Totals over hidden rows | sum() over a null-masking view adds up only the visible rows and says nothing. | sum(), max() and window aggregates are rejected per unreadable row, so a total is correct or an error. |
| Switching it off | Any role with the right privilege can GRANT the column back with one SQL statement. | No SQL statement disables masking for a role, a database or a session, not even as a superuser. It takes a config edit and a reload, or a restart. |
What it does not do
- Postgres 17 or later only. The rewrite depends on planner internals that change between major versions.
- Logical replication is not filtered. WAL records are not query results, so a subscriber has to apply the same predicates itself.
- Roles in supatype_mask.exempt_roles and superusers see every column unmasked, by design.
- A non-leakproof operator applied to the predicate's own inputs can still reveal something through an error message. References to the masked column itself are safe.
- COPY FROM and MERGE are refused on a table with masked columns.
- A whole-row predicate runs once per row per masked column. On a full scan with a per-row lookup that can cost around 100 times the unmasked scan. A predicate with no arguments runs once per scan instead.
- An aggregate over a subquery that masked some rows skips those rows rather than raising an error.
- The library must be in shared_preload_libraries, which needs a restart. CREATE EXTENSION without the preload does nothing.
- A predicate declared IMMUTABLE, missing, or not returning boolean masks the column for everyone and logs a WARNING.
Two ways to run it
supatype_mask ships in the supatype/postgres image. Use the image as your Postgres, with or without the rest of Supatype.
# The image preloads supatype_mask and creates the extension on first boot. docker run -d --name supatype-postgres \ -e POSTGRES_PASSWORD=your-password \ -p 5432:5432 \ supatype/postgres:17-latest # Already set in the image's postgresql.conf: # shared_preload_libraries = '..., supatype_mask' # and in /etc/postgresql-custom/supatype_mask.conf: # supatype_mask.exempt_roles = 'service_role,supatype_sql_editor' # In any other database, create the extension yourself: docker exec -it supatype-postgres psql -U supatype_admin -d mydb \ -c "CREATE EXTENSION IF NOT EXISTS supatype_mask;"
Get the image already running, with migrations, access rules, an API, a typed client and Studio generated from your TypeScript types. Cloud starts with 2 projects, 500 MB database and 1 GB storage, free and with no card.