Kafka topics, with no broker to run
pg_topics turns a Postgres table into a Kafka topic. Stock Kafka clients produce and consume on a port the extension opens, and SQL can do the same work inside your own transaction.
Not in the supatype/postgres image yet. Build it from source in the supatype/postgres repository.
-- A topic is a table whose name ends in _q. A band is a Kafka partition.
SELECT topic.create_topic('shop.orders_q', band_count => 4);
SELECT topic.grant_consume('shop.orders_q', 'billing_service');
-- Publish in the same transaction as your own writes. No outbox table.
BEGIN;
UPDATE shop.stock SET count = count - 1 WHERE sku = 'A-100';
SELECT topic.publish('shop.orders_q', '{"order_id": 2, "sku": "A-100"}', key => 'customer-42');
COMMIT;
-- Read it back in SQL.
SELECT * FROM topic.fetch('shop.orders_q',
band => topic.band_for('customer-42', 4), from_offset => 0);
-- Or with a stock Kafka client: SASL_SSL, PLAIN, a Postgres role and its password.
-- kafka-console-consumer --bootstrap-server kafka.example.internal:9092 \
-- --consumer.config client.properties --topic shop.orders_q --from-beginning
-- Alert when ok = false.
SELECT * FROM topic.health(); Why pg_topics
Most teams that want an event log already run Postgres. The usual answer is a second system: a Kafka cluster with its own backups, patching, users and monitoring. Then every write that must also publish an event needs an outbox table to keep the two in step.
pg_topics puts the topic in the database. Each topic is a table. A Kafka client logs in with a Postgres role and its password, over TLS, and produces and consumes as it would against a broker. A SQL user calls topic.publish inside the same transaction as the rest of their writes, so the event commits with the data or not at all.
History stays queryable with SQL, Postgres grants are the only permission system, and an optional worker keeps a normal table current from a topic so you can read the latest state with SELECT. It suits a team that needs up to tens of thousands of events a second, not hundreds of thousands.
pg_topics or a Kafka cluster
| a Kafka cluster | pg_topics | |
|---|---|---|
| What you run | A broker cluster beside your database, with its own backups, patching and monitoring. | Your PostgreSQL 17 server. There is no broker to run, back up or patch. |
| Publish with a database write | Needs an outbox table, or a second write that can fail on its own. | topic.publish joins your transaction. A ROLLBACK removes the record. |
| Access control | Its own users and ACLs, kept apart from the database's. | Postgres roles and grants. A Kafka client logs in as a role. |
| Reading history | Consume the log, or copy it into a database first. | Every topic is a table that SQL can query, on the primary or a standby. |
| Current state per key | Log compaction. | No compaction. A sync worker keeps a normal table current instead. |
| Throughput | Scales out across brokers. | One primary does all the writes. About 10,000 records a second with low latency, and up to about 44,000 at top speed, on the test machine. |
| Transactions and exactly once | Supported. | Not supported. Idempotent producers are supported. |
| Monitoring | JMX metrics. | SQL views and topic.health(). There are no JMX metrics. |
What it does not do
- PostgreSQL 17 only. Other versions are not built or tested.
- A record value must be valid JSON, and a key must be UTF-8 text of 40 characters or fewer.
- A topic name is schema.table_q, and clients must use the full name. A producer aimed at orders gets TOPIC_AUTHORIZATION_FAILED.
- No Kafka transactions or exactly once. A transactional.id gets TRANSACTIONAL_ID_AUTHORIZATION_FAILED.
- No log compaction, and no size based retention. Retention is by time only and drops whole partitions.
- The band count is fixed when you make the topic. There is no CreatePartitions.
- Only SASL PLAIN over TLS. No SCRAM, OAUTHBEARER, GSSAPI, mutual TLS or plain text.
- Classic consumer groups only. The KIP-848 protocol, static membership and share groups are not offered.
- Kafka Connect in distributed mode, Kafka Streams and Schema Registry make compacted topics, so they are expected to fail. They are not tested.
- One broker and one primary. Publish throughput does not grow with more machines. Standbys give failover and SQL history reads.
- Fetch returns the value as jsonb prints it, so a signature over the raw bytes does not match.
- librdkafka clients must set partitioner=murmur2_random, or keyed records land on different bands from a SQL publish.
- A restart of PostgreSQL makes every consumer group rebalance once.
Two ways to run it
Not in the supatype/postgres image yet. Build it from source in the supatype/postgres repository. Its README covers the install.
# Add pg_topics to your existing shared_preload_libraries list. shared_preload_libraries = 'pg_topics' pg_topics.databases = 'app' pg_topics.failover_is_fenced = on # The Kafka port speaks TLS only. pg_topics.tls_cert_file = '/etc/pg_topics/server.crt' pg_topics.tls_key_file = '/etc/pg_topics/server.key' pg_topics.advertised_host = 'kafka.example.internal' # Restart PostgreSQL, then in the app database run: # CREATE EXTENSION pg_topics;
Get the image already running, with migrations, access rules, an API, a typed client and Studio generated from your TypeScript types. Cloud starts with 2 projects, 500 MB database and 1 GB storage, free and with no card.