You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
('The Godfather', 1972, 'Francis Ford Coppola', 'Crime', 2),
36
+
('The Dark Knight', 2008, 'Christopher Nolan', 'Action', 3);
37
+
38
+
-- Data should have been replicated on the standby server
39
+
SELECT*
40
+
FROM movies
41
+
42
+
-- 3. READONLY STANDBY AND WRITING QUERIES
43
+
44
+
-- Although only read queries can be performed on standby, write queries can still run on the standby as result of replication
45
+
-- Open a transaction in standby server
46
+
BEGIN;
47
+
SELECT*FROM movies;
48
+
49
+
-- Add new column on movies table in primary server
50
+
ALTERTABLE movies ADD COLUMN rating int;
51
+
52
+
-- Open a new connection to the standby. The following query won't run because the ALTER TABLE statement queued, which has priority, is blocked by the opened transaction.
53
+
SELECT*FROM movies;
54
+
55
+
-- Unblock the recently opened connection by running END; on standby
56
+
-- If you don't do it a mechanism provided by postgresql, and active by default, will forcibly cancel the standby query that conflict with to-be-applied WAL records. The delay, before the query get cancelled, can be controlled via the max_standby_streaming_delay parameter (30s by default).
57
+
-- This error will be then showed to the client in the stanby:
58
+
59
+
-- SQL Error [40001]: FATAL: terminating connection due to conflict with recovery
60
+
-- Detail: User was holding a relation lock for too long.
61
+
-- Hint: In a moment you should be able to reconnect to the database and repeat your command.
62
+
63
+
-- The problem doesn't happen if the opened transaction is on the primary, since the statement are sent after commit
64
+
65
+
-- Primary server connection 1
66
+
BEGIN;
67
+
SELECT*FROM movies;
68
+
69
+
-- Primary server connection 2
70
+
ALTERTABLE movies ADD COLUMN budget float;
71
+
72
+
-- At this point SELECT * FROM movies; on primary server will wait. However it will run on the standby.
0 commit comments