目录
force_view是视图的无效状态,而视图是基于SQL语句结果集的可视化的表,其内容由查询定义。视图有无效和有效两种状态。使用一个无效视图(force_view)时候,若该无效视图经过重新编译依然无效,那么将会抛出错误信息。
创建一个无效视图:
使用create force view创建一个无效视图。创建force view时若依赖检查成功,则创建为一个有效视图,否则创建为一个无效视图。这里依赖检查就是视图编译,视图编译阶段的检查主要包括:依赖对象是否存在、创建者是否有依赖对象的select权限等。create force view vtab as select * from tab; --tab不存在
WARNING: View created with compilation errors
CREATE VIEW
select reloptions from sys_class where relname = 'vtab';
reloptions
----------------
{status=false}
有效视图变成无效视图:
删除视图依赖,改变视图依赖列等会使被依赖视图变成无效状态。
create or replace也可能会将有效视图替换为无效视图。
create table tab(a int);
CREATE TABLE
create force view vtab as select * from tab;
CREATE VIEW
select reloptions from sys_class where relname = 'vtab';
reloptions
------------
drop table tab;
NOTICE: view vtab depends on table tab
DROP TABLE
select reloptions from sys_class where relname = 'vtab';
reloptions
----------------
{status=false}
使用(查询,插入,更新等)一个无效视图时,会重新编译该视图。若编译失败视图依然无效,抛出错误。create force view vtab as select * from tab; --tab不存在
WARNING: View created with compilation errors
CREATE VIEW
select reloptions from sys_class where relname = 'vtab';
reloptions
----------------
{status=false}
select * from vtab;
ERROR: view "vtab" is still invalid after recompiling
重新编译后有效:
使用一个无效视图时候,会重新编译该视图。若编译成功,视图有效,按用户指令操作该视图。create force view vtab as select * from tab; --tab不存在
WARNING: View created with compilation errors
CREATE VIEW select reloptions from sys_class where relname = 'vtab';
reloptions
----------------
{status=false}
create table tab(a int);
CREATE TABLE
select * from vtab;
a
---
(0 rows)
select reloptions from sys_class where relname = 'vtab';
reloptions
---------------
{status=true}
创建视图时候若依赖检查成功则创建为一个有效视图。创建视图 (不带force ),依赖检查失败,视图创建会失败。create table tab(a int);
CREATE TABLE
create force view vtab as select * from tab;
CREATE VIEW
select reloptions from sys_class where relname = 'vtab';
reloptions
------------
create view vtab1 as select * from tab1;
ERROR: relation "tab1" does not exist
LINE 1: create view vtab1 as select * from tab1;
无效视图变成有效视图:
使用一个无效视图,若视图重新编译成功,视图变成有效视图。create force view vtab as select * from tab;
CREATE VIEW
select reloptions from sys_class where relname = 'vtab';
reloptions
----------------
{status=false}
create table tab(a int);
CREATE TABLE
select * from vtab;
a
---
(0 rows)
select reloptions from sys_class where relname = 'vtab';
reloptions
------------
create table tab(a int);
CREATE TABLE
insert into tab values(3);
INSERT 0 1
create view vtab as select * from tab;
CREATE VIEW
select reloptions from sys_class where relname = 'vtab';
reloptions
------------
select * from vtab;
a
---
3
create table tab(a int);
CREATE TABLE
create view vtab as select * from tab;
CREATE VIEW
create view vvtab as select * from vtab;
CREATE VIEW
select relname, reloptions from sys_class where relname in ('vtab', 'vvtab');
relname | reloptions
---------+------------
vtab |
vvtab |
(2 rows)
drop table tab;
NOTICE: there are(is) 2 objects that depend(s) on it
DETAIL: view vtab depends on table tab
view vvtab depends on view vtab
DROP TABLE
select relname, reloptions from sys_class where relname in ('vtab', 'vvtab');
relname | reloptions
---------+----------------
vtab | {status=false}
vvtab | {status=false}
(2 rows)