华为云用户手册

  • SQL差异 表1 GaussDB(DWS)不支持的PostgreSQL数据库语言 分类 GaussDB(DWS)不支持 说明 数据类型 几何类型line GaussDB(DWS)所支持的数据类型参见数据类型。 pg_node_tree 函数 枚举支持函数: enum_first(anyenum) enum_last(anyenum) enum_range(anyenum) enum_range(anyenum, anyenum) GaussDB(DWS)所支持的函数参见函数和操作符。 访问权限查询函数: has_sequence_privilege(user, sequence, privilege) has_sequence_privilege(sequence, privilege) 系统目录信息函数: pg_get_triggerdef(trigger_oid) pg_get_triggerdef(trigger_oid, pretty_bool) 几何类型转换函数: line(point, point) pg_node_tree函数 SQL语法 CREATE TABLE子句: INHERITS ( parent_table [, ... ] ) 继承表。 CREATE TABLE的列约束: REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] 列约束中用REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] 为表创建外键约束。 CREATE TABLE的表约束: EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) 表约束中用EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] )为表创建排除约束。 CREATE/ALTER/DROP EXTENSION 扩展的加载、修改和删除。 CREATE/ALTER/DROP AGGREGATE 聚集函数的定义、修改和删除。 CREATE/ALTER/DROP OPERATOR 操作符(OPERATOR)的创建、修改和删除。 CREATE/ALTER/DROP OPERATOR CLASS 操作符类(OPERATOR CLASS)的创建、修改和删除。 CREATE/ALTER/DROP OPERATOR FAMILY 操作符族(OPERATOR FAMILY)的创建、修改和删除。 CREATE/ALTER/DROP TEXT SEARCH PARSER 文本检索解析器(TEXT SEARCH PARSER)的创建、修改和删除。 CREATE/ALTER/DROP TEXT SEARCH TEMPLATE 文本检索模板(TEXT SEARCH TEMPLATE)的创建、修改和删除。 CREATE/ALTER/DROP COLLATION 排序规则(COLLATION)的创建、修改和删除 CREATE/ALTER/DROP CONVERSION 字符集编码转换(CONVERSION)的定义、修改和删除。 CREATE/ALTER/DROP RULE 规则(RULE)的创建、修改和删除。 CREATE/ALTER/DROP LANGUAGE 过程语言(LANGUAGE)的注册、修改和删除。 CREATE/ALTER/DROP DOMAIN 域(DOMAIN)的创建、修改和删除。 CREATE/DROP CAST 类型转换(CAST)的定义和删除。 CREATE/ALTER/DROP USER MAPPING 用户映射(USER MAPPING)的定义、修改和删除。 SECURITY LABEL 定义或更改对象的安全标签。 NOTIFY 生成一个通知。 LISTEN 监听一个通知。 UNLISTEN 停止监听通知信息。 LOAD 加载或重新加载一个共享库文件。 DISCARD 释放一个数据库的会话资源。(8.2.0及以上集群版本已支持DISCARD。) MOVE BACKWARD 反向移动游标。 COPY的COPY FROM FILE和COPY TO FILE 为了权限的隔离,GaussDB(DWS)禁用COPY FROM FILE和COPY TO FILE。 其他 自定义C函数 DWS支持的用户自定义函数参见用户自定义函数。
  • 参数说明 configuration_parameter 运行时参数的名称。 取值范围:可以使用SHOW ALL命令查看运行时参数。 部分通过SHOW ALL查看的参数不能通过SET设置。如max_datanodes。 CURRENT_SCHEMA 当前模式。 TIME ZONE 时区。 TRANSACTION ISOLATION LEVEL 事务的隔离级别。 SESSION AUTHORIZATION 当前会话的用户标识符。 ALL 所有运行时参数。
  • 搜索表 本章节主要介绍如何使用文本搜索运算符搜索数据库表。 一个简单查询:将body字段中包含science的每一行打印出来。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 9 10 11 12 13 14 15 16 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篇文档: 1 2 3 4 5 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的行。 以上例子均在没有索引的情况下进行查询。对于大多数应用程序来说,这个方法很慢。因此除了偶尔的特定搜索,文本搜索在实际使用中通常需要创建索引。 父主题: 在数据库表中搜索文本
  • 参数说明 IF EXISTS 如果不存在相同名称的表,不会抛出错误,而会返回一个通知,告知表不存在。 tablename 需要修改的外表名称。 取值范围:已存在的外表名。 new_owner 外表的新所有者。 取值范围:字符串,有效的用户名。 data_type 现存字段的新类型。 取值范围:字符串,需符合标识符的命名规范。 column_name 现存字段的名称。 取值范围:字符串,需符合标识符的命名规范。 修改外表语法中其它参数,请参见ALTER TABLE的参数说明。
  • 语法格式 设置外表属性: 1 2 ALTER FOREIGN TABLE [ IF EXISTS ] tablename OPTIONS ( {[ SET ] option ['value']} [, ... ]); 设置外表的所有者: 1 2 ALTER FOREIGN TABLE [ IF EXISTS ] tablename OWNER TO new_owner; 更新外表列类型: 1 2 ALTER FOREIGN TABLE [ IF EXISTS ] table_name MODIFY ( { column_name data_type [, ...] } ); 修改外表的列: 1 2 ALTER FOREIGN TABLE [ IF EXISTS ] tablename action [, ... ]; 其中action语法为: 1 2 ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type | MODIFY column_name data_type 参考ALTER TABLE。
  • generate_series(start, stop, step) 描述:生成一个数值序列,从start到stop,步长为step。 参数类型:int、bigint、numeric 返回值类型:setof int、setof bigint、setof numeric(与参数类型相同) 示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 SELECT * FROM generate_series(5,1,-2); generate_series ----------------- 5 3 1 (3 rows) SELECT * FROM generate_series(4,6,-5); generate_series ----------------- (0 rows) SELECT * FROM generate_series(4,3,0); ERROR: step size cannot equal zero
  • generate_series(start, stop, step interval) 描述:生成一个数值序列,从start到stop,步长为step。 参数类型:timestamp或timestamp with time zone 返回值类型:setof timestamp或setof timestamp with time zone(与参数类型相同) 示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 --这个示例应用于date-plus-integer操作符。 SELECT current_date + s.a AS dates FROM generate_series(0,14,7) AS s(a); dates ------------ 2017-06-02 2017-06-09 2017-06-16 (3 rows) SELECT * FROM generate_series('2008-03-01 00:00'::timestamp, '2008-03-04 12:00', '10 hours'); generate_series --------------------- 2008-03-01 00:00:00 2008-03-01 10:00:00 2008-03-01 20:00:00 2008-03-02 06:00:00 2008-03-02 16:00:00 2008-03-03 02:00:00 2008-03-03 12:00:00 2008-03-03 22:00:00 2008-03-04 08:00:00 (9 rows)
  • generate_series(start, stop) 描述:生成一个数值序列,从start到stop,步长默认为1。 参数类型:int、bigint、numeric 返回值类型:setof int、setof bigint、setof numeric(与参数类型相同) 示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 SELECT * FROM generate_series(2,4); generate_series ----------------- 2 3 4 (3 rows) SELECT * FROM generate_series(4,3); generate_series ----------------- (0 rows) SELECT * FROM generate_series(1,NULL); generate_series ----------------- (0 rows)
  • proc_drop_partition (relname regclass, older_than interval) 描述:用于给开启自动删除分区功能的表删除分区。 返回值类型:void 备注:该函数运行时,遍历表所有分区,并删除其中boundary小于(now_time - older_than)的分区;如果所有分区都满足删除条件,则保留一个分区,并truncate该表。 示例: 1 2 3 4 5 call proc_drop_partition('my_schema.my_table', interval '7 day'); proc_drop_partition -------------------- (1 row)
  • - 描述:减 示例: 1 2 3 4 5 6 7 8 9 10 SELECT inet '192.168.1.43' - 36 AS RESULT; result ------------- 192.168.1.7 (1 row) SELECT inet '192.168.1.43' - inet '192.168.1.19' AS RESULT; result -------- 24 (1 row)
  • 功能描述 RELEASE SAVEPOINT删除当前事务先前定义的保存点。 保存点删除后便无法再作为回滚点使用,除此之外没有其它用户可见的行为。删除保存点并不能撤销在保存点建立起来之后执行的命令的影响。要撤销那些命令可以使用ROLLBACK TO SAVEPOINT 。在不再需要的时候删除保存点可以令系统在事务结束之前提前回收一些资源。 RELEASE SAVEPOINT也删除所有在指定的保存点建立之后的所有保存点。
  • || 描述:数组与元素交叉组合进行连接,即数组与数组进行连接、元素与数组进行连接、数组与元素进行连接。 示例: 1 2 3 4 5 SELECT ARRAY[1,2,3] || ARRAY[4,5,6] AS RESULT; result --------------- {1,2,3,4,5,6} (1 row) 1 2 3 4 5 SELECT ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]] AS RESULT; result --------------------------- {{1,2,3},{4,5,6},{7,8,9}} (1 row) 1 2 3 4 5 SELECT 3 || ARRAY[4,5,6] AS RESULT; result ----------- {3,4,5,6} (1 row) 1 2 3 4 5 SELECT ARRAY[4,5,6] || 7 AS RESULT; result ----------- {4,5,6,7} (1 row)
  • hll_add(hll, hll_hashval) 描述:把hll_hashval加入到hll中。 返回值类型:hll 示例: 1 2 3 4 5 SELECT hll_add(hll_empty(), hll_hash_integer(1)); hll_add -------------------------- \x128b7f8895a3f5af28cafe (1 row)
  • hll_add_rev(hll_hashval, hll) 描述:把hll_hashval加入到hll中,和hll_add功能一样,只是参数位置进行了交换。 返回值类型:hll 示例: 1 2 3 4 5 SELECT hll_add_rev(hll_hash_integer(1), hll_empty()); hll_add_rev -------------------------- \x128b7f8895a3f5af28cafe (1 row)
  • hll_empty(int32 log2m, int32 regwidth, int64 expthresh) 描述:创建空的hll并依次指定参数log2m、regwidth、expthresh。expthresh取值范围是-1到7之间的整数。该参数可以用来设置从Explicit模式到Sparse模式的阈值大小。-1表示自动模式,0表示跳过Explicit模式,取1-7表示在基数到达2expthresh时切换模式。 返回值类型:hll 示例: 1 2 3 4 5 SELECT hll_empty(10, 4, 7); hll_empty ----------- \x116a48 (1 row)
  • hll_empty(int32 log2m, int32 regwidth, int64 expthresh, int32 sparseon) 描述:创建空的hll并依次指定参数log2m、regwidth、expthresh、sparseon。sparseon取0或者1。 返回值类型:hll 示例: 1 2 3 4 5 SELECT hll_empty(10,4,7,0); hll_empty ----------- \x116a08 (1 row)
  • hll_union(hll, hll) 描述:把两个hll数据结构union成一个。 返回值类型:hll 示例: 1 2 3 4 5 SELECT hll_union(hll_add(hll_empty(), hll_hash_integer(1)), hll_add(hll_empty(), hll_hash_integer(2))); hll_union ------------------------------------------ \x128b7f8895a3f5af28cafeda0ce907e4355b60 (1 row)
  • hll_print(hll) 描述:打印hll的一些debug参数信息。 返回值类型:cstring 示例: 1 2 3 4 5 SELECT hll_print(hll_empty()); hll_print ----------------------------------------------------------- EMPTY, nregs=2048, nbits=5, expthresh=-1(160), sparseon=1gongne (1 row)
  • 解析器测试 函数ts_parse可以直接测试文本搜索解析器。 1 2 ts_parse(parser_name text, document text, OUT tokid integer, OUT token text) returns setof record ts_parse解析指定的document并返回一系列的记录,一条记录代表一个解析生成的token。每条记录包括标识token类型的tokid,及token文本。比如: 1 2 3 4 5 6 7 8 9 10 SELECT * FROM ts_parse('default', '123 - a number'); tokid | token -------+-------- 22 | 123 12 | 12 | - 1 | a 12 | 1 | number (6 rows) 1 2 ts_token_type(parser_name text, OUT tokid integer, OUT alias text, OUT description text) returns setof record ts_token_type返回一个表,这个表描述了指定解析器可以识别的每种token类型。对于每个token类型,表中给出了整数类型的tokid--用于解析器标记对应的token类型;alias——命名分词器命令中的token类型;及简单描述。比如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 SELECT * FROM ts_token_type('default'); tokid | alias | description -------+-----------------+------------------------------------------ 1 | asciiword | Word, all ASCII 2 | word | Word, all letters 3 | numword | Word, letters and digits 4 | email | Email address 5 | url | URL 6 | host | Host 7 | sfloat | Scientific notation 8 | version | Version number 9 | hword_numpart | Hyphenated word part, letters and digits 10 | hword_part | Hyphenated word part, all letters 11 | hword_asciipart | Hyphenated word part, all ASCII 12 | blank | Space symbols 13 | tag | XML tag 14 | protocol | Protocol head 15 | numhword | Hyphenated word, letters and digits 16 | asciihword | Hyphenated word, all ASCII 17 | hword | Hyphenated word, all letters 18 | url_path | URL path 19 | file | File or path name 20 | float | Decimal notation 21 | int | Signed integer 22 | uint | Unsigned integer 23 | entity | XML entity (23 rows) 父主题: 测试和调试文本搜索
  • 文档概念 文档是全文搜索系统的搜索单元,例如:杂志上的一篇文章或电子邮件消息。文本搜索引擎必须能够解析文档,而且可以存储父文档的关联词素(关键词)。后续,这些关联词素用来搜索包含查询词的文档。 在GaussDB(DWS)中,文档通常是一个数据库表中的一行文本字段,或者这些字段的可能组合(级联)。文档可能存储在多个表中或者动态获取。换句话说,一个文档由被索引化的不同部分构成,可以不作为整体存储在任何地方。比如: 1 2 3 4 5 6 7 8 9 10 11 SELECT d_dow || '-' || d_dom || '-' || d_fy_week_seq AS identify_serials FROM tpcds.date_dim WHERE d_fy_week_seq = 1; identify_serials ------------------ 5-6-1 0-8-1 2-3-1 3-4-1 4-5-1 1-2-1 6-7-1 (7 rows) 实际上,在这些示例查询中,应该使用coalesce防止一个独立的NULL属性导致整个文档的NULL结果。 另外一种可能是:文档在文件系统中作为简单的文本文件存储。在这种情况下,数据库可以用于存储全文索引并且执行搜索,同时可以使用一些唯一标识从文件系统中检索文档。然而,从数据库外部检索文件需要拥有系统管理员权限或者特殊函数支持。因此,还是将所有数据保存在数据库中比较方便。同时,将所有数据保存在数据库中可以方便地访问文档元数据以便于索引和显示。 为了实现文本搜索目的,必须将每个文档减少至预处理后的tsvector格式。搜索和相关性排序都是在tsvector形式的文档上执行的。原始文档只有在被选中要呈现给用户时才会被当检索。因此,常将tsvector说成文档,但是很显然其实它只是完整文档的一种紧凑表示。 父主题: 介绍
  • date_part date_part函数是在传统的Ingres函数的基础上制作的(该函数等效于SQL标准函数extract): 1 date_part('field', source) 这里的field参数必须是一个字符串,而不是一个名字。有效的field与extract一样,详细信息请参见EXTRACT。 示例: 1 2 3 4 5 SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40'); date_part ----------- 16 (1 row) 1 2 3 4 5 SELECT date_part('hour', interval '4 hours 3 minutes'); date_part ----------- 4 (1 row) 父主题: 时间、日期处理函数和操作符
  • 语法格式 向用户组中添加用户。 1 2 ALTER GROUP group_name ADD USER user_name [, ... ]; 从用户组中删除用户。 1 2 ALTER GROUP group_name DROP USER user_name [, ... ]; 修改用户组的名称。 1 2 ALTER GROUP group_name RENAME TO new_name;
  • pg_lifecycle_node_data_distribute() 描述:查看所有冷热表数据分布情况。 返回值:record 示例:数据库中当前存在两个冷热表,其数据分布情况如下。 1 2 3 4 5 6 SELECT * FROM pg_catalog.pg_lifecycle_node_data_distribute(); schemaname | tablename | nodename | hotpartition | coldpartition | switchablepartition | hotdatasize | colddatasize | switchabledatasize ------------+-----------+----------+--------------+---------------+---------------------+-------------+--------------+-------------------- public | w1 | dn_1 | p2 | p1 | | 81920 | 0 | 0 public | w2 | dn_1 | p2 | p1 | | 81920 | 0 | 0 (2 rows)
  • pg_obs_cold_refresh_time(table_name, time) 描述:用来修改冷热表的冷数据切换至OBS上的时间,默认为每日0点。 table_name为冷热表表名,类型为name,time为数据切换任务调度时间,类型为Time。 返回值:SUCCESS,任务时间修改成功。 示例: 1 2 3 4 5 SELECT * FROM pg_obs_cold_refresh_time('lifecycle_table', '06:30:00'); pg_obs_cold_refresh_time -------------------------- SUCCESS (1 row)
  • pg_refresh_storage() 描述:切换所有冷热表,将符合冷热切换规则的数据由热数据切换至冷数据(OBS中)。 返回值类型:int 返回值字段: success_count int:切换成功的表个数 failed_count int:切换失败的表个数 示例: 1 2 3 4 5 SELECT * FROM pg_refresh_storage(); success_count | failed_count ---------------+-------------- 1 | 0 (1 row)
  • pg_lifecycle_table_data_distribute(table_name) 描述:查看某个冷热表的数据分布情况。 table_name为表名,不可缺省。 返回值:record 示例:根据节点数量形成多条记录,如下示例为只有一个dn节点时w1表数据分布情况。 1 2 3 4 5 SELECT * FROM pg_catalog.pg_lifecycle_table_data_distribute('w1'); schemaname | tablename | nodename | hotpartition | coldpartition | switchablepartition | hotdatasize | colddatasize | switchabledatasize ------------+-----------+----------+--------------+---------------+---------------------+-------------+--------------+-------------------- public | w1 | dn_1 | p2 | p1 | | 80 KB | 0 bytes | 0 bytes (1 row)
  • tsquery tsquery类型表示一个检索条件,存储用于检索的词汇,并且使用布尔操作符&(AND),|(OR)和!(NOT)将这些词汇进行组合,圆括号用来强调操作符的分组。如果没有圆括号,(NOT)的优先级最高,其次是&(AND),最后是|(OR)。to_tsquery函数及plainto_tsquery函数会将单词转换为tsquery类型前进行规范化处理。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 SELECT 'fat & rat'::tsquery; tsquery --------------- 'fat' & 'rat' (1 row) SELECT 'fat & (rat | cat)'::tsquery; tsquery --------------------------- 'fat' & ( 'rat' | 'cat' ) (1 row) SELECT 'fat & rat & ! cat'::tsquery; tsquery ------------------------ 'fat' & 'rat' & !'cat' (1 row) tsquery中的词汇可以用一个或多个权重字母来标记,这些权重字母限制词汇只能与匹配权重的tsvector词汇进行匹配。 1 2 3 4 5 SELECT 'fat:ab & cat'::tsquery; tsquery ------------------ 'fat':AB & 'cat' (1 row) 同样,tsquery中的词汇可以用*标记来指定前缀匹配。例如:这个查询可以匹配tsvector中以“super”开始的任意单词。 1 2 3 4 5 SELECT 'super:*'::tsquery; tsquery ----------- 'super':* (1 row) 需注意,前缀匹配会首先被文本搜索分词器处理。例如:postgres中提取的词干是postgr,匹配到了postgraduate,也就意味着下面的结果为真: 1 2 3 4 5 SELECT to_tsvector( 'postgraduate' ) @@ to_tsquery( 'postgres:*' ) AS RESULT; result ---------- t (1 row) 1 2 3 4 5 SELECT to_tsquery('postgres:*'); to_tsquery ------------ 'postgr':* (1 row) to_tsquery函数会将单词转换为tsquery类型前进行规范化处理。'Fat:ab & Cats'规范化转为tsquery类型结果如下: 1 2 3 4 5 SELECT to_tsquery('Fat:ab & Cats'); to_tsquery ------------------ 'fat':AB & 'cat' (1 row)
  • tsvector tsvector类型表示一个检索单元,通常是一个数据库表中的一行文本字段或者这些字段的组合。 tsvector类型的值是唯一分词的分类列表,把一句话的词格式化为不同的词条,在进行分词处理的时候tsvector会按照一定的顺序录入,并自动去掉分词中重复的词条。 to_tsvector函数通常用于解析和标准化文档字符串。 通过tsvector把一个字符串按照空格进行分词,分词的顺序是按照字母和长短排序的,请看以下例子: 1 2 3 4 5 SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector; tsvector ---------------------------------------------------- 'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat' (1 row) 如果词条中包含空格或标点符号,可以用引号包围: 1 2 3 4 5 SELECT $$the lexeme ' ' contains spaces$$::tsvector; tsvector ------------------------------------------- ' ' 'contains' 'lexeme' 'spaces' 'the' (1 row) 使用常规的单引号引起来的字符串,字符串中嵌入的单引号(')和反斜杠(\)必须双写进行转义: 1 2 3 4 5 SELECT $$the lexeme 'Joe''s' contains a quote$$::tsvector; tsvector ------------------------------------------------ 'Joe''s' 'a' 'contains' 'lexeme' 'quote' 'the' (1 row) 词条位置常量也可以放到词汇中: 1 2 3 4 5 SELECT 'a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12'::tsvector; tsvector ------------------------------------------------------------------------------- 'a':1,6,10 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'on':5 'rat':12 'sat':4 (1 row) 位置常量通常表示文档中源字的位置。位置信息可以用于进行排名。位置常量的范围是1到16383,最大值默认是16383。相同词的重复位会被忽略掉。 拥有位置的词汇可以进一步地被标注一个权重,它可以是A,B,C或D。 D是默认权重,因此输出中不会显示: 1 2 3 4 5 SELECT 'a:1A fat:2B,4C cat:5D'::tsvector; tsvector ---------------------------- 'a':1A 'cat':5 'fat':2B,4C (1 row) 权重通常被用来反映文档结构,如:将标题标记成与正文词不同。文本检索排序函数可以为不同的权重标记分配不同的优先级。 tsvector类型标准用法示例如下: 1 2 3 4 5 SELECT 'The Fat Rats'::tsvector; tsvector -------------------- 'Fat' 'Rats' 'The' (1 row) 但是对于英文全文检索应用来说,上面的单词会被认为非规范化的,所以需要通过to_tsvector函数对这些单词进行规范化处理: 1 2 3 4 5 SELECT to_tsvector('english', 'The Fat Rats'); to_tsvector ----------------- 'fat':2 'rat':3 (1 row)
  • str_to_date(str, format) 描述:将日期/时间格式的字符串(str),按照所提供的显示格式(format)转换为日期类型的值。 返回值类型:timestamp 示例: 1 2 3 4 5 6 7 8 9 10 SELECT str_to_date('01,5,2021','%d,%m,%Y'); str_to_date --------------------- 2021-05-01 00:00:00 (1 row) SELECT str_to_date('01,5,2021,09,30,17','%d,%m,%Y,%h,%i,%s'); str_to_date --------------------- 2021-05-01 09:30:17 (1 row) 适用于str_to_date的格式化输入的格式类型参考表1。这里仅支持“日期”格式、“日期+时间”格式的输入转换,对于仅“时间”格式的输入场景请使用str_to_time。
  • str_to_time(str, format) 描述:将时间格式的字符串(str),按照所提供的显示格式(format)转换为时间类型的值。 返回值类型:time 示例: 1 2 3 4 5 SELECT str_to_time('09:30:17','%h:%i:%s'); str_to_time ------------- 09:30:17 (1 row) 适用于str_to_time的格式化输入的格式类型参考表1,这里仅支持“时间”格式的输入转换,对于“日期”格式、“日期+时间”格式的输入场景请使用str_to_date。
共100000条