Given a table employees with columns id, name, and department, and a row-level security policy that allows users to see only rows where department = current_setting('app.current_department'), what will be the output of the following query if app.current_department is set to 'Sales'?
SELECT name FROM employees ORDER BY id;
CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT, department TEXT); INSERT INTO employees (name, department) VALUES ('Alice', 'Sales'), ('Bob', 'HR'), ('Charlie', 'Sales'); ALTER TABLE employees ENABLE ROW LEVEL SECURITY; CREATE POLICY sales_only ON employees FOR SELECT TO public USING (department = current_setting('app.current_department')); SET app.current_department = 'Sales';
Think about which rows match the department filter in the policy.
The policy restricts rows to those where department equals the current setting 'Sales'. Only Alice and Charlie belong to Sales, so only their names appear.
Consider a table documents with a row-level security policy that allows UPDATE only if owner = current_user. What happens if a user tries to update a row where owner is different from their username?
CREATE TABLE documents (id SERIAL PRIMARY KEY, content TEXT, owner TEXT); ALTER TABLE documents ENABLE ROW LEVEL SECURITY; CREATE POLICY owner_update ON documents FOR UPDATE TO public USING (owner = current_user);
RLS policies filter rows visible for the operation based on the condition.
The policy restricts UPDATE to rows where owner matches the current user. Rows not owned by the user are invisible for UPDATE, so they cannot be changed.
Which option contains a syntax error when creating a row-level security policy in PostgreSQL?
Check the syntax of the USING clause.
Option D is missing parentheses around the USING expression, causing a syntax error.
You have a large sales table with a row-level security policy filtering rows by region. Which approach improves query performance when using RLS?
Think about how databases speed up WHERE clause filtering.
Creating an index on the filtered column helps PostgreSQL quickly find matching rows, improving RLS query performance.
A table projects has RLS enabled with this policy:
CREATE POLICY project_access ON projects FOR SELECT TO public USING (owner = current_user);
However, users can still see all rows. What is the most likely reason?
ALTER TABLE projects ENABLE ROW LEVEL SECURITY;
Check user permissions and special role attributes that affect RLS.
Users with the bypassrls attribute ignore RLS policies and see all rows regardless of policies.