PostgreSQL Oracle兼容性 之 - parser SQL保留|关键字(keywrods)大全

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS SQL Server,基础系列 2核4GB
简介:

标签

PostgreSQL , keywords , 关键字 , Oracle 兼容性


背景

PostgreSQL数据库中有哪些关键字,这些关键字的使用限制如何?

https://www.postgresql.org/docs/10/static/sql-keywords-appendix.html

文档中的说明并不是特别清晰,(并且KEYWORDS与版本强相关),所以使用pg_get_keywords这个系统函数得到的,更加准确可读。

https://www.postgresql.org/docs/10/static/functions-info.html

pg_get_keywords() returns a set of records describing the SQL keywords recognized by the server.   
  
1、The word column contains the keyword.   
2、The catcode column contains a category code:   
  U for unreserved, C for column name, T for type or function name, or R for reserved.  
3、The catdesc column contains a possibly-localized string describing the category.  

关键字查找

keyword不同的分类,含义不一样。

postgres=# select * from pg_get_keywords();  
       word        | catcode |                   catdesc                      
-------------------+---------+----------------------------------------------  
 abort             | U       | unreserved  
 absolute          | U       | unreserved  
 access            | U       | unreserved  
 action            | U       | unreserved  
 add               | U       | unreserved  
 admin             | U       | unreserved  
.............  

例子,使用keywrod作为identity的报错示例:

postgres=# \set VERBOSITY verbose  
postgres=# create table cast (id int);  
ERROR:  42601: syntax error at or near "cast"  
LINE 1: create table cast (id int);  
                     ^  
LOCATION:  scanner_yyerror, scan.l:1086  

注意:keyword的分类解释

1、unreserved,不保留,可以用于任何identity(视图、表、函数、类型、索引、字段、类型 等名称)。

2、reserved,保留,不可用于任何identity。

3、reserved (can be function or type name),保留,但是可用于函数、类型名。

4、unreserved (cannot be function or type name),不保留,但是不可用于函数、类型名。

理解了这几类意思,你就知道keyword什么地方能用,什么地方不能用了。

keyword相关代码

src/backend/utils/adt/misc.c

/* Function to return the list of grammar keywords */  
Datum  
pg_get_keywords(PG_FUNCTION_ARGS)  
{  
..........  
                switch (ScanKeywords[funcctx->call_cntr].category)  
                {  
                        case UNRESERVED_KEYWORD:  
                                values[1] = "U";  
                                values[2] = _("unreserved");  
                                break;  
                        case COL_NAME_KEYWORD:  
                                values[1] = "C";  
                                values[2] = _("unreserved (cannot be function or type name)");  
                                break;  
                        case TYPE_FUNC_NAME_KEYWORD:  
                                values[1] = "T";  
                                values[2] = _("reserved (can be function or type name)");  
                                break;  
                        case RESERVED_KEYWORD:  
                                values[1] = "R";  
                                values[2] = _("reserved");  
                                break;  
                        default:                        /* shouldn't be possible */  
                                values[1] = NULL;  
                                values[2] = NULL;  
                                break;  
                }  
...........  

src/include/common/keywords.h

/* Keyword categories --- should match lists in gram.y */  
#define UNRESERVED_KEYWORD              0  
#define COL_NAME_KEYWORD                1  
#define TYPE_FUNC_NAME_KEYWORD  2  
#define RESERVED_KEYWORD                3  

src/include/parser/kwlist.h

/* name, value, category */  
PG_KEYWORD("abort", ABORT_P, UNRESERVED_KEYWORD)  
PG_KEYWORD("absolute", ABSOLUTE_P, UNRESERVED_KEYWORD)  
PG_KEYWORD("access", ACCESS, UNRESERVED_KEYWORD)  
PG_KEYWORD("action", ACTION, UNRESERVED_KEYWORD)  
....................  
PG_KEYWORD("xmlparse", XMLPARSE, COL_NAME_KEYWORD)  
PG_KEYWORD("xmlpi", XMLPI, COL_NAME_KEYWORD)  
PG_KEYWORD("xmlroot", XMLROOT, COL_NAME_KEYWORD)  
PG_KEYWORD("xmlserialize", XMLSERIALIZE, COL_NAME_KEYWORD)  
PG_KEYWORD("xmltable", XMLTABLE, COL_NAME_KEYWORD)  
PG_KEYWORD("year", YEAR_P, UNRESERVED_KEYWORD)  
PG_KEYWORD("yes", YES_P, UNRESERVED_KEYWORD)  
PG_KEYWORD("zone", ZONE, UNRESERVED_KEYWORD)  

plpgsql中的保留字

src/pl/plpgsql/src/pl_scanner.c

/*  
 * A word about keywords:  
 *  
 * We keep reserved and unreserved keywords in separate arrays.  The  
 * reserved keywords are passed to the core scanner, so they will be  
 * recognized before (and instead of) any variable name.  Unreserved words  
 * are checked for separately, usually after determining that the identifier  
 * isn't a known variable name.  If plpgsql_IdentifierLookup is DECLARE then  
 * no variable names will be recognized, so the unreserved words always work.  
 * (Note in particular that this helps us avoid reserving keywords that are  
 * only needed in DECLARE sections.)  
 *  
 * In certain contexts it is desirable to prefer recognizing an unreserved  
 * keyword over recognizing a variable name.  In particular, at the start  
 * of a statement we should prefer unreserved keywords unless the statement  
 * looks like an assignment (i.e., first token is followed by ':=' or '[').  
 * This rule allows most statement-introducing keywords to be kept unreserved.  
 * (We still have to reserve initial keywords that might follow a block  
 * label, unfortunately, since the method used to determine if we are at  
 * start of statement doesn't recognize such cases.  We'd also have to  
 * reserve any keyword that could legitimately be followed by ':=' or '['.)  
 * Some additional cases are handled in pl_gram.y using tok_is_keyword().  
 *  
 * We try to avoid reserving more keywords than we have to; but there's  
 * little point in not reserving a word if it's reserved in the core grammar.  
 * Currently, the following words are reserved here but not in the core:  
 * BEGIN BY DECLARE EXECUTE FOREACH IF LOOP STRICT WHILE  
 */  
  
/*  
 * Lists of keyword (name, token-value, category) entries.  
 *  
 * !!WARNING!!: These lists must be sorted by ASCII name, because binary  
 *               search is used to locate entries.  
 *  
 * Be careful not to put the same word in both lists.  Also be sure that  
 * pl_gram.y's unreserved_keyword production agrees with the second list.  
 */  
..........  
static const ScanKeyword reserved_keywords[] = {  
        PG_KEYWORD("all", K_ALL, RESERVED_KEYWORD)  
        PG_KEYWORD("begin", K_BEGIN, RESERVED_KEYWORD)  
        PG_KEYWORD("by", K_BY, RESERVED_KEYWORD)  
        PG_KEYWORD("case", K_CASE, RESERVED_KEYWORD)  
        PG_KEYWORD("declare", K_DECLARE, RESERVED_KEYWORD)  
        PG_KEYWORD("else", K_ELSE, RESERVED_KEYWORD)  
        PG_KEYWORD("end", K_END, RESERVED_KEYWORD)  
        PG_KEYWORD("execute", K_EXECUTE, RESERVED_KEYWORD)  
        PG_KEYWORD("for", K_FOR, RESERVED_KEYWORD)  
        PG_KEYWORD("foreach", K_FOREACH, RESERVED_KEYWORD)  
        PG_KEYWORD("from", K_FROM, RESERVED_KEYWORD)  
        PG_KEYWORD("if", K_IF, RESERVED_KEYWORD)  
        PG_KEYWORD("in", K_IN, RESERVED_KEYWORD)  
        PG_KEYWORD("into", K_INTO, RESERVED_KEYWORD)  
        PG_KEYWORD("loop", K_LOOP, RESERVED_KEYWORD)  
        PG_KEYWORD("not", K_NOT, RESERVED_KEYWORD)  
        PG_KEYWORD("null", K_NULL, RESERVED_KEYWORD)  
        PG_KEYWORD("or", K_OR, RESERVED_KEYWORD)  
        PG_KEYWORD("strict", K_STRICT, RESERVED_KEYWORD)  
        PG_KEYWORD("then", K_THEN, RESERVED_KEYWORD)  
        PG_KEYWORD("to", K_TO, RESERVED_KEYWORD)  
        PG_KEYWORD("using", K_USING, RESERVED_KEYWORD)  
        PG_KEYWORD("when", K_WHEN, RESERVED_KEYWORD)  
        PG_KEYWORD("while", K_WHILE, RESERVED_KEYWORD)  
};  
  
...........  
  
static const ScanKeyword unreserved_keywords[] = {  
        PG_KEYWORD("absolute", K_ABSOLUTE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("alias", K_ALIAS, UNRESERVED_KEYWORD)  
        PG_KEYWORD("array", K_ARRAY, UNRESERVED_KEYWORD)  
        PG_KEYWORD("assert", K_ASSERT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("backward", K_BACKWARD, UNRESERVED_KEYWORD)  
        PG_KEYWORD("close", K_CLOSE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("collate", K_COLLATE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("column", K_COLUMN, UNRESERVED_KEYWORD)  
        PG_KEYWORD("column_name", K_COLUMN_NAME, UNRESERVED_KEYWORD)  
        PG_KEYWORD("constant", K_CONSTANT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("constraint", K_CONSTRAINT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("constraint_name", K_CONSTRAINT_NAME, UNRESERVED_KEYWORD)  
        PG_KEYWORD("continue", K_CONTINUE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("current", K_CURRENT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("cursor", K_CURSOR, UNRESERVED_KEYWORD)  
        PG_KEYWORD("datatype", K_DATATYPE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("debug", K_DEBUG, UNRESERVED_KEYWORD)  
        PG_KEYWORD("default", K_DEFAULT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("detail", K_DETAIL, UNRESERVED_KEYWORD)  
        PG_KEYWORD("diagnostics", K_DIAGNOSTICS, UNRESERVED_KEYWORD)  
        PG_KEYWORD("dump", K_DUMP, UNRESERVED_KEYWORD)  
        PG_KEYWORD("elseif", K_ELSIF, UNRESERVED_KEYWORD)  
        PG_KEYWORD("elsif", K_ELSIF, UNRESERVED_KEYWORD)  
        PG_KEYWORD("errcode", K_ERRCODE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("error", K_ERROR, UNRESERVED_KEYWORD)  
        PG_KEYWORD("exception", K_EXCEPTION, UNRESERVED_KEYWORD)  
        PG_KEYWORD("exit", K_EXIT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("fetch", K_FETCH, UNRESERVED_KEYWORD)  
        PG_KEYWORD("first", K_FIRST, UNRESERVED_KEYWORD)  
        PG_KEYWORD("forward", K_FORWARD, UNRESERVED_KEYWORD)  
        PG_KEYWORD("get", K_GET, UNRESERVED_KEYWORD)  
        PG_KEYWORD("hint", K_HINT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("import", K_IMPORT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("info", K_INFO, UNRESERVED_KEYWORD)  
        PG_KEYWORD("insert", K_INSERT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("is", K_IS, UNRESERVED_KEYWORD)  
        PG_KEYWORD("last", K_LAST, UNRESERVED_KEYWORD)  
        PG_KEYWORD("log", K_LOG, UNRESERVED_KEYWORD)  
        PG_KEYWORD("message", K_MESSAGE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("message_text", K_MESSAGE_TEXT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("move", K_MOVE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("next", K_NEXT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("no", K_NO, UNRESERVED_KEYWORD)  
        PG_KEYWORD("notice", K_NOTICE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("open", K_OPEN, UNRESERVED_KEYWORD)  
        PG_KEYWORD("option", K_OPTION, UNRESERVED_KEYWORD)  
        PG_KEYWORD("perform", K_PERFORM, UNRESERVED_KEYWORD)  
        PG_KEYWORD("pg_context", K_PG_CONTEXT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("pg_datatype_name", K_PG_DATATYPE_NAME, UNRESERVED_KEYWORD)  
        PG_KEYWORD("pg_exception_context", K_PG_EXCEPTION_CONTEXT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("pg_exception_detail", K_PG_EXCEPTION_DETAIL, UNRESERVED_KEYWORD)  
        PG_KEYWORD("pg_exception_hint", K_PG_EXCEPTION_HINT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("print_strict_params", K_PRINT_STRICT_PARAMS, UNRESERVED_KEYWORD)  
        PG_KEYWORD("prior", K_PRIOR, UNRESERVED_KEYWORD)  
        PG_KEYWORD("query", K_QUERY, UNRESERVED_KEYWORD)  
        PG_KEYWORD("raise", K_RAISE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("relative", K_RELATIVE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("result_oid", K_RESULT_OID, UNRESERVED_KEYWORD)  
        PG_KEYWORD("return", K_RETURN, UNRESERVED_KEYWORD)  
        PG_KEYWORD("returned_sqlstate", K_RETURNED_SQLSTATE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("reverse", K_REVERSE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("row_count", K_ROW_COUNT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("rowtype", K_ROWTYPE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("schema", K_SCHEMA, UNRESERVED_KEYWORD)  
        PG_KEYWORD("schema_name", K_SCHEMA_NAME, UNRESERVED_KEYWORD)  
        PG_KEYWORD("scroll", K_SCROLL, UNRESERVED_KEYWORD)  
        PG_KEYWORD("slice", K_SLICE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("sqlstate", K_SQLSTATE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("stacked", K_STACKED, UNRESERVED_KEYWORD)  
        PG_KEYWORD("table", K_TABLE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("table_name", K_TABLE_NAME, UNRESERVED_KEYWORD)  
        PG_KEYWORD("type", K_TYPE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("use_column", K_USE_COLUMN, UNRESERVED_KEYWORD)  
        PG_KEYWORD("use_variable", K_USE_VARIABLE, UNRESERVED_KEYWORD)  
        PG_KEYWORD("variable_conflict", K_VARIABLE_CONFLICT, UNRESERVED_KEYWORD)  
        PG_KEYWORD("warning", K_WARNING, UNRESERVED_KEYWORD)  
};  

如何强制使用保留字keyword

对于identity,可以使用双引号,强制使用关键字。

postgres=# create table "cast" (id int);  
CREATE TABLE  
  
postgres=# select * from "cast";  
 id   
----  
(0 rows)  

参考

《PostgreSQL Oracle 兼容性之 - 系统列(关键字、保留字)的处理(ctid, oid, cmin, cmax, xmin, xmax)》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
2月前
|
SQL 监控 Oracle
Oracle SQL性能优化全面指南
在数据库管理领域,Oracle SQL性能优化是确保数据库高效运行和数据查询速度的关键
|
2月前
|
SQL 存储 Oracle
Oracle数据库SQL语句详解与应用指南
在数字化时代,数据库已成为各类企业和组织不可或缺的核心组件。Oracle数据库作为业界领先的数据库管理系统之一,广泛应用于各种业务场景。掌握Oracle数据库的SQL语句是数据库管理员、开发人员及运维人员的基本技能。本文将详细介绍Oracle数据库SQL语句的基本概念、语法、应用及最佳实践。一、Or
60 3
|
2月前
|
SQL Oracle 关系型数据库
Oracle SQL:了解执行计划和性能调优
Oracle SQL:了解执行计划和性能调优
58 1
|
3月前
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
464 2
|
5月前
|
SQL
SQL FULL OUTER JOIN 关键字
【7月更文挑战第17天】SQL FULL OUTER JOIN 关键字。
42 6
|
5月前
|
SQL 数据库
SQL RIGHT JOIN 关键字
【7月更文挑战第17天】SQL RIGHT JOIN 关键字。
39 2
|
5月前
|
SQL 存储 Oracle
TDengine 3.3.2.0 发布:新增 UDT 及 Oracle、SQL Server 数据接入
**TDengine 3.3.2.0 发布摘要** - 开源与企业版均强化性能,提升WebSocket、stmt模式写入与查询效率,解决死锁,增强列显示。 - taos-explorer支持geometry和varbinary类型。 - 企业版引入UDT,允许自定义数据转换。 - 新增Oracle和SQL Server数据接入。 - 数据同步优化,支持压缩,提升元数据同步速度,错误信息细化,支持表名修改。 - 扩展跨平台支持,包括麒麟、Euler、Anolis OS等。
128 0
|
SQL Cloud Native 关系型数据库
ADBPG(AnalyticDB for PostgreSQL)是阿里云提供的一种云原生的大数据分析型数据库
ADBPG(AnalyticDB for PostgreSQL)是阿里云提供的一种云原生的大数据分析型数据库
1277 1
|
数据可视化 关系型数据库 MySQL
将 PostgreSQL 迁移到 MySQL 数据库
将 PostgreSQL 迁移到 MySQL 数据库
1761 2
|
SQL 存储 自然语言处理
玩转阿里云RDS PostgreSQL数据库通过pg_jieba插件进行分词
在当今社交媒体的时代,人们通过各种平台分享自己的生活、观点和情感。然而,对于平台管理员和品牌经营者来说,了解用户的情感和意见变得至关重要。为了帮助他们更好地了解用户的情感倾向,我们可以使用PostgreSQL中的pg_jieba插件对这些发帖进行分词和情感分析,来构建一个社交媒体情感分析系统,系统将根据用户的发帖内容,自动判断其情感倾向是积极、消极还是中性,并将结果存储在数据库中。
玩转阿里云RDS PostgreSQL数据库通过pg_jieba插件进行分词

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 下一篇
    无影云桌面