华为云用户手册

  • gs_wlm_node_recover(boolean isForce) 描述:动态资源管理模式下,对CCN管控计数和作业信息进行更新恢复。该函数仅支持管理员执行,通常用于CN实例故障重启后的实例恢复,由集群管理组件(CM)调用,不建议用户直接调用。具体功能如下: CN执行:通知CCN清理该CN执行的作业信息和作业对应的管控计数信息。 CCN执行:重置管控计数信息,并从CN上获取最新的慢车道作业信息。 返回值类型:bool
  • gs_wlm_get_queryband_action(cstring) 描述:查询query_band关联行为和次序。 返回值类型:record 函数返回字段如下: 名称 类型 描述 qband cstring query_band键值对。 respool_id Oid query band关联资源池OID。 respool text query band关联资源池名。 priority text query band关联队列内优先级。 qborder int4 query_band搜索次序。 示例: 12345 SELECT * FROM gs_wlm_get_queryband_action('a=1');qband | respool_id | respool | priority | qborder-------+------------+---------+----------+--------- a=1 | 16388 | p1 | Medium | -1(1 row)
  • gs_cgroup_reload_conf() 描述:在当前实例上进行cgroup配置文件在线加载。 返回值类型:record 函数返回字段如下: 名称 类型 描述 node_name text 实例名称。 node_host text 实例所在节点的IP地址。 result text cgroup在线加载是否成功。 示例: 1234 SELECT * FROM gs_cgroup_reload_conf(); node_name | node_host | result-----------+----------------+--------- cn_5001 | 192.168.178.35 | success
  • pgxc_cgroup_reload_conf() 描述:在系统所有实例上进行cgroup配置文件在线加载。 返回值类型:record 函数返回字段如下: 名称 类型 描述 node_name text 实例名称。 node_host text 实例所在节点的IP地址。 result text cgroup在线加载是否成功。 示例: 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243444546 SELECT * FROM pgxc_cgroup_reload_conf(); node_name | node_host | result--------------+-----------------+--------- dn_6025_6026 | 192.168.178.177 | success dn_6049_6050 | 192.168.179.79 | success dn_6051_6052 | 192.168.179.79 | success dn_6055_6056 | 192.168.179.79 | success dn_6067_6068 | 192.168.181.57 | success dn_6023_6024 | 192.168.178.39 | success dn_6009_6010 | 192.168.181.21 | success dn_6011_6012 | 192.168.181.21 | success dn_6015_6016 | 192.168.181.21 | success dn_6029_6030 | 192.168.178.177 | success dn_6031_6032 | 192.168.178.177 | success dn_6045_6046 | 192.168.179.45 | success cn_5001 | 192.168.178.35 | success cn_5003 | 192.168.178.39 | success dn_6061_6062 | 192.168.181.179 | success cn_5006 | 192.168.179.45 | success cn_5004 | 192.168.178.177 | success cn_5002 | 192.168.181.21 | success cn_5005 | 192.168.178.187 | success dn_6019_6020 | 192.168.178.39 | success dn_6007_6008 | 192.168.178.35 | success dn_6071_6072 | 192.168.181.57 | success dn_6003_6004 | 192.168.178.35 | success dn_6013_6014 | 192.168.181.21 | success dn_6035_6036 | 192.168.178.187 | success dn_6037_6038 | 192.168.178.187 | success dn_6001_6002 | 192.168.178.35 | success dn_6063_6064 | 192.168.181.179 | success dn_6005_6006 | 192.168.178.35 | success dn_6057_6058 | 192.168.181.179 | success dn_6069_6070 | 192.168.181.57 | success dn_6027_6028 | 192.168.178.177 | success dn_6059_6060 | 192.168.181.179 | success dn_6041_6042 | 192.168.179.45 | success dn_6043_6044 | 192.168.179.45 | success dn_6047_6048 | 192.168.179.45 | success dn_6033_6034 | 192.168.178.187 | success dn_6065_6066 | 192.168.181.57 | success dn_6021_6022 | 192.168.178.39 | success dn_6017_6018 | 192.168.178.39 | success dn_6039_6040 | 192.168.178.187 | success dn_6053_6054 | 192.168.179.79 | success(42 rows)
  • gs_wlm_set_queryband_action(cstring,cstring,int4) 描述:设置query_band关联行为和次序。 返回值类型:boolean 函数入参字段如下: 名称 类型 描述 qband cstring query_band键值对,长度上限为63个字符。 action cstring query_band关联行为。 order int4 query_band搜索次序,缺省参数,默认为-1。 示例: 1 2 3 4 5 6 7 8 91011 SELECT * FROM gs_wlm_set_queryband_action('a=1','respool=p1'); gs_wlm_set_queryband_action----------------------------- t(1 row)SELECT * FROM gs_wlm_set_queryband_action('a=3','respool=p1;priority=rush',1); gs_wlm_set_queryband_action----------------------------- t(1 row)
  • gs_wlm_set_queryband_order(cstring,int4) 描述:设置query_band次序。 返回值类型:boolean 函数入参字段如下: 名称 类型 描述 qband cstring query_band键值对。 order int4 query_band搜索次序,缺省参数,默认为-1。 示例: 12345 SELECT * FROM gs_wlm_set_queryband_order('a=1',2); gs_wlm_set_queryband_action----------------------------- t(1 row)
  • pgxc_wlm_analyze_schema_space(cstring) 描述:在CN上查询某个逻辑集群下集群整体的Schema空间信息,入参为逻辑集群名称。 返回值类型:record 函数返回字段如下: 名称 类型 描述 schemaname text 模式名 databasename text 数据库名 nodegroup text 节点组名称 total_value bigint 该模式的集群空间总值 avg_value bigint 该模式的各实例空间平均值 skew_percent integer 倾斜率 extend_info text 提供信息包括:单实例空间最大值、单实例空间最小值以及最大最小空间所在的实例名 示例: 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435 SELECT * FROM pgxc_wlm_analyze_schema_space('group1'); schemaname | databasename | nodegroup | total_value | avg_value | skew_percent | extend_info--------------------+--------------+--------------+-------------+-----------+--------------+----------------------------------------------- pg_catalog | test1 | installation | 56819712 | 9469952 | 0 | min:9469952 datanode1,max:9469952 datanode1 public | postgres | installation | 150495232 | 25082538 | 0 | min:24903680 datanode6,max:25280512 datanode1 pg_toast | test1 | installation | 11157504 | 1859584 | 0 | min:1859584 datanode1,max:1859584 datanode1 cstore | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 data_redis | postgres | installation | 1966080 | 327680 | 50 | min:0 datanode4,max:655360 datanode1 data_redis | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 public | test1 | installation | 98304 | 16384 | 0 | min:16384 datanode1,max:16384 datanode1 dbms_om | postgres | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_job | postgres | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_om | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_job | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 sys | postgres | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 sys | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 utl_file | postgres | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 utl_raw | postgres | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_sql | postgres | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_output | postgres | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_random | postgres | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_lob | postgres | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 information_schema | postgres | installation | 1769472 | 294912 | 0 | min:294912 datanode1,max:294912 datanode1 information_schema | test1 | installation | 1769472 | 294912 | 0 | min:294912 datanode1,max:294912 datanode1 utl_file | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_output | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_random | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 utl_raw | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_sql | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 dbms_lob | test1 | installation | 0 | 0 | 0 | min:0 datanode1,max:0 datanode1 pg_catalog | postgres | installation | 75431936 | 12571989 | 3 | min:12124160 datanode4,max:13049856 datanode1 redisuser | postgres | installation | 1884160 | 314026 | 50 | min:16384 datanode4,max:630784 datanode1 pg_toast | postgres | installation | 17154048 | 2859008 | 7 | min:2637824 datanode4,max:3080192 datanode1 cstore | postgres | installation | 15294464 | 2549077 | 5 | min:2408448 datanode1,max:2703360 datanode6(31 rows)
  • pgxc_wlm_get_schema_space(cstring) 描述:在CN上查询某个逻辑集群下各实例的Schema空间信息,入参为逻辑集群名称。 返回值类型:record 函数返回字段如下: 名称 类型 描述 schemaname text 模式名 schemaid oid 模式OID databasename text 数据库名 databaseid oid 数据库OID nodename text 实例名称 nodegroup text 节点组名称 usedspace bigint 已使用的空间大小 permspace bigint 空间上限值 示例: 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839404142434445 SELECT * FROM pgxc_wlm_get_schema_space('group1'); schemaname | schemaid | databasename | databaseid | nodename | nodegroup | usedspace | permspace--------------------+----------+--------------+------------+--------------+--------------+-----------+----------- pg_catalog | 11 | test1 | 16384 | datanode1 | installation | 9469952 | -1 public | 2200 | postgres | 15253 | datanode1 | installation | 25280512 | -1 pg_toast | 99 | test1 | 16384 | datanode1 | installation | 1859584 | -1 cstore | 100 | test1 | 16384 | datanode1 | installation | 0 | -1 data_redis | 18106 | postgres | 15253 | datanode1 | installation | 655360 | -1 data_redis | 18116 | test1 | 16384 | datanode1 | installation | 0 | -1 public | 2200 | test1 | 16384 | datanode1 | installation | 16384 | -1 dbms_om | 3987 | postgres | 15253 | datanode1 | installation | 0 | -1 dbms_job | 3988 | postgres | 15253 | datanode1 | installation | 0 | -1 dbms_om | 3987 | test1 | 16384 | datanode1 | installation | 0 | -1 dbms_job | 3988 | test1 | 16384 | datanode1 | installation | 0 | -1 sys | 11693 | postgres | 15253 | datanode1 | installation | 0 | -1 sys | 11693 | test1 | 16384 | datanode1 | installation | 0 | -1 utl_file | 14644 | postgres | 15253 | datanode1 | installation | 0 | -1 utl_raw | 14669 | postgres | 15253 | datanode1 | installation | 0 | -1 dbms_sql | 14674 | postgres | 15253 | datanode1 | installation | 0 | -1 dbms_output | 14662 | postgres | 15253 | datanode1 | installation | 0 | -1 dbms_random | 14666 | postgres | 15253 | datanode1 | installation | 0 | -1 dbms_lob | 14701 | postgres | 15253 | datanode1 | installation | 0 | -1 information_schema | 14300 | postgres | 15253 | datanode1 | installation | 294912 | -1 information_schema | 14300 | test1 | 16384 | datanode1 | installation | 294912 | -1 utl_file | 14644 | test1 | 16384 | datanode1 | installation | 0 | -1 dbms_output | 14662 | test1 | 16384 | datanode1 | installation | 0 | -1 dbms_random | 14666 | test1 | 16384 | datanode1 | installation | 0 | -1 utl_raw | 14669 | test1 | 16384 | datanode1 | installation | 0 | -1 dbms_sql | 14674 | test1 | 16384 | datanode1 | installation | 0 | -1 dbms_lob | 14701 | test1 | 16384 | datanode1 | installation | 0 | -1 pg_catalog | 11 | postgres | 15253 | datanode1 | installation | 13049856 | -1 redisuser | 16387 | postgres | 15253 | datanode1 | installation | 630784 | -1 pg_toast | 99 | postgres | 15253 | datanode1 | installation | 3080192 | -1 cstore | 100 | postgres | 15253 | datanode1 | installation | 2408448 | -1 pg_catalog | 11 | test1 | 16384 | datanode2 | installation | 9469952 | -1 public | 2200 | postgres | 15253 | datanode2 | installation | 25214976 | -1 pg_toast | 99 | test1 | 16384 | datanode2 | installation | 1859584 | -1 cstore | 100 | test1 | 16384 | datanode2 | installation | 0 | -1 data_redis | 18106 | postgres | 15253 | datanode2 | installation | 655360 | -1 data_redis | 18116 | test1 | 16384 | datanode2 | installation | 0 | -1 public | 2200 | test1 | 16384 | datanode2 | installation | 16384 | -1 dbms_om | 3987 | postgres | 15253 | datanode2 | installation | 0 | -1 dbms_job | 3988 | postgres | 15253 | datanode2 | installation | 0 | -1 dbms_om | 3987 | test1 | 16384 | datanode2 | installation | 0 | -1 dbms_job | 3988 | test1 | 16384 | datanode2 | installation | 0 | -1
  • gs_increase_except_num(unique_sql_id int8,except_num int4) 描述:作业异常信息记录函数。入参要求必须大于0。调用该函数后会将作业异常次数加except_num,同时将作业最新异常时间更新为当前时间。 返回值类型:bool 示例: 12345 SELECT gs_increase_except_num(111, 4);gs_increase_except_num-----------------------t(1 row)
  • gs_remove_blocklist(unique_sql_id int8) 描述:用于从作业黑名单中移除一个作业。即从系统表GS_BLOCKLIST_QUERY中移除一个黑名单,并更新GS_BLOCKLIST_QUERY中黑名单信息。 返回值类型:bool 示例: 12345 SELECT gs_remove_blocklist(111);gs_append_blocklist--------------------t(1 row)
  • gs_append_blocklist(sql_hash text) 描述:用于新增作业黑名单。即在系统表GS_BLOCKLIST_SQL中新增一个黑名单,并更新GS_BLOCKLIST_SQL中黑名单信息。该函数仅9.1.0.200及以上集群版本支持。 返回值类型:bool 示例: 12345 SELECT gs_append_blocklist('sql_7bf6381d2a5a456a1936027198ad8b12');gs_append_blocklist--------------------t(1 row)
  • gs_remove_blocklist(sql_hash text) 描述:用于从作业黑名单中移除一个作业。即从系统表GS_BLOCKLIST_SQL中移除一个黑名单,并更新GS_BLOCKLIST_SQL中黑名单信息。该函数仅9.1.0.200及以上集群版本支持。 返回值类型:bool 示例: 12345 SELECT gs_remove_blocklist('sql_7bf6381d2a5a456a1936027198ad8b12');gs_remove_blocklist--------------------t(1 row)
  • gs_append_blocklist(unique_sql_id int8) 描述:用于新增作业黑名单。即在系统表GS_BLOCKLIST_QUERY中新增一个黑名单,并更新GS_BLOCKLIST_QUERY中黑名单信息。 返回值类型:bool 示例: 12345 SELECT gs_append_blocklist(111);gs_append_blocklist--------------------t(1 row)
  • gs_increase_sql_except_num(sql_hash text) 描述:作业异常信息记录函数。调用该函数后会将作业异常次数+1,同时将作业最新异常时间更新为当前时间。该函数仅9.1.0.200及以上集群版本支持,主要用于内部调用。 返回值类型:bool 示例: 12345 SELECT gs_increase_sql_except_num('sql_7bf6381d2a5a456a1936027198ad8b12');gs_increase_sql_except_num------------------------t(1 row)
  • gs_switch_respool(query_id bigint, resource_pool_name name) 描述:作业切换资源池函数,9.1.0及以上集群版本支持。 入参要求resource_pool_name必须小于64字符。调用该函数后,如果作业处于排队阶段,作业切到新资源池会进行判断是否需要启动运行;如果作业处于运行阶段,作业切到新资源池可以启动原资源池的排队作业,同时受到新资源池的CPU管控限制。 该函数同时支持静态管控和动态管控模式下的资源池切换,也支持短查询切换资源池。 返回值类型:bool 该函数仅支持管理员或者管理员权限用户使用,禁止普通用户使用,该函数的使用前提是集群的CPU管控功能正常,即Cgroup功能正常。可使用pgxc_cgroup_reload_conf()函数查询cgroup是否正常,如果返回结果有非success内容,则需要进行修复。 该函数切换资源池后,会按照新资源池的所属异常规则进行管控。 当作业处于全局并发排队,即waiting in global queue时,切换资源池后并不会立即生效,此时还未进入资源池排队队列。 作业CPU管控切换,支持CN和DN,比如:资源池A占用40个核,资源池B占用5个核,将作业从资源池A切换到资源池B后,作业会在资源池B占用的5个核上执行,此时作业运行会更慢,但是隔离了该作业,不会再影响资源池A原有的作业运行。即实现语句的降级处理。 切换资源池时,用户可能会使用如下类似语句进行批量切换,在使用此类语句操作时,需要确保修改的是同一类状态的语句,如切换的都是排队作业或者切换的都是运行作业。如果切换不同类的语句,建议用户分两次进行,如果混合起来一起切换会导致语句运行并发数超限,作业异常唤醒。 1 SELECT gs_switch_respool(queryid,'xxx') FROM xxx where xxx; 在切换语句资源池时,有以下约束: 切换资源池A运行的作业到资源池B,资源池A的排队队首作业会被唤醒,如果资源池B并发作业数达到上限,作业A1不会直接运行。 切换资源池A排队的作业A2到资源池B,如果资源池B并发作业数达到上限,作业A2会继续排队,如果未达到上限,则会被唤醒,直接运行。 资源池切换时仅支持同车道切换,不支持不同车道切换,如资源池A的快车道切换后会到资源池B的快车道中,资源池A的慢车道切换后会到资源池B的慢车道中,无论资源池B是否开启了短查询加速功能,如资源池B未开启则作业会直接运行。 该函数仅支持在所有节点正常的情况下使用,如果节点故障,该函数会执行失败。 示例: 查询ID为74590868828368101的语句在执行过程中,管理员将其切换至poolg1资源池,切换成功: 12345 CALL gs_switch_respool(74590868828368101,'poolg1'); gs_switch_respool ------------------- t(1 row)
  • gs_increase_except_num(unique_sql_id int8) 描述:作业异常信息记录函数。入参要求必须大于0。调用该函数后会将作业异常次数+1,同时将作业最新异常时间更新为当前时间。 返回值类型:bool 示例: 12345 SELECT gs_increase_except_num(111);gs_increase_except_num------------------------t(1 row)
  • gs_increase_sql_except_num(sql_hash text,except_num int4) 描述:作业异常信息记录函数。调用该函数后会将作业异常次数加except_num,同时将作业最新异常时间更新为当前时间。该函数仅9.1.0.200及以上集群版本支持,主要用于内部调用。 返回值类型:bool 示例: 12345 SELECT gs_increase_sql_except_num('sql_7bf6381d2a5a456a1936027198ad8b12', 4);gs_increase_sql_except_num-----------------------t(1 row)
  • gs_wlm_readjust_user_space(oid) 描述:用户永久存储空间校准函数。入参为用户oid,入参为0的情况下,会校准所有用户的永久存储空间。 返回值类型:text 示例: 12345 SELECT gs_wlm_readjust_user_space(0);gs_wlm_readjust_user_space----------------------------Exec Success(1 row)
  • pgxc_wlm_readjust_relfilenode_size_table() 描述:空间统计校准函数,不重建PG_RELFILENODE_SIZE系统表,重新校准用户和schema空间。 由于校准函数执行时与其余业务之间的事务隔离性,校准函数对其它的正在执行的业务不可见,导致校准函数会漏掉此类业务的空间变化,为避免校准之后出现空间差误,建议用户使用空间校准函数时,应选择空间大小稳定无变化的时候执行校准。 返回值类型:text 示例: 12345 SELECT pgxc_wlm_readjust_schema_space();pgxc_wlm_readjust_relfilenode_size_table-----------------------------------------Exec Success(1 row)
  • gs_wlm_rebuild_except_rule_hash() 描述:用于重建当前节点的异常规则内存hash表,异常规则的生效阈值由监控线程实时从内存hash获取,hash异常时可以使用该函数进行rebuild恢复。 返回值类型:bool 示例: 12345 SELECT gs_wlm_rebuild_except_rule_hash();gs_wlm_rebuild_except_rule_hash--------------------t(1 row)
  • gs_increase_except_num(unique_sql_id int8,except_num int4, except_time int8) 描述:作业异常信息记录函数。入参要求必须大于0。调用该函数后会将作业异常次数加except_num,同时将作业最新异常时间更新为except_time,except_time为时间戳,该函数主要用于内部调用。 返回值类型:bool 示例: 12345 SELECT gs_increase_except_num(111, 4, 714623414421256);gs_increase_except_num-----------------------t(1 row)
  • gs_increase_sql_except_num(sql_hash text,except_num int4, except_time int8) 描述:作业异常信息记录函数。调用该函数后会将作业异常次数加except_num,同时将作业最新异常时间更新为except_time。其中,except_time为时间戳。该函数仅9.1.0.200及以上集群版本支持,主要用于内部调用。 返回值类型:bool 示例: 12345 SELECT gs_increase_sql_except_num('sql_7bf6381d2a5a456a1936027198ad8b12', 4, 714623414421256);gs_increase_sql_except_num-----------------------t(1 row)
  • pgxc_wlm_readjust_relfilenode_size_table(integer) 描述:空间统计校准函数。 由于校准函数执行时与其余业务之间的事务隔离性,校准函数对其它的正在执行的业务不可见,导致校准函数会漏掉此类业务的空间变化,为避免校准之后出现空间差误,建议用户使用空间校准函数时,应选择空间大小稳定无变化的时候执行校准。 参数:取值范围为0~4,不同的入参值代表不同的校准粒度。 入参为0(默认入参为0):不重建PG_RELFILENODE_SIZE系统表,重新校准用户和schema空间。 入参为1:重建PG_RELFILENODE_SIZE系统表,并且重新校准用户和schema空间。 入参为2:重建PG_RELFILENODE_SIZE系统表。 入参为3:重新校准schema空间。 入参为4:重新校准用户空间。 返回值类型:text 示例: 12345 SELECT * FROM pgxc_wlm_readjust_relfilenode_size_table(1); result-------------- Exec success(1 row)
  • 全文检索概述 文本搜索操作符在数据库中已存在多年。 GaussDB (DWS)为文本数据类型提供~、~*、LIKE和ILIKE操作符,但这些操作符缺乏现代信息系统所要求的许多必要属性,不过这一问题可以通过使用索引及词典进行解决。 文本检索缺乏信息系统所要求的必要属性: 没有语义支持,即使是英语。 由于要识别派生词并不是那么容易,因此正则表达式也不能满足要求。如,satisfies和satisfy,当使用正则表达式寻找satisfy时,并不会查询到包含satisfies的文档。用户可以使用OR搜索多种派生形式,但过程非常繁琐。并且有些词会有上千的派生词,因此容易出错。 没有对搜索结果的分类(排序)。当搜索出成千的文档时,查找效率很低。 由于没有索引的支持,每一次的搜索需要遍历所有的文档,整体搜索比较缓慢。 使用全文索引可以对文档进行预处理,并且可以使后续的搜索更快速。预处理过程包括: 将文档解析成token。 为每个文档标记不同类别的token是非常有必要的,例如:数字、文字、复合词、电子邮件地址,这样就可以做不同的处理。原则上token的类别依赖于具体的应用,但对于大多数的应用来说,可以使用一组预定义的token类。 将token转换为词素。 词素像token一样是一个字符串,但它已经标准化处理,这样同一个词的不同形式是一样的。例如,标准化通常包括:将大写字母折成小写字母、删除后缀(如英语中的s或者es)。这将允许通过搜索找到同一个词的不同形式,不需要繁琐地输入所有可能的变形样式。同时,这一步通常会删除停用词。这些停用词通常因为太常见而对搜索无用。(总之,token是文档文本的原片段,而词素被认为是有用的索引和搜索词。)GaussDB(DWS)使用词典执行这一步,且提供了各种标准的词典。 保存搜索优化后的预处理文档。 比如,每个文档可以呈现为标准化词素的有序组合。伴随词素,通常还需要存储词素位置信息以用于邻近排序。因此文档包含的查询词越密集其排序越高。 词典能够对token如何标准化做到细粒度控制。使用合适的词典,可以定义不被索引的停用词。 数据类型tsvector用于存储预处理文档,tsquery用于存储查询条件,详细内容可参见文本搜索类型。为数据类型tsvector提供的函数和操作符可参见文本检索函数和操作符,其中最重要的是匹配运算符@@,请参见基本文本匹配。 父主题: 介绍
  • 注意事项 由于TZ词典需要识别短语,所以在处理过程中必须保存当前状态并与解析器进行交互,以决定是否处理下一个token或是结束当前识别。此外,TZ词典配置时需谨慎,如果设置TZ词典仅处理asciiword类型的token,则类似one 7的分类词典定义将不会生效,因为uint类型的token不会传给TZ词典处理。 在索引期间要用到分类词典,因此分类词典参数中的任何变化都要求重新索引。对于其他大多数类型的词典来说,类似添加或删除停用词这种修改并不需要强制重新索引。
  • 操作步骤 创建一个名为thesaurus_astro的TZ词典。 以一个简单的天文学词典thesaurus_astro为例,其中定义了两组天文短语及其同义词如下: 12 supernovae stars : sn crab nebulae : crab 执行如下语句创建TZ词典: 123456 CREATE TEXT SEARCH DICTIONARY thesaurus_astro ( TEMPLATE = thesaurus, DictFile = thesaurus_astro, Dictionary = pg_catalog.english_stem, FILEPATH = 'obs://bucket_name/path accesskey=ak secretkey=sk region=rg'); 其中,词典定义文件全名为thesaurus_astro.ths,所在目录为 "obs://bucket_name/path accesskey=ak secretkey=sk region=rg"。子词典pg_catalog.english_stem是预定义的Snowball类型的英语词干词典,用于规范化输入词,子词典自身相关配置(例如停用词等)不在此处显示。关于创建词典的语法和更多参数,请参见CREATE TEXT SEARCH DICTIONARY。 创建词典后,将其绑定到对应文本搜索配置中需要处理的token类型上: 123 ALTER TEXT SEARCH CONFIGURATION english ALTER MAPPING FOR asciiword, asciihword, hword_asciipart WITH thesaurus_astro, english_stem; 使用TZ词典。 测试TZ词典。 ts_lexize函数对于测试TZ词典作用不大,因为该函数是按照单个token处理输入。可以使用plainto_tsquery、to_tsvector、to_tsquery函数测试TZ词典,这些函数能够将输入分解成多个token(to_tsquery函数需要将输入加上引号)。 1 2 3 4 5 6 7 8 91011121314151617 SELECT plainto_tsquery('english','supernova star'); plainto_tsquery ----------------- 'sn'(1 row)SELECT to_tsvector('english','supernova star'); to_tsvector ------------- 'sn':1(1 row)SELECT to_tsquery('english','''supernova star'''); to_tsquery ------------ 'sn'(1 row) 其中,supernova star匹配了词典thesaurus_astro定义中的supernovae stars,这是因为在thesaurus_astro词典定义中指定了Snowball类型的子词典english_stem,该词典移除了e和s。 如果同时需要索引原始短语,只要将其同时放置在词典定义文件中对应定义的右侧即可,如下: 1 2 3 4 5 6 7 8 91011 supernovae stars : sn supernovae starsALTER TEXT SEARCH DICTIONARY thesaurus_astro ( DictFile = thesaurus_astro, FILEPATH = 'file:///home/dicts/');SELECT plainto_tsquery('english','supernova star'); plainto_tsquery ----------------------------- 'sn' & 'supernova' & 'star'(1 row)
  • 停用词 停用词是很常见的词,几乎出现在每一个文档中,并且没有区分值。因此,在全文搜索的语境下可忽视它们。停用词处理逻辑和词典类型相关。例如,Ispell词典会先对标记进行规范化,然后再查看停用词表,而Snowball词典会最先检查输入标记是否为停用词。 例如,每个英文文本包含像a和the的单词,因此没必要将它们存储在索引中。然而,停用词影响tsvector中的位置,同时位置也会影响相关度: 1234 SELECT to_tsvector('english','in the list of stop words'); to_tsvector---------------------------- 'list':3 'stop':5 'word':6 位置1、2、4是停用词,所以不显示。为包含和不包含停用词的文档计算出的排序是完全不同的: 123456789 SELECT ts_rank_cd (to_tsvector('english','in the list of stop words'), to_tsquery('list & stop')); ts_rank_cd------------ .05SELECT ts_rank_cd (to_tsvector('english','list stop words'), to_tsquery('list & stop')); ts_rank_cd------------ .1 父主题: 词典
  • 排序查询结果 排序试图针对特定查询衡量文档的相关度,从而将众多的匹配文档中相关度最高的文档排在最前。GaussDB(DWS)提供了两个预置的排序函数。函数考虑了词法,距离,和结构信息;也就是,考虑查询词在文档中出现的频率、紧密程度、以及出现的地方在文档中的重要性。然而,相关性的概念是模糊的,并且是跟应用强相关的。不同的应用程序可能需要额外的信息来排序,比如,文档的修改时间,内置的排序函数等。也可以开发自己的排序函数或者采用附加因素组合这些排序函数的结果来满足特定需求。 两个预置的排序函数: 1 ts_rank([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ]) returns float4 基于词素匹配率对vector进行排序: 1 ts_rank_cd([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ]) returns float4 该函数需要位置信息的输入。因此它不能在"剥离"tsvector值的情况下运行—它将总是返回零。 对于这两个函数,可选的weights参数提供给词加权重的能力,词的权重大小取决于所加的权值。权重阵列指定在排序时为每类词汇加多大的权重。 {D-weight, C-weight, B-weight, A-weight} 如果没有提供weights,则使用缺省值:{0.1, 0.2, 0.4, 1.0} 通常的权重是用来标记文档特殊领域的词,如标题或最初的摘要,所以相对于文章主体中的词它们有着更高或更低的重要性。 由于较长的文档有更多的机会包含查询词,因此有必要考虑文档的大小。例如,包含有5个搜索词的一百字文档比包含有5个搜索词的一千字文档相关性更高。两个预置的排序函数都采用了一个整型的标准化选项来定义文档长度是否影响排序及如何影响。这个整型选项控制多个行为,所以它是一个屏蔽字:可以使用|指定一个或多个行为(例如,2|4)。 0(缺省)表示:跟长度大小没有关系 1 表示:排名(rank)除以(文档长度的对数+1) 2表示:排名除以文档的长度 4表示:排名除以两个扩展词间的调和平均距离。只能使用ts_rank_cd实现 8表示:排名除以文档中单独词的数量 16表示:排名除以单独词数量的对数+1 32表示:排名除以排名本身+1 当指定多个标志位时,会按照所列的顺序依次进行转换。 需要特别注意的是,排序函数不使用任何全局信息,所以不可能产生一个某些情况下需要的1%或100%的理想标准值。标准化选项32 (rank/(rank+1))可用于所有规模的从零到一之间的排序。需要注意的是:这只是一个表面变化,并不会影响搜索结果的排序。 下面是一个例子,仅选择排名前十的匹配: 由于SQL_ASCII的数据库编码格式不支持中文字符,请在Encoding为UTF8/GBK的数据库中执行以下示例: 1 2 3 4 5 6 7 8 9101112 SELECT id, title, ts_rank_cd(to_tsvector(body), query) AS rank FROM tsearch.pgweb, to_tsquery('science') query WHERE query @@ to_tsvector(body) ORDER BY rank DESC LIMIT 10; id | title | rank ----+---------+------ 11 | Philology | .2 2 | Mathematics | .1 12 | Geography | .1 13 | Computer science | .1(4 rows) 这是使用标准化排序的相同例子: 1 2 3 4 5 6 7 8 9101112 SELECT id, title, ts_rank_cd(to_tsvector(body), query, 32 /* rank/(rank+1) */ ) AS rank FROM tsearch.pgweb, to_tsquery('science') query WHERE query @@ to_tsvector(body) ORDER BY rank DESC LIMIT 10; id | title | rank ----+---------+---------- 11 | Philology | .166667 2 | Mathematics | .0909091 12 | Geography | .0909091 13 | Computer science | .0909091(4 rows) 下面是使用中文分词法排序查询的例子: 1 2 3 4 5 6 7 8 910111213141516171819 CREATE TABLE tsearch.ts_zhparser(id int, body text);INSERT INTO tsearch.ts_zhparser VALUES(1, '排序');INSERT INTO tsearch.ts_zhparser VALUES(2, '排序查询');INSERT INTO tsearch.ts_zhparser VALUES(3, '查询排序');--精确匹配SELECT id, body, ts_rank_cd(to_tsvector('zhparser',body), query) AS rank FROM tsearch.ts_zhparser, to_tsquery('排序') query WHERE query @@ to_tsvector(body); id | body | rank ----+------+------ 1 | 排序 | .1(1 row)--模糊匹配SELECT id, body, ts_rank_cd(to_tsvector('zhparser',body), query) AS rank FROM tsearch.ts_zhparser, to_tsquery('排序') query WHERE query @@ to_tsvector('zhparser',body); id | body | rank ----+----------+------ 3 | 查询排序 | .1 1 | 排序 | .1 2 | 排序查询 | .1(3 rows) 排序要遍历每个匹配的tsvector,因此资源消耗多,可能会因为I/O限制导致排序慢。可是这是很难避免的,因为实际查询中通常会有大量的匹配。 父主题: 控制文本搜索
  • 搜索表 在不使用索引的情况下也可以进行全文检索。 一个简单查询:将body字段中包含science的每一行打印出来。 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132 DROP SCHEMA IF EXISTS tsearch CASCADE;CREATE SCHEMA tsearch;CREATE TABLE tsearch.pgweb(id int, body text, title text, last_mod_date date);INSERT INTO tsearch.pgweb VALUES(1, 'Philology is the study of words, especially the history and development of the words in a particular language or group of languages.', 'Philology', '2010-1-1');INSERT INTO tsearch.pgweb VALUES(2, 'Mathematics is the science that deals with the logic of shape, quantity and arrangement.', 'Mathematics', '2010-1-1');INSERT INTO tsearch.pgweb VALUES(3, 'Computer science is the study of processes that interact with data and that can be represented as data in the form of programs.', 'Computer science', '2010-1-1');INSERT INTO tsearch.pgweb VALUES(4, 'Chemistry is the scientific discipline involved with elements and compounds composed of atoms, molecules and ions.', 'Chemistry', '2010-1-1');INSERT INTO tsearch.pgweb VALUES(5, 'Geography is a field of science devoted to the study of the lands, features, inhabitants, and phenomena of the Earth and planets.', 'Geography', '2010-1-1');INSERT INTO tsearch.pgweb VALUES(6, 'History is a subject studied in schools, colleges, and universities that deals with events that have happened in the past.', 'History', '2010-1-1');INSERT INTO tsearch.pgweb VALUES(7, 'Medical science is the science of dealing with the maintenance of health and the prevention and treatment of disease.', 'Medical science', '2010-1-1');INSERT INTO tsearch.pgweb VALUES(8, 'Physics is one of the most fundamental scientific disciplines, and its main goal is to understand how the universe behaves.', 'Physics', '2010-1-1');SELECT id, body, title FROM tsearch.pgweb WHERE to_tsvector('english', body) @@ to_tsquery('english', 'science'); id | body | title ----+-------------------------------------------------------------------------------------------------------------------------+--------- 2 | Mathematics is the science that deals with the logic of shape, quantity and arrangement. | Mathematics 3 | Computer science is the study of processes that interact with data and that can be represented as data in the form of programs. | Computer science 5 | Geography is a field of science devoted to the study of the lands, features, inhabitants, and phenomena of the Earth and planets. | Geography 7 | Medical science is the science of dealing with the maintenance of health and the prevention and treatment of disease. | Medical science(4 rows) 像science这样的相关词也会被找到,因为这些词都被处理成了相同标准的词条。 上面的查询指定english配置来解析和规范化字符串。也可以省略此配置,通过default_text_search_config进行配置设置: 1 2 3 4 5 6 7 8 910111213141516 SHOW default_text_search_config; default_text_search_config ---------------------------- pg_catalog.english(1 row)SELECT id, body, title FROM tsearch.pgweb WHERE to_tsvector(body) @@ to_tsquery('science'); id | body | title ----+-------------------------------------------------------------------------------------------------------------------------+--------- 2 | Mathematics is the science that deals with the logic of shape, quantity and arrangement. | Mathematics 3 | Computer science is the study of processes that interact with data and that can be represented as data in the form of programs. | Computer science 5 | Geography is a field of science devoted to the study of the lands, features, inhabitants, and phenomena of the Earth and planets. | Geography 7 | Medical science is the science of dealing with the maintenance of health and the prevention and treatment of disease. | Medical science(4 rows) 一个复杂查询:检索出在title或者body字段中包含treatment和science的最近10篇文档: 1234567 SELECT title FROM tsearch.pgweb WHERE to_tsvector(title || ' ' || body) @@ to_tsquery('treatment & science') ORDER BY last_mod_date DESC LIMIT 10; title -------- Medical science(1 rows) 为了清晰,举例中没有调用coalesce函数在两个字段中查找包含NULL的行。 以上例子均在没有索引的情况下进行查询。对于大多数应用程序来说,这个方法很慢。因此除了偶尔的特定搜索,文本搜索在实际使用中通常需要创建索引。 父主题: 表和索引
  • 示例 Synonym词典可用于解决语言学相关问题,例如,为避免使单词"Paris"变成"pari",可在Synonym词典文件中定义一行"Paris paris",并将该词典放置在预定义的english_stem词典之前。 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738394041 SELECT * FROM ts_debug('english', 'Paris'); alias | description | token | dictionaries | dictionary | lexemes -----------+-----------------+-------+----------------+--------------+--------- asciiword | Word, all ASCII | Paris | {english_stem} | english_stem | {pari}(1 row)CREATE TEXT SEARCH DICTIONARY my_synonym ( TEMPLATE = synonym, SYNONYMS = my_synonyms, FILEPATH = 'obs://bucket01/obs.example.com accesskey=xxxxx secretkey=xxxxx region=cn-north-1');ALTER TEXT SEARCH CONFIGURATION english ALTER MAPPING FOR asciiword WITH my_synonym, english_stem;SELECT * FROM ts_debug('english', 'Paris'); alias | description | token | dictionaries | dictionary | lexemes -----------+-----------------+-------+---------------------------+------------+--------- asciiword | Word, all ASCII | Paris | {my_synonym,english_stem} | my_synonym | {paris}(1 row)SELECT * FROM ts_debug('english', 'paris'); alias | description | token | dictionaries | dictionary | lexemes -----------+-----------------+-------+---------------------------+------------+--------- asciiword | Word, all ASCII | Paris | {my_synonym,english_stem} | my_synonym | {paris}(1 row)ALTER TEXT SEARCH DICTIONARY my_synonym ( CASESENSITIVE=true);SELECT * FROM ts_debug('english', 'Paris'); alias | description | token | dictionaries | dictionary | lexemes -----------+-----------------+-------+---------------------------+------------+--------- asciiword | Word, all ASCII | Paris | {my_synonym,english_stem} | my_synonym | {paris}(1 row)SELECT * FROM ts_debug('english', 'paris'); alias | description | token | dictionaries | dictionary | lexemes -----------+-----------------+-------+---------------------------+------------+--------- asciiword | Word, all ASCII | Paris | {my_synonym,english_stem} | my_synonym | {pari}(1 row) 其中,同义词词典文件全名为my_synonyms.syn,所在目录为 'obs://bucket01/obs.example.com accesskey=xxxxx secretkey=xxxxx region=cn-north-1'。关于创建词典的语法和更多参数,请参见CREATE TEXT SEARCH DICTIONARY。 星号(*)可用于词典文件中的同义词结尾,表示该同义词是一个前缀。在to_tsvector()中该星号将被忽略,但在to_tsquery()中会匹配该前缀并对应输出结果(参照处理查询一节)。 假设词典文件synonym_sample.syn内容如下: 12345 postgres pgsqlpostgresql pgsql postgre pgsql gogle googl indices index* 创建并使用词典: 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738 CREATE TEXT SEARCH DICTIONARY syn ( TEMPLATE = synonym, SYNONYMS = synonym_sample);SELECT ts_lexize('syn','indices'); ts_lexize ----------- {index}(1 row)CREATE TEXT SEARCH CONFIGURATION tst (copy=simple);ALTER TEXT SEARCH CONFIGURATION tst ALTER MAPPING FOR asciiword WITH syn;SELECT to_tsvector('tst','indices'); to_tsvector ------------- 'index':1(1 row)SELECT to_tsquery('tst','indices'); to_tsquery ------------ 'index':*(1 row)SELECT 'indexes are very useful'::tsvector; tsvector --------------------------------- 'are' 'indexes' 'useful' 'very'(1 row)SELECT 'indexes are very useful'::tsvector @@ to_tsquery('tst','indices'); ?column? ---------- t(1 row)
共100000条
提示

您即将访问非华为云网站,请注意账号财产安全