Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

PL/Perl and Large PostgreSQL Databases

DZone's Guide to

PL/Perl and Large PostgreSQL Databases

PL/Perl is particularly useful for extracting data from structured text documents. Its elements combine to create a great procedural language for PostgreSQL.

· Database Zone ·
Free Resource

Discover Tarantool's unique features which include powerful stored procedures, SQL support, smart cache, and the speed of 1 million ACID transactions on a single CPU core!

One of the topics discussed in the large database talk is the way we used PL/Perl to solve some data variety problems in terms of extracting data from structured text documents.

It is certainly possible to use other languages to do the same, but PL/Perl has an edge in a number of important ways. PL/Perl is lightweight, flexible, and fills this particular need better than any other language I have worked with.

While one of the considerations has often been knowledge of Perl in the team, PL/Perl has a number of specific reasons to recommend it:

  1. It is lightweight compared to PL/Java and many other languages
  2. It excels at processing text in general ways.
  3. It has extremely mature regular expression support

These features combine to create a procedural language for PostgreSQL that is particularly good at extracting data from structured text documents in the scientific space. Structured text files are very common and being able to extract, for example, a publication date or other information from the file is very helpful.

Moreover, when you mark your functions as immutable, you can index the output, and this is helpful when you want ordered records starting at a certain point.

So for example, suppose we want to be able to query on plasmid lines in UNIPROT documents but we have not set this up before we loaded the table. We could easily create a PL/Perl function like:

CREATE OR REPLACE FUNCTION plasmid_lines(uniprot text) 
RETURNS text[]


LANGUAGE PLPERL IMMUTABLE AS
$$

use strict;
use warnings;

my ($uniprot) = @_;
my @lines = grep { /^OG\s+Plasmid/ } split /\n/ $uniprot;

return [ map {  my $l = $_; $l =~ s/^OG\s+Plasmid  \s*//; $l } @lines  ];
$$;


You could then create a GIN index on the array elements:

CREATE INDEX uniprot_doc_plasmids ON uniprot_docs USING gin (plasmid_lines(doc));


Neat!

Discover Tarantool's unique features such as powerful stored procedures, SQL support, smart cache, and the speed of 1 million ACID transactions on a single CPU.

Topics:
database ,postgresql ,data extraction ,perl ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}