Tracking Plperl Shared Variables

If you use as much Perl and PostgreSQL as we do at OmniTI, you’re bound to start building complex software solutions using [ plperl]. One of our latest projects (internally known as abyss) involves long term storage and archiving of email inside of PostgreSQL. That part isn’t interesting. The interesting part is how we make use of a bunch of custom perl modules we wrote, inside of plperl, to parse, digest, and dissect the information inside of those emails, transforming the data into something that can be analyzed more thoroughly by normal database tools; essentially giving structure to the semi-structured data inside of email. One of the parts that makes plperl so handy is it’s ability to use shared variables, to pass data from one function to another inside of a backend session. Using this technique, you can really start to do some interesting things, but it does present one problem when your trying to debug, specifically how to see the values that are getting passed around in a session. Luckily the solution is a lot simpler than you would think.
CREATE OR REPLACE FUNCTION dump_shared() RETURNS VOID LANGUAGE plperlU AS $$ use Data::Dumper; elog NOTICE, Dumper(%_SHARED); RETURN; $$; pgams=# select dump_shared(); NOTICE: $VAR1 = { 'ingest' => bless( { 'client' => { 'userid' => {48211276}, 'mailer' => {san-i-tize} }, 'fulltext' => ' ', 'ip_address_list' => { '' => 1, '' => 1, '' => 1, '' => 1, '' => 2 }, 'emails' => { '[email protected]' => 1, '[email protected]' => 1, '[email protected]' => 2, '[email protected]' => 4 }, 'urls' => {}, 'related_message_id' => {}, 'size' => 2152 }, 'InterNet::Ingest' ), 'payload' => bless( { 'length' => 2152, 'hash_sha256' => '50640c59 63f3f77a a8bba0b4 530ddb45 cb753a73 b0f2143f 28b8d350 52ec4a8e', 'hash_md5' => '2d6f9e7e0c1970caf42f1782a1ee8f83' }, 'OmniTI::Abyss::Payload' ) };
As you can see, we use the perl "[ Dumper]" module to dump out all of the shared variables you might be looking at, in a fairly oragnized manner. Woohoo! Incidentally, we're hiring Perl developers to work on things like this, as well as some other big projects using Perl and PostgreSQL, among other stuff, so if you think you have what it takes, check out our [ job posting] and send in that resume!