DBD::Pg で非同期クエリ投げっぱなし

# チラシの裏です

重いクエリをエミュレートするために Pl/PgSQL で sleep() を用意。(busy loop するので CPU 使います)
http://www.issociate.de/board/post/130019/sleep_function.html

create or replace function sleep (integer) returns time as '
  declare
    seconds alias for $1;
    later time;
    thetime time;
  begin
    thetime := timeofday()::timestamp;
    later := thetime + (seconds::text || '' seconds'')::interval;
    loop
      if thetime >= later then
        exit;
      else
        thetime := timeofday()::timestamp;
      end if;
    end loop;
    return later;
  end;
' language plpgsql;

DBD::Pg の async interface を使って、10秒掛かる INSERT のクエリを投げっぱなしてすぐに終わる。

use DBI;
use DBD::Pg ":async";

my $dbh = DBI->connect(
    "dbi:Pg:dbname=fujiwara", "fujiwara", "", { AutoCommit => 1 }
);
my $sth = $dbh->prepare(
    "INSERT INTO mq(message) VALUES(sleep(10))",
    { pg_async => PG_ASYNC },
);
$sth->execute();
$dbh->disconnect;

これで、Perl のほうの実行が終わってもちゃんと (キャンセルされないで) INSERT ができてるみたい。