Postgresql

什麼決定了一個 bytea 是否有 1 個或 4 個額外字節?

  • May 25, 2017

在 PostgreSQL 中插入 BYTEA 字元串時,文件說這樣的字元串將佔用 1 或 4 個字節加上字節字元串長度。

我似乎找不到答案的問題是什麼決定了添加 1 個字節還是 4 個字節,這種效果是一致的還是會因某些因素或其他因素而有所不同?

我相信答案就在heaptuple.c的標題註釋中:

* Before Postgres 8.3 varlenas always had a 4-byte length header, and
* therefore always needed 4-byte alignment (at least).  This wasted space
* for short varlenas, for example CHAR(1) took 5 bytes and could need up to
* 3 additional padding bytes for alignment.
*
* Now, a short varlena (up to 126 data bytes) is reduced to a 1-byte header
* and we don't align it.  To hide this from datatype-specific functions that
* don't want to deal with it, such a datum is considered "toasted" and will
* be expanded back to the normal 4-byte-header format by pg_detoast_datum.

引用自:https://dba.stackexchange.com/questions/83173