libpg_query
libpg_query copied to clipboard
char data type
- "CREATE TABLE brintest (charcol char)" parse char data type as bpchar:
{"ColumnDef":{"colname":"charcol","typeName":{"names":[{"String":{"str":"pg_catalog"}},{"String":{"str":"bpchar"}}],"typmods":[{"A_Const":{"val":{"Integer":{"ival":1}},"location":-1}}],"typemod":-1,"location":48},"is_local":true,"location":40}}
- "CREATE TABLE brintest (charcol "char")" parse "char" data type as char:
{"ColumnDef":{"colname":"charcol","typeName":{"names":[{"String":{"str":"char"}}],"typemod":-1,"location":48},"is_local":true,"location":40}}
- "CREATE TABLE brintest (charcol char(1))" parse char data type as bpchar:
{"ColumnDef":{"colname":"charcol","typeName":{"names":[{"String":{"str":"pg_catalog"}},{"String":{"str":"bpchar"}}],"typmods":[{"A_Const":{"val":{"Integer":{"ival":1}},"location":-1}}],"typemod":-1,"location":48},"is_local":true,"location":40}}
Is it correct ?
Thanks hong
Hi @hcheng2002cn,
Yes, this is since the Postgres parser special cases the character data types (see here: https://github.com/postgres/postgres/blob/master/src/backend/parser/gram.y#L13095)
When you specify the type as "char" on the other hand you avoid all that logic, since you are telling Postgres the exact type name you want to reference.