[postgres@pg-00:/var/lib/pgsql]$ rpm -qa|grep plperl ## 필자 linux 에는 plperl 모듈이 깔려 있습니다.
postgresql92-plperl-9.2.2-1PGDG.rhel6.i686
[postgres@pg-00:/var/lib/pgsql]$ psql -h pg-00 -U scott -d scottdb
Null display is "NULL".
Timing is off.
Pager usage is off.
psql (9.2.2)
Type "help" for help.
scott@pg-00:5432:scottdb]
SQL> select * from pg_language;
lanname | lanowner | lanispl | lanpltrusted | lanplcallfoid | laninline | lanvalidator | lanacl
----------+----------+---------+--------------+---------------+-----------+--------------+--------
internal | 10 | f | f | 0 | 0 | 2246 | NULL
c | 10 | f | f | 0 | 0 | 2247 | NULL
sql | 10 | f | t | 0 | 0 | 2248 | NULL
plpgsql | 10 | t | t | 12596 | 12597 | 12598 | NULL
(4 rows)
scott@pg-00:5432:scottdb]
SQL> create extension plperl; ## PL/perl extension 을 설치합니다.
CREATE EXTENSION
scott@pg-00:5432:scottdb]
SQL>
select * from pg_language; ## plperl 이 성공적을 설치 된것을 확인할수 있습니다.
lanname | lanowner | lanispl | lanpltrusted | lanplcallfoid | laninline | lanvalidator | lanacl
----------+----------+---------+--------------+---------------+-----------+--------------+--------
internal | 10 | f | f | 0 | 0 | 2246 | NULL
c | 10 | f | f | 0 | 0 | 2247 | NULL
sql | 10 | f | t | 0 | 0 | 2248 | NULL
plpgsql | 10 | t | t | 12596 | 12597 | 12598 | NULL
plperl | 16384 | t | t | 16786 | 16787 | 16788 | NULL
(5 rows)
scott@pg-00:5432:scottdb]
SQL>
그럼 이제 Perl 을 이용하여 함수를 생성해 보겠습니다.
scott@pg-00:5432:scottdb]
SQL> CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
if ($_[0] > $_[1]) { return $_[0]; }
return $_[1];
$$ LANGUAGE plperl;
CREATE FUNCTION
scott@pg-00:5432:scottdb]
SQL> SELECT prosrc FROM pg_proc WHERE proname='perl_max'; ## 소스 확인
prosrc
------------------------------------------
+
if ($_[0] > $_[1]) { return $_[0]; }+
return $_[1]; +
(1 row)
scott@pg-00:5432:scottdb]
SQL> select perl_max(100,10); ## 펄로 짠 함수 호출
perl_max
----------
100
(1 row)
scott@pg-00:5432:scottdb]
SQL>
Very Good!