Using PL/Perl for datatype abstraction

As stated in this chapter, functions in PostgreSQL are pretty universal and can be used in many different contexts. If you want to use functions to improve data quality, you can use a CREATE DOMAIN clause:

test=# \h CREATE DOMAIN  Command: CREATE DOMAINDescription: Define a new domainSyntax:CREATE DOMAIN name  [ AS ] data_type 
[ COLLATE collation ] 
[ DEFAULT expression ] [ constraint [ ... ] ]  Here, constraint is as follows:    [ CONSTRAINT constraint_name ] 
{ NOT NULL  | NULL  | CHECK  (expression) } 

In this example, the PL/Perl function will be used to create a domain called email, which in turn can be used as a datatype.

The following listing shows how the domain can be created:

test=# CREATE DOMAIN email ...

Get Mastering PostgreSQL 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.