Published on

The Folly of Using CHAR over VARCHAR

Authors

Today I ran into a weird bug where values returned from our Postgres DB appeared fine but in the code it was misbehaving inexplicably.

To debug this I first output the value wrapped with square brackets to see if any characters where being added anywhere:

LOGGER.debug("[$myVarFromTheDB]")

which output something like:

[John    ]

Clearly it is adding spaces somewhere. After doing a bit of digging I saw that the variable was being pulled from a DB column that had type CHAR. The excess space that the field did not fill up was padded with spaces. To fix this I altered the column to rather be a VARCHAR instead.