Row-level security
Row-level security (RLS) is a data security mechanism that controls which rows of a table each user can access. You define policies on a table, and after you enable row-level security, users who query the table see only the rows that match the policies that apply to them. This gives you fine-grained data isolation within a single table, without splitting the data or maintaining a separate view for each audience.
Hologres is compatible with the native PostgreSQL row-level security syntax. You manage policies with the same CREATE POLICY and ALTER TABLE statements that you already use in PostgreSQL.
Usage notes
-
Row-level security is supported only in Hologres V5.0 and later.
-
Only a Superuser can manage row-level security.
-
Syntax: CREATE POLICY and ALTER POLICY do not support the WITH CHECK clause.
-
Data import and export:
-
INSERT and UPDATE do not check whether the values you write satisfy the policy conditions. Rows written this way are invisible to SELECT, but they still exist in the table.
-
A user who is subject to row-level security cannot run INSERT ON CONFLICT DO UPDATE on the protected table. Split the operation into a separate INSERT and UPDATE, or run it as a role that has the BYPASSRLS attribute. INSERT ON CONFLICT DO NOTHING is not affected by this limit.
-
COPY TO silently skips invisible rows during export. To export the complete data set, run the export as a role that has the BYPASSRLS attribute.
-
INSERT OVERWRITE (both the native INSERT OVERWRITE statement and the
hg_insert_overwritestored procedure) and TRUNCATE are not subject to row-level security. Both clear all data in the table instead of only the visible rows. The policies on the table remain unchanged after the operation. -
When you rebuild a table, Hologres automatically keeps all row-level security policies on that table.
-
-
Queries:
-
When you query a view, the policies on the underlying tables are evaluated as the view owner, not as the user who runs the query.
-
For a physical partitioned table, a query that accesses a child partition directly is not protected by the policies on the parent table. To control row access on child partitions, enable row-level security and create policies on each child partition separately.
-
If the USING expression of a policy references another table, the user who runs the query must have the SELECT permission on the referenced table. Otherwise, the query fails with an insufficient permission error.
-
When a table has both row-level security policies and
hg_anonmasking rules, row-level security filters the rows first, and masking is applied to the remaining rows.
-
-
Fixed Plan limits:
-
A table that has row-level security enabled does not support Fixed Plan. Hologres routes the affected requests to HQE instead, which can change the read and write performance of the table. Test the workload thoroughly before you enable row-level security in production. For more information about Fixed Plan, see Accelerate SQL execution with Fixed Plan.
-
A table that has a Global Index does not support Fixed Plan after you configure row-level security on it. If a query still covers the secondary index after Hologres rewrites it with the policy conditions, the query is still optimized by the secondary index.
-
-
Dynamic Table limits:
-
You can create a Dynamic Table on a table that has row-level security enabled. Only the new syntax that was introduced in Hologres V3.1 is supported. For more information about Dynamic Table, see Dynamic table.
-
A Dynamic Table refresh runs as the owner of the Dynamic Table, so the data in the Dynamic Table always reflects the rows that are visible to its owner. Any user who has the UPDATE permission on the Dynamic Table can trigger a refresh.
-
When the row-level security rules of a base table change, for example when you create, modify, or drop a policy, or run ENABLE, DISABLE, or FORCE ROW LEVEL SECURITY, the next refresh of the Dynamic Table runs as REFRESH OVERWRITE so that the data is recomputed under the new rules.
-
-
Other limits:
-
You cannot configure row-level security directly on a Foreign Table, External Table, view, or materialized view.
-
You cannot create a materialized view on a table that has row-level security enabled.
-
The native PostgreSQL LEAKPROOF semantics are not supported. Rows that a policy filters out can still take part in projection evaluation.
-
When a table has row-level security enabled (ENABLE ROW LEVEL SECURITY) and the current user cannot bypass it, MaxCompute cannot read the table directly.
-
You cannot apply row-level security to system catalog tables such as
pg_class.
-
Syntax
Enable and disable row-level security
Use the ALTER TABLE statement to enable or disable row-level security on a table:
-- Enable row-level security on a table.
ALTER TABLE <TABLE_NAME> ENABLE ROW LEVEL SECURITY;
-- Disable row-level security. Policy definitions are kept but not enforced.
ALTER TABLE <TABLE_NAME> DISABLE ROW LEVEL SECURITY;
-- Apply row-level security to the table owner as well.
ALTER TABLE <TABLE_NAME> FORCE ROW LEVEL SECURITY;
-- Stop applying row-level security to the table owner.
ALTER TABLE <TABLE_NAME> NO FORCE ROW LEVEL SECURITY;
The following table describes the parameters.
|
Parameter |
Description |
|
ENABLE ROW LEVEL SECURITY |
Enables row-level security on the table. After you enable it, a user who is subject to row-level security sees only the rows that satisfy the policy conditions. |
|
DISABLE ROW LEVEL SECURITY |
Disables row-level security. The policy definitions are kept but are not enforced, and every user who has permission on the table sees all rows. |
|
FORCE ROW LEVEL SECURITY |
Applies row-level security to the table owner as well. By default, the table owner bypasses row-level security. After you set FORCE, the owner must also comply with the policies. A Superuser and a role that has the BYPASSRLS attribute still bypass row-level security. |
|
NO FORCE ROW LEVEL SECURITY |
Stops applying row-level security to the table owner and restores the default behavior, in which the owner bypasses row-level security. |
Create a policy
Use the CREATE POLICY statement to create a row-level security policy on a table:
CREATE POLICY <POLICY_NAME> ON <TABLE_NAME>
[AS {PERMISSIVE | RESTRICTIVE}]
[FOR {ALL | SELECT | INSERT | UPDATE | DELETE}]
[TO {<ROLE_NAME> | PUBLIC | CURRENT_ROLE | CURRENT_USER | SESSION_USER}]
[USING (<USING_EXPRESSION>)];
The following table describes the parameters.
|
Parameter |
Description |
|
<POLICY_NAME> |
The name of the policy. The name must be unique within a table. |
|
<TABLE_NAME> |
The name of the target table on which you create the policy. |
|
AS PERMISSIVE |
Creates a permissive policy. This is the default value. Multiple PERMISSIVE policies are combined with OR, so a row is accessible if it satisfies any one of them. |
|
AS RESTRICTIVE |
Creates a restrictive policy. A RESTRICTIVE policy is combined with the PERMISSIVE policies using AND, so a row must satisfy all of the conditions. |
|
FOR ALL |
Applies the policy to all commands. This is the default value. |
|
FOR SELECT |
Applies the policy to SELECT queries only. |
|
FOR INSERT |
Applies the policy to INSERT operations only. |
|
FOR UPDATE |
Applies the policy to UPDATE operations only. |
|
FOR DELETE |
Applies the policy to DELETE operations only. |
|
TO <ROLE_NAME> |
Specifies the role to which the policy applies. |
|
TO PUBLIC |
Applies the policy to all users. |
|
TO CURRENT_ROLE, TO CURRENT_USER, TO SESSION_USER |
Applies the policy to the current role, the current user, or the session user at query time. |
|
USING (<USING_EXPRESSION>) |
Defines the row filter expression. Only the rows for which the expression evaluates to true are visible to the specified users. The expression can reference columns of the table, and it can use a subquery to reference other tables. |
Modify and delete policies
Use ALTER POLICY to modify an existing policy, and use DROP POLICY to delete a policy:
-- Modify the filter condition of a policy.
ALTER POLICY <POLICY_NAME> ON <TABLE_NAME>
[TO {<ROLE_NAME> | PUBLIC | CURRENT_ROLE | CURRENT_USER | SESSION_USER}]
[USING (<NEW_USING_EXPRESSION>)];
-- Rename a policy.
ALTER POLICY <POLICY_NAME> ON <TABLE_NAME> RENAME TO <NEW_POLICY_NAME>;
-- Delete a policy.
DROP POLICY [IF EXISTS] <POLICY_NAME> ON <TABLE_NAME>;
Manage roles that bypass row-level security
Use the ALTER ROLE statement to grant or revoke the BYPASSRLS attribute. A role that has the BYPASSRLS attribute is not subject to row-level security and can see all rows:
-- Allow a role to bypass row-level security.
ALTER ROLE <ROLE_NAME> BYPASSRLS;
-- Stop a role from bypassing row-level security.
ALTER ROLE <ROLE_NAME> NOBYPASSRLS;
View row-level security information
Run the following statements to view the row-level security configuration of a table:
-- View the policies on a table.
SELECT * FROM pg_policy WHERE polrelid = '<TABLE_NAME>'::regclass;
-- View the tables that have row-level security enabled.
SELECT relname, relrowsecurity, relforcerowsecurity
FROM pg_class
WHERE relrowsecurity = true;
-- View the roles that have the BYPASSRLS attribute.
SELECT rolname, rolbypassrls
FROM pg_authid
WHERE rolbypassrls = true;
Row-level security behavior
Role priority
When Hologres decides whether row-level security applies to a statement, it evaluates the current role against the following priority order. A higher priority wins.
|
Priority |
Role |
Behavior |
Description |
|
1 (highest) |
Superuser |
Always bypasses |
FORCE ROW LEVEL SECURITY cannot constrain a superuser. |
|
2 |
Role with the BYPASSRLS attribute |
Bypasses |
BYPASSRLS takes precedence over FORCE ROW LEVEL SECURITY. |
|
3 |
Table owner |
Bypasses by default |
You can use FORCE ROW LEVEL SECURITY to make the owner subject to policies. |
|
4 (lowest) |
Regular user |
Subject to row-level security |
Once row-level security is enabled, policy filtering always applies. |
Policy merging model
When a table has multiple policies, Hologres merges their conditions as follows:
-
PERMISSIVE policies are combined with OR. A user can access a row if the row satisfies any one of the PERMISSIVE policies. PERMISSIVE is the default.
-
RESTRICTIVE policies are combined with the merged PERMISSIVE result by using AND. A user can access a row only if the row satisfies the merged PERMISSIVE result and every RESTRICTIVE condition.
The final condition is evaluated as:
(permissive_1 OR permissive_2 OR ...) AND restrictive_1 AND restrictive_2 AND ...
If row-level security is enabled on a table but the table has no PERMISSIVE policy, Hologres denies all access. No user can see any row.
Behavior by command
Row-level security affects each SQL command differently, as described in the following table.
|
Command |
Read filtering (USING) |
Write validation (WITH CHECK) |
Description |
|
SELECT |
Supported |
— |
The USING condition filters out invisible rows. You see only the rows that satisfy the condition. |
|
UPDATE |
Supported |
Not supported |
The USING condition determines which rows you can modify. You can update only visible rows. Hologres does not validate the new values. |
|
DELETE |
Supported |
— |
The USING condition determines which rows you can delete. You can delete only visible rows. |
|
INSERT |
— |
Not supported |
Row-level security neither filters nor validates INSERT. You can insert any data. |
|
INSERT ON CONFLICT DO UPDATE |
— |
— |
Not allowed when the current user is subject to row-level security, that is, when row-level security is enabled on the table and the user cannot bypass it. The statement returns an error. |
|
INSERT ON CONFLICT DO NOTHING |
— |
— |
Not subject to row-level security. The statement is allowed. |
|
COPY TO |
Supported |
— |
Equivalent to SELECT. Invisible rows are silently filtered out. |
|
COPY FROM |
— |
Not supported |
Returns an error when the current user is subject to row-level security. Use a role that has the BYPASSRLS attribute instead. |
Behavior by permission model
SPM
In the simple permission model (SPM), Hologres automatically sets the owner of a new table to the {database_name}_developer group. Members of that group bypass row-level security by default. For more information about SPM, see Simple Permission Model Overview.
|
User type |
SPM group |
Member of the owner group |
Without FORCE |
With FORCE |
|
developer user |
developer |
Yes |
Bypasses row-level security and sees all rows |
Subject to row-level security |
|
admin user |
admin, which inherits developer |
Yes |
Bypasses row-level security and sees all rows |
Subject to row-level security |
|
writer user |
writer |
No |
Does not bypass row-level security |
Does not bypass row-level security |
|
viewer user |
viewer |
No |
Does not bypass row-level security |
Does not bypass row-level security |
|
Superuser |
— |
— |
Always bypasses row-level security |
Always bypasses row-level security |
The developer and admin users bypass row-level security because role inheritance identifies them as the table Owner, not because they hold the BYPASSRLS attribute. As a result, FORCE ROW LEVEL SECURITY can constrain both of them. A superuser still takes precedence over FORCE ROW LEVEL SECURITY and always bypasses row-level security.
SLPM
In the schema-level simple permission model (SLPM), Hologres automatically sets the owner of a new table to the {database_name}.{schema_name}.developer group. Members of that group bypass row-level security by default. For more information about SLPM, see Introduction to schema-level simple permission model.
|
User type |
SLPM group |
Member of the owner group |
Without FORCE |
With FORCE |
|
developer user |
{database_name}.{schema_name}.developer |
Yes |
Bypasses row-level security and sees all rows |
Subject to row-level security |
|
admin user |
{database_name}.admin, which inherits developer |
Yes |
Bypasses row-level security and sees all rows |
Subject to row-level security |
|
writer user |
{database_name}.{schema_name}.writer |
No |
Does not bypass row-level security |
Does not bypass row-level security |
|
viewer user |
{database_name}.{schema_name}.viewer |
No |
Does not bypass row-level security |
Does not bypass row-level security |
|
Superuser |
— |
— |
Always bypasses row-level security |
Always bypasses row-level security |
As with SPM, FORCE ROW LEVEL SECURITY can constrain both developer and admin users. A superuser still takes precedence over FORCE ROW LEVEL SECURITY and always bypasses row-level security.
Examples
Example 1: Isolate data by department
This example shows how to use row-level security so that users from different departments see only the rows for their own department.
Step 1: Create a test table and insert data.
-- Create the employee table.
CREATE TABLE employee (
id INT PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL,
salary INT NOT NULL
);
-- Insert test data.
INSERT INTO employee VALUES
(1, 'Zhang San', 'eng', 50),
(2, 'Li Si', 'eng', 70),
(3, 'Wang Wu', 'eng', 30),
(4, 'Zhao Liu', 'sales', 80),
(5, 'Qian Qi', 'sales', 60),
(6, 'Sun Ba', 'sales', 40);
Step 2: Create users and grant permissions.
-- Create users.
CREATE ROLE alice;
CREATE ROLE bob;
-- Grant query permissions.
-- In SPM, use spm_grant:
-- CALL spm_grant('{database_name}_viewer', 'alice');
-- CALL spm_grant('{database_name}_viewer', 'bob');
-- Outside SPM, use: GRANT SELECT ON employee TO alice, bob;
Step 3: Enable row-level security on the table.
ALTER TABLE employee ENABLE ROW LEVEL SECURITY;
Step 4: Create a policy for each user.
-- alice sees only the engineering department.
CREATE POLICY eng_policy ON employee
FOR SELECT
TO alice
USING (dept = 'eng');
-- bob sees only the sales department.
CREATE POLICY sales_policy ON employee
FOR SELECT
TO bob
USING (dept = 'sales');
Step 5: Verify the effect of row-level security.
-- alice sees only engineering rows.
SET ROLE alice;
SELECT * FROM employee;
-- Returns Zhang San, Li Si, and Wang Wu: all 3 rows in the eng department.
RESET ROLE;
-- bob sees only sales rows.
SET ROLE bob;
SELECT * FROM employee;
-- Returns Zhao Liu, Qian Qi, and Sun Ba: all 3 rows in the sales department.
RESET ROLE;
Example 2: Merge PERMISSIVE and RESTRICTIVE policies
This example shows how to combine PERMISSIVE and RESTRICTIVE policies to express more complex access control.
-- Two PERMISSIVE policies: alice can see the eng or sales departments.
CREATE POLICY r1 ON employee AS PERMISSIVE
FOR SELECT TO alice
USING (dept = 'eng');
CREATE POLICY r2 ON employee AS PERMISSIVE
FOR SELECT TO alice
USING (dept = 'sales');
-- One RESTRICTIVE policy: the row must also satisfy salary > 50.
CREATE POLICY r3 ON employee AS RESTRICTIVE
FOR SELECT TO alice
USING (salary > 50);
-- Final condition: (dept='eng' OR dept='sales') AND salary > 50
-- alice sees Li Si (eng, 70), Zhao Liu (sales, 80), and Qian Qi (sales, 60).
SET ROLE alice;
SELECT * FROM employee;
RESET ROLE;
Example 3: Reference another table with an EXISTS subquery
The USING expression of a policy can reference another table with an EXISTS subquery. This lets you drive access control from an external access control list.
-- Create the access control table.
CREATE TABLE acl_table (
user_name TEXT NOT NULL,
allowed_dept TEXT NOT NULL
);
INSERT INTO acl_table VALUES
('alice', 'eng'),
('alice', 'hr'),
('bob', 'sales');
-- Grant SELECT on acl_table to the users.
-- In SPM: CALL spm_grant('{database_name}_viewer', 'alice');
-- Outside SPM: GRANT SELECT ON acl_table TO alice, bob;
-- Create a policy based on an EXISTS subquery.
CREATE POLICY acl_policy ON employee
FOR SELECT TO alice
USING (EXISTS (
SELECT 1 FROM acl_table
WHERE acl_table.user_name = CURRENT_USER
AND acl_table.allowed_dept = employee.dept
));
-- alice now sees only employees in the eng and hr departments.
SET ROLE alice;
SELECT * FROM employee;
RESET ROLE;
Example 4: Apply row-level security to the table owner
By default, the table owner bypasses row-level security. Use FORCE ROW LEVEL SECURITY to make the owner subject to policies as well.
-- Assume that alice owns the employee table.
-- By default, alice bypasses row-level security and sees all rows.
-- Apply row-level security to the owner.
ALTER TABLE employee FORCE ROW LEVEL SECURITY;
-- alice now sees only the rows that satisfy the policy conditions.
-- Remove the constraint on the owner.
ALTER TABLE employee NO FORCE ROW LEVEL SECURITY;
-- alice can bypass row-level security again.
Superusers and roles that have the BYPASSRLS attribute are not affected by FORCE ROW LEVEL SECURITY. They always bypass row-level security.
Example 5: INSERT OVERWRITE and TRUNCATE preserve policies
When you run INSERT OVERWRITE or TRUNCATE on a table that has row-level security enabled, the operation clears all rows rather than only the visible ones. After the operation completes, the policies on the table remain unchanged, and subsequent reads and writes are still subject to row-level security.
-- Enable row-level security and create a policy.
ALTER TABLE employee ENABLE ROW LEVEL SECURITY;
CREATE POLICY eng_policy ON employee FOR SELECT TO alice USING (dept = 'eng');
-- INSERT OVERWRITE clears all rows, writes the new rows, and keeps the policies.
INSERT OVERWRITE employee VALUES
(10, 'New user A', 'eng', 90),
(11, 'New user B', 'sales', 85);
-- TRUNCATE clears all rows and keeps the policies.
TRUNCATE employee;
-- The policies still exist, so subsequent reads and writes remain constrained.
SELECT relname, relrowsecurity, relforcerowsecurity
FROM pg_class WHERE relname = 'employee';
-- relrowsecurity is true.
FAQ
Why does the table owner still see all rows after I enable row-level security?
By default, the table owner bypasses row-level security. To make the owner subject to policies, run ALTER TABLE <TABLE_NAME> FORCE ROW LEVEL SECURITY.
Why can't I see the rows that I just inserted?
Row-level security does not validate INSERT, so you can write any data. However, if the data you write does not satisfy your own policy conditions, you cannot see those rows in a subsequent SELECT. This is expected behavior. We recommend that you validate data in your application before writing it, so that the data satisfies the policy conditions.
Why does INSERT ON CONFLICT DO UPDATE return an error?
INSERT ON CONFLICT DO UPDATE is not allowed on a table when the current user is subject to row-level security. The semantics are ambiguous when an INSERT hits a primary key conflict and the conflicting row is invisible to the user. Use a separate INSERT and UPDATE instead, or run the statement as a role that has the BYPASSRLS attribute. INSERT ON CONFLICT DO NOTHING is not subject to this limit.
Why does COPY TO export fewer rows than expected?
COPY TO is equivalent to SELECT, so invisible rows are silently filtered out. To export the complete dataset, run the export as a role that has the BYPASSRLS attribute.
Why isn't a child partition filtered after I enable row-level security on a physically partitioned table?
Accessing a child partition directly is not protected by the row-level security of the parent table. To control row-level access on child partitions, enable row-level security and create policies on each child partition individually.
Error messages
The following table lists the errors that row-level security can return, together with the scenario that triggers each error.
|
Scenario |
Error message |
|
CREATE POLICY includes a WITH CHECK clause. |
|
|
ALTER POLICY includes a WITH CHECK clause. |
|
|
CREATE POLICY runs on an external table. |
|
|
CREATE POLICY or ALTER POLICY runs on an object that is not a regular table, such as a view, foreign table, materialized view, or index. |
|
|
ALTER TABLE ENABLE, DISABLE, or FORCE ROW LEVEL SECURITY runs on an external table. |
|
|
A materialized view is created on a table that has row-level security enabled. |
|
|
A user who is subject to row-level security runs INSERT ON CONFLICT DO UPDATE. |
|
|
A user who is subject to row-level security runs COPY FROM. |
|
|
A user without SELECT permission on the base table queries the base table directly. |
|
|
The user lacks SELECT permission on the access control table that the EXISTS subquery in a USING expression references. |
|
|
A policy with an existing name is created again. |
|
|
A policy that does not exist is dropped without IF EXISTS. |
|
|
A user who is subject to row-level security runs CREATE TABLE SNAPSHOT. |
|
|
A non-driver-based dynamic table is created on a base table that has row-level security enabled. |
|
|
Row-level security is enabled on a base table that has non-driver-based dynamic table dependents. |
|