postgresqlstore 프로세스 초기 탐색 중 하나

os: centos 7.4 db: postgresql 11.5
postgresql11 이전의 함수(function)와 저장 프로세스(procedure)는 같은 의미어로 문법은 다음과 같다.
CREATE [OR REPLACE] FUNCTION function_name (arguments)   
RETURNS return_datatype AS $variable_name$  
  DECLARE  
    declaration;  
    [...]  
  BEGIN  
    < function_body >  
    [...]  
    RETURN { variable_name | value }  
  END; LANGUAGE plpgsql;
  

예:
CREATE OR REPLACE FUNCTION totalRecords ()  
RETURNS integer AS $body$  
declare  
    total integer;  
BEGIN  
   SELECT count(*) into total FROM EMPLOYEES;  
   RETURN total;  
END;  
$body$ LANGUAGE plpgsql;


select 호출 사용하기
select totalRecords();


postgresql11은 저장 프로세스(procedure)의 사용을 시작합니다.commit,rollback의 사무 문장을 포함할 수 있습니다.
CREATE [ OR REPLACE ] PROCEDURE
    name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] )
  { LANGUAGE lang_name
    | TRANSFORM { FOR TYPE type_name } [, ... ]
    | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
    | SET configuration_parameter { TO value | = value | FROM CURRENT }
    | AS 'definition'
    | AS 'obj_file', 'link_symbol'
  } ...
  

호출
CALL name ( [ argument ] [, ...] )


버전

# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 

# su - postgres
$ psql -c "select version();"
                                                 version                                                 
---------------------------------------------------------------------------------------------------------
 PostgreSQL 11.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
(1 row)


테스트

create table t_log (
 id            bigserial,
 log_level     varchar(100),
 log_info      varchar(100),
 log_timestamp timestamp
)
;

CREATE OR REPLACE PROCEDURE sp_write_log (
	iv_log_level in varchar,
	iv_log_info  in varchar
) AS 
$body$
declare
BEGIN
      INSERT INTO t_log(log_level,log_info,log_timestamp) 
           VALUES (iv_log_level,iv_log_info,clock_timestamp())
      ;
END;
$body$
LANGUAGE 'plpgsql';


select 호출 사용하기
postgres=# select * from t_log;
 id | log_level | log_info | log_timestamp 
----+-----------+----------+---------------
(0 rows)

postgres=# select sp_write_log('a','a');
ERROR:  sp_write_log(unknown, unknown) is a procedure
LINE 1: select sp_write_log('a','a');
               ^
HINT:  To call a procedure, use CALL.


콜 호출을 사용해야 합니다
postgres=# select * from t_log;
 id | log_level | log_info | log_timestamp 
----+-----------+----------+---------------
(0 rows)

postgres=# call sp_write_log('a','a');
CALL
postgres=# select * from t_log;
 id | log_level | log_info |       log_timestamp        
----+-----------+----------+----------------------------
  1 | a         | a        | 2019-10-21 09:47:40.235877
(1 row)


commit, rollback 태그 추가

CREATE OR REPLACE PROCEDURE sp_write_log2 (
	iv_log_level in varchar,
	iv_log_info  in varchar
) AS 
$body$
declare
BEGIN
      INSERT INTO t_log(log_level,log_info,log_timestamp) 
           VALUES (iv_log_level,iv_log_info,clock_timestamp())
      ;
	  INSERT INTO t_log(log_level,log_info,log_timestamp) 
           VALUES (iv_log_level,iv_log_info,clock_timestamp())
      ;
	  rollback;
	  INSERT INTO t_log(log_level,log_info,log_timestamp) 
           VALUES (iv_log_level,iv_log_info,clock_timestamp())
      ;
	  commit;
END;
$body$
LANGUAGE 'plpgsql';

postgres=# call sp_write_log('a','a');
CALL
postgres=# call sp_write_log('a','a');
CALL
postgres=# select * from t_log;
 id | log_level | log_info |       log_timestamp        
----+-----------+----------+----------------------------
  1 | a         | a        | 2019-10-21 10:07:00.627889
  2 | a         | a        | 2019-10-21 10:07:05.477492
(2 rows)


Sequence도 굴러갔어요.
참조:https://github.com/dbeaver/dbeaver/releases https://www.postgresql.org/docs/11/sql-createprocedure.html https://www.postgresql.org/docs/11/sql-createfunction.html

좋은 웹페이지 즐겨찾기