Waiting for 8.5 – PL/Perl DO

Just recently I wrote about DO command in PostgreSQL 8.5, and now (since 29th of November) we have DO with PL/Perl support.

Written by Joshua Tolley and committed by Tom Lane, the patch:

Log Message:
-----------
Add support for anonymous code blocks (DO blocks) to PL/Perl.
 
Joshua Tolley, reviewed by Brendan Jurd and Tim Bunce

DO command basically runs code in any PL/ language, but it requires some kind of support from the language itself. And now this support has been added to PL/Perl.

So, now we can do stuff like this:

DO $$
FOR (1..10) {
    my $table_name = sprintf "p_%02u", $_;
    spi_exec_query("CREATE TABLE $table_name (i int4)");
    warn "table $table_name created.";
}
$$ LANGUAGE plperl;

This isn't of course the only thing possible – it's just a hint, and what you will use it for is up to you 🙂