功能介绍

创建视图时,指定FORCE选项,则视图的基表不存在、引用了不存在的对象类型或者当前模式的拥有者不具有创建视图的权限时,可以成功创建视图,并伴随着警告,语法如下:

CREATE [ OR REPLACE ] [ NO ] [ FORCE ] VIEW [ SCHEMA. ] <view_name>
( col1, col2, … , coln) --- 可省略
AS
SELECT 语句

但是在对视图进行SELECT、INSERT、UPDATE或DELETE语句之前,以上这些条件必须为真。

如果视图定义包含任何约束,并且基表不存在或者引用的对象类型不存在,创建FORCE VIEW将会失败。视图定义指定了不存在的约束,创建FORCE VIEW也会失败。

实现

系统表pg_foce_view

# 查看当前在使用的OID
grep -r ',[0-9]\{4,\},' src/include/catalog/  > ~/find.log
# psql中查看在用的OID
SELECT oid, relname FROM pg_class WHERE oid < 16384 ORDER BY oid DESC LIMIT 100; 
SELECT oid, typname FROM pg_type WHERE oid < 16384 ORDER BY oid DESC LIMIT 100; 
SELECT oid, proname FROM pg_proc WHERE oid < 16384 ORDER BY oid DESC LIMIT 100;

表结构

CATALOG(pg_force_view,9120,ForceViewRelationId)
{
    /* oid of force view */
    Oid         fvoid;
    /* see IDENT_CASE__xxx constants below */      
    char        ident_case;    
#ifdef CATALOG_VARLEN           /* variable-length fields start here */
    /* sql definition */
    text        source;        
#endif
} FormData_pg_force_view;

TOAST表

The Oversized-Attribute Storage Technique TOAST 机制是用来处理物理表中超大字段的存储问题的 The source column in your table is text, meaning it could contain a very long SQL query. Storing huge amounts of text directly inside the main table row makes the table bloated and slow to scan.

TOAST solves this by acting like a separate storage locker 📦. If the source text is too big, PostgreSQL automatically moves it to this special TOAST table and leaves a small “claim ticket” in the main pg_force_view row.

语法规则

oracle_parser/ora_gram.y

%type <boolean> OptViewForce
%type <node> ora_alter_view_cmd
%type <list> ora_alter_view_cmds

增加对应的类型标签, 增加对应的创建视图的语法规则 因为FORCE VIEW存在无效状态,所以在创建时还应该将FORCE VIEW的定义以文本的形式保存在ViewStmt中,以避免在使用复合语句语法树时获得错误的视图定义 无效状态的FORCE VIEW并不会因为满足视图定义的条件而自动将其状态转为有效状态,所以增加了通过ALTER VIEW的语法规则来完成视图的编译