@@ -285,7 +285,6 @@ static Query *create_dummy_view_query_for_broken_view(Oid viewOid);
285285static bool repair_broken_view_recursive (Oid viewOid , List * visitedViews );
286286static bool is_dummy_view (Oid viewOid );
287287static bool update_bbf_view_flags (Oid viewOid , uint64 flags_to_set , uint64 flags_to_clear , bool update_validity );
288- // static HeapTuple get_bbf_view_def_tuple(Oid viewOid);
289288static Oid get_view_oid_from_rule (Oid ruleOid );
290289
291290/* Save hook values in case of unload */
@@ -3173,7 +3172,7 @@ bbf_object_access_hook(ObjectAccessType access, Oid classId, Oid objectId, int s
31733172
31743173 obj .classId = RelationRelationId ;
31753174 obj .objectId = objectId ;
3176- obj .objectSubId = 0 ;
3175+ obj .objectSubId = subId ;
31773176
31783177 /* Call view dependency handling function */
31793178 handle_bbf_view_binding_on_object_drop (& obj , NULL , NULL );
@@ -6949,7 +6948,7 @@ bbf_view_set_broken(Oid viewOid, bool mark_broken)
69496948 (flag_validity & BBF_VIEW_DEF_FLAG_IS_WEAK_VIEW ) != 0 ;
69506949
69516950 /* Cannot mark a strong-bound view as broken */
6952- if (mark_broken && !is_weak_view && ! pltsql_weak_view_binding )
6951+ if (mark_broken && !is_weak_view )
69536952 {
69546953 ereport (ERROR ,
69556954 (errcode (ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST ),
@@ -7014,8 +7013,7 @@ is_dummy_view(Oid viewOid)
70147013 foreach (lc , viewQuery -> targetList )
70157014 {
70167015 TargetEntry * te = (TargetEntry * ) lfirst (lc );
7017- Const * con = (Const * ) te -> expr ;
7018- if (!te -> resjunk && !IsA (te -> expr , Const ) && !con -> constisnull )
7016+ if (te -> resjunk || !IsA (te -> expr , Const ) || !((Const * ) (te -> expr ))-> constisnull )
70197017 {
70207018 is_dummy = false;
70217019 break ;
@@ -7217,7 +7215,7 @@ repair_broken_views(Query *parsetree)
72177215 List * view_oids = NIL ;
72187216 bool repaired = false;
72197217
7220- if (sql_dialect != SQL_DIALECT_TSQL || !IS_TDS_CONN () || parsetree -> rtable == NIL )
7218+ if (sql_dialect != SQL_DIALECT_TSQL || !IS_TDS_CONN ())
72217219 return false;
72227220
72237221 /* Find all views referenced in the query, including those in subqueries and sublinks */
@@ -7333,12 +7331,15 @@ create_dummy_view_query_for_broken_view(Oid viewOid)
73337331bool
73347332handle_bbf_view_binding_on_object_drop (const ObjectAddress * droppedObject , Relation depRel , ViewStmt * stmt )
73357333{
7336- ScanKeyData key [2 ];
7334+ ScanKeyData key [3 ];
73377335 SysScanDesc scan ;
73387336 HeapTuple tup ;
73397337 List * processed_views = NIL ;
7338+ Oid viewOid = InvalidOid ;
73407339 bool is_alter_view = false;
73417340 bool is_weak_view = false;
7341+ bool processed = true;
7342+ int nkeys = 2 ;
73427343
73437344 /* Check if this is an ALTER VIEW operation */
73447345 is_alter_view = (stmt != NULL && stmt -> createOrAlter );
@@ -7356,46 +7357,42 @@ handle_bbf_view_binding_on_object_drop(const ObjectAddress *droppedObject, Relat
73567357 Anum_pg_depend_refobjid ,
73577358 BTEqualStrategyNumber , F_OIDEQ ,
73587359 ObjectIdGetDatum (droppedObject -> objectId ));
7360+ if (droppedObject -> objectSubId )
7361+ {
7362+ ScanKeyInit (& key [2 ],
7363+ Anum_pg_depend_refobjsubid ,
7364+ BTEqualStrategyNumber , F_INT4EQ ,
7365+ Int32GetDatum (droppedObject -> objectSubId ));
7366+ nkeys = 3 ;
7367+ }
73597368
7369+ // scan = systable_beginscan(depRel, DependReferenceIndexId, true,
7370+ // NULL, 2, key);
73607371 scan = systable_beginscan (depRel , DependReferenceIndexId , true,
7361- NULL , 2 , key );
7372+ NULL , nkeys , key );
73627373
73637374 /* Loop over all the direct dependents */
73647375 while ((tup = systable_getnext (scan )) != NULL )
73657376 {
73667377 Form_pg_depend depform = (Form_pg_depend ) GETSTRUCT (tup );
73677378
73687379 /* Only handle entries for pg_rewrite rules with NORMAL dependency */
7369- if (depform -> classid == RewriteRelationId &&
7370- depform -> deptype == DEPENDENCY_NORMAL )
7380+ if (depform -> classid != RewriteRelationId || depform -> deptype != DEPENDENCY_NORMAL )
7381+ continue ;
7382+
7383+ viewOid = get_view_oid_from_rule (depform -> objid );
7384+
7385+ if (OidIsValid (viewOid ) && !list_member_oid (processed_views , viewOid ))
73717386 {
7372- Oid viewOid = get_view_oid_from_rule (depform -> objid );
7373-
7374- if (OidIsValid (viewOid ) && !list_member_oid (processed_views , viewOid ))
7387+ processed_views = lappend_oid (processed_views , viewOid );
7388+ if (!is_alter_view )
73757389 {
7376- processed_views = lappend_oid (processed_views , viewOid );
7377- if (!is_alter_view )
7390+ /* DROP operation: if dependent is weak view, mark broken */
7391+ is_weak_view = false;
7392+ if (check_is_tsql_view (viewOid , & is_weak_view ) && is_weak_view )
73787393 {
7379- /* DROP operation: if dependent is weak view, mark broken */
7380- is_weak_view = false;
7381- if (check_is_tsql_view (viewOid , & is_weak_view ) && is_weak_view )
7382- {
7383- /* Mark view as broken */
7384- bool updated = bbf_view_set_broken (viewOid , true);
7385-
7386- /* If the view was successfully marked as broken, create a dummy query */
7387- if (updated )
7388- {
7389- Query * dummyQuery = create_dummy_view_query_for_broken_view (viewOid );
7390- StoreViewQuery (viewOid , dummyQuery , true);
7391- }
7392- CommandCounterIncrement ();
7393- }
7394- }
7395- else
7396- {
7397- /* ALTER operation: mark weak & broken */
7398- bool updated = bbf_view_set_broken (viewOid , true);
7394+ /* Mark view as broken */
7395+ bool updated = bbf_view_set_broken (viewOid , true);
73997396
74007397 /* If the view was successfully marked as broken, create a dummy query */
74017398 if (updated )
@@ -7404,16 +7401,35 @@ handle_bbf_view_binding_on_object_drop(const ObjectAddress *droppedObject, Relat
74047401 StoreViewQuery (viewOid , dummyQuery , true);
74057402 }
74067403 CommandCounterIncrement ();
7407- update_bbf_view_flags (viewOid , BBF_VIEW_DEF_FLAG_IS_WEAK_VIEW , 0 , true);
74087404 }
74097405 }
7406+ else if (is_alter_view && pltsql_weak_view_binding )
7407+ {
7408+ /* ALTER operation: mark weak & broken */
7409+ update_bbf_view_flags (viewOid , BBF_VIEW_DEF_FLAG_IS_WEAK_VIEW , 0 , true);
7410+ CommandCounterIncrement ();
7411+
7412+ bool updated = bbf_view_set_broken (viewOid , true);
7413+
7414+ /* If the view was successfully marked as broken, create a dummy query */
7415+ if (updated )
7416+ {
7417+ Query * dummyQuery = create_dummy_view_query_for_broken_view (viewOid );
7418+ StoreViewQuery (viewOid , dummyQuery , true);
7419+ }
7420+ }
7421+ else
7422+ {
7423+ processed = false;
7424+ break ;
7425+ }
74107426 }
74117427 }
74127428 systable_endscan (scan );
74137429 table_close (depRel , AccessShareLock );
74147430 list_free (processed_views );
74157431 CommandCounterIncrement ();
7416- return true ;
7432+ return processed ;
74177433}
74187434
74197435/*
0 commit comments