standard_conforming_strings

控制反斜杠是否被字面解释的 GUC 参数

standard_conforming_strings 是一个 配置参数,它决定了在字符串字面量 ('...') 中反斜杠是否被视为字面字符(按照 SQL 标准的规定)。

standard_conforming_stringsPostgreSQL 8.1 中添加。

默认

standard_conforming_strings 的默认值为:on

用法

PostgreSQL 8.1 之前,字符串字面量中的反斜杠总是被解释为引入一个转义序列,例如 '\n'(换行符),这根据 SQL 标准来说是非标准行为。

PostgreSQL 8.1 中,引入了 E'' 语法来明确表示需要处理转义序列的字符串字面量。

PostgreSQL 9.1 开始,standard_conforming_strings 默认设置为 on,但可以设置为 off 来恢复 PostgreSQL 8.1 之前的默认行为。但是不推荐这样做,并且除非明确禁用,否则每次处理包含转义序列的字符串字面量时都会发出 WARNING 警告(参见下面的示例)。

psql

请注意,在 psql 中,只有当 standard_conforming_strings 设置为 off 时,才能使用单个引号的转义形式(\'),否则 psql 将无法正确解析字符串字面量(参见下面的示例)。

按 PostgreSQL 版本详细信息

standard_conforming_strings (PostgreSQL 19)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 18)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 17)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 16)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 15)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 14)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 13)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 12)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 11)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 10)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 9.6)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 9.5)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  
需要重启 false

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 9.4)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 9.3)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 9.2)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 9.1)

设置 开启
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 开启
重置值 开启
源文件  
源行  

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 9.0)

设置 off
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 off
重置值 off
源文件  
源行  

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 8.4)

设置 off
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  
枚举值  
启动值 off
重置值 off
源文件  
源行  

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 8.3)

设置  
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 使 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  

文档standard_conforming_strings

standard_conforming_strings (PostgreSQL 8.2)

设置  
单位  
类别 版本和平台兼容性 / 之前的 PostgreSQL 版本
简短描述 '...' 字符串中的反斜杠被字面解释。
扩展描述  
上下文 user
变量类型 bool
来源 默认
最小值  
最大值  

文档standard_conforming_strings

变更历史

示例

用法示例,当 standard_conforming_stringson 时 (自 PostgreSQL 9.1 起的默认值) - 此处 '\n' 字符序列被字面解释

postgres=# SELECT 'hello \nworld';
   ?column?    
---------------
 hello \nworld
(1 row)

standard_conforming_strings 设置为 off 时,'\n' 字符序列被解释为换行符转义序列

postgres=# SELECT 'hello \nworld';
WARNING:  nonstandard use of escape in a string literal
LINE 1: SELECT 'hello \nworld';
               ^
HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
 ?column? 
----------
 hello   +
 world
(1 row)

请注意,可以通过将 escape_string_warning 设置为 off 来禁用 WARNING 警告。

postgres=# SET escape_string_warning TO off;
SET
postgres=# SELECT 'hello \nworld'; ?column? ---------- hello + world (1 row)

只有当 standard_conforming_strings 设置为 off 时,psql 才能解析转义的单引号 (\')。

postgres=# SELECT 'hello \'world\'';
invalid command \'';
Try \? for help.
           ^C

postgres=# SET standard_conforming_strings TO off;
SET

postgres=# SELECT 'hello \'world\'';
WARNING:  nonstandard use of \' in a string literal
LINE 1: SELECT 'hello \'world\'';
               ^
HINT:  Use '' to write quotes in strings, or use the escape string syntax (E'...').
   ?column?    
---------------
 hello 'world'
(1 row)

有关上述警告的更多详细信息,请参见 backslash_quote

分类

向后兼容性, GUC 配置项, 字符串处理

另请参阅

escape_string_warning, backslash_quote

反馈

提交对 "standard_conforming_strings" 的任何评论、建议或更正 请在此处