@@ -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 );
0 commit comments