프로젝트를 수행하다 보면 특정 테이블에 대해서 기능/단위테스트 또는 성능측정/쿼리튜닝을 수행하는 경우가 발생한다.
개발자 또는 테스터가 일일히 테이터를 넣는 것은 한계가 있고 10만건, 100만건 이상의 대용량의 상황을 가정할 경우도 생기기 때문에 Postgresql DBMS에서 제공하는 general_series 함수를 이용하여 더미데이터, 테스트데이터를 만드는 방법을 추천한다.
실행환경
- Postgresql 9.4 + Greenplum 6.11.x
- Postgresql 13
- dbeaver 7.3.4
먼저 general_series(1, 10) 을 실행해서 어떻게 작동하는지 알아보자.
select * from generate_series(1, 10)
general_series 1, 10을 입력하면 1부터 10까지 1씩 증가하면서 ROW가 생성된 것을 확인할 수가 있을 것이다.
이제 테스트 테이블을 만들고 더미데이터를 만들어 보자.
1. 더미 테이블 생성(public.dummy_table)
-- Postgresql
create table public.dummy_table(
no integer not null primary key
, title varchar(300) not null
, content text null
, create_dt timestamp(0) not null default current_timestamp
, update_dt timestamp(0) null
);
-- Greenplum
create table public.dummy_table(
no integer not null primary key
, title varchar(300) not null
, content text null
, create_dt timestamp(0) not null default current_timestamp
, update_dt timestamp(0) null
)
distributed by (no);
select * from public.dummy_table;
Greenplum 일 경우에는 distributed by (no) 구문을 추가하자.
Greenplum 아키덱처에서는 분산키가 필요하다. (primary key 가 존재하여 randomly 로는 설정이 불가능 하다.)
2. 더미데이터 만건 생성 후 INSERT
-- 더미데이터 INSERT
insert into public.dummy_table
(no, title, content)
select series as no
, md5(trunc(random() * 40)::varchar) as title
, substr('가나다라마바사아자차카타파하ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', trunc(random() * 40)::integer + 1, trunc(random() * 10)::integer) as content
from generate_series(1, 10000) series;
-- 건수조회
select count(*)
from public.dummy_table;
-- 데이터 조회
select *
from public.dummy_table
limit 100;
3. general_series function - 공식사이트도 한번 확인해보자.
www.postgresql.org/docs/9.4/functions-srf.html
Set Returning Functions
This section describes functions that possibly return more than one row. The most widely used functions in this class are series generating functions, as detailed in Table 9-54 and Table 9-55. Other, more specialized set-returning functions are described e
www.postgresql.org
general_series 공식사이트를 방문하여 날짜 증감 기능 및 숫자형/날짜형 증감값 설정을 확인해보자.
'Database > Postgresql' 카테고리의 다른 글
[Postgresql/Greenplum] 테이블명 길이 63Byte 제한 (0) | 2021.08.10 |
---|---|
postgresql/greenplum uptime을 to_char 함수로 보기편하게 변경하기 (0) | 2021.04.04 |
PGTune 을 이용하여 postgresql.conf 파라미터 설정하기 (0) | 2021.01.26 |
Postgresql function 개발 및 예제 (0) | 2021.01.22 |
pgagroal 란? (0) | 2021.01.15 |