PostgreSQL标识符长度限制不能超过63字节

文章目录


问题:标识符太长会被截断

在创建表时,发现表名太长会自动被截断,导致查询表时报错了。

分析

参考:https://www.postgresql.org/docs/current/limits.html

Item Upper Limit Comment
identifier length 63 bytes can be increased by recompiling PostgreSQL

PostgreSQL里面限制标识符(库名、表名等)长度不能超过63个字节,虽然这个参数可以在编译时修改,但是一般都不会去修改(63其实已经够用了,相当于6个长的单词,如果这都说不清楚,那么建议用简称+comment的模式)。

相关源码

实际上定义的变量NAMEDATALEN为64,但是C语言字符串结尾字符 \0 占一个字节,所以用的时候使用 NAMEDATALEN-1来进行判断的,即63。

源码详见:src\interfaces\ecpg\include\sqlda-native.h

在线可参考:https://github.com/postgres/postgres/blob/master/src/interfaces/ecpg/include/sqlda-native.h#L8-L16

c 复制代码
/*
 * Maximum length for identifiers (e.g. table names, column names,
 * function names).  Names actually are limited to one fewer byte than this,
 * because the length must include a trailing zero byte.
 *
 * This should be at least as much as NAMEDATALEN of the database the
 * applications run against.
 */
/*
 * 标识符的最大长度(例如表名、列名、函数名)。 名称实际上限制为比此少一个字节,因为长度必须包括一个尾随的零字节。
 *
 * 这至少应该与应用程序运行所针对的数据库的 NAMEDATALEN 一样多。
 */
#define NAMEDATALEN 64

可以尝试以下案例

sql 复制代码
-- 如果超过63(>63,可以为63)会被自动截断,会有提示(NOTICE),但是不会报错
SELECT repeat('d', 64);
create database dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd;
-- > NOTICE:  identifier "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" will be truncated to "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
SELECT length(datname),* from pg_database;
-- 63	ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

ps:注意这里的63是字节,如果是中文的话是21个中文,但是强烈不推荐使用中文标识符。

相关推荐
RestCloud9 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud9 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence11 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger18 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud1 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
IvorySQL2 天前
IvorySQL 4.6:DocumentDB+FerretDB 实现 MongoDB 兼容部署指南
postgresql