Search code examples
postgresqlecpg

Postgres ECPG char[] vs. VARCHAR[]


We are using ECPG and host variables to connect to a postgres database. We're trying to understand when to use char[] vs VARCHAR[] as our host binding variable. The documentation doesn't provide any pros/cons or use-cases.

For example:

Given column

x VARCHAR (10)

Why would I use

EXEC SQL BEGIN DECLARE SECTION;
 char theX[10];
EXEC SQL END DECLARE SECTION;
cout << theX;

vs. say

EXEC SQL BEGIN DECLARE SECTION;
 VARCHAR theX[10];
EXEC SQL END DECLARE SECTION;
cout << theX.arr;

Thanks!

https://www.postgresql.org/docs/current/ecpg-variables.html


Solution

  • It does not matter which type you use in C. As the documentation describes, the difference is that VARCHAR is a struct that also contains the length of the string, while char is the normal null-terminated C string.

    If you need the length, VARCHAR might be more convenient.