Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XID as a 12 byte (bytea) unique indexed Postgres column #14

Closed
nkev opened this issue Sep 9, 2017 · 1 comment
Closed

XID as a 12 byte (bytea) unique indexed Postgres column #14

nkev opened this issue Sep 9, 2017 · 1 comment

Comments

@nkev
Copy link

nkev commented Sep 9, 2017

In my Postgres 9.6+ tables, I want to use an xid as a unique indexed column and also sometimes as primary key or foreign key column too. Is it more performant to store it as a 20 character string or 12 byte binary? I initially thought binary would be more efficient, but this article suggests otherwise.

Does anyone have any experience using a bytea as a primary key? If so, is it more efficient to use the default bytea hex format or the older bytea escape format?

@feikesteenbergen
Copy link

feikesteenbergen commented Nov 12, 2017

If so, is it more efficient to use the default bytea hex format or the older bytea escape format?

As far as I know, the escape and hex options only apply to how to represent the bytea value (input/output), it does not concern itself with the on-disk format, so efficiency of storage is a non-issue.
See also:
https://www.postgresql.org/docs/current/static/datatype-binary.html

a 20 character string or 12 byte binary

You'll lose 8 bytes storage per row, so for storage efficiency, bytea(12) beats text(20).

=# select pg_column_size(repeat('c', '20')), pg_column_size(convert_to(repeat('c', 12), 'UTF8'));
 pg_column_size | pg_column_size
----------------+----------------
             24 |             16

Is it more performant

Perhaps you can benchmark your suggested schema yourself, as for my (simple) setup I don't see a significant enough difference.

On the whole, I prefer text if it is text, and bytea if it is binary (encrypted values for example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants