更新时间:2024-05-31 GMT+08:00
分享

GUC使用说明

数据库提供了许多GUC参数,配置这些参数可以影响数据库系统的行为。在修改这些参数时请确保用户理解了这些参数对数据库的影响,否则可能会导致无法预料的结果。

注意事项

  • 参数中如果取值范围为字符串,此字符串应遵循操作系统的路径和文件名命名规则。
  • 取值范围最大值为INT_MAX的参数,此选项最大值跟所在的操作系统有关。
  • 取值范围最大值为DBL_MAX的参数,此选项最大值跟所在的操作系统有关。
  • 部分GUC参数会影响函数/操作符的选择、存储过程的编译以及执行计划的生成,从而会影响到视图、函数参数默认值、存储过程编译产物、计划缓存的行为。由于这些机制原因,后续GUC参数变更可能不会再影响这些行为。
    • 视图:数据库在视图定义时会根据当前GUC参数状态生成重写规则,后续对视图操作时直接使用对应重写规则而不再受GUC参数(会影响重写规则生成的参数)的影响。
    • 函数参数默认值:如果函数参数默认值使用了函数,数据库在创建该函数时会根据当前GUC参数状态为默认值参数选择对应的函数并记录选中的函数oid到对应系统表中(pg_proc),后续执行该函数时会使用系统表中记录的函数oid给默认值参数赋值而不再受GUC参数的影响。
    • 存储过程编译产物:数据库对存储过程进行编译时会根据当前GUC参数状态生成编译产物,执行存储过程时直接使用编译产物而不再受GUC参数(会影响编译产物生成的参数)的影响。
    • 计划缓存:数据库在执行SQL时会根据当前GUC参数状态生成对应的执行计划,如果将该计划缓存起来后,后续再执行该SQL将直接使用该计划而不再受GUC参数(会影响执行计划生成的参数)的影响。

    有影响GUC参数如下表(GUC参数具体功能详见对应GUC参数):

    GUC参数

    打开参数

    关闭参数

    影响点

    示例

    convert_string_to_digit

    优先将字符串转为数字。

    不支持打开参数时优先将字符串转为数字的行为。

    视图、函数参数默认值、存储过程编译产物、计划缓存。

    CREATE or REPLACE FUNCTION test(c numeric) RETURN text package
    AS
    BEGIN
    RETURN 'test(c numeric)';
    END;
    /
    CREATE or REPLACE FUNCTION test(c varchar2) RETURN text package
    AS
    BEGIN
    RETURN 'test(c varchar2)';
    END;
    /
    
    SET convert_string_to_digit=on;
    CREATE or REPLACE VIEW test_view AS SELECT test('123'::text);
    
    SELECT test('123'::text);
    test
    -----------------
    test(c numeric)
    (1 row)
    
    SELECT * FROM test_view;
    test
    -----------------
    test(c numeric)
    (1 row)
    
    --关闭参数
    SET convert_string_to_digit=off;
    
    SELECT test('123'::text);
    test
    ------------------
    test(c varchar2)
    (1 row)
    
    --视图行为和直接调用函数行为不一致
    SELECT * FROM test_view;
    test
    -----------------
    test(c numeric)
    (1 row))

    set behavior_compat_options = 'current_sysdate';

    SYSDATE函数获取当前操作系统时间(底层使用current_sysdate函数)。

    SYSDATE函数获取当前数据库时间(底层使用sysdate函数)。

    视图、函数参数默认值、存储过程编译产物、计划缓存。

    SET behavior_compat_options = 'current_sysdate';
    CREATE or REPLACE VIEW test_view_SYSDATE AS SELECT SYSDATE AS test;
    SELECT * FROM test_view_SYSDATE;--返回当前操作系统时间
    SELECT SYSDATE;--返回当前操作系统时间
    --关闭参数
    SET behavior_compat_options = '';
    --视图行为和直接调用函数行为不一致
    SELECT * FROM test_view_SYSDATE;--返回当前操作系统时间
    SELECT SYSDATE;--返回当前数据库时间

    set a_format_version='10c';

    set a_format_dev_version='s1';

    • 新增支持NVL2。
    • 新增部分系统函数:具体见a_format_disable_func参数支持的被禁用系统函数。
    • case when返回类型有变更。
    • 集合类型构造器优先级高于函数。
    • 支持timestamp到timestamptz的隐式转换。

    不支持打开参数时功能。

    视图、函数参数默认值、存储过程编译产物、计划缓存。

    SET a_format_version='10c';
    SET a_format_dev_version='s1';
    
    CREATE OR REPLACE VIEW test_view_nvl2 AS SELECT nvl2(1,2,3) AS test;
    SELECT * FROM test_view_nvl2;
    test
    ------
    2
    (1 row)
    
    RESET a_format_dev_version;
    
    SELECT * FROM test_view_nvl2;
    test
    ------
    2
    (1 row)

    set a_format_version='10c';

    set a_format_dev_version='s2';

    • 新增支持CURRENT_TIMESTAMP '(' FCONST ')'。
    • 新增支持DBTIMEZONE。
    • 新增支持LNNVL。
    • CURRENT_DATE(a_format_date_timestamp =off 情况下):返回日期+时间(timestamp)。
    • 新增部分系统函数:具体见a_format_disable_func参数支持的被禁用系统函数。

    不支持打开参数时功能。

    视图、函数参数默认值、存储过程编译产物、计划缓存。

    SET a_format_version='10c';
    SET a_format_dev_version='s2';
    
    CREATE or REPLACE VIEW test_view_LNNVL AS SELECT LNNVL(123=123) AS test;
    SELECT * FROM test_view_LNNVL;
    
    RESET a_format_dev_version;
    
    SELECT * FROM test_view_LNNVL;

    set a_format_version='10c';

    set a_format_dev_version='s4';

    新增部分系统函数:具体见a_format_disable_func参数支持的被禁用系统函数。

    不支持打开参数时功能。

    视图、函数参数默认值、存储过程编译产物、计划缓存。

    -

    set a_format_version='10c';

    set a_format_dev_version='s5';

    • 新增部分系统函数:具体见a_format_disable_func参数支持的被禁用系统函数。
    • 复合类型构造器优先级高于函数。

    不支持打开参数时功能。

    视图、函数参数默认值、存储过程编译产物、计划缓存。

    --复合类型构造器优先级高于函数:
    CREATE TYPE tt AS  (
    val1 int,
    val2 int
    );
    CREATE OR REPLACE FUNCTION tt(va int, vb int)
    RETURN int IS
    ret int;
    BEGIN
    ret := va;
    RETURN ret;
    END;
    /
    
    SET a_format_version='10c';
    SET a_format_dev_version='s5';
    
    --赋值给tt变量
    CREATE OR REPLACE FUNCTION test0
    RETURN int IS
    va  tt;
    ret int;
    BEGIN
    va := tt(1,2);
    RAISE INFO 'tt: %' ,va;
    ret := 9;
    RETURN ret;
    END;
    /
    
    SELECT test0();
    INFO:  tt: (1,2)
    test0
    -------
    9
    (1 row)
    
    SET a_format_version='';
    SET a_format_dev_version='';
    
    SELECT test0();
    INFO:  tt: (1,2)
    test0
    -------
    9
    (1 row)
    
    --赋值给tt变量
    CREATE OR REPLACE FUNCTION test0
    RETURN int IS
    va  tt;
    ret int;
    BEGIN
    va := tt(1,2);
    RAISE INFO 'tt: %' ,va;
    ret := 9;
    RETURN ret;
    END;
    /
    
    select test0();
    ERROR:  cannot assign non-composite value to a row variable

    set behavior_compat_options = 'enable_bpcharlikebpchar_compare';

    启用bpcharlikebpchar和bpcharnlikebpchar操作符。

    关闭bpcharlikebpchar和bpcharnlikebpchar操作符。

    视图、存储过程编译产物、计划缓存。

    CREATE TABLE op_test (
    col BPCHAR(2) DEFAULT NULL
    );
    CREATE INDEX op_index ON op_test(col);
    
    INSERT INTO op_test VALUES ('a');
    INSERT INTO op_test VALUES ('1');
    INSERT INTO op_test VALUES ('11');
    INSERT INTO op_test VALUES ('12');
    INSERT INTO op_test VALUES ('sd');
    INSERT INTO op_test VALUES ('aa');
    
    SET behavior_compat_options = 'enable_bpcharlikebpchar_compare';
    
    EXPLAIN (COSTS OFF) SELECT * FROM op_test WHERE col LIKE col::BPCHAR ORDER BY col;
    QUERY PLAN
    ------------------------------
    Sort
    Sort Key: col
    ->  Seq Scan on op_test
    Filter: (col ~~ col)
    (4 rows)
    
    CREATE OR REPLACE VIEW test_view_bpchar AS SELECT * FROM op_test WHERE col LIKE col::BPCHAR ORDER BY col;
    
    EXPLAIN SELECT * FROM test_view_bpchar;
                               QUERY PLAN                           
    ----------------------------------------------------------------
     Sort  (cost=34.48..34.50 rows=10 width=12)
       Sort Key: op_test.col
       ->  Seq Scan on op_test  (cost=0.00..34.31 rows=10 width=12)
             Filter: (col ~~ col)
    (4 rows)
    
    SET behavior_compat_options = '';
    
    EXPLAIN (COSTS OFF) SELECT * FROM op_test WHERE col LIKE col::BPCHAR ORDER BY col;
    QUERY PLAN
    --------------------------------------
    Sort
    Sort Key: col
    ->  Seq Scan on op_test
    Filter: (col ~~ (col)::text)
    (4 rows)
    
    EXPLAIN SELECT * FROM test_view_bpchar;
    QUERY PLAN
    ----------------------------------------------------------------
    Sort  (cost=34.48..34.50 rows=10 width=12)
    Sort Key: op_test.col
    ->  Seq Scan on op_test  (cost=0.00..34.31 rows=10 width=12)
    Filter: (col OPERATOR(pg_catalog.~~) col)
    (4 rows)
    
    --关闭开关的情况下重新创建视图:
    CREATE OR REPLACE VIEW test_view_bpchar AS SELECT * FROM op_test WHERE col LIKE col::BPCHAR ORDER BY col;
    
    explain select * from test_view_bpchar;
    QUERY PLAN
    ----------------------------------------------------------------
    Sort  (cost=39.34..39.37 rows=10 width=12)
    Sort Key: op_test.col
    ->  Seq Scan on op_test  (cost=0.00..39.17 rows=10 width=12)
    Filter: (col ~~ (col)::text)
    (4 rows)

    set b_format_version='5.7';

    set b_format_dev_version='s1';

    • CURDATE、CURRENT_TIME、CURRENT_TIMESTAMP、LOCALTIME、LOCALTIMESTAMP NOW返回语句执行时间。
    • SYSDATE返回函数执行时间。

    函数返回事务开始时间。

    视图、函数参数默认值、存储过程编译产物、计划缓存。

    -

    set b_format_version='5.7';

    set b_format_dev_version='s1';

    DB_JSOBJ、LAST_DAY_FUNC、EXTRACT、TIMESTAMPDIFF、SUBSTRING函数行为变更为与B模式数据库一致。

    恢复默认行为。

    视图、函数参数默认值、存储过程编译产物、计划缓存。

    -

    set behavior_compat_options = 'bind_procedure_searchpath';

    • 未指定模式名的存储过程中的数据库对象的搜索路径配置项。
    • 在存储过程中如果不显示指定模式名,会优先在存储过程所属的模式下搜索。

    恢复默认行为。

    存储过程编译产物。

    -

    set behavior_compat_options = 'enable_out_param_override';

    控制存储过程出参的重载行为。

    恢复默认行为。

    存储过程编译产物。

    -

    enable_bitmapscan

    scan算子选择bitmapscan。

    scan算子不选择bitmapscan。

    计划缓存。

    SET enable_auto_explain=true;
    SET auto_explain_level=notice;
    CREATE TABLE t1( a int, b int, c int,d varchar)WITH(storage_type=ustore);
    CREATE TABLE t2( a int, b int, c int,d varchar)WITH(storage_type=ustore);
    INSERT INTO t1 SELECT i,10*random()*i,i%20,'atr'||i from generate_series(1,1000) i;
    CREATE INDEX index1 ON t1(a);
    
    SET enable_bitmapscan=on;
    SET enable_seqscan=off;
    SET enable_indexscan=off;
    CREATE OR REPLACE PROCEDURE proc_while_loop()
    AS
    DECLARE
    i int :=1;
    BEGIN
    WHILE i < 5 LOOP
    INSERT INTO t2 SELECT * FROM t1 WHERE a = i ;
    i:=i+1;
    END LOOP;
    END;
    /
    CALL proc_while_loop();
    SET enable_bitmapscan=off;
    CALL proc_while_loop();
    SET enable_bitmapscan=on;
    CALL proc_while_loop();

    enable_indexscan

    scan算子选择indexscan。

    scan算子不选择indexscan。

    计划缓存。

    SET enable_auto_explain=true;
    SET auto_explain_level=notice;
    CREATE TABLE t1( a int, b int, c int,d varchar)WITH(storage_type=ustore);
    CREATE TABLE t2( a int, b int, c int,d varchar)WITH(storage_type=ustore);
    INSERT INTO t1 SELECT i,10*random()*i,i%20,'atr'||i from generate_series(1,1000) i;
    CREATE INDEX index1 on t1(a);
    SET enable_bitmapscan=off;
    SET enable_seqscan=off;
    CREATE OR REPLACE PROCEDURE proc_while_loop()
    AS
    DECLARE
    i int :=1;
    BEGIN
    WHILE i < 5 LOOP
    insert into t2 select * from t1 where a = i ;
    i:=i+1;
    END LOOP;
    END;
    /
    CALL proc_while_loop();
    SET enable_indexscan=off;
    CALL proc_while_loop();

    query_Dop

    用户自定义的查询并行度。开启SMP功能,系统会使用设定的并行度执行。

    默认值为1,1表示关闭并行查询。

    计划缓存。

    SET enable_auto_explain=true;
    SET auto_explain_level=notice;
    CREATE TABLE t1( a int, b int, c int,d varchar)WITH(storage_type=ustore);
    CREATE TABLE t2( a int, b int, c int,d varchar)WITH(storage_type=ustore);
    INSERT INTO t1 SELECT i,10*random()*i,i%20,'atr'||i from generate_series(1,1000) i;
    CREATE INDEX index1 ON t1(a);
    SET enable_bitmapscan=f;
    CREATE OR REPLACE PROCEDURE proc_while_loop()
    AS
    DECLARE
    i int :=1;
    BEGIN
    WHILE i < 5 LOOP
    INSERT INTO t2 SELECT /*+ indexscan(t1 index1) */* FROM t1 WHERE a = i ;
    i:=i+1;
    END LOOP;
    END;
    /
    SET enable_auto_explain=true;
    SET auto_explain_level=notice;
    SET client_min_messages=log;
    SET query_dop=4;
    SET sql_beta_feature='enable_plsql_smp';
    CALL proc_while_loop();
    SET enable_force_Smp=on;
    CALL proc_while_loop();
    SET query_Dop=6;
    CALL proc_while_loop();

    set plan_cache_mode = force_generic_plan

    pbe情况下,强制执行gplan。

    pbe情况下,不强制执行gplan。

    计划缓存。

    SET enable_auto_explain=true;
    SET auto_explain_level=notice;
    CREATE TABLE t1( a int, b int, c int,d varchar)WITH(storage_type=ustore);
    CREATE TABLE t2( a int, b int, c int,d varchar)WITH(storage_type=ustore);
    INSERT INTO t1 SELECT i,10*random()*i,i%20,'atr'||i from generate_series(1,1000) i;
    CREATE INDEX index1 ON t1(a);
    SET enable_bitmapscan=on;
    SET enable_seqscan=off;
    SET enable_indexscan=off;
    CREATE OR REPLACE PROCEDURE proc_while_loop()
    AS
    DECLARE
    i int :=1;
    BEGIN
    WHILE i < 5 LOOP
    insert into t2 select * from t1 where a = i ;
    i:=i+1;
    END LOOP;
    END;
    /
    CALL proc_while_loop();
    SET plan_cache_mode = force_generic_plan;
    PREPARE aa AS SELECT proc_while_loop();
    EXECUTE aa;
    SET enable_bitmapscan=off;
    EXECUTE aa;

    a_format_date_timestamp

    CURRENT_DATE返回当前SQL启动的时间戳。

    CURRENT_DATE返回事务开启的日期或日期及时间。

    视图、函数参数默认值、存储过程编译产物、计划缓存。

    SET a_format_date_timestamp=on;
    CREATE OR REPLACE PROCEDURE proc_while_loop()
    AS
    DECLARE
    i date;
    BEGIN
    i:=current_date;
    raise info 'step2:%',TO_CHAR(current_date, 'MM-DD-YYYY HH24:MI:SS');
    END;
    /
    CALL proc_while_loop();
    SET a_format_date_timestamp=off;
    CALL proc_while_loop();

    proc_implicit_for_loop_variable

    控制存储过程中FOR_LOOP查询语句行为设置此项时,在FOR rec IN query LOOP语句中,若rec已经定义,不会复用已经定义的rec变量,而是会重新建立一个新的变量。否则,会复用已经定义的rec变量,不会建立新的变量。

    恢复默认行为。

    存储过程编译产物。

    -

    adjust_systemview_priority

    同名视图优先访问用户自定义视图。

    同名视图优先访问系统视图。

    视图参数默认值。

    CREATE DATABASE database_test DBCOMPATIBILITY = 'A';
    SELECT datname,datcompatibility,dattimezone FROM pg_database;
    \c database_test
    CREATE TABLE t1(c int);
    CREATE VIEW dual AS SELECT * FROM t1;
    SHOW adjust_systemview_priority;
    SELECT * FROM dual;
    SET adjust_systemview_priority = on;
    SELECT * FROM dual;
分享:

    相关文档

    相关产品