@@ -172,6 +172,17 @@ defmodule Postgrex.ReplicationConnection do
172172
173173 """
174174 @ 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+
175186 @ max_lsn_component_size 8
176187 @ max_uint64 18_446_744_073_709_551_615
177188 @ max_messages 500
@@ -192,6 +203,7 @@ defmodule Postgrex.ReplicationConnection do
192203 { :noreply , state }
193204 | { :noreply , ack , state }
194205 | { :query , query , state }
206+ | { :query , query , query_opts , state }
195207 | { :stream , query , stream_opts , state }
196208 | { :disconnect , reason }
197209
@@ -220,6 +232,7 @@ defmodule Postgrex.ReplicationConnection do
220232 { :noreply , state }
221233 | { :noreply , ack , state }
222234 | { :query , query , state }
235+ | { :query , query , query_opts , state }
223236 | { :stream , query , stream_opts , state }
224237 | { :disconnect , reason }
225238
@@ -230,6 +243,7 @@ defmodule Postgrex.ReplicationConnection do
230243 { :noreply , state }
231244 | { :noreply , ack , state }
232245 | { :query , query , state }
246+ | { :query , query , query_opts , state }
233247 | { :stream , query , stream_opts , state }
234248 | { :disconnect , reason }
235249
@@ -249,17 +263,19 @@ defmodule Postgrex.ReplicationConnection do
249263 { :noreply , state }
250264 | { :noreply , ack , state }
251265 | { :query , query , state }
266+ | { :query , query , query_opts , state }
252267 | { :stream , query , stream_opts , state }
253268 | { :disconnect , reason }
254269
255270 @ doc """
256271 Callback for `:query` outputs.
257272
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.
263279 Due to this constraint, this callback will be passed
264280 either a list with a single successful query result or
265281 an error.
@@ -268,6 +284,7 @@ defmodule Postgrex.ReplicationConnection do
268284 { :noreply , state }
269285 | { :noreply , ack , state }
270286 | { :query , query , state }
287+ | { :query , query , query_opts , state }
271288 | { :stream , query , stream_opts , state }
272289 | { :disconnect , reason }
273290
@@ -586,25 +603,35 @@ defmodule Postgrex.ReplicationConnection do
586603 stream_in_progress ( :stream , mod , mod_state , from , s )
587604
588605 { :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 )
595607
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 )
599610
600611 { :query , _query , mod_state } ->
601612 stream_in_progress ( :query , mod , mod_state , from , s )
602613
614+ { :query , _query , _opts , mod_state } ->
615+ stream_in_progress ( :query , mod , mod_state , from , s )
616+
603617 { :disconnect , reason } ->
604618 reconnect_or_stop ( :disconnect , reason , s . protocol , s )
605619 end
606620 end
607621
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+
608635 defp stream_in_progress ( command , mod , mod_state , from , s ) do
609636 Logger . warning ( "received #{ command } while stream is already in progress" )
610637 from && reply ( from , { __MODULE__ , :stream_in_progress } )
0 commit comments