# 잘 알려지지 않은 PostgreSQL 기능들

> Clean Markdown view of GeekNews topic #5353. Use the original source for factual precision when an external source URL is present.

## Metadata

- GeekNews HTML: [https://news.hada.io/topic?id=5353](https://news.hada.io/topic?id=5353)
- GeekNews Markdown: [https://news.hada.io/topic/5353.md](https://news.hada.io/topic/5353.md)
- Type: news
- Author: [xguru](https://news.hada.io/@xguru)
- Published: 2021-11-10T10:50:01+09:00
- Updated: 2021-11-10T10:50:01+09:00
- Original source: [hakibenita.com](https://hakibenita.com/postgresql-unknown-features)
- Points: 13
- Comments: 4

## Topic Body

- Insert on Conflict(Upsert) 시 수정/추가된 Row 수 알기 : xmax

- 특정 컬럼에 권한 부여하기

- 멀티 패턴 매칭 : SIMILAR TO 또는 ~ 정규식

- 현재 시퀀스값 증가시키지 않고 알아내기 : pg_sequence_last_value()

- 멀티라인 SQL과 \COPY 같이 사용하려면 : TEMPORARY VIEW 또는 COPY와 \g 이용

- 자동 생성 키에 값 설정 막기 : 테이블 생성시 GENERATED ALWAYS 지정 (GENERATED BY DEFAULT 대신)

- Pivot 테이블 만들기 : pandas.pivot_table, \crosstabview 또는 tablefunc 확장 사용

- Dollar Quoting

ㅤ→ $$ 와 $$ 사이의 모든 글자는 문자열로 인식

ㅤ→ $JSON$ / $function$ 처럼 안에 Tag이용 가능

ㅤ→ ::jsonb 를 붙이면 빠르게 jsonb객체 생성

- DB객체에 코멘트 달기 : COMMENT ON TABLE/COLUMN, Dollar Quoting 이용해서 긴 문자열 설명 추가도 가능

ㅤ→ 작성 : COMMENT ON TABLE sale IS 'Sales made in the system';

ㅤ→ 보기 : \d+ sale

- DB 별 History 별도로 기록하기

ㅤ→ \set HISTFILE ~/.psql_history- :DBNAME

- 자동완성을 대문자로 하기 : \set COMP_KEYWORD_CASE upper

- 슬립 주기 : pg_sleep(초), pg_sleep_for('4 minutes 14 seconds')

- 서브 쿼리 없이 그룹의 첫/마지막 줄 가져오기 : DISTINCT ON (그룹 컬럼)

- uuid-ossp 확장없이 UUID 생성하기 : gen_random_uuid()  version 4 UUID를 생성

- 재현 가능한 랜덤 데이터 생성 : setseed() 로 시드를 같게

- 기존 데이터를 즉시 검증하지 않고 조건 추가하기 : ALTER 할때 NOT VALID 로 조건만 먼저 추가하고, ALTER VALIDATE로 기존 데이터 검증은 따로 실행

- 오라클의 Synonym 같은 기능을 이용하기 : search_path 변경 (Zero Downtime Migration시 유용)

- 겹치는 Range 찾기 : OVERLAPS 연산자

## Comments



### Comment 7547

- Author: galadbran
- Created: 2021-11-11T10:38:11+09:00
- Points: 1

GeekNews 에는 너무 postgres 소식만 올라오는 거아닌가요! (농담입니다.)

저희 제품도 pg 를 주로 사용하고 있어, 감사할 따름입니다. :)

### Comment 7550

- Author: xguru
- Created: 2021-11-11T12:09:28+09:00
- Points: 2
- Parent comment: 7547
- Depth: 1

요즘 postgres가 대세라서 그런겁니다..!?

### Comment 7538

- Author: nallwhy
- Created: 2021-11-10T10:59:41+09:00
- Points: 1

postgres 가 최고시다

### Comment 7539

- Author: roxie
- Created: 2021-11-10T15:38:31+09:00
- Points: 1
- Parent comment: 7538
- Depth: 1

동감합니다.
