concept pg_amqp in category rabbitmq

This is an excerpt from Manning's book RabbitMQ in Depth.
Given this solid history of event-based replication for PostgreSQL, it seems only natural that someone would add flexible messaging capabilities to its ecosystem. In 2009, Theo Schlossnagle of OmniTI released pg_amqp, a PostgreSQL extension that exposes AMQP publishing via PostgreSQL functions. Although pg_amqp only exposes a subset of the AMQP 0-8 specification, it performs solidly when publishing messages from PostgreSQL’s trigger functions. The functionality exposed by pg_amqp is accessible just like any other PostgreSQL function and can be invoked in SQL statements and stored procedures alike. Pg_amqp exposes a simple AMQP with two methods for interacting with RabbitMQ: amqp.publish and amqp.disconnect. The amqp.publish method creates an AMQP message and delivers it using the Basic.Publish RPC method, just like any other AMQP publisher (figure 10.1). Connections are automatically established and destroyed, but if you want to directly terminate a connection after publishing a message, you can invoke the amqp.disconnect function.
When you use pg_amqp, you’re invoking synchronous communication with RabbitMQ, and care should be taken to ensure that your use doesn’t impact your overall query velocity. As with any tightly coupled integration, benchmarking should be performed and failure scenarios should be tested prior to putting a system into production. For example, if you wrap your use of amqp.publish in a transaction, what happens if pg_amqp can’t connect to the RabbitMQ broker? Will your database transactions complete if there’s a publishing failure?
As you can see, publishing messages via pg_amqp is a fairly trivial exercise once you have it set up and configured. It’s worth noting that you can’t set the AMQP message properties as of version 0.3. Additionally, message publishing is wrapped in an AMQP transaction. Should you invoke the amqp.publish function in a PostgreSQL transaction and then roll back the transaction, the RabbitMQ transaction will be rolled back as well. In most cases, publish will be wrapped within a stored procedure, either along with other actions inside the stored procedure, or as a trigger function that’s executed on the INSERT, UPDATE, or DELETE of rows in a table.