diff --git a/src/backend/commands/lockcmds.c b/src/backend/commands/lockcmds.c index 1be3a0d1edb..dbd8cf47068 100644 --- a/src/backend/commands/lockcmds.c +++ b/src/backend/commands/lockcmds.c @@ -32,7 +32,8 @@ static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, Oid use static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid); static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg); -static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views); +static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, + List *ancestor_views); /* * LOCK TABLE @@ -216,12 +217,12 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context) (!strcmp(rte->eref->aliasname, "old") || !strcmp(rte->eref->aliasname, "new"))) continue; - /* Check infinite recursion in the view definition. */ + /* + * We might be dealing with a self-referential view. If so, we + * can just stop recursing, since we already locked it. + */ if (list_member_oid(context->ancestor_views, relid)) - ereport(ERROR, - (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), - errmsg("infinite recursion detected in rules for relation \"%s\"", - get_rel_name(relid)))); + continue; /* Check permissions with the view owner's privilege. */ aclresult = LockTableAclCheck(relid, context->lockmode, context->viewowner); @@ -239,7 +240,8 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context) get_rel_name(relid)))); if (rte->relkind == RELKIND_VIEW) - LockViewRecurse(relid, context->lockmode, context->nowait, context->ancestor_views); + LockViewRecurse(relid, context->lockmode, context->nowait, + context->ancestor_views); else if (rte->inh) LockTableRecurse(relid, context->lockmode, context->nowait, context->viewowner); } @@ -256,13 +258,14 @@ LockViewRecurse_walker(Node *node, LockViewRecurse_context *context) } static void -LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views) +LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, + List *ancestor_views) { LockViewRecurse_context context; - Relation view; Query *viewquery; + /* caller has already locked the view */ view = heap_open(reloid, NoLock); viewquery = get_view_query(view); diff --git a/src/test/regress/expected/lock.out b/src/test/regress/expected/lock.out index 9e719abaae0..25117b5f617 100644 --- a/src/test/regress/expected/lock.out +++ b/src/test/regress/expected/lock.out @@ -124,16 +124,14 @@ select relname from pg_locks l, pg_class c (2 rows) ROLLBACK; --- detecting infinite recursions in view definitions +-- Verify that we cope with infinite recursion in view definitions. CREATE OR REPLACE VIEW lock_view2 AS SELECT * from lock_view3; BEGIN TRANSACTION; LOCK TABLE lock_view2 IN EXCLUSIVE MODE; -ERROR: infinite recursion detected in rules for relation "lock_view2" ROLLBACK; CREATE VIEW lock_view7 AS SELECT * from lock_view2; BEGIN TRANSACTION; LOCK TABLE lock_view7 IN EXCLUSIVE MODE; -ERROR: infinite recursion detected in rules for relation "lock_view2" ROLLBACK; -- Verify that we can lock a table with inheritance children. CREATE TABLE lock_tbl2 (b BIGINT) INHERITS (lock_tbl1); diff --git a/src/test/regress/sql/lock.sql b/src/test/regress/sql/lock.sql index 6cfa5865bc6..5fe18a1aff1 100644 --- a/src/test/regress/sql/lock.sql +++ b/src/test/regress/sql/lock.sql @@ -87,7 +87,7 @@ select relname from pg_locks l, pg_class c where l.relation = c.oid and relname like '%lock_%' and mode = 'ExclusiveLock' order by relname; ROLLBACK; --- detecting infinite recursions in view definitions +-- Verify that we cope with infinite recursion in view definitions. CREATE OR REPLACE VIEW lock_view2 AS SELECT * from lock_view3; BEGIN TRANSACTION; LOCK TABLE lock_view2 IN EXCLUSIVE MODE;