@@ -172,6 +172,17 @@ defmodule Postgrex.ReplicationConnection do
172
172
173
173
"""
174
174
@ type stream_opts :: [ max_messages: pos_integer ]
175
+
176
+ @ query_timeout :infinity
177
+
178
+ @ typedoc """
179
+ The following options configure querying:
180
+
181
+ * `:timeout` - Query request timeout (default: `#{ @ query_timeout } `);
182
+
183
+ """
184
+ @ type query_opts :: [ timeout: timeout ]
185
+
175
186
@ max_lsn_component_size 8
176
187
@ max_uint64 18_446_744_073_709_551_615
177
188
@ max_messages 500
@@ -192,6 +203,7 @@ defmodule Postgrex.ReplicationConnection do
192
203
{ :noreply , state }
193
204
| { :noreply , ack , state }
194
205
| { :query , query , state }
206
+ | { :query , query , query_opts , state }
195
207
| { :stream , query , stream_opts , state }
196
208
| { :disconnect , reason }
197
209
@@ -220,6 +232,7 @@ defmodule Postgrex.ReplicationConnection do
220
232
{ :noreply , state }
221
233
| { :noreply , ack , state }
222
234
| { :query , query , state }
235
+ | { :query , query , query_opts , state }
223
236
| { :stream , query , stream_opts , state }
224
237
| { :disconnect , reason }
225
238
@@ -230,6 +243,7 @@ defmodule Postgrex.ReplicationConnection do
230
243
{ :noreply , state }
231
244
| { :noreply , ack , state }
232
245
| { :query , query , state }
246
+ | { :query , query , query_opts , state }
233
247
| { :stream , query , stream_opts , state }
234
248
| { :disconnect , reason }
235
249
@@ -249,17 +263,19 @@ defmodule Postgrex.ReplicationConnection do
249
263
{ :noreply , state }
250
264
| { :noreply , ack , state }
251
265
| { :query , query , state }
266
+ | { :query , query , query_opts , state }
252
267
| { :stream , query , stream_opts , state }
253
268
| { :disconnect , reason }
254
269
255
270
@ doc """
256
271
Callback for `:query` outputs.
257
272
258
- If any callback returns `{:query, iodata, state}`,
259
- then this callback will be immediately called with
260
- the result of the query. Please note that even though
261
- replication connections use the simple query protocol,
262
- Postgres currently limits them to single command queries.
273
+ If any callback returns `{:query, iodata, state}` or
274
+ `{:query, iodata, opts, state}`, then this callback will
275
+ be immediately called with the result of the query.
276
+ Please note that even though replication connections use
277
+ the simple query protocol, Postgres currently limits them to
278
+ single command queries.
263
279
Due to this constraint, this callback will be passed
264
280
either a list with a single successful query result or
265
281
an error.
@@ -268,6 +284,7 @@ defmodule Postgrex.ReplicationConnection do
268
284
{ :noreply , state }
269
285
| { :noreply , ack , state }
270
286
| { :query , query , state }
287
+ | { :query , query , query_opts , state }
271
288
| { :stream , query , stream_opts , state }
272
289
| { :disconnect , reason }
273
290
@@ -586,25 +603,35 @@ defmodule Postgrex.ReplicationConnection do
586
603
stream_in_progress ( :stream , mod , mod_state , from , s )
587
604
588
605
{ :query , query , mod_state } when streaming == nil ->
589
- case Protocol . handle_simple ( query , [ ] , s . protocol ) do
590
- { :ok , results , protocol } when is_list ( results ) ->
591
- handle ( mod , :handle_result , [ results , mod_state ] , from , % { s | protocol: protocol } )
592
-
593
- { :error , % Postgrex.Error { } = error , protocol } ->
594
- handle ( mod , :handle_result , [ error , mod_state ] , from , % { s | protocol: protocol } )
606
+ handle_query ( query , mod , from , s , mod_state , timeout: @ query_timeout )
595
607
596
- { :disconnect , reason , protocol } ->
597
- reconnect_or_stop ( :disconnect , reason , protocol , % { s | state: { mod , mod_state } } )
598
- end
608
+ { :query , query , opts , mod_state } when streaming == nil ->
609
+ handle_query ( query , mod , from , s , mod_state , opts )
599
610
600
611
{ :query , _query , mod_state } ->
601
612
stream_in_progress ( :query , mod , mod_state , from , s )
602
613
614
+ { :query , _query , _opts , mod_state } ->
615
+ stream_in_progress ( :query , mod , mod_state , from , s )
616
+
603
617
{ :disconnect , reason } ->
604
618
reconnect_or_stop ( :disconnect , reason , s . protocol , s )
605
619
end
606
620
end
607
621
622
+ defp handle_query ( query , mod , from , s , mod_state , opts ) do
623
+ case Protocol . handle_simple ( query , opts , s . protocol ) do
624
+ { :ok , results , protocol } when is_list ( results ) ->
625
+ handle ( mod , :handle_result , [ results , mod_state ] , from , % { s | protocol: protocol } )
626
+
627
+ { :error , % Postgrex.Error { } = error , protocol } ->
628
+ handle ( mod , :handle_result , [ error , mod_state ] , from , % { s | protocol: protocol } )
629
+
630
+ { :disconnect , reason , protocol } ->
631
+ reconnect_or_stop ( :disconnect , reason , protocol , % { s | state: { mod , mod_state } } )
632
+ end
633
+ end
634
+
608
635
defp stream_in_progress ( command , mod , mod_state , from , s ) do
609
636
Logger . warning ( "received #{ command } while stream is already in progress" )
610
637
from && reply ( from , { __MODULE__ , :stream_in_progress } )
0 commit comments