Skip to content

Commit 232dd33

Browse files
jianhe-funhackorum
authored andcommitted
CREATE POLICY IF NOT EXISTS
discussion: https://postgr.es/m/
1 parent 68b9273 commit 232dd33

6 files changed

Lines changed: 91 additions & 30 deletions

File tree

‎doc/src/sgml/ref/create_policy.sgml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">table_name</replaceable>
24+
CREATE POLICY [ IF NOT EXISTS ] <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">table_name</replaceable>
2525
[ AS { PERMISSIVE | RESTRICTIVE } ]
2626
[ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ]
2727
[ TO { <replaceable class="parameter">role_name</replaceable> | PUBLIC | CURRENT_ROLE | CURRENT_USER | SESSION_USER } [, ...] ]
@@ -103,6 +103,19 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
103103
<title>Parameters</title>
104104

105105
<variablelist>
106+
107+
<varlistentry>
108+
<term><literal>IF NOT EXISTS</literal></term>
109+
<listitem>
110+
<para>
111+
Do not throw an error if the policy object with the same name already
112+
exists. A notice is issued in this case. Note that only the name of
113+
the policy object is considered here, not the details of its
114+
definition.
115+
</para>
116+
</listitem>
117+
</varlistentry>
118+
106119
<varlistentry>
107120
<term><replaceable class="parameter">name</replaceable></term>
108121
<listitem>

‎src/backend/commands/policy.c‎

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,57 @@ CreatePolicy(CreatePolicyStmt *stmt)
662662
RangeVarCallbackForPolicy,
663663
stmt);
664664

665+
/* Open pg_policy catalog */
666+
pg_policy_rel = table_open(PolicyRelationId, RowExclusiveLock);
667+
668+
/* Set key - policy's relation id. */
669+
ScanKeyInit(&skey[0],
670+
Anum_pg_policy_polrelid,
671+
BTEqualStrategyNumber, F_OIDEQ,
672+
ObjectIdGetDatum(table_id));
673+
674+
/* Set key - policy's name. */
675+
ScanKeyInit(&skey[1],
676+
Anum_pg_policy_polname,
677+
BTEqualStrategyNumber, F_NAMEEQ,
678+
CStringGetDatum(stmt->policy_name));
679+
680+
sscan = systable_beginscan(pg_policy_rel,
681+
PolicyPolrelidPolnameIndexId, true, NULL, 2,
682+
skey);
683+
684+
policy_tuple = systable_getnext(sscan);
685+
686+
/* Complain or bail out if the policy name already exists for the table */
687+
if (HeapTupleIsValid(policy_tuple))
688+
{
689+
/*
690+
* If the policy already exists and the user specified "IF NOT EXISTS",
691+
* bail out with a NOTICE.
692+
*/
693+
if (stmt->if_not_exists)
694+
{
695+
/*
696+
* Since policy objects aren't members of extensions no need for
697+
* checkMembershipInCurrentExtension here.
698+
*/
699+
ereport(NOTICE,
700+
errcode(ERRCODE_DUPLICATE_OBJECT),
701+
errmsg("policy \"%s\" for table \"%s\" already exists",
702+
stmt->policy_name, get_rel_name(table_id)));
703+
704+
systable_endscan(sscan);
705+
table_close(pg_policy_rel, RowExclusiveLock);
706+
707+
return InvalidObjectAddress;
708+
}
709+
else
710+
ereport(ERROR,
711+
errcode(ERRCODE_DUPLICATE_OBJECT),
712+
errmsg("policy \"%s\" for table \"%s\" already exists",
713+
stmt->policy_name, get_rel_name(table_id)));
714+
}
715+
665716
/* Open target_table to build quals. No additional lock is necessary. */
666717
target_table = relation_open(table_id, NoLock);
667718

@@ -691,34 +742,6 @@ CreatePolicy(CreatePolicyStmt *stmt)
691742
assign_expr_collations(qual_pstate, qual);
692743
assign_expr_collations(with_check_pstate, with_check_qual);
693744

694-
/* Open pg_policy catalog */
695-
pg_policy_rel = table_open(PolicyRelationId, RowExclusiveLock);
696-
697-
/* Set key - policy's relation id. */
698-
ScanKeyInit(&skey[0],
699-
Anum_pg_policy_polrelid,
700-
BTEqualStrategyNumber, F_OIDEQ,
701-
ObjectIdGetDatum(table_id));
702-
703-
/* Set key - policy's name. */
704-
ScanKeyInit(&skey[1],
705-
Anum_pg_policy_polname,
706-
BTEqualStrategyNumber, F_NAMEEQ,
707-
CStringGetDatum(stmt->policy_name));
708-
709-
sscan = systable_beginscan(pg_policy_rel,
710-
PolicyPolrelidPolnameIndexId, true, NULL, 2,
711-
skey);
712-
713-
policy_tuple = systable_getnext(sscan);
714-
715-
/* Complain if the policy name already exists for the table */
716-
if (HeapTupleIsValid(policy_tuple))
717-
ereport(ERROR,
718-
(errcode(ERRCODE_DUPLICATE_OBJECT),
719-
errmsg("policy \"%s\" for table \"%s\" already exists",
720-
stmt->policy_name, RelationGetRelationName(target_table))));
721-
722745
policy_id = GetNewOidWithIndex(pg_policy_rel, PolicyOidIndexId,
723746
Anum_pg_policy_oid);
724747
values[Anum_pg_policy_oid - 1] = ObjectIdGetDatum(policy_id);

‎src/backend/parser/gram.y‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5980,7 +5980,7 @@ AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generi
59805980
/*****************************************************************************
59815981
*
59825982
* QUERIES:
5983-
* CREATE POLICY name ON table
5983+
* CREATE POLICY [IF NOT EXISTS] name ON table
59845984
* [AS { PERMISSIVE | RESTRICTIVE } ]
59855985
* [FOR { SELECT | INSERT | UPDATE | DELETE } ]
59865986
* [TO role, ...]
@@ -5998,6 +5998,7 @@ CreatePolicyStmt:
59985998
CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
59995999

60006000
n->policy_name = $3;
6001+
n->if_not_exists = false;
60016002
n->table = $5;
60026003
n->permissive = $6;
60036004
n->cmd_name = $7;
@@ -6006,6 +6007,22 @@ CreatePolicyStmt:
60066007
n->with_check = $10;
60076008
$$ = (Node *) n;
60086009
}
6010+
| CREATE POLICY IF_P NOT EXISTS name ON qualified_name RowSecurityDefaultPermissive
6011+
RowSecurityDefaultForCmd RowSecurityDefaultToRole
6012+
RowSecurityOptionalExpr RowSecurityOptionalWithCheck
6013+
{
6014+
CreatePolicyStmt *n = makeNode(CreatePolicyStmt);
6015+
6016+
n->policy_name = $6;
6017+
n->if_not_exists = true;
6018+
n->table = $8;
6019+
n->permissive = $9;
6020+
n->cmd_name = $10;
6021+
n->roles = $11;
6022+
n->qual = $12;
6023+
n->with_check = $13;
6024+
$$ = (Node *) n;
6025+
}
60096026
;
60106027

60116028
AlterPolicyStmt:

‎src/include/nodes/parsenodes.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,6 +3112,7 @@ typedef struct CreatePolicyStmt
31123112
{
31133113
NodeTag type;
31143114
char *policy_name; /* Policy's name */
3115+
bool if_not_exists; /* just do nothing if it already exists? */
31153116
RangeVar *table; /* the table name the policy applies to */
31163117
char *cmd_name; /* the command name the policy applies to */
31173118
bool permissive; /* restrictive or permissive policy */

‎src/test/regress/expected/rowsecurity.out‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ ALTER TABLE document ENABLE ROW LEVEL SECURITY;
380380
-- user's security level must be higher than or equal to document's
381381
CREATE POLICY p1 ON document AS PERMISSIVE
382382
USING (dlevel <= (SELECT seclv FROM uaccount WHERE pguser = current_user));
383+
CREATE POLICY p1 ON document AS PERMISSIVE USING (true); --error
384+
ERROR: policy "p1" for table "document" already exists
385+
CREATE POLICY IF NOT EXISTS p1 ON document AS PERMISSIVE USING (true); --notice, no-op
386+
NOTICE: policy "p1" for table "document" already exists
383387
-- try to create a policy of bogus type
384388
CREATE POLICY p1 ON document AS UGLY
385389
USING (dlevel <= (SELECT seclv FROM uaccount WHERE pguser = current_user));

‎src/test/regress/sql/rowsecurity.sql‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ ALTER TABLE document ENABLE ROW LEVEL SECURITY;
247247
CREATE POLICY p1 ON document AS PERMISSIVE
248248
USING (dlevel <= (SELECT seclv FROM uaccount WHERE pguser = current_user));
249249

250+
CREATE POLICY p1 ON document AS PERMISSIVE USING (true); --error
251+
CREATE POLICY IF NOT EXISTS p1 ON document AS PERMISSIVE USING (true); --notice, no-op
252+
250253
-- try to create a policy of bogus type
251254
CREATE POLICY p1 ON document AS UGLY
252255
USING (dlevel <= (SELECT seclv FROM uaccount WHERE pguser = current_user));

0 commit comments

Comments
 (0)