시작 하기 전에 ..

Postgres 에서는 저장 프로시저를 사용하지 않고 함수를 사용한다고 한다.

우리가 아는 procedure == function 이라고 생각하면 될 것 같다.

 

Stored Procedure는 이미 컴파일되어 DBMS 단계에서 바로 실행할 수 있도록 준비되어 있어 그 실행 속도가 빠르며, 호출시 인자만 변경되기 때문에 재활용이 가능하다.

 

이 글에서는 단점에 대한 내용은 정리 하지 않도록 하겠다.

 

그럼 pl/pgsql 을 이용해 10자리 난수를 얻는 간단한 Procedure를 작성해 보자

create or replace function custom_random_number()
returns bigint language plpgsql as
$$
   declare result bigint := (select rpad(cast(trunc(random() * 10000000000) as varchar), 10 , '0'));
   begin
        return result;
   end;
$$;

alter function custom_random_number() owner to yeokgank;

작성한 Procedure 를 호출 해본다.

select custom_random_number() as random_number
from generate_series(1,10);

결과 

 

 

EDB: Power to Postgres | Get the most out of PostgreSQL with tools, products, services, and support for enterprises

EDB provides best in class database management software and wide-range services with 24x7 support to get more from Postgres. EDB offers secure, scalable, advanced and enterprise-class Postgres solutions.

www.enterprisedb.com

Reference

+ Recent posts