PostgreSQL 일일 게시 --PL/pgSQL 저장 프로세스 예

코드 예제
--1. psql                 RAISE NOTICE
--2.       perform function_name(...)
--3.     
--4.    
--5.     
drop table if exists test_table;
CREATE TABLE test_table(start_time timestamp, end_time timestamp, str_flag varchar(200));

CREATE OR REPLACE FUNCTION test_function() RETURNS double precision AS $$
DECLARE
    start_time timestamp;
	end_time timestamp;
	time_consuming double precision;
BEGIN
    time_consuming := -1;
    
	select clock_timestamp() into start_time;
	RAISE NOTICE 'Quantity here is %', start_time;
	
	perform pg_sleep(3);
	
	select clock_timestamp() into end_time;
	RAISE NOTICE 'Quantity here is %', end_time;
	
	select round(EXTRACT(EPOCH from end_time)-EXTRACT(EPOCH from start_time)) into time_consuming;
	
    insert into test_table values(start_time, end_time, 'TEST_CLOCK_TIMESTAMP');
	
	return time_consuming;
END;
$$ LANGUAGE plpgsql;

테스트 결과
postgres=> select * from test_function(); NOTICE:  Quantity here is 2015-01-24 20:38:50.38032 NOTICE:  Quantity here is 2015-01-24 20:38:53.384296  test_function  ---------------              3 (1 row) postgres=> select * from test_table;         start_time         |          end_time          |       str_flag        ---------------------------+----------------------------+----------------------  2015-01-24 20:38:50.38032 | 2015-01-24 20:38:53.384296 | TEST_CLOCK_TIMESTAMP (1 row) postgres=>

좋은 웹페이지 즐겨찾기