DB/PostgreSQL

[PostgreSQL] PostgreSQL의 이진 타입에 대해서 알아보자. (Binary Type)

빙기때침식곡 2022. 6. 14. 01:27
반응형

HEX와 Escape 방식이 있다.

 

binary 문자열은 프로그래머가 원시 데이터로 생각하는 데이터를 저장하는데 적합하다.

문자열은 텍스트를 저장하는데 적합하다.

 

 

1. Bytes Hex Format

 - 대문자/소문자 둘다 가능

 - 숫자 사이게 공백 허용

 - 16진 형식은 광범위한 외부 응용 프로그램 및 프로토콜과 호환되며 Escape 형식보다 변환속도가 빠르다.

 - ex) SELECT '\xDEADBEEF';

 

2. Bytes Escape Format

 - The "escape" format is the traditional PostgreSQL format for the bytea type.

 - 응용 프로그램의 관점에서 바이트를 문자로 나타내는 것이 적합하면 표현이 편리할 수 있다.

 - this format should probably be avoided for most new applications.

 - 가능하면 쓰지말자... HEX 타입을 쓰자.

 

 

SET bytea_output 커맨드로 타입을 변경할 수 있다.

디폴트는 HEX다.

 

 

 

 

 

예시1. HEX 타입

SET  bytea_output = ‘hex’;
SELECT ‘abc \153\154\155 \052\251\124’::bytea;

      bytea
—------------------
abc klm *\251T

 

 

 

 

예시2. Escape 타입

SET bytea_output = ‘escape’;
SELECT ‘abc \153\154\155 \052\251\124’::bytea;

      bytea
—---------------
\x616263206b6c6d202aa954

 

 

 

 

REFERENCES

 

PostgreSQL : Documentation: 13: PostgreSQL 13.7 Documentation

 

postgrespro.com

 

반응형