Skip to content

Commit bc3cdc0

Browse files
committed
Describe connecting with read-only credentions on GCP
1 parent 29a501e commit bc3cdc0

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

docs/docs/hosting/aws.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ keywords: [Bemi, AWS RDS, PostgreSQL, Change Data Capture, real-time data tracki
77
image: 'img/social-card.png'
88
---
99

10-
# AWS RDS
10+
# Amazon Web Services RDS
1111

1212
## WAL level
1313

docs/docs/hosting/gcp.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ keywords: [Bemi, GCP Cloud SQL, PostgreSQL, Change Data Capture, real-time data
77
image: 'img/social-card.png'
88
---
99

10-
# GCP Cloud SQL
10+
# Google Cloud Platform Cloud SQL
1111

1212
## WAL level
1313

@@ -20,3 +20,40 @@ Run the below command and then you can connect with the same credentials on the
2020
-- Grant replication permission to allow using replication slots
2121
ALTER USER [user] WITH REPLICATION;
2222
```
23+
24+
## Read-only credentials
25+
26+
Alternatively, you can manually create read-only PostgreSQL database credentials to connect to the primary instance's WAL.
27+
At a high level, you need to run these commands that are safe to execute without any downtime or performance issues:
28+
29+
* `CREATE ROLE` creates a new read-only user for Bemi to read database changes.
30+
* `CREATE PUBLICATION` creates a "channel" that we'll subscribe to and track changes in real-time.
31+
* `REPLICA IDENTITY FULL` enhances records stored in WAL to record the previous state (“before”) in addition to the tracked by default new state (“after”).
32+
33+
```sql
34+
-- Create read-only user with REPLICATION permission
35+
CREATE ROLE [username] WITH LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE REPLICATION PASSWORD '[password]';
36+
-- Grant SELECT access to tables for selective tracking
37+
GRANT SELECT ON ALL TABLES IN SCHEMA public TO [username];
38+
-- Grant SELECT access to new tables created in the future for selective tracking
39+
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO [username];
40+
41+
-- Create "bemi" PUBLICATION to enable logical replication
42+
CREATE PUBLICATION bemi FOR ALL TABLES;
43+
44+
-- Create a procedure to set REPLICA IDENTITY FULL for tables to track the "before" state on DB row changes
45+
CREATE OR REPLACE PROCEDURE _bemi_set_replica_identity() AS $$ DECLARE current_tablename TEXT;
46+
BEGIN
47+
FOR current_tablename IN SELECT tablename FROM pg_tables LEFT JOIN pg_class ON relname = tablename WHERE schemaname = 'public' AND relreplident != 'f' LOOP
48+
EXECUTE format('ALTER TABLE %I REPLICA IDENTITY FULL', current_tablename);
49+
END LOOP;
50+
END $$ LANGUAGE plpgsql;
51+
-- Call the created procedure
52+
CALL _bemi_set_replica_identity();
53+
-- Create a trigger function that calls the created procedure
54+
CREATE OR REPLACE FUNCTION _bemi_set_replica_identity_func() RETURNS event_trigger AS $$
55+
BEGIN CALL _bemi_set_replica_identity(); END $$ LANGUAGE plpgsql;
56+
-- Create a trigger to set REPLICA IDENTITY FULL for all new created tables
57+
CREATE EVENT TRIGGER _bemi_set_replica_identity_trigger ON ddl_command_end WHEN TAG IN ('CREATE TABLE')
58+
EXECUTE FUNCTION _bemi_set_replica_identity_func();
59+
```

0 commit comments

Comments
 (0)