-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpostgres_replication.ex
More file actions
248 lines (207 loc) · 8.03 KB
/
Copy pathpostgres_replication.ex
File metadata and controls
248 lines (207 loc) · 8.03 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
defmodule PostgresReplication do
@moduledoc """
PostgresReplication is a module that provides a way to stream data from a PostgreSQL database using logical replication.
## Struct parameters
* `connection_opts` - The connection options to connect to the database.
* `table` - The table to replicate. If `:all` is passed, it will replicate all tables.
* `schema` - The schema of the table to replicate. If not provided, it will use the `public` schema. If `:all` is passed, this option is ignored.
* `opts` - The options to pass to this module
* `step` - The current step of the replication process
* `publication_name` - The name of the publication to create. If not provided, it will use the schema and table name.
* `replication_slot_name` - The name of the replication slot to create. If not provided, it will use the schema and table name.
* `output_plugin` - The output plugin to use. Default is `pgoutput`.
* `output_plugin_options` - The options to pass to the output plugin If you use :publication_name as the value of the key, the lib will automatically set the generated or set value of `publication_name`. Default is [proto_version: "1", publication_names: :publication_name].
* `handler_module` - The module that will handle the data received from the replication stream.
* `target_pid` - The PID of the parent process that will receive the data.
"""
use Postgrex.ReplicationConnection
require Logger
alias PostgresReplication.Handler
@default_opts [
auto_reconnect: true,
sync_connect: true
]
@type t :: %__MODULE__{
connection_opts: Keyword.t(),
table: String.t(),
schema: String.t(),
opts: Keyword.t(),
step:
:disconnected
| :check_replication_slot
| :create_publication
| :check_publication
| :create_slot
| :start_replication_slot
| :streaming,
publication_name: String.t(),
replication_slot_name: String.t(),
output_plugin: String.t(),
output_plugin_options: Keyword.t(),
handler_module: Handler.t(),
metadata: map()
}
defstruct connection_opts: nil,
table: nil,
schema: "public",
opts: [],
step: :disconnected,
publication_name: nil,
replication_slot_name: nil,
output_plugin: "pgoutput",
output_plugin_options: [proto_version: "1", publication_names: :publication_name],
handler_module: nil,
metadata: %{}
def start_link(%__MODULE__{opts: opts, connection_opts: connection_opts} = attrs) do
Postgrex.ReplicationConnection.start_link(
__MODULE__,
attrs,
@default_opts |> Keyword.merge(opts) |> Keyword.merge(connection_opts)
)
end
@impl true
def init(attrs) do
Logger.info(
"Initializing connection with the status: #{inspect(attrs |> Map.from_struct() |> Map.drop([:connection_opts]))}"
)
publication_name = publication_name(attrs)
replication_slot_name = replication_slot_name(attrs)
{:ok,
%{
attrs
| step: :disconnected,
publication_name: publication_name,
replication_slot_name: replication_slot_name
}}
end
@impl true
def handle_connect(state) do
replication_slot_name = replication_slot_name(state)
Logger.info("Checking if replication slot #{replication_slot_name} exists")
query =
"SELECT * FROM pg_replication_slots WHERE slot_name = '#{replication_slot_name}'"
{:query, query, %{state | step: :check_replication_slot}}
end
@impl true
def handle_result(
[%Postgrex.Result{num_rows: 1}],
%__MODULE__{step: :check_replication_slot} = state
) do
{:query, "SELECT 1", %{state | step: :check_publication}}
end
def handle_result(
[%Postgrex.Result{num_rows: 0}],
%__MODULE__{step: :check_replication_slot} = state
) do
%__MODULE__{
output_plugin: output_plugin,
replication_slot_name: replication_slot_name,
step: :check_replication_slot
} = state
Logger.info("Create replication slot #{replication_slot_name} using plugin #{output_plugin}")
query =
"CREATE_REPLICATION_SLOT #{replication_slot_name} TEMPORARY LOGICAL #{output_plugin} NOEXPORT_SNAPSHOT"
{:query, query, %{state | step: :check_publication}}
end
def handle_result(
[%Postgrex.Result{}],
%__MODULE__{step: :check_publication} = state
) do
%__MODULE__{table: table, schema: schema, publication_name: publication_name} = state
Logger.info("Check publication #{publication_name} for table #{schema}.#{table} exists")
query = "SELECT * FROM pg_publication WHERE pubname = '#{publication_name}'"
{:query, query, %{state | step: :create_publication}}
end
def handle_result(
[%Postgrex.Result{num_rows: 0}],
%__MODULE__{step: :create_publication, table: :all} = state
) do
%{publication_name: publication_name} = state
Logger.info("Create publication #{publication_name} for all tables")
query =
"CREATE PUBLICATION #{publication_name} FOR ALL TABLES"
{:query, query, %{state | step: :start_replication_slot}}
end
def handle_result(
[%Postgrex.Result{num_rows: 0}],
%__MODULE__{step: :create_publication} = state
) do
%__MODULE__{
table: table,
schema: schema,
publication_name: publication_name
} = state
Logger.info("Create publication #{publication_name} for table #{schema}.#{table}")
query =
"CREATE PUBLICATION #{publication_name} FOR TABLE #{schema}.#{table}"
{:query, query, %{state | step: :start_replication_slot}}
end
def handle_result(
[%Postgrex.Result{num_rows: 1}],
%__MODULE__{step: :create_publication} = state
) do
{:query, "SELECT 1", %{state | step: :start_replication_slot}}
end
@impl true
def handle_result(
[%Postgrex.Result{}],
%__MODULE__{step: :start_replication_slot} = state
) do
%__MODULE__{
replication_slot_name: replication_slot_name,
publication_name: publication_name,
output_plugin: output_plugin,
output_plugin_options: output_plugin_options
} = state
Logger.info(
"Starting stream replication for slot #{replication_slot_name} using the #{output_plugin} plugin with the options: #{inspect(output_plugin_options)}"
)
output_plugin_options =
output_plugin_options
|> Enum.map_join(", ", fn
{k, :publication_name} -> "#{k} '#{publication_name}'"
{k, v} -> "#{k} '#{v}'"
end)
|> String.trim()
|> then(fn
"" -> ""
config -> " (#{config})"
end)
query =
"START_REPLICATION SLOT #{replication_slot_name} LOGICAL 0/0#{output_plugin_options}"
{:stream, query, [], %{state | step: :streaming}}
end
@impl true
def handle_disconnect(state) do
Logger.error(
"Disconnected from the server: #{inspect(state |> Map.from_struct() |> Map.drop([:connection_opts]))}"
)
{:noreply, %{state | step: :disconnected}}
end
@impl true
def handle_data(data, state) do
%__MODULE__{handler_module: handler_module} = state
case handler_module.call(data, state) do
{:reply, messages} -> {:noreply, messages, state}
:noreply -> {:noreply, state}
end
end
def publication_name(%__MODULE__{publication_name: nil, table: :all}) do
"all_table_publication"
end
def publication_name(%__MODULE__{publication_name: nil, table: table, schema: schema}) do
"#{schema}_#{table}_publication"
end
def publication_name(%__MODULE__{publication_name: publication_name}) do
publication_name
end
def replication_slot_name(%__MODULE__{replication_slot_name: nil, table: :all}) do
"all_table_slot"
end
def replication_slot_name(%__MODULE__{replication_slot_name: nil, table: table, schema: schema}) do
"#{schema}_#{table}_replication_slot"
end
def replication_slot_name(%__MODULE__{replication_slot_name: replication_slot_name}) do
replication_slot_name
end
end