libpg_query icon indicating copy to clipboard operation
libpg_query copied to clipboard

char data type

Open hcheng2002cn opened this issue 4 years ago • 1 comments

  • "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

hcheng2002cn avatar Apr 12 '21 04:04 hcheng2002cn

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.

lfittl avatar May 07 '21 17:05 lfittl