All Products
Search
Document Center

PolarDB:DBMS_REDACT

Last Updated:Mar 28, 2026

The DBMS_REDACT package masks sensitive data in query results to protect data security without altering the underlying stored values. Masking is applied at query time — privileged users and applications see redacted values while the original data remains unchanged.

Two Grand Unified Configuration (GUC) parameters control the feature's behavior:

ParameterDescription
polar_enable_masking_policySpecifies whether to enable data masking globally.
polar_enable_subquery_maskingSpecifies whether to apply masking inside subqueries when the feature is enabled.

Subprograms

SubprogramDescription
ADD_POLICYAdds a data masking policy to a table.
ALTER_POLICYModifies an existing data masking policy.
DISABLE_POLICYDisables a data masking policy.
ENABLE_POLICYEnables a data masking policy.
DROP_POLICYRemoves a data masking policy from a table.
UPDATE_FULL_REDACTION_VALUESUpdates the default masked values used for each data type under full masking.

Function types

The function_type parameter appears in ADD_POLICY and ALTER_POLICY. All valid values are listed here for reference.

ConstantDescription
DBMS_REDACT.NONENo masking is applied.
DBMS_REDACT.FULLReplaces the column value with a type-specific default. This is the default function type.
DBMS_REDACT.NULLIFYReturns NULL as the masked value.
DBMS_REDACT.PARTIALMasks a portion of the column value. Requires function_parameters.
DBMS_REDACT.RANDOMReturns a different random value on each query.
DBMS_REDACT.REGEXPMasks values matching a regular expression pattern.
DBMS_REDACT.REGEXP_WIDTHProvided for Oracle compatibility. Behaves the same as REGEXP.

Subquery masking behavior

When a masked column inside a subquery is referenced by an outer query, the outer query operates on the already-masked values by default.

For example, given a table mask_tbl(a int, b int) with a masking policy on column a, the following query evaluates the JOIN condition against masked values:

SELECT t1.a, t2.a
FROM (SELECT a FROM mask_tbl) t1
LEFT JOIN (SELECT a FROM mask_tbl) t2 ON t1.a = t2.a;

To evaluate the JOIN condition against the original pre-masking values, disable subquery masking for the session:

SET polar_enable_subquery_masking = off;

With polar_enable_subquery_masking set to off, the LEFT JOIN uses pre-masking values. The values returned by the statement are not masked.

ADD_POLICY

Adds a data masking policy to a table.

Syntax

DBMS_REDACT.ADD_POLICY(
    object_schema          VARCHAR2 DEFAULT NULL,
    object_name            VARCHAR2,
    policy_name            VARCHAR2,
    policy_description     VARCHAR2 DEFAULT NULL,
    column_name            VARCHAR2 DEFAULT NULL,
    column_description     VARCHAR2 DEFAULT NULL,
    function_type          INTEGER DEFAULT 1,
    function_parameters    VARCHAR2 DEFAULT NULL,
    expression             VARCHAR2,
    enable                 BOOLEAN DEFAULT TRUE,
    regexp_pattern         VARCHAR2 DEFAULT NULL,
    regexp_replace_string  VARCHAR2 DEFAULT NULL,
    regexp_position        INTEGER DEFAULT 1,
    regexp_occurrence      INTEGER DEFAULT 0,
    regexp_match_parameter VARCHAR2 DEFAULT NULL);

Parameters

ParameterRequiredDescription
object_schemaNoThe schema of the target table. Defaults to the current user's schema.
object_nameYesThe name of the table to apply the policy to.
policy_nameYesThe name of the data masking policy.
policy_descriptionNoThe description of the policy.
column_nameNoThe name of the column to mask.
column_descriptionNoThe description of the masked column.
function_typeNoThe masking function type. Default: DBMS_REDACT.FULL. See Function types for all valid values.
function_parametersNoThe masking format string. Required when function_type is DBMS_REDACT.PARTIAL. See PARTIAL masking format for syntax.
expressionYesA BOOLEAN expression that controls when masking is applied. Masking is active when this expression evaluates to TRUE.
enableNoSpecifies whether to enable the policy when it is created. Default: TRUE.
regexp_patternNoThe regular expression pattern to match. Used with REGEXP and REGEXP_WIDTH.
regexp_replace_stringNoThe replacement string for matched patterns.
regexp_positionNoThe character position at which the regular expression search starts. Default: 1.
regexp_occurrenceNoThe number of pattern occurrences to replace. 0 replaces all occurrences. A positive integer n replaces the first n occurrences.
regexp_match_parameterNoThe match behavior modifier. Valid values: i, c, n, m, x.

PARTIAL masking format

The function_parameters string format depends on the column's data type.

String columns

Format: <input_format>, <output_format>, <mask_char>, <mask_from>, <mask_to>

FieldDescription
input_formatDefines the structure of the input. V marks characters that can be masked; F marks characters that are skipped.
output_formatDefines the structure of the output. Must have the same number of V characters as the input format.
mask_charThe character displayed in masked positions.
mask_fromThe start position for masking, counted by the index of V characters in the input format.
mask_toThe end position for masking, counted the same way.

Example: VVVFFVVV, VV-VV-VV, X, 1, 3 masks abcdefg as XX-Xe-fg.

Numeric columns

Format: <mask_digit>, <mask_from>, <mask_to>

FieldDescription
mask_digitThe digit (0–9) that replaces masked positions.
mask_fromThe start position for masking, counted from the first digit of the column value.
mask_toThe end position for masking.

Example: 5, 2, 5 masks 123456789 as 155556789.

Date columns

Format: <field><value>[...] — a sequence of field codes alternating between a field identifier and a replacement value.

Field codeMeaningExample
Uppercase letterThe field is not masked.D — day is not masked
Lowercase letter + digitThe field is replaced with the given value.d15 — day becomes 15

Field identifiers: m (month), d (day), y (year), h (hour), m (minute), s (second).

Example: m12Dy2001Hm1s1 masks 2023-01-01 12:00:00 as 2001-12-01 12:01:01.

Example

The following example creates a partial masking policy on the num column, then verifies the output.

CREATE TABLE masktbl(name varchar2(40), num integer);

INSERT INTO masktbl VALUES('Alice', 123456789);
INSERT INTO masktbl VALUES('Bob', 234567890);

-- Create a data masking policy.
BEGIN
    DBMS_REDACT.ADD_POLICY(object_name => 'masktbl',
                            policy_name => 'p1',
                            expression => '1=1',
                            column_name => 'num',
                            function_type => DBMS_REDACT.PARTIAL,
                            function_parameters => '5,1,4');
END;

-- Query the table. The num column shows masked values.
SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 555556789
    Bob   | 555567890
(2 rows)

ALTER_POLICY

Modifies an existing data masking policy.

Syntax

DBMS_REDACT.ALTER_POLICY (
    object_schema              VARCHAR2 DEFAULT NULL,
    object_name                VARCHAR2,
    policy_name                VARCHAR2,
    action                     INTEGER  DEFAULT 1,
    column_name                VARCHAR2 DEFAULT NULL,
    function_type              INTEGER  DEFAULT 1,
    function_parameters        VARCHAR2 DEFAULT NULL,
    expression                 VARCHAR2 DEFAULT NULL,
    regexp_pattern             VARCHAR2 DEFAULT NULL,
    regexp_replace_string      VARCHAR2 DEFAULT NULL,
    regexp_position            INTEGER  DEFAULT 1,
    regexp_occurrence          INTEGER  DEFAULT 0,
    regexp_match_parameter     VARCHAR2 DEFAULT NULL,
    policy_description         VARCHAR2 DEFAULT NULL,
    column_description         VARCHAR2 DEFAULT NULL);

Parameters

ParameterRequiredDescription
object_schemaNoThe schema of the target table. Defaults to the current user's schema.
object_nameYesThe name of the table to which the policy applies.
policy_nameYesThe name of the data masking policy to modify.
actionNoThe operation to perform. Valid values: DBMS_REDACT.ADD_COLUMN, DBMS_REDACT.DROP_COLUMN, DBMS_REDACT.MODIFY_EXPRESSION, DBMS_REDACT.MODIFY_COLUMN, DBMS_REDACT.SET_POLICY_DESCRIPTION, DBMS_REDACT.SET_COLUMN_DESCRIPTION.
column_nameNoThe name of the column to modify.
function_typeNoThe masking function type. See Function types.
function_parametersNoThe masking format string. See PARTIAL masking format.
expressionNoThe updated BOOLEAN expression for the policy.
regexp_patternNoThe regular expression pattern to match.
regexp_replace_stringNoThe replacement string for matched patterns.
regexp_positionNoThe start position for the regular expression search. Default: 1.
regexp_occurrenceNoThe number of occurrences to replace. 0 replaces all. A positive integer n replaces the first n.
regexp_match_parameterNoThe match behavior modifier. Valid values: i, c, n, m, x.
policy_descriptionNoThe updated description for the policy.
column_descriptionNoThe updated description for the masked column.

Example

The following example creates a policy with no masked columns, then uses ALTER_POLICY to add a column and later modify the masking parameters.

CREATE TABLE masktbl(name varchar2(40), num integer);

INSERT INTO masktbl VALUES('Alice', 123456789);
INSERT INTO masktbl VALUES('Bob', 234567890);

-- Create a policy with no columns. Queries return unmasked data.
BEGIN
    DBMS_REDACT.ADD_POLICY(object_name => 'masktbl',
                            policy_name => 'p1',
                            expression => '1=1');
END;

SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 123456789
    Bob   | 234567890
(2 rows)

-- Add the num column with partial masking (digit 9, positions 1-4).
BEGIN
    DBMS_REDACT.ALTER_POLICY(object_name => 'masktbl',
                            policy_name => 'p1',
                            action => DBMS_REDACT.ADD_COLUMN,
                            column_name => 'num',
                            function_type => DBMS_REDACT.PARTIAL,
                            function_parameters => '9, 1, 4');
END;

SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 999956789
    Bob   | 999967890
(2 rows)

-- Modify the masking parameters (digit 5, positions 1-4).
BEGIN
    DBMS_REDACT.ALTER_POLICY(object_name => 'masktbl',
                            policy_name => 'p1',
                            action => DBMS_REDACT.MODIFY_COLUMN,
                            column_name => 'num',
                            function_type => DBMS_REDACT.PARTIAL,
                            function_parameters => '5, 1, 4');
END;

SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 555556789
    Bob   | 555567890
(2 rows)

DISABLE_POLICY

Disables a data masking policy without removing it.

Syntax

DBMS_REDACT.DISABLE_POLICY (
    object_schema           VARCHAR2 DEFAULT NULL,
    object_name             VARCHAR2,
    policy_name             VARCHAR2);

Parameters

ParameterRequiredDescription
object_schemaNoThe schema of the target table. Defaults to the current user's schema.
object_nameYesThe name of the table for which to disable the policy.
policy_nameYesThe name of the data masking policy to disable.

Example

CREATE TABLE masktbl(name varchar2(40), num integer);

INSERT INTO masktbl VALUES('Alice', 123456789);
INSERT INTO masktbl VALUES('Bob', 234567890);

-- Create a policy (enabled by default).
BEGIN
    DBMS_REDACT.ADD_POLICY(object_name => 'masktbl',
                            policy_name => 'p1',
                            expression => '1=1',
                            column_name => 'num',
                            function_type => DBMS_REDACT.PARTIAL,
                            function_parameters => '5,1,4');
END;

-- The num column shows masked values.
SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 555556789
    Bob   | 555567890
(2 rows)

-- Disable the policy.
CALL DBMS_REDACT.DISABLE_POLICY(object_name => 'masktbl', policy_name => 'p1');

-- The num column shows original values.
SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 123456789
    Bob   | 234567890
(2 rows)

ENABLE_POLICY

Enables a data masking policy that was created with enable => false or previously disabled.

Syntax

DBMS_REDACT.ENABLE_POLICY (
    object_schema    VARCHAR2 DEFAULT NULL,
    object_name      VARCHAR2,
    policy_name      VARCHAR2);

Parameters

ParameterRequiredDescription
object_schemaNoThe schema of the target table. Defaults to the current user's schema.
object_nameYesThe name of the table for which to enable the policy.
policy_nameYesThe name of the data masking policy to enable.

Example

CREATE TABLE masktbl(name varchar2(40), num integer);

INSERT INTO masktbl VALUES('Alice', 123456789);
INSERT INTO masktbl VALUES('Bob', 234567890);

-- Create a policy in a disabled state.
BEGIN
    DBMS_REDACT.ADD_POLICY(object_name => 'masktbl',
                            policy_name => 'p1',
                            expression => '1=1',
                            column_name => 'num',
                            function_type => DBMS_REDACT.PARTIAL,
                            function_parameters => '5,1,4',
                            enable => false);
END;

-- The policy is disabled. The num column shows original values.
SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 123456789
    Bob   | 234567890
(2 rows)

-- Enable the policy.
CALL DBMS_REDACT.ENABLE_POLICY(object_name => 'masktbl', policy_name => 'p1');

-- The num column now shows masked values.
SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 555556789
    Bob   | 555567890
(2 rows)

DROP_POLICY

Removes a data masking policy from a table.

Syntax

DBMS_REDACT.DROP_POLICY (
    object_schema      VARCHAR2 DEFAULT NULL,
    object_name        VARCHAR2,
    policy_name        VARCHAR2);

Parameters

ParameterRequiredDescription
object_schemaNoThe schema of the target table. Defaults to the current user's schema.
object_nameYesThe name of the table from which to remove the policy.
policy_nameYesThe name of the data masking policy to remove.

Example

CREATE TABLE masktbl(name varchar2(40), num integer);

INSERT INTO masktbl VALUES('Alice', 123456789);
INSERT INTO masktbl VALUES('Bob', 234567890);

-- Create a policy.
BEGIN
    DBMS_REDACT.ADD_POLICY(object_name => 'masktbl',
                            policy_name => 'p1',
                            expression => '1=1',
                            column_name => 'num',
                            function_type => DBMS_REDACT.PARTIAL,
                            function_parameters => '5,1,4');
END;

-- The num column shows masked values.
SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 555556789
    Bob   | 555567890
(2 rows)

-- Remove the policy.
CALL DBMS_REDACT.DROP_POLICY(object_name => 'masktbl', policy_name => 'p1');

-- The policy no longer exists. The num column shows original values.
SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |    num
-------+-----------
    Alice | 123456789
    Bob   | 234567890
(2 rows)

UPDATE_FULL_REDACTION_VALUES

Updates the default values shown for each data type when DBMS_REDACT.FULL masking is applied.

Syntax

DBMS_REDACT.UPDATE_FULL_REDACTION_VALUES (
    number_val       NUMBER        DEFAULT  NULL,
    binfloat_val     FLOAT4        DEFAULT  NULL,
    bindouble_val    FLOAT8        DEFAULT  NULL,
    char_val         CHAR          DEFAULT  NULL,
    varchar_val      VARCHAR2      DEFAULT  NULL,
    nchar_val        NCHAR         DEFAULT  NULL,
    nvarchar_val     NVARCHAR2     DEFAULT  NULL,
    date_val         DATE          DEFAULT  NULL,
    ts_val           TIMESTAMP     DEFAULT  NULL,
    tswtz_val        TIMESTAMPTZ   DEFAULT  NULL,
    blob_val         BLOB          DEFAULT  NULL,
    clob_val         CLOB          DEFAULT  NULL,
    nclob_val        NCLOB         DEFAULT  NULL);

Parameters

ParameterDescription
number_valThe default masked value for NUMBER columns.
binfloat_valThe default masked value for FLOAT columns.
bindouble_valThe default masked value for DOUBLE columns.
char_valThe default masked value for CHAR or NCHAR columns.
varchar_valThe default masked value for VARCHAR2, NVARCHAR2, CLOB, or NCLOB columns.
nchar_valOracle compatibility only. No configuration needed.
nvarchar_valOracle compatibility only. No configuration needed.
date_valThe default masked value for DATE columns.
ts_valThe default masked value for TIMESTAMP columns.
tswtz_valThe default masked value for TIMESTAMPTZ columns.
blob_valThe default masked value for BLOB columns.
clob_valOracle compatibility only. No configuration needed.
nclob_valOracle compatibility only. No configuration needed.

Example

The following example applies full masking to the num column, then changes the default masked value from 0 to 12345.

CREATE TABLE masktbl(name varchar2(40), num integer);

INSERT INTO masktbl VALUES('Alice', 123456789);
INSERT INTO masktbl VALUES('Bob', 234567890);

-- Create a full masking policy.
BEGIN
    DBMS_REDACT.ADD_POLICY(object_name => 'masktbl',
                            policy_name => 'p1',
                            expression => '1=1',
                            column_name => 'num',
                            function_type => DBMS_REDACT.FULL);
END;

-- The num column shows 0 (the default full-masking value for NUMBER).
SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  | num
-------+-----
    Alice |   0
    Bob   |   0
(2 rows)

-- Update the default masked value for NUMBER columns.
CALL DBMS_REDACT.UPDATE_FULL_REDACTION_VALUES(number_val => 12345);

-- The num column now shows 12345.
SELECT name, num FROM masktbl ORDER BY 1, 2;
    name  |  num
-------+-------
    Alice | 12345
    Bob   | 12345
(2 rows)