1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Max Identifier Length Is 63 Bytes as a postgres til.

This commit is contained in:
jbranchaud
2015-12-08 19:36:58 -06:00
parent 9c0d436596
commit 2c85dca0eb
2 changed files with 27 additions and 0 deletions

View File

@@ -132,6 +132,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
- [List All Columns Of A Specific Type](postgres/list-all-columns-of-a-specific-type.md)
- [List All Versions Of A Function](postgres/list-all-versions-of-a-function.md)
- [List Database Users](postgres/list-database-users.md)
- [Max Identifier Length Is 63 Bytes](postgres/max-identifier-length-is-63-bytes.md)
- [Restart A Sequence](postgres/restart-a-sequence.md)
- [Send A Command To psql](postgres/send-a-command-to-psql.md)
- [Special Math Operators](postgres/special-math-operators.md)

View File

@@ -0,0 +1,26 @@
# Max Identifier Length Is 63 Bytes
In PostgreSQL, identifiers -- table names, column names, constraint names,
etc. -- are limited to a maximum length of 63 bytes. Identifiers longer than
63 characters can be used, but they will be truncated to the allowed length
of 63.
```sql
> alter table articles
add constraint this_constraint_is_going_to_be_longer_than_sixty_three_characters_id_idx
check (char_length(title) > 0);
NOTICE: identifier "this_constraint_is_going_to_be_longer_than_sixty_three_characters_id_idx" will be truncated to "this_constraint_is_going_to_be_longer_than_sixty_three_characte"
ALTER TABLE
```
Postgres warns us of identifiers longer than 63 characters, informing us of
what it will be truncated to. It then proceeds to create the identifier.
If postgres is trying to generate an identifier for us - say, for a foreign
key constraint - and that identifier is longer than 63 characters, postgres
will truncate the identifier somewhere in the middle so as to maintain the
convention of terminating with, for example, `_fkey`.
The 63 byte limit is not arbitrary. It comes from `NAMEDATALEN - 1`. By default
`NAMEDATALEN` is 64. If need be, this value can be modified in the Postgres
source. Yay, open-source database implementations.