Virtual Private Database (VPD) in Oracle Database 23ai — Enablement & Usage Runbook
Updated: Oct 24
Environment used in this runbook:
Database: Oracle Database 23ai, PDB PDB1
Demo schema: SALES (table ORDERS)
Security schema: SALES_SEC (policy code)
Licensing: VPD is part of Enterprise Edition and 23ai Free, no extra option required
What VPD does
VPD (also called fine-grained access control or row-level security) attaches a policy function to a table, view or synonym. Every time a SQL statement touches that object, the database calls the function, gets back a WHERE predicate and silently appends it. Users see only the rows (or columns) the predicate allows — regardless of the tool they connect with.
Core building blocks:
Application context — session attributes (region, department, tenant id) set securely at logon.
Policy function — PL/SQL returning the predicate string, usually built on SYS_CONTEXT.
DBMS_RLS.ADD_POLICY — binds the function to the object and statement types.
23ai touches you will notice: SELECT without FROM DUAL works, schema-level privileges (GRANT SELECT ANY TABLE ON SCHEMA sales TO ...) simplify reader grants, and IF NOT EXISTS is available for object creation scripts.
Step 1 — Create the demo objects and users
SQL |
ALTER SESSION SET CONTAINER = PDB1;
CREATE USER sales IDENTIFIED BY "Sales#2026" QUOTA UNLIMITED ON users; CREATE USER sales_sec IDENTIFIED BY "SalesSec#2026"; CREATE USER rep_east IDENTIFIED BY "East#2026"; CREATE USER rep_west IDENTIFIED BY "West#2026"; CREATE USER sales_mgr IDENTIFIED BY "Mgr#2026";
GRANT CREATE SESSION, CREATE TABLE TO sales; GRANT CREATE SESSION TO rep_east, rep_west, sales_mgr;
-- security schema: needs to build packages, contexts, policies GRANT CREATE SESSION, CREATE PROCEDURE, CREATE ANY CONTEXT, ADMINISTER DATABASE TRIGGER, CREATE TRIGGER TO sales_sec; GRANT EXECUTE ON dbms_rls TO sales_sec; GRANT EXECUTE ON dbms_session TO sales_sec;
CREATE TABLE sales.orders ( order_id NUMBER PRIMARY KEY, region VARCHAR2(10) NOT NULL, sales_rep VARCHAR2(30) NOT NULL, amount NUMBER(12,2), discount NUMBER(5,2));
INSERT INTO sales.orders VALUES (1,'EAST','REP_EAST',1200,5); INSERT INTO sales.orders VALUES (2,'EAST','REP_EAST', 800,0); INSERT INTO sales.orders VALUES (3,'WEST','REP_WEST',2500,10); COMMIT;
-- who belongs to which region (the source of truth for the context) CREATE TABLE sales.rep_region (username VARCHAR2(30) PRIMARY KEY, region VARCHAR2(10)); INSERT INTO sales.rep_region VALUES ('REP_EAST','EAST'); INSERT INTO sales.rep_region VALUES ('REP_WEST','WEST'); COMMIT;
GRANT SELECT, INSERT, UPDATE, DELETE ON sales.orders TO rep_east, rep_west, sales_mgr; GRANT SELECT ON sales.rep_region TO sales_sec; |
Why: keep the security code in its own schema (SALES_SEC) so application owners cannot change or drop the policies. ADMINISTER DATABASE TRIGGER is required for the logon trigger in Step 3.
Step 2 — Create the application context and its trusted package
SQL |
-- as SALES_SEC CREATE OR REPLACE PACKAGE sales_sec.sales_ctx_pkg AS PROCEDURE set_region; END; / CREATE OR REPLACE PACKAGE BODY sales_sec.sales_ctx_pkg AS PROCEDURE set_region IS v_region sales.rep_region.region%TYPE; BEGIN SELECT region INTO v_region FROM sales.rep_region WHERE username = SYS_CONTEXT('USERENV', 'SESSION_USER'); DBMS_SESSION.SET_CONTEXT('SALES_CTX', 'REGION', v_region); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_SESSION.SET_CONTEXT('SALES_CTX', 'REGION', 'NONE'); END; END; / CREATE OR REPLACE CONTEXT sales_ctx USING sales_sec.sales_ctx_pkg; |
Why: a context attribute can only be set by the package named in CREATE CONTEXT. Users cannot forge their region by calling DBMS_SESSION.SET_CONTEXT themselves.
Step 3 — Populate the context automatically at logon
SQL |
CREATE OR REPLACE TRIGGER sales_sec.trg_set_sales_ctx AFTER LOGON ON DATABASE BEGIN sales_sec.sales_ctx_pkg.set_region; END; / |
Why: the predicate must work for every tool (SQL*Plus, JDBC, reporting tools), so the context is set on every session, not by the application. Keep the trigger body exception-safe — an unhandled error here blocks logins.
Check it as a user:
SQL |
CONNECT rep_east/"East#2026"@pdb1 SELECT SYS_CONTEXT('SALES_CTX', 'REGION') AS my_region; -- 23ai: no FROM DUAL needed |
Step 4 — Write the policy function
SQL |
CREATE OR REPLACE FUNCTION sales_sec.region_predicate ( p_schema IN VARCHAR2, p_object IN VARCHAR2) RETURN VARCHAR2 IS BEGIN -- managers see everything IF SYS_CONTEXT('USERENV', 'SESSION_USER') = 'SALES_MGR' THEN RETURN NULL; END IF; -- everybody else: only their region RETURN q'[region = SYS_CONTEXT('SALES_CTX', 'REGION')]'; END; / |
Why: the signature (two VARCHAR2 inputs, VARCHAR2 return) is fixed by VPD. Returning NULL means "no restriction"; returning '1=2' blocks every row. Referencing SYS_CONTEXT inside the predicate (rather than concatenating the literal value) keeps the SQL text identical for every user, so cursors are shared.
Step 5 — Attach the policy with DBMS_RLS.ADD_POLICY
SQL |
BEGIN DBMS_RLS.ADD_POLICY( object_schema => 'SALES', object_name => 'ORDERS', policy_name => 'ORDERS_REGION_POL', function_schema => 'SALES_SEC', policy_function => 'REGION_PREDICATE', statement_types => 'SELECT,INSERT,UPDATE,DELETE', update_check => TRUE, policy_type => DBMS_RLS.CONTEXT_SENSITIVE); END; / |
Why:
statement_types — which statements get the predicate; INDEX is also possible.
update_check => TRUE — rows inserted/updated must still satisfy the predicate (a rep cannot move an order to another region).
policy_type controls how often the function runs: DYNAMIC (default, every execution), CONTEXT_SENSITIVE (re-evaluated only when the session context changes), STATIC (once, cached — the fastest, use when the predicate never changes). SHARED_* variants share the cache across objects using the same function.
Step 6 — Test as each user
SQL |
CONNECT rep_east/"East#2026"@pdb1 SELECT order_id, region, amount FROM sales.orders; -- 2 rows, EAST only
UPDATE sales.orders SET region = 'WEST' WHERE order_id = 1; -- ORA-28115: policy with check option violation (update_check working)
CONNECT sales_mgr/"Mgr#2026"@pdb1 SELECT COUNT(*) FROM sales.orders; -- 3 rows |
Why: the application saw no change: same table name, same SQL. VPD is doing the filtering at parse time.
Step 7 — Column-level VPD (column masking)
SQL |
BEGIN DBMS_RLS.ADD_POLICY( object_schema => 'SALES', object_name => 'ORDERS', policy_name => 'ORDERS_AMOUNT_MASK', function_schema => 'SALES_SEC', policy_function => 'REGION_PREDICATE', statement_types => 'SELECT', sec_relevant_cols => 'AMOUNT,DISCOUNT', sec_relevant_cols_opt => DBMS_RLS.ALL_ROWS); END; / |
Why: two behaviours are possible:
Without sec_relevant_cols_opt: the predicate applies only when the query references AMOUNT or DISCOUNT; SELECT order_id, region returns all rows.
With DBMS_RLS.ALL_ROWS: every row is returned, but AMOUNT/DISCOUNT show NULL for rows failing the predicate. Column masking works for SELECT only.
If you don't want the masking policy stacked on the row policy from Step 5, drop or disable one of them for the test.
Step 8 — Group policies for multi-application tables (optional)
SQL |
BEGIN DBMS_RLS.CREATE_POLICY_GROUP('SALES', 'ORDERS', 'CRM_GROUP'); DBMS_RLS.ADD_GROUPED_POLICY( object_schema => 'SALES', object_name => 'ORDERS', policy_group => 'CRM_GROUP', policy_name => 'CRM_REGION_POL', function_schema => 'SALES_SEC', policy_function => 'REGION_PREDICATE', statement_types => 'SELECT'); -- driving context decides which group is active for the session DBMS_RLS.ADD_POLICY_CONTEXT('SALES', 'ORDERS', 'SALES_CTX', 'APP_GROUP'); END; / |
Why: when several applications share a table, each gets its own policy group; the value of SYS_CONTEXT('SALES_CTX','APP_GROUP') (set at logon) selects which group applies. Policies in SYS_DEFAULT always apply.
Step 9 — Exemptions and administration
SQL |
-- bypass VPD for batch/reporting accounts (audit this privilege!) GRANT EXEMPT ACCESS POLICY TO etl_batch;
-- inventory SELECT object_owner, object_name, policy_name, function, policy_type, enable, sel, ins, upd, del FROM dba_policies ORDER BY 1,2,3; SELECT * FROM dba_sec_relevant_cols;
-- switch off / on without dropping EXEC DBMS_RLS.ENABLE_POLICY('SALES', 'ORDERS', 'ORDERS_AMOUNT_MASK', FALSE); EXEC DBMS_RLS.ENABLE_POLICY('SALES', 'ORDERS', 'ORDERS_AMOUNT_MASK', TRUE);
-- refresh cached predicates after changing a STATIC policy function EXEC DBMS_RLS.REFRESH_POLICY('SALES', 'ORDERS', 'ORDERS_REGION_POL');
-- remove EXEC DBMS_RLS.DROP_POLICY('SALES', 'ORDERS', 'ORDERS_AMOUNT_MASK'); |
Why: SYS and users with EXEMPT ACCESS POLICY bypass every VPD policy — Data Pump exports run by such users contain all rows. Exports by normal users are filtered, which is often a surprise.
Step 10 — Troubleshooting
SQL |
-- see the predicate actually applied to a cursor SELECT sql_id, object_name, policy, predicate FROM v$vpd_policy WHERE object_name = 'ORDERS';
-- trace VPD predicate generation for one session ALTER SESSION SET EVENTS '10730 trace name context forever, level 1'; |
Typical errors:
ORA-28110: policy function or package has error — recompile the function; check dba_errors.
ORA-28112: failed to execute policy function — runtime error inside the function (e.g. the context lookup raised an exception); details in the session trace file.
ORA-28113: policy predicate has error — the returned string is not valid SQL (quotes, column names).
Empty result set for everyone — the context is NONE/NULL because the logon trigger did not run or the user is missing in rep_region.
Design rules that keep VPD fast and safe
Set context once at logon; never query tables inside the policy function on every call.
Prefer CONTEXT_SENSITIVE or STATIC policies; DYNAMIC reruns the function for every execution.
Keep policy functions in a locked-down schema; grant EXECUTE ON DBMS_RLS to one role only.
Combine with Data Redaction (mask column values) and Unified Auditing (audit EXEMPT ACCESS POLICY and policy changes) — VPD covers rows, not values.
Test with the application's real connection pool: proxy users and connection-pool user switching (DBMS_SESSION.SET_IDENTIFIER, CLIENT_IDENTIFIER) change what SESSION_USER returns.
Comments