云数据库 GAUSSDB-失效重编译:示例

时间:2024-11-13 14:46:35

示例

  • 一次性入库举例:
    --设置如下参数:
    SET ddl_invalid_mode='invalid';
    SET enable_force_create_obj=on;
    
    --新建函数,入参使用的类型不存在。
    CREATE OR REPLACE FUNCTION func1(var type_not_exist) RETURN int
    IS
    BEGIN
    RETURN NULL;
    END;
    /
    
    --执行结果。
    WARNING:  Type type_not_exist does not exist.
    WARNING:  Function created with compilation errors.
    CREATE FUNCTION
    
    --查询状态。
    SELECT proname,valid FROM pg_object obj JOIN pg_proc proc ON obj.object_oid = proc.oid AND proname = 'func1' AND propackageid = 0;
    
    --执行结果。
    proname | valid
    ---------+-------
    func1   | f
    (1 row)
    
    --新建PACKAGE,入参使用的类型不存在。
    CREATE OR REPLACE PACKAGE pkg
    IS
    FUNCTION func1(var type_not_exist) RETURN int;
    END pkg;
    /
    
    --执行结果。
    WARNING:  Type type_not_exist does not exist.
    WARNING:  Package created with compilation errors.
    CREATE PACKAGE
    CREATE OR REPLACE PACKAGE BODY pkg
    IS
    FUNCTION func1(var type_not_exist) RETURN int AS
    DECLARE
    BEGIN
    RETURN NULL;
    END;
    END pkg;
    /
    
    --执行结果。
    WARNING:  Type type_not_exist does not exist.
    WARNING:  Package Body created with compilation errors.
    CREATE PACKAGE BODY
    
    --查询状态。
    SELECT pkgname,object_type,valid FROM pg_object obj JOIN gs_package pkg ON obj.object_oid = pkg.oid AND pkgname = 'pkg';
    pkgname | object_type | valid
    ---------+-------------+-------
    pkg     | B           | f
    pkg     | S           | f
    (2 rows)
    
    --重新创建入参使用的类型。
    CREATE TYPE type_not_exist AS (f1 int, f2 text);
  • 失效重编译功能举例
    --运行失效重编译高级包。
    CALL pkg_util.gs_compile_schema('public', false, 1);
    
    --执行结果。
    INFO:  The number of executions is 1
    INFO:  successful
    gs_compile_schema
    -------------------
    
    (1 row)
    
    --查询状态。
    SELECT proname,valid FROM pg_object obj JOIN pg_proc proc ON obj.object_oid = proc.oid AND proname = 'func1' AND propackageid = 0;
    proname | valid
    ---------+-------
    func1   | t
    (1 row)
    SELECT pkgname,object_type,valid FROM pg_object obj JOIN gs_package pkg ON obj.object_oid = pkg.oid AND pkgname = 'pkg';
    pkgname | object_type | valid
    ---------+-------------+-------
    pkg     | S           | t
    pkg     | B           | t
    (2 rows)
    
    --执行函数。
    CALL func1(null);
    func1
    -------
    
    (1 row)
    CALL pkg.func1(null);
    func1
    -------
    
    (1 row)
    
    --删除已创建的对象。
    DROP FUNCTION func1;
    DROP PACKAGE pkg;
    DROP TYPE type_not_exist;
  • 级联失效功能举例
    CREATE TYPE type1 AS (f1 int, f2 text);
    CREATE OR REPLACE FUNCTION func1(var type1) RETURN varchar
    IS
    BEGIN
        RETURN NULL;
    END;
    /
    SELECT proname,valid FROM pg_object obj JOIN pg_proc proc ON obj.object_oid = proc.oid AND proname = 'func1' AND propackageid = 0;
     proname | valid 
    ---------+-------
     func1   | t
    (1 row)
    DROP TYPE type1;
    
    --查询状态。
    SELECT proname,valid FROM pg_object obj JOIN pg_proc proc ON obj.object_oid = proc.oid AND proname = 'func1' AND propackageid = 0;
     proname | valid 
    ---------+-------
     func1   | f
    (1 row)
    
    --函数未被删除,函数依然存在。
    SELECT proname FROM pg_proc WHERE proname = 'func1' AND propackageid = 0;
     proname 
    ---------
     func1
    (1 row)
    
    --删除已创建的对象。
    DROP FUNCTION func1;
support.huaweicloud.com/centralized-devg-v8-gaussdb/gaussdb-42-1719.html