Technical articles from Pivotal engineers.

ByteA versus TEXT in PostgreSQL (for textual data)

One of our customers switched from MongoDB to PostgreSQL, and the migration tool created all data fields as ByteA instead of TEXT. Makes one wonder, if there is a performance difference and if TEXT could be a wiser choice.

One of our customers switched from MongoDB to PostgreSQL, because the JSON performance in MongoDB was not sufficient.

The application is already running faster on PostgreSQL, but for now it must run in parallel on both platforms, and queries (using a framework) have to work equally on both databases. Therefore the superior JSON and JSONB functionality in PostgreSQL can’t be used here - for now. However the migration tool chose to make every data field a ByteA column in PostgreSQL - and I wondered if that is a good choice. Therefore I created a small test case to measure the read and write performance of ByteA and TEXT. A common recommendation is to encode data somehow (in Base64, as example) and then store it in TEXT instead of ByteA. So I measured the overhead of that approach as well.

Test approach

The actual test is written in Perl, but here is an abstract of what is measured.

The database

The single table used here is always an integer column (think of a primary key), and a data field:

The Perl module “String::Random” takes care of creating a random string. For ByteA, a truly random character, for TEXT the string is in the printable characters area. The Base64 encoding takes place here, but the original string is stored and compared later with what was read from the database (not part of the performance test).

The read test

To measure the read performance, a single string is written into the table, and then read multiple times.

No index overhead, just a very simple query and one single row in the table. The average over three runs is used. The data is compared which was written to disk, but the comparisation happens after the second time is taken.

The write test

To measure the write performance, the data is written into the table using a prepared statement, only the execution time is measured.

The results

Read performance

Many use cases rely on a good read performance. Not surprisingly, the performance for the TEXT datatype is around 15% better than the ByteA datatype.

It is a bit surprising that the overhead for reading base64 encoded data is around 10% better than reading the binary data in ByteA.

Write performance

At first glance, the numbers for writing into the different datatypes show no real difference.

Looking into the detail, there is a very small difference, and using TEXT is only slightly faster than using ByteA.

Conclusions

Switching from ByteA to TEXT will result in an instant performance improvement for read queries. That is a big gain, given that no application code has to be changed. The write performance will not change much.

Even switching from ByteA to encoded data (like Base64) will improve the read performance - this approach can be considered if the application can be changed, and truly binary data is stored in the database.

Transformation

In our case, the table is created automatically, and it is not a problem to redo the entire operation using TEXT instead of ByteA. However if you are in a similar situation, the following SQL command shows how to change the datatype: