SlideShare a Scribd company logo
PostgreSQL:
meet your Queue



    / Presentation / Theo Schlossnagle



                                         1
PostgreSQL is Awesome



    •   Fast.

    •   Extensible.

    •   Tablespaces.

    •   Robust data types.

    •   Partitioning (albeit fake).

    •   Partial and functional indexes.

    •   Extremely supportive community.

    •   Extremely compliant with database standards.




                                                       2
PostgreSQL is not the ā€œworldā€




     •   Inevitably, we must interact with the rest of the world.

     •   ā€œnon-SQLā€ components:

         •   nosql systems

         •   caching systems

         •   search systems (solr/lucene)

         •   management processes




                                                                    3
Appropiare Typicalis



     •   Enforce in the application:

         •   the application code that updates the price or description of a
             product in the products table;

         •   the application submits the updates to the search index system.

     •   The flaw:

         •   psql# UPDATE products
                      SET description =
                          REPLACE(description, ā€˜behaviour’, ā€˜behavior’);

         •   Administrative fixes like that require out-of-band dependency
             handling.




                                                                               4
A Solution


     •   Ideally, the database would notify all of these systems.

     •   The most common case I see: memcached.

         •   app: pull from memcached user::jesus@omniti.com
                  if not found:
                    select * from users where email=ā€˜jesus@omniti.comā€˜
                    put row in memcached at user::jesus@omniti.com

         •   app: update users set mood=ā€˜happyā€˜
                   where email=ā€˜jesus@omniti.comā€˜
                  (a) purge memcached record
                  (b) get full row and replace in memcached

         •   hence: pgmemcache

     •   Problem:

         •   need a Postgres module for each remote component




                                                                         5
Enter Queueing



    •   Queueing?

        •   A generic message bus that allows PostgreSQL to communicate
            with other components in the architecture.

        •   Enter AMQP: ā€œAdvanced Message Queueing Protocolā€

            •   Why not STOMP?

            •   Why not Starling?

            •   AMQP has been around the block, and the specification is quite
                complete.

            •   Almost every ā€œrealā€ message broker implementation supports
                AMQP




                                                                                 6
Setups: Installing



         •   svn export 
              https://labs.omniti.com/pgtreats/trunk/contrib/pg_amqp

         •   cd pg_amqp

         •   make USE_PGXS=1

         •   make install

         •   add to postgresql.conf:
             shared_preload_libraries = 'pg_amqp.so'

         •   (re)start postgres

         •   load the pg_amqp.sql file into your database.




                                                                       7
Setup: configuring your broker




      INSERT INTO amqp.broker (host,port,vhost,username,password)
          VALUES (ā€˜localhost’,5672,NULL,ā€˜guest’,ā€˜guest’)
       RETURNING broker_id




                                                                    8
Setup: declaring an exchange




     •   This can often be done outside of the AMQP client

         •   using an AMQP management process
             (that is just, in fact, an AMQP client)

     •   Often, another component has already created the exchange.

     •   If you really need to do it within PostgreSQL:

         SELECT amqp.declare_exchange(broker_id,
                            exchange_name, exchange_type,
                            passive, durable, auto_delete)




                                                                      9
Usage: sending messages




    •   How do I connect?

        •   It’s implicit... you don’t need to.

    •   How do I disconnect?

        •   Broker connections are cached and live across transactions.

        •   If you want to explicitly disconnect:

            SELECT amqp.disconnect(broker_id);




                                                                          10
Usage: sending messages for real



     •   Sending messages as a part of my transaction:

         SELECT amqp.publish(broker_id, exchange, routing_key, message);

         •   This will publish the ā€œmessageā€ over the specified ā€œexchangeā€ using
             the specified ā€œrouting_key,ā€ but only on transaction commit within
             Postgres.



     •   Sending messages NOW:

         SELECT amqp.autonomous_publish(broker_id, exchange,
                                        routing_key, message);

         •   Publish the same message, but do it immediately.




                                                                                   11
A dark side: unsafe? WTF?




    •   Currently, pg_amqp uses the AMQP 0.8 specification.

    •   The AMQP 1.0 specification introduces formal 2PC.

    •   It is possible in the current implementation to have AMQP transaction
        fail when we commit it (in postgres’s commit txn hook) in a fashion
        that we cannot act on. Ouch.

    •   This only happens when the AMQP broker crashes between the last
        in-transaction publish call and the COMMIT on the database side.

    •   Once RabbitMQ supports AMQP 1.0, I’ll update the driver to use 2PC.




                                                                                12
Thank you for listening.
https://labs.omniti.com/pgtreats/trunk/contrib/pg_amqp/




                 / Presentation



                                                          13

More Related Content

What's hot (20)

PDF
Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)
James Titcumb
Ā 
PDF
Dissecting the rabbit: RabbitMQ Internal Architecture
Alvaro Videla
Ā 
PPTX
Spring RabbitMQ
Martin Toshev
Ā 
PDF
Scaling applications with RabbitMQ at SunshinePHP
Alvaro Videla
Ā 
PDF
Troubleshooting RabbitMQ and services that use it
Michael Klishin
Ā 
PDF
Messaging with RabbitMQ and AMQP
Eberhard Wolff
Ā 
PDF
Apache James/Hupa & GWT
Manuel Carrasco MoƱino
Ā 
PDF
Messaging with amqp and rabbitmq
Selasie Hanson
Ā 
PDF
Apache james more than emails in the cloud
Ioan Eugen Stan
Ā 
PDF
Rabbitmq, amqp Intro - Messaging Patterns
Javier Arias Losada
Ā 
PDF
A Closer Look at RabbitMQ
Kyumars Sheykh Esmaili
Ā 
PDF
Lessons from managing a Pulsar cluster (Nutanix)
StreamNative
Ā 
PDF
Scaling PostgreSQL with Skytools
Gavin Roy
Ā 
PPTX
What's New in Apache Pulsar 2.9- Pulsar Summit Asia 2021
StreamNative
Ā 
PPTX
Modern Distributed Messaging and RPC
Max Alexejev
Ā 
PDF
Apache Kafka: A high-throughput distributed messaging system @ JCConf 2014
Chen-en Lu
Ā 
PDF
[@NaukriEngineering] Messaging Queues
Naukri.com
Ā 
PDF
Configuration Management - Finding the tool to fit your needs
SaltStack
Ā 
PPTX
RabbitMQ vs Apache Kafka Part II Webinar
Erlang Solutions
Ā 
PDF
Using Apache Pulsar as a Modern, Scalable, High Performing JMS Platform - Pus...
StreamNative
Ā 
Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)
James Titcumb
Ā 
Dissecting the rabbit: RabbitMQ Internal Architecture
Alvaro Videla
Ā 
Spring RabbitMQ
Martin Toshev
Ā 
Scaling applications with RabbitMQ at SunshinePHP
Alvaro Videla
Ā 
Troubleshooting RabbitMQ and services that use it
Michael Klishin
Ā 
Messaging with RabbitMQ and AMQP
Eberhard Wolff
Ā 
Apache James/Hupa & GWT
Manuel Carrasco MoƱino
Ā 
Messaging with amqp and rabbitmq
Selasie Hanson
Ā 
Apache james more than emails in the cloud
Ioan Eugen Stan
Ā 
Rabbitmq, amqp Intro - Messaging Patterns
Javier Arias Losada
Ā 
A Closer Look at RabbitMQ
Kyumars Sheykh Esmaili
Ā 
Lessons from managing a Pulsar cluster (Nutanix)
StreamNative
Ā 
Scaling PostgreSQL with Skytools
Gavin Roy
Ā 
What's New in Apache Pulsar 2.9- Pulsar Summit Asia 2021
StreamNative
Ā 
Modern Distributed Messaging and RPC
Max Alexejev
Ā 
Apache Kafka: A high-throughput distributed messaging system @ JCConf 2014
Chen-en Lu
Ā 
[@NaukriEngineering] Messaging Queues
Naukri.com
Ā 
Configuration Management - Finding the tool to fit your needs
SaltStack
Ā 
RabbitMQ vs Apache Kafka Part II Webinar
Erlang Solutions
Ā 
Using Apache Pulsar as a Modern, Scalable, High Performing JMS Platform - Pus...
StreamNative
Ā 

Viewers also liked (17)

PDF
Skytools: PgQ Queues and applications
elliando dias
Ā 
PDF
Rabbitmq Boot System
Alvaro Videla
Ā 
PDF
Использование очереГей асинхронных сообщений с PostgreSQL (Š˜Š»ŃŒŃ ŠšŠ¾ŃŠ¼Š¾Š“ŠµŠ¼ŃŒŃŠ½ŃŠŗŠøŠ¹)
Ontico
Ā 
PDF
Pg amqp
Command Prompt., Inc
Ā 
PDF
Š˜Š½Š“ŠµŠŗŃŃ‹ в MSSQL: принципы работы Šø способы оптимизации
Alexander Byndyu
Ā 
PDF
BayLISA meetup: 8/16/12
bcantrill
Ā 
PDF
Fi fo euc 2014
Licenser
Ā 
PDF
The Kitchen Cloud How To: Automating Joyent SmartMachines with Chef
Chef Software, Inc.
Ā 
PDF
Chef on SmartOS
Eric Saxby
Ā 
PDF
SmartOS ZFS Architecture
Bill Pijewski
Ā 
PDF
Taming the rabbit
Alvaro Videla
Ā 
PDF
OpenStack on SmartOS
Daniele Stroppa
Ā 
PDF
PostgreSQL в Š²Ń‹ŃŠ¾ŠŗŠ¾Š½Š°Š³Ń€ŃƒŠ¶ŠµŠ½Š½Ń‹Ń… проектах
Alexey Vasiliev
Ā 
PDF
Experiences porting KVM to SmartOS
bcantrill
Ā 
PDF
Spilo, Š¾Ń‚ŠŗŠ°Š·Š¾ŃƒŃŃ‚Š¾Š¹Ń‡ŠøŠ²Ń‹Š¹ PostgreSQL кластер / Oleksii Kliukin (Zalando SE)
Ontico
Ā 
PDF
SmartOS Primer
Daniele Stroppa
Ā 
PPTX
Steve Jobs Inspirational Quotes
InsideView
Ā 
Skytools: PgQ Queues and applications
elliando dias
Ā 
Rabbitmq Boot System
Alvaro Videla
Ā 
Использование очереГей асинхронных сообщений с PostgreSQL (Š˜Š»ŃŒŃ ŠšŠ¾ŃŠ¼Š¾Š“ŠµŠ¼ŃŒŃŠ½ŃŠŗŠøŠ¹)
Ontico
Ā 
Š˜Š½Š“ŠµŠŗŃŃ‹ в MSSQL: принципы работы Šø способы оптимизации
Alexander Byndyu
Ā 
BayLISA meetup: 8/16/12
bcantrill
Ā 
Fi fo euc 2014
Licenser
Ā 
The Kitchen Cloud How To: Automating Joyent SmartMachines with Chef
Chef Software, Inc.
Ā 
Chef on SmartOS
Eric Saxby
Ā 
SmartOS ZFS Architecture
Bill Pijewski
Ā 
Taming the rabbit
Alvaro Videla
Ā 
OpenStack on SmartOS
Daniele Stroppa
Ā 
PostgreSQL в Š²Ń‹ŃŠ¾ŠŗŠ¾Š½Š°Š³Ń€ŃƒŠ¶ŠµŠ½Š½Ń‹Ń… проектах
Alexey Vasiliev
Ā 
Experiences porting KVM to SmartOS
bcantrill
Ā 
Spilo, Š¾Ń‚ŠŗŠ°Š·Š¾ŃƒŃŃ‚Š¾Š¹Ń‡ŠøŠ²Ń‹Š¹ PostgreSQL кластер / Oleksii Kliukin (Zalando SE)
Ontico
Ā 
SmartOS Primer
Daniele Stroppa
Ā 
Steve Jobs Inspirational Quotes
InsideView
Ā 
Ad

Similar to PostgreSQL: meet your queue (20)

PDF
On Rabbits and Elephants
Gavin Roy
Ā 
PDF
Life in a Queue - Using Message Queue with django
Tareque Hossain
Ā 
PDF
Small Overview of Skype Database Tools
elliando dias
Ā 
PDF
Lindsay distributed geventzmq
Robin Xiao
Ā 
ODP
The Art of Message Queues - TEKX
Mike Willbanks
Ā 
PDF
Building scalable flexible messaging systems using qpid
Jack Gibson
Ā 
PDF
Database Tools by Skype
elliando dias
Ā 
KEY
Integration and Batch Processing on Cloud Foundry
Joshua Long
Ā 
PDF
The Accidental DBA
PostgreSQL Experts, Inc.
Ā 
PDF
Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...
Jimmy DeadcOde
Ā 
PDF
PHP projects beyond the LAMP stack
Codemotion
Ā 
PDF
Evented applications with RabbitMQ and CakePHP
markstory
Ā 
PDF
Messaging With ActiveMQ
Bruce Snyder
Ā 
KEY
Real time system_performance_mon
Tomas Doran
Ā 
PDF
Cosug 2012-lzy
OpenCity Community
Ā 
PDF
Hacking on OpenStack\'s Nova source code
Zhongyue Luo
Ā 
KEY
Cooking a rabbit pie
Tomas Doran
Ā 
PDF
Enterprise Messaging With ActiveMQ and Spring JMS
Bruce Snyder
Ā 
PDF
IBM IMPACT 2014 AMC-1866 Introduction to IBM Messaging Capabilities
Peter Broadhurst
Ā 
PDF
RabbitMQ with python and ruby RuPy 2009
Paolo Negri
Ā 
On Rabbits and Elephants
Gavin Roy
Ā 
Life in a Queue - Using Message Queue with django
Tareque Hossain
Ā 
Small Overview of Skype Database Tools
elliando dias
Ā 
Lindsay distributed geventzmq
Robin Xiao
Ā 
The Art of Message Queues - TEKX
Mike Willbanks
Ā 
Building scalable flexible messaging systems using qpid
Jack Gibson
Ā 
Database Tools by Skype
elliando dias
Ā 
Integration and Batch Processing on Cloud Foundry
Joshua Long
Ā 
The Accidental DBA
PostgreSQL Experts, Inc.
Ā 
Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...
Jimmy DeadcOde
Ā 
PHP projects beyond the LAMP stack
Codemotion
Ā 
Evented applications with RabbitMQ and CakePHP
markstory
Ā 
Messaging With ActiveMQ
Bruce Snyder
Ā 
Real time system_performance_mon
Tomas Doran
Ā 
Cosug 2012-lzy
OpenCity Community
Ā 
Hacking on OpenStack\'s Nova source code
Zhongyue Luo
Ā 
Cooking a rabbit pie
Tomas Doran
Ā 
Enterprise Messaging With ActiveMQ and Spring JMS
Bruce Snyder
Ā 
IBM IMPACT 2014 AMC-1866 Introduction to IBM Messaging Capabilities
Peter Broadhurst
Ā 
RabbitMQ with python and ruby RuPy 2009
Paolo Negri
Ā 
Ad

More from Theo Schlossnagle (20)

PPTX
Adding Simplicity to Complexity
Theo Schlossnagle
Ā 
PPTX
Put Some SRE in Your Shipped Software
Theo Schlossnagle
Ā 
PPTX
Monitoring 101
Theo Schlossnagle
Ā 
PPTX
Distributed Systems - Like It Or Not
Theo Schlossnagle
Ā 
PDF
Applying SRE techniques to micro service design
Theo Schlossnagle
Ā 
PPTX
Craftsmanship
Theo Schlossnagle
Ā 
PDF
SRECon Coherent Performance
Theo Schlossnagle
Ā 
PDF
Commandments of scale
Theo Schlossnagle
Ā 
PDF
Adaptive availability
Theo Schlossnagle
Ā 
PDF
Project reality
Theo Schlossnagle
Ā 
PDF
Monitoring the #DevOps way
Theo Schlossnagle
Ā 
PDF
Operational Software Design
Theo Schlossnagle
Ā 
PDF
A Coherent Discussion About Performance
Theo Schlossnagle
Ā 
PDF
The math behind big systems analysis.
Theo Schlossnagle
Ā 
PDF
Understanding Slowness
Theo Schlossnagle
Ā 
PDF
OmniOS Motivation and Design ~ LISA 2012
Theo Schlossnagle
Ā 
PDF
Monitoring and observability
Theo Schlossnagle
Ā 
PDF
Omnios and unix
Theo Schlossnagle
Ā 
PDF
Monitoring and observability
Theo Schlossnagle
Ā 
PDF
Xtreme Deployment
Theo Schlossnagle
Ā 
Adding Simplicity to Complexity
Theo Schlossnagle
Ā 
Put Some SRE in Your Shipped Software
Theo Schlossnagle
Ā 
Monitoring 101
Theo Schlossnagle
Ā 
Distributed Systems - Like It Or Not
Theo Schlossnagle
Ā 
Applying SRE techniques to micro service design
Theo Schlossnagle
Ā 
Craftsmanship
Theo Schlossnagle
Ā 
SRECon Coherent Performance
Theo Schlossnagle
Ā 
Commandments of scale
Theo Schlossnagle
Ā 
Adaptive availability
Theo Schlossnagle
Ā 
Project reality
Theo Schlossnagle
Ā 
Monitoring the #DevOps way
Theo Schlossnagle
Ā 
Operational Software Design
Theo Schlossnagle
Ā 
A Coherent Discussion About Performance
Theo Schlossnagle
Ā 
The math behind big systems analysis.
Theo Schlossnagle
Ā 
Understanding Slowness
Theo Schlossnagle
Ā 
OmniOS Motivation and Design ~ LISA 2012
Theo Schlossnagle
Ā 
Monitoring and observability
Theo Schlossnagle
Ā 
Omnios and unix
Theo Schlossnagle
Ā 
Monitoring and observability
Theo Schlossnagle
Ā 
Xtreme Deployment
Theo Schlossnagle
Ā 

Recently uploaded (20)

PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
Ā 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
Ā 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
Ā 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
Ā 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
Ā 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
Ā 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
Ā 
PPTX
CapCut Pro PC Crack Latest Version Free Free
josanj305
Ā 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
Ā 
PDF
Deploy Faster, Run Smarter: Learn Containers with QNAP
QNAP Marketing
Ā 
PDF
Kit-Works Team Study_20250627_ķ•œė‹¬ė§Œģ—ė§Œė“ ģ‚¬ė‚“ģ„œė¹„ģŠ¤ķ‚¤ė§(ģ–‘ė‹¤ģœ—).pdf
Wonjun Hwang
Ā 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
Ā 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
Ā 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
Ā 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
Ā 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
Ā 
PPTX
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
Ā 
PDF
šŸš€ Let’s Build Our First Slack Workflow! šŸ”§.pdf
SanjeetMishra29
Ā 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
Ā 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
Ā 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
Ā 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
Ā 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
Ā 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
Ā 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
Ā 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
Ā 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
Ā 
CapCut Pro PC Crack Latest Version Free Free
josanj305
Ā 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
Ā 
Deploy Faster, Run Smarter: Learn Containers with QNAP
QNAP Marketing
Ā 
Kit-Works Team Study_20250627_ķ•œė‹¬ė§Œģ—ė§Œė“ ģ‚¬ė‚“ģ„œė¹„ģŠ¤ķ‚¤ė§(ģ–‘ė‹¤ģœ—).pdf
Wonjun Hwang
Ā 
Wondershare Filmora Crack Free Download 2025
josanj305
Ā 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
Ā 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
Ā 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
Ā 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
Ā 
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
Ā 
šŸš€ Let’s Build Our First Slack Workflow! šŸ”§.pdf
SanjeetMishra29
Ā 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
Ā 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
Ā 

PostgreSQL: meet your queue

  • 1. PostgreSQL: meet your Queue / Presentation / Theo Schlossnagle 1
  • 2. PostgreSQL is Awesome • Fast. • Extensible. • Tablespaces. • Robust data types. • Partitioning (albeit fake). • Partial and functional indexes. • Extremely supportive community. • Extremely compliant with database standards. 2
  • 3. PostgreSQL is not the ā€œworldā€ • Inevitably, we must interact with the rest of the world. • ā€œnon-SQLā€ components: • nosql systems • caching systems • search systems (solr/lucene) • management processes 3
  • 4. Appropiare Typicalis • Enforce in the application: • the application code that updates the price or description of a product in the products table; • the application submits the updates to the search index system. • The flaw: • psql# UPDATE products SET description = REPLACE(description, ā€˜behaviour’, ā€˜behavior’); • Administrative fixes like that require out-of-band dependency handling. 4
  • 5. A Solution • Ideally, the database would notify all of these systems. • The most common case I see: memcached. • app: pull from memcached user::[email protected] if not found: select * from users where email=ā€˜[email protected]ā€˜ put row in memcached at user::[email protected] • app: update users set mood=ā€˜happyā€˜ where email=ā€˜[email protected]ā€˜ (a) purge memcached record (b) get full row and replace in memcached • hence: pgmemcache • Problem: • need a Postgres module for each remote component 5
  • 6. Enter Queueing • Queueing? • A generic message bus that allows PostgreSQL to communicate with other components in the architecture. • Enter AMQP: ā€œAdvanced Message Queueing Protocolā€ • Why not STOMP? • Why not Starling? • AMQP has been around the block, and the specification is quite complete. • Almost every ā€œrealā€ message broker implementation supports AMQP 6
  • 7. Setups: Installing • svn export https://labs.omniti.com/pgtreats/trunk/contrib/pg_amqp • cd pg_amqp • make USE_PGXS=1 • make install • add to postgresql.conf: shared_preload_libraries = 'pg_amqp.so' • (re)start postgres • load the pg_amqp.sql file into your database. 7
  • 8. Setup: configuring your broker INSERT INTO amqp.broker (host,port,vhost,username,password) VALUES (ā€˜localhost’,5672,NULL,ā€˜guest’,ā€˜guest’) RETURNING broker_id 8
  • 9. Setup: declaring an exchange • This can often be done outside of the AMQP client • using an AMQP management process (that is just, in fact, an AMQP client) • Often, another component has already created the exchange. • If you really need to do it within PostgreSQL: SELECT amqp.declare_exchange(broker_id, exchange_name, exchange_type, passive, durable, auto_delete) 9
  • 10. Usage: sending messages • How do I connect? • It’s implicit... you don’t need to. • How do I disconnect? • Broker connections are cached and live across transactions. • If you want to explicitly disconnect: SELECT amqp.disconnect(broker_id); 10
  • 11. Usage: sending messages for real • Sending messages as a part of my transaction: SELECT amqp.publish(broker_id, exchange, routing_key, message); • This will publish the ā€œmessageā€ over the specified ā€œexchangeā€ using the specified ā€œrouting_key,ā€ but only on transaction commit within Postgres. • Sending messages NOW: SELECT amqp.autonomous_publish(broker_id, exchange, routing_key, message); • Publish the same message, but do it immediately. 11
  • 12. A dark side: unsafe? WTF? • Currently, pg_amqp uses the AMQP 0.8 specification. • The AMQP 1.0 specification introduces formal 2PC. • It is possible in the current implementation to have AMQP transaction fail when we commit it (in postgres’s commit txn hook) in a fashion that we cannot act on. Ouch. • This only happens when the AMQP broker crashes between the last in-transaction publish call and the COMMIT on the database side. • Once RabbitMQ supports AMQP 1.0, I’ll update the driver to use 2PC. 12
  • 13. Thank you for listening. https://labs.omniti.com/pgtreats/trunk/contrib/pg_amqp/ / Presentation 13