Database Reference
In-Depth Information
Why untrusted?
PostgreSQL's ability to use an untrusted language is a powerful way to perform some
nontraditional things from database functions. Creating these functions in a PL is an
order of magnitude smaller task than writing an extension function in C. For example,
a function to look up a hostname for an IP address is only a few lines in PL/Pythonu:
CREATE FUNCTION gethostbyname(hostname text)
RETURNS inet
AS $$
import socket
return socket.gethostbyname(hostname)
$$ LANGUAGE plpythonu SECURITY DEFINER;
You can test it immediately after creating the function by using psql :
hannu=# select
gethostbyname('www.postgresql.org');
gethostbyname
----------------
98.129.198.126
(1 row)
Creating the same function in the most untrusted language, C, involves writing tens
of lines of boilerplate code, worrying about memory leaks, and all the other problems
coming from writing code in a low-level language. While we will look at extending
PostgreSQL in C in the next chapter, I recommend prototyping in some PL language
if possible, and in an untrusted language if the function needs something that the re-
stricted languages do not offer.
Why PL/Python?
All of these tasks could be done equally well using PL/Perlu or PL/Tclu; I chose PL/
Pythonu mainly because Python is the language I am most comfortable with. This
also translates to having written some PL/Python code, which I plan to discuss and
share with you in this chapter.
Search WWH ::




Custom Search