2026년 2월 5일 목요일

다수 Array 컬럼이 포함된 테이블에 Analyze 수행시 메모리 사용량 및 수행시간 개선

 

1. 개선 요약
   - Greenplum 6.32.0에서 Array 컬럼이 다수 포함된 테이블에 Analyze 수행시 메모리 사용량 감소 및 수행시간 획기적 개선
   - 실 테스트 데이터 기준: 테이블 용량 237GB, 전체 컬럼수 136개, array 컬럼 97개
   - 마스터 노드 메모리 사용량 : 28,000MB -> 128MB (245배 메모리 사용량 개선)
   - 수행 속도: 140초 -> 2초 (70배 속도 향상)

2. 적용 방법
   1) Greenplum 6.32.0+ 버전 사용
      - 기능 개선 사항: 
         * Analyze 수행시 테이블의 데이터 샘플링을 하는데, 컬럼에 statistics = 0으로 설정된 컬럼에 대해서는 샘플링하지 않음.
         * 기존에는 analyze table_name(column1) 으로 수행하더라도 데이터 샘플링하는 경우에는 모든 컬럼에 대해서 샘플링 하였음.
      - https://techdocs.broadcom.com/us/en/vmware-tanzu/data-solutions/tanzu-greenplum/6/greenplum-database/relnotes-release-notes.html
      - The ANALYZE command now skips columns with a zero statistics target during sampling. 
        This optimization significantly improves overall runtime and reduces coordinator memory usage 
         when tables contain numerous columns with zero statistics targets.
   
   2) 적용 방법
      - 모든 Array 컬럼에 대해서 통계 수집을 0으로 설정
        ALTER TABLE table_name ALTER COLUMN array_colmun SET STATISTICS 0;  
      - Analyze table_name;

3. 사용자 테이블 실 테스트 결과 
      - 테이블 용량 237GB, 전체 컬럼 136개, array 컬럼 97개
      - 데이터 건수와 Array 컬럼 개수, Array 건수에 영향을 받기 때문에, 각 케이스마다는 차이가 발생할 수 있으나, 
         메모리 및 Analyze 수행시간은 확실하게 개선 됨.
Greenplum 6.28.2Greenplum 6.32.0개선비(배)
메모리 사용량 (MB)소요시간(sec)메모리 사용량 (MB)소요시간(sec)메모리 개선비소요시간 개선비
1차28,380139116224570
2차28,632141116224771
3차28,456141116224571
평균28,489140116224670

4. 테이블 통계 (Analyze) 
     1)  Analyze는 테이블 통계를 업데이트하는데 쿼리 플랜 생성시 테이블 통계를 이용함.
          - 테이블 통계는 쿼리 플랜 생성시 테이블 건수, Join, Where, Sort, Group by, Having 절에 사용됨.
     2) array 컬럼을 조인/where조건, Order by, Group by, Having 절에 사용하지 않으므로, 통계 생성할 필요 없음.

2026년 2월 1일 일요일

Greenplum에서 RESTful API 사용하기

 Greenplum에서 RESTful API 사용하기


Greenplum 7.6+에서 PostgREST 13을 지원


## 1. RESTAPI를 위한 DB 계정 및 테스트 테이블


-- 1) API 전용 스키마 생성

CREATE SCHEMA api;


-- 2) 샘플 테이블 생성

CREATE TABLE api.todos (

id serial PRIMARY KEY,

done boolean DEFAULT false,

task text NOT NULL,

due timestamptz

) DISTRIBUTED BY (id); -- Greenplum 특유의 DISTRIBUTED 절


-- 3) 데이터 삽입

INSERT INTO api.todos (task)

VALUES ('finish tutorial 0'), ('pat self on back');


SELECT * FROM api.todos;

id|done |task |due|

--+-----+-----------------+---+

2|false|pat self on back | |

1|false|finish tutorial 0| |

-- 4) 익명 사용자용 역할(Role) 생성

-- 실제 로그인 권한은 없으며, PostgREST가 이 권한으로 전환하여 쿼리합니다.

CREATE ROLE web_anon NOLOGIN;

GRANT USAGE ON SCHEMA api TO web_anon;

GRANT SELECT ON api.todos TO web_anon;


-- 5) 인증용 관리자 역할 생성 (PostgREST가 DB에 실제 접속할 때 사용)

CREATE ROLE authenticator NOINHERIT LOGIN PASSWORD 'changeme';

GRANT web_anon TO authenticator;


## 2. PostgREST 13 설치 및 구동

-- 1) Greenplum PostgREST 13.0.4-1 for RHEL 9 다운로드

-- https://support.broadcom.com/

-- 다운로드 파일 : postgrest-13.0.4-1.el9.x86_64.rpm


-- 2) PostgREST 설치

# yum install postgrest-13.0.4-1.el9.x86_64.rpm

# which postgrest

/usr/bin/postgrest

# ls -la /usr/bin/postgrest

-rwxr-xr-x 1 root root 20148784 Jul 11 2025 /usr/bin/postgrest


-- 3) PostgREST 설정

-- postgrest.conf 설정

$ cat << EOF > $COORDINATOR_DATA_DIRECTORY/postgrest.conf # Prepare CONFIGURATION

# postgrest.conf


# Greenplum Coordinator 노드의 접속 정보

db-uri = "postgres://authenticator:changeme@172.16.65.31:5432/gpkrtpch"


# API로 노출할 스키마

db-schemas = "api"


# 인증되지 않은 요청이 사용할 역할

db-anon-role = "web_anon"


# 서버 포트 설정

server-port = 3000


# (선택) PostgREST 13 신규 기능: 스키마 캐시 최적화

# Greenplum은 스키마가 방대할 수 있으므로 필요한 경우 설정

db-pool = 10

EOF


-- postgrest.conf 설정 확인

$ cat $COORDINATOR_DATA_DIRECTORY/postgrest.conf


-- 4) PostgREST 구동

$ postgrest $COORDINATOR_DATA_DIRECTORY/postgrest.conf



## 3. RESTAPI 테스트

-- 1) 테이블 조회

[gpadmin@r9g7s1 ~]$ curl http://172.16.65.31:3000/todos

[{"id":2,"done":false,"task":"pat self on back","due":null},

{"id":1,"done":false,"task":"finish tutorial 0","due":null}]

[gpadmin@r9g7s1 ~]$


-- 2) 테이블 조회 및 Json 문서로 출력

[gpadmin@r9g7s1 ~]$ curl http://172.16.65.31:3000/todos | jq

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 123 100 123 0 0 7687 0 --:--:-- --:--:-- --:--:-- 8200

[

{

"id": 2,

"done": false,

"task": "pat self on back",

"due": null

},

{

"id": 1,

"done": false,

"task": "finish tutorial 0",

"due": null

}

]


## 4. RESTAPI Features

-- 1) Filtering

$ curl "http://172.16.65.31:3000/todos?id=eq.1"

=# SELECT * FROM api.todos WHERE id = 1;


$ curl "http://172.16.65.31:3000/todos?id=eq.1&done=eq.false"

=# SELECT * FROM api.todos WHERE id = 1 AND done = false;


$ curl "http://172.16.65.31:3000/todos?task=like.*tutorial*"

=# SELECT * FROM api.todos WHERE task LIKE '%tutorial%';


-- 2) sorting

$ curl "http://172.16.65.31:3000/todos?order=id.asc"

=# SELECT * FROM api.todos ORDER id;


$ curl "http://172.16.65.31:3000/todos?order=id.desc"

=# SELECT * FROM api.todos ORDER id desc;


-- 3) pagination

$ curl "http://172.16.65.31:3000/todos?limit=15&offset=30"

=# SELECT * FROM api.todos LIMIT 15 OFFSET 30;

다수 Array 컬럼이 포함된 테이블에 Analyze 수행시 메모리 사용량 및 수행시간 개선

  1. 개선 요약    - Greenplum 6.32.0에서 Array 컬럼이 다수 포함된 테이블에 Analyze 수행시 메모리 사용량 감소 및 수행시간 획기적 개선    - 실 테스트 데이터 기준: 테이블 용량 237GB, 전체 컬럼수 136개, ...