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:
@@ -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)
|
||||
|
||||
26
postgres/max-identifier-length-is-63-bytes.md
Normal file
26
postgres/max-identifier-length-is-63-bytes.md
Normal 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.
|
||||
Reference in New Issue
Block a user