~verterok/+junk/postgresql-amqp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
BEGIN;
create schema amqp;

create function amqp.exchange_declare(integer, varchar, varchar, boolean, boolean, boolean)
returns boolean as 'pg_amqp.so', 'pg_amqp_exchange_declare'
language C immutable;

comment on function amqp.exchange_declare(integer, varchar, varchar, boolean, boolean, boolean) is
'Declares a exchange (broker_id, exchange_name, exchange_type, passive, durable, auto_delete)
auto_delete should be set to false as unexpected errors can cause disconnect/reconnect which
would trigger the auto deletion of the exchange.';

create function amqp.publish(integer, varchar, varchar, varchar)
returns boolean as 'pg_amqp.so', 'pg_amqp_publish'
language C immutable;

comment on function amqp.publish(integer, varchar, varchar, varchar) is
'Publishes a message (broker_id, exchange, routing_key, message).  The message will only
be published if the containing PostgreSQL transaction successfully commits.  Under certain
circumstances, the AMQP commit might fail.  In this case, a WARNING is emitted.

Publish returns a boolean indicating if the publish command was successful.  Note that as
AMQP publish is asynchronous, you may find out later it was unsuccessful.';

create function amqp.autonomous_publish(integer, varchar, varchar, varchar)
returns boolean as 'pg_amqp.so', 'pg_amqp_autonomous_publish'
language C immutable;

comment on function amqp.autonomous_publish(integer, varchar, varchar, varchar) is
'Works as amqp.publish does, but the message is published immediately irrespective of the
current transaction state.  PostgreSQL commit and rollback at a later point will have no
effect on this message being sent to AMQP.';

create function amqp.disconnect(integer)
returns void as 'pg_amqp.so', 'pg_amqp_disconnect'
language C immutable strict;

comment on function amqp.disconnect(integer) is
'Explicitly disconnect the specified (broker_id) if it is current connected. Broker
connections, once established, live until the PostgreSQL backend terminated.  This
allows for more precise control over that.
select amqp.disconnect(broker_id) from amqp.broker
will disconnect any brokers that may be connected.';

create table amqp.broker (
  broker_id serial not null primary key,
  host text,
  port integer,
  vhost text,
  username text,
  password text
);

COMMIT;