emqx_postgresql.erl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_postgresql).
  17. -include("emqx_postgresql.hrl").
  18. -include_lib("emqx_connector/include/emqx_connector.hrl").
  19. -include_lib("typerefl/include/types.hrl").
  20. -include_lib("emqx/include/logger.hrl").
  21. -include_lib("hocon/include/hoconsc.hrl").
  22. -include_lib("epgsql/include/epgsql.hrl").
  23. -include_lib("snabbkaffe/include/snabbkaffe.hrl").
  24. -export([roots/0, fields/1, namespace/0]).
  25. -behaviour(emqx_resource).
  26. %% callbacks of behaviour emqx_resource
  27. -export([
  28. callback_mode/0,
  29. on_start/2,
  30. on_stop/2,
  31. on_query/3,
  32. on_batch_query/3,
  33. on_get_status/2,
  34. on_add_channel/4,
  35. on_remove_channel/3,
  36. on_get_channels/1,
  37. on_get_channel_status/3,
  38. on_format_query_result/1
  39. ]).
  40. -export([connect/1]).
  41. -export([
  42. query/3,
  43. prepared_query/3,
  44. execute_batch/3
  45. ]).
  46. -export([disable_prepared_statements/0]).
  47. %% for ecpool workers usage
  48. -export([do_get_status/1, prepare_sql_to_conn/2]).
  49. -define(PGSQL_HOST_OPTIONS, #{
  50. default_port => ?PGSQL_DEFAULT_PORT
  51. }).
  52. -type connector_resource_id() :: binary().
  53. -type action_resource_id() :: binary().
  54. -type template() :: {unicode:chardata(), emqx_template_sql:row_template()}.
  55. -type state() ::
  56. #{
  57. pool_name := binary(),
  58. query_templates := #{binary() => template()},
  59. prepares := disabled | #{binary() => epgsql:statement()} | {error, _}
  60. }.
  61. %% FIXME: add `{error, sync_required}' to `epgsql:execute_batch'
  62. %% We want to be able to call sync if any message from the backend leaves the driver in an
  63. %% inconsistent state needing sync.
  64. -dialyzer({nowarn_function, [execute_batch/3]}).
  65. %%=====================================================================
  66. namespace() -> postgres.
  67. roots() ->
  68. [{config, #{type => hoconsc:ref(?MODULE, config)}}].
  69. fields(config) ->
  70. [
  71. {server, server()},
  72. disable_prepared_statements()
  73. ] ++
  74. adjust_fields(emqx_connector_schema_lib:relational_db_fields()) ++
  75. emqx_connector_schema_lib:ssl_fields() ++
  76. emqx_connector_schema_lib:prepare_statement_fields().
  77. server() ->
  78. Meta = #{desc => ?DESC("server")},
  79. emqx_schema:servers_sc(Meta, ?PGSQL_HOST_OPTIONS).
  80. disable_prepared_statements() ->
  81. {disable_prepared_statements,
  82. hoconsc:mk(
  83. boolean(),
  84. #{
  85. default => false,
  86. required => false,
  87. desc => ?DESC("disable_prepared_statements")
  88. }
  89. )}.
  90. adjust_fields(Fields) ->
  91. lists:map(
  92. fun
  93. ({username, Sc}) ->
  94. %% to please dialyzer...
  95. Override = #{type => hocon_schema:field_schema(Sc, type), required => true},
  96. {username, hocon_schema:override(Sc, Override)};
  97. (Field) ->
  98. Field
  99. end,
  100. Fields
  101. ).
  102. %% ===================================================================
  103. callback_mode() -> always_sync.
  104. -spec on_start(binary(), hocon:config()) -> {ok, state()} | {error, _}.
  105. on_start(
  106. InstId,
  107. #{
  108. server := Server,
  109. disable_prepared_statements := DisablePreparedStatements,
  110. database := DB,
  111. username := User,
  112. pool_size := PoolSize,
  113. ssl := SSL
  114. } = Config
  115. ) ->
  116. #{hostname := Host, port := Port} = emqx_schema:parse_server(Server, ?PGSQL_HOST_OPTIONS),
  117. ?SLOG(info, #{
  118. msg => "starting_postgresql_connector",
  119. connector => InstId,
  120. config => emqx_utils:redact(Config)
  121. }),
  122. SslOpts =
  123. case maps:get(enable, SSL) of
  124. true ->
  125. [
  126. %% note: this is converted to `required' in
  127. %% `conn_opts/2', and there's a boolean guard
  128. %% there; if this is set to `required' here,
  129. %% that'll require changing `conn_opts/2''s guard.
  130. {ssl, true},
  131. {ssl_opts, emqx_tls_lib:to_client_opts(SSL)}
  132. ];
  133. false ->
  134. [{ssl, false}]
  135. end,
  136. Options = [
  137. {host, Host},
  138. {port, Port},
  139. {username, User},
  140. {password, maps:get(password, Config, emqx_secret:wrap(""))},
  141. {database, DB},
  142. {auto_reconnect, ?AUTO_RECONNECT_INTERVAL},
  143. {pool_size, PoolSize}
  144. ],
  145. State1 = parse_sql_template(Config, <<"send_message">>),
  146. State2 = State1#{installed_channels => #{}},
  147. case emqx_resource_pool:start(InstId, ?MODULE, Options ++ SslOpts) of
  148. ok ->
  149. Prepares =
  150. case DisablePreparedStatements of
  151. true -> disabled;
  152. false -> #{}
  153. end,
  154. {ok, init_prepare(State2#{pool_name => InstId, prepares => Prepares})};
  155. {error, Reason} ->
  156. ?tp(
  157. pgsql_connector_start_failed,
  158. #{error => Reason}
  159. ),
  160. {error, Reason}
  161. end.
  162. on_stop(InstId, State) ->
  163. ?SLOG(info, #{
  164. msg => "stopping_postgresql_connector",
  165. connector => InstId
  166. }),
  167. close_connections(State),
  168. Res = emqx_resource_pool:stop(InstId),
  169. ?tp(postgres_stopped, #{instance_id => InstId}),
  170. Res.
  171. close_connections(#{pool_name := PoolName} = _State) ->
  172. WorkerPids = [Worker || {_WorkerName, Worker} <- ecpool:workers(PoolName)],
  173. close_connections_with_worker_pids(WorkerPids),
  174. ok.
  175. close_connections_with_worker_pids([WorkerPid | Rest]) ->
  176. %% We ignore errors since any error probably means that the
  177. %% connection is closed already.
  178. try ecpool_worker:client(WorkerPid) of
  179. {ok, Conn} ->
  180. _ = epgsql:close(Conn),
  181. close_connections_with_worker_pids(Rest);
  182. _ ->
  183. close_connections_with_worker_pids(Rest)
  184. catch
  185. _:_ ->
  186. close_connections_with_worker_pids(Rest)
  187. end;
  188. close_connections_with_worker_pids([]) ->
  189. ok.
  190. on_add_channel(
  191. _InstId,
  192. #{
  193. installed_channels := InstalledChannels
  194. } = OldState,
  195. ChannelId,
  196. ChannelConfig
  197. ) ->
  198. %% The following will throw an exception if the bridge producers fails to start
  199. {ok, ChannelState} = create_channel_state(ChannelId, OldState, ChannelConfig),
  200. case ChannelState of
  201. #{prepares := {error, Reason}} ->
  202. {error, {unhealthy_target, Reason}};
  203. _ ->
  204. NewInstalledChannels = maps:put(ChannelId, ChannelState, InstalledChannels),
  205. %% Update state
  206. NewState = OldState#{installed_channels => NewInstalledChannels},
  207. {ok, NewState}
  208. end.
  209. create_channel_state(
  210. ChannelId,
  211. #{
  212. pool_name := PoolName,
  213. prepares := Prepares
  214. } = _ConnectorState,
  215. #{parameters := Parameters} = _ChannelConfig
  216. ) ->
  217. State1 = parse_sql_template(Parameters, ChannelId),
  218. {ok,
  219. init_prepare(State1#{
  220. pool_name => PoolName,
  221. prepares => Prepares,
  222. prepare_statement => #{}
  223. })}.
  224. on_remove_channel(
  225. _InstId,
  226. #{
  227. installed_channels := InstalledChannels
  228. } = OldState,
  229. ChannelId
  230. ) ->
  231. %% Close prepared statements
  232. ok = close_prepared_statement(ChannelId, OldState),
  233. NewInstalledChannels = maps:remove(ChannelId, InstalledChannels),
  234. %% Update state
  235. NewState = OldState#{installed_channels => NewInstalledChannels},
  236. {ok, NewState}.
  237. close_prepared_statement(_ChannelId, #{prepares := disabled}) ->
  238. ok;
  239. close_prepared_statement(ChannelId, #{pool_name := PoolName} = State) ->
  240. WorkerPids = [Worker || {_WorkerName, Worker} <- ecpool:workers(PoolName)],
  241. close_prepared_statement(WorkerPids, ChannelId, State),
  242. ok.
  243. close_prepared_statement([WorkerPid | Rest], ChannelId, State) ->
  244. %% We ignore errors since any error probably means that the
  245. %% prepared statement doesn't exist. If it exists when we try
  246. %% to insert one with the same name, we will try to remove it
  247. %% again anyway.
  248. try ecpool_worker:client(WorkerPid) of
  249. {ok, Conn} ->
  250. Statement = get_templated_statement(ChannelId, State),
  251. _ = epgsql:close(Conn, Statement),
  252. close_prepared_statement(Rest, ChannelId, State);
  253. _ ->
  254. close_prepared_statement(Rest, ChannelId, State)
  255. catch
  256. _:_ ->
  257. close_prepared_statement(Rest, ChannelId, State)
  258. end;
  259. close_prepared_statement([], _ChannelId, _State) ->
  260. ok.
  261. on_get_channel_status(
  262. _ResId,
  263. ChannelId,
  264. #{
  265. pool_name := PoolName,
  266. installed_channels := Channels
  267. } = _State
  268. ) ->
  269. ChannelState = maps:get(ChannelId, Channels),
  270. case
  271. do_check_channel_sql(
  272. PoolName,
  273. ChannelId,
  274. ChannelState
  275. )
  276. of
  277. ok ->
  278. connected;
  279. {error, undefined_table} ->
  280. {error, {unhealthy_target, <<"Table does not exist">>}}
  281. end.
  282. do_check_channel_sql(
  283. PoolName,
  284. ChannelId,
  285. #{query_templates := ChannelQueryTemplates} = _ChannelState
  286. ) ->
  287. {SQL, _RowTemplate} = maps:get(ChannelId, ChannelQueryTemplates),
  288. WorkerPids = [Worker || {_WorkerName, Worker} <- ecpool:workers(PoolName)],
  289. validate_table_existence(WorkerPids, SQL).
  290. on_get_channels(ResId) ->
  291. emqx_bridge_v2:get_channels_for_connector(ResId).
  292. -spec on_query
  293. %% Called from authn and authz modules
  294. (connector_resource_id(), {prepared_query, binary(), [term()]}, state()) ->
  295. {ok, _} | {error, term()};
  296. %% Called from bridges
  297. (connector_resource_id(), {action_resource_id(), map()}, state()) ->
  298. {ok, _} | {error, term()}.
  299. on_query(InstId, {TypeOrKey, NameOrMap}, State) ->
  300. on_query(InstId, {TypeOrKey, NameOrMap, []}, State);
  301. on_query(
  302. InstId,
  303. {TypeOrKey, NameOrMap, Params},
  304. #{pool_name := PoolName} = State
  305. ) ->
  306. ?SLOG(debug, #{
  307. msg => "postgresql_connector_received_sql_query",
  308. connector => InstId,
  309. type => TypeOrKey,
  310. sql => NameOrMap,
  311. state => State
  312. }),
  313. {QueryType, NameOrSQL2, Data} = proc_sql_params(TypeOrKey, NameOrMap, Params, State),
  314. emqx_trace:rendered_action_template(
  315. TypeOrKey,
  316. #{
  317. statement_type => QueryType,
  318. statement_or_name => NameOrSQL2,
  319. data => Data
  320. }
  321. ),
  322. Res = on_sql_query(InstId, PoolName, QueryType, NameOrSQL2, Data),
  323. ?tp(postgres_bridge_connector_on_query_return, #{instance_id => InstId, result => Res}),
  324. handle_result(Res).
  325. on_batch_query(
  326. InstId,
  327. [{Key, _} = Request | _] = BatchReq,
  328. #{pool_name := PoolName} = State
  329. ) ->
  330. BinKey = to_bin(Key),
  331. case get_template(BinKey, State) of
  332. undefined ->
  333. Log = #{
  334. connector => InstId,
  335. first_request => Request,
  336. state => State,
  337. msg => "batch prepare not implemented"
  338. },
  339. ?SLOG(error, Log),
  340. {error, {unrecoverable_error, batch_prepare_not_implemented}};
  341. {_Statement, RowTemplate} ->
  342. StatementTemplate = get_templated_statement(BinKey, State),
  343. Rows = [render_prepare_sql_row(RowTemplate, Data) || {_Key, Data} <- BatchReq],
  344. emqx_trace:rendered_action_template(
  345. Key,
  346. #{
  347. statement_type => execute_batch,
  348. statement_or_name => StatementTemplate,
  349. data => Rows
  350. }
  351. ),
  352. case on_sql_query(InstId, PoolName, execute_batch, StatementTemplate, Rows) of
  353. {error, _Error} = Result ->
  354. handle_result(Result);
  355. {_Column, Results} ->
  356. handle_batch_result(Results, 0)
  357. end
  358. end;
  359. on_batch_query(InstId, BatchReq, State) ->
  360. ?SLOG(error, #{
  361. connector => InstId,
  362. request => BatchReq,
  363. state => State,
  364. msg => "invalid request"
  365. }),
  366. {error, {unrecoverable_error, invalid_request}}.
  367. proc_sql_params(ActionResId, #{} = Map, [], State) when is_binary(ActionResId) ->
  368. %% When this connector is called from actions/bridges.
  369. DisablePreparedStatements = prepared_statements_disabled(State),
  370. {ExprTemplate, RowTemplate} = get_template(ActionResId, State),
  371. Rendered = render_prepare_sql_row(RowTemplate, Map),
  372. case DisablePreparedStatements of
  373. true ->
  374. {query, ExprTemplate, Rendered};
  375. false ->
  376. {prepared_query, ActionResId, Rendered}
  377. end;
  378. proc_sql_params(prepared_query, ConnResId, Params, State) ->
  379. %% When this connector is called from authn/authz modules
  380. DisablePreparedStatements = prepared_statements_disabled(State),
  381. case DisablePreparedStatements of
  382. true ->
  383. #{query_templates := #{ConnResId := {ExprTemplate, _VarsTemplate}}} = State,
  384. {query, ExprTemplate, Params};
  385. false ->
  386. %% Connector resource id itself is the prepared statement name
  387. {prepared_query, ConnResId, Params}
  388. end;
  389. proc_sql_params(QueryType, SQL, Params, _State) when
  390. is_atom(QueryType) andalso
  391. (is_binary(SQL) orelse is_list(SQL)) andalso
  392. is_list(Params)
  393. ->
  394. %% When called to do ad-hoc commands/queries.
  395. {QueryType, SQL, Params}.
  396. prepared_statements_disabled(State) ->
  397. maps:get(prepares, State, #{}) =:= disabled.
  398. get_template(Key, #{installed_channels := Channels} = _State) when is_map_key(Key, Channels) ->
  399. BinKey = to_bin(Key),
  400. ChannelState = maps:get(BinKey, Channels),
  401. ChannelQueryTemplates = maps:get(query_templates, ChannelState),
  402. maps:get(BinKey, ChannelQueryTemplates);
  403. get_template(Key, #{query_templates := Templates}) ->
  404. BinKey = to_bin(Key),
  405. maps:get(BinKey, Templates, undefined).
  406. get_templated_statement(Key, #{installed_channels := Channels} = _State) when
  407. is_map_key(Key, Channels)
  408. ->
  409. BinKey = to_bin(Key),
  410. ChannelState = maps:get(BinKey, Channels),
  411. case ChannelState of
  412. #{prepares := disabled, query_templates := #{BinKey := {ExprTemplate, _}}} ->
  413. ExprTemplate;
  414. #{prepares := #{BinKey := ExprTemplate}} ->
  415. ExprTemplate
  416. end;
  417. get_templated_statement(Key, #{prepares := PrepStatements}) ->
  418. BinKey = to_bin(Key),
  419. maps:get(BinKey, PrepStatements).
  420. on_sql_query(InstId, PoolName, Type, NameOrSQL, Data) ->
  421. try ecpool:pick_and_do(PoolName, {?MODULE, Type, [NameOrSQL, Data]}, no_handover) of
  422. {error, Reason} ->
  423. ?tp(
  424. pgsql_connector_query_return,
  425. #{error => Reason}
  426. ),
  427. TranslatedError = translate_to_log_context(Reason),
  428. ?SLOG(
  429. error,
  430. maps:merge(
  431. #{
  432. msg => "postgresql_connector_do_sql_query_failed",
  433. connector => InstId,
  434. type => Type,
  435. sql => NameOrSQL
  436. },
  437. TranslatedError
  438. )
  439. ),
  440. case Reason of
  441. sync_required ->
  442. {error, {recoverable_error, Reason}};
  443. ecpool_empty ->
  444. {error, {recoverable_error, Reason}};
  445. {error, error, _, undefined_table, _, _} ->
  446. {error, {unrecoverable_error, export_error(TranslatedError)}};
  447. _ ->
  448. {error, export_error(TranslatedError)}
  449. end;
  450. Result ->
  451. ?tp(
  452. pgsql_connector_query_return,
  453. #{result => Result}
  454. ),
  455. Result
  456. catch
  457. error:function_clause:Stacktrace ->
  458. ?SLOG(error, #{
  459. msg => "postgresql_connector_do_sql_query_failed",
  460. connector => InstId,
  461. type => Type,
  462. sql => NameOrSQL,
  463. reason => function_clause,
  464. stacktrace => Stacktrace
  465. }),
  466. {error, {unrecoverable_error, invalid_request}}
  467. end.
  468. on_get_status(_InstId, #{pool_name := PoolName} = State) ->
  469. case emqx_resource_pool:health_check_workers(PoolName, fun ?MODULE:do_get_status/1) of
  470. true ->
  471. case do_check_prepares(State) of
  472. ok ->
  473. connected;
  474. {ok, NState} ->
  475. %% return new state with prepared statements
  476. {connected, NState};
  477. {error, undefined_table} ->
  478. %% return new state indicating that we are connected but the target table is not created
  479. {disconnected, State, unhealthy_target};
  480. {error, _Reason} ->
  481. %% do not log error, it is logged in prepare_sql_to_conn
  482. connecting
  483. end;
  484. false ->
  485. connecting
  486. end.
  487. do_get_status(Conn) ->
  488. ok == element(1, epgsql:squery(Conn, "SELECT count(1) AS T")).
  489. do_check_prepares(
  490. #{
  491. pool_name := PoolName,
  492. query_templates := #{<<"send_message">> := {SQL, _RowTemplate}}
  493. }
  494. ) ->
  495. WorkerPids = [Worker || {_WorkerName, Worker} <- ecpool:workers(PoolName)],
  496. case validate_table_existence(WorkerPids, SQL) of
  497. ok ->
  498. ok;
  499. {error, Reason} ->
  500. {error, Reason}
  501. end;
  502. do_check_prepares(#{prepares := disabled}) ->
  503. ok;
  504. do_check_prepares(#{prepares := Prepares}) when is_map(Prepares) ->
  505. ok;
  506. do_check_prepares(#{prepares := {error, _}} = State) ->
  507. %% retry to prepare
  508. case prepare_sql(State) of
  509. {ok, PrepStatements} ->
  510. %% remove the error
  511. {ok, State#{prepares := PrepStatements}};
  512. {error, Reason} ->
  513. {error, Reason}
  514. end.
  515. -spec validate_table_existence([pid()], binary()) -> ok | {error, undefined_table}.
  516. validate_table_existence([WorkerPid | Rest], SQL) ->
  517. try ecpool_worker:client(WorkerPid) of
  518. {ok, Conn} ->
  519. case epgsql:parse2(Conn, "", SQL, []) of
  520. {error, {_, _, _, undefined_table, _, _}} ->
  521. {error, undefined_table};
  522. Res when is_tuple(Res) andalso ok == element(1, Res) ->
  523. ok;
  524. Res ->
  525. ?tp(postgres_connector_bad_parse2, #{result => Res}),
  526. validate_table_existence(Rest, SQL)
  527. end;
  528. _ ->
  529. validate_table_existence(Rest, SQL)
  530. catch
  531. exit:{noproc, _} ->
  532. validate_table_existence(Rest, SQL)
  533. end;
  534. validate_table_existence([], _SQL) ->
  535. %% All workers either replied an unexpected error; we will retry
  536. %% on the next health check.
  537. ok.
  538. %% ===================================================================
  539. connect(Opts) ->
  540. Host = proplists:get_value(host, Opts),
  541. Username = proplists:get_value(username, Opts),
  542. %% TODO: teach `epgsql` to accept 0-arity closures as passwords.
  543. Password = emqx_secret:unwrap(proplists:get_value(password, Opts)),
  544. case epgsql:connect(Host, Username, Password, conn_opts(Opts)) of
  545. {ok, _Conn} = Ok ->
  546. Ok;
  547. {error, Reason} ->
  548. {error, Reason}
  549. end.
  550. query(Conn, SQL, Params) ->
  551. case epgsql:equery(Conn, SQL, Params) of
  552. {error, sync_required} = Res ->
  553. ok = epgsql:sync(Conn),
  554. Res;
  555. Res ->
  556. Res
  557. end.
  558. prepared_query(Conn, Name, Params) ->
  559. case epgsql:prepared_query2(Conn, Name, Params) of
  560. {error, sync_required} = Res ->
  561. ok = epgsql:sync(Conn),
  562. Res;
  563. Res ->
  564. Res
  565. end.
  566. execute_batch(Conn, Statement, Params) ->
  567. case epgsql:execute_batch(Conn, Statement, Params) of
  568. {error, sync_required} = Res ->
  569. ok = epgsql:sync(Conn),
  570. Res;
  571. Res ->
  572. Res
  573. end.
  574. conn_opts(Opts) ->
  575. conn_opts(Opts, []).
  576. conn_opts([], Acc) ->
  577. Acc;
  578. conn_opts([Opt = {database, _} | Opts], Acc) ->
  579. conn_opts(Opts, [Opt | Acc]);
  580. conn_opts([{ssl, Bool} | Opts], Acc) when is_boolean(Bool) ->
  581. Flag =
  582. case Bool of
  583. true -> required;
  584. false -> false
  585. end,
  586. conn_opts(Opts, [{ssl, Flag} | Acc]);
  587. conn_opts([Opt = {port, _} | Opts], Acc) ->
  588. conn_opts(Opts, [Opt | Acc]);
  589. conn_opts([Opt = {timeout, _} | Opts], Acc) ->
  590. conn_opts(Opts, [Opt | Acc]);
  591. conn_opts([Opt = {ssl_opts, _} | Opts], Acc) ->
  592. conn_opts(Opts, [Opt | Acc]);
  593. conn_opts([_Opt | Opts], Acc) ->
  594. conn_opts(Opts, Acc).
  595. parse_sql_template(Config, SQLID) ->
  596. Queries =
  597. case Config of
  598. #{prepare_statement := Qs} ->
  599. Qs;
  600. #{sql := Query} ->
  601. #{SQLID => Query};
  602. #{} ->
  603. #{}
  604. end,
  605. Templates = maps:fold(fun parse_sql_template/3, #{}, Queries),
  606. #{query_templates => Templates}.
  607. parse_sql_template(Key, Query, Acc) ->
  608. Template = emqx_template_sql:parse_prepstmt(Query, #{parameters => '$n'}),
  609. Acc#{Key => Template}.
  610. render_prepare_sql_row(RowTemplate, Data) ->
  611. % NOTE: ignoring errors here, missing variables will be replaced with `null`.
  612. {Row, _Errors} = emqx_template_sql:render_prepstmt(RowTemplate, {emqx_jsonish, Data}),
  613. Row.
  614. init_prepare(State = #{prepares := disabled}) ->
  615. State;
  616. init_prepare(State = #{query_templates := Templates}) when map_size(Templates) == 0 ->
  617. State;
  618. init_prepare(State = #{}) ->
  619. case prepare_sql(State) of
  620. {ok, PrepStatements} ->
  621. State#{prepares => PrepStatements};
  622. Error ->
  623. TranslatedError = translate_to_log_context(Error),
  624. ?SLOG(
  625. error,
  626. maps:merge(
  627. #{msg => <<"postgresql_init_prepare_statement_failed">>},
  628. TranslatedError
  629. )
  630. ),
  631. %% mark the prepares failed
  632. State#{prepares => {error, export_error(TranslatedError)}}
  633. end.
  634. prepare_sql(#{query_templates := Templates, pool_name := PoolName}) ->
  635. prepare_sql(maps:to_list(Templates), PoolName).
  636. prepare_sql(Templates, PoolName) ->
  637. case do_prepare_sql(Templates, PoolName) of
  638. {ok, _Sts} = Ok ->
  639. %% prepare for reconnect
  640. ecpool:add_reconnect_callback(PoolName, {?MODULE, prepare_sql_to_conn, [Templates]}),
  641. Ok;
  642. Error ->
  643. Error
  644. end.
  645. do_prepare_sql(Templates, PoolName) ->
  646. do_prepare_sql(ecpool:workers(PoolName), Templates, #{}).
  647. do_prepare_sql([{_Name, Worker} | Rest], Templates, _LastSts) ->
  648. {ok, Conn} = ecpool_worker:client(Worker),
  649. case prepare_sql_to_conn(Conn, Templates) of
  650. {ok, Sts} ->
  651. do_prepare_sql(Rest, Templates, Sts);
  652. Error ->
  653. Error
  654. end;
  655. do_prepare_sql([], _Prepares, LastSts) ->
  656. {ok, LastSts}.
  657. prepare_sql_to_conn(Conn, Prepares) ->
  658. prepare_sql_to_conn(Conn, Prepares, #{}, 0).
  659. prepare_sql_to_conn(Conn, [], Statements, _Attempts) when is_pid(Conn) ->
  660. {ok, Statements};
  661. prepare_sql_to_conn(Conn, [{_Key, _} | _Rest], _Statements, _MaxAttempts = 2) when is_pid(Conn) ->
  662. failed_to_remove_prev_prepared_statement_error();
  663. prepare_sql_to_conn(
  664. Conn, [{Key, {SQL, _RowTemplate}} | Rest] = ToPrepare, Statements, Attempts
  665. ) when is_pid(Conn) ->
  666. LogMeta = #{msg => "postgresql_prepare_statement", name => Key, sql => SQL},
  667. ?SLOG(info, LogMeta),
  668. case epgsql:parse2(Conn, Key, SQL, []) of
  669. {ok, Statement} ->
  670. prepare_sql_to_conn(Conn, Rest, Statements#{Key => Statement}, 0);
  671. {error, #error{severity = error, codename = undefined_table} = Error} ->
  672. %% Target table is not created
  673. ?tp(pgsql_undefined_table, #{}),
  674. LogMsg =
  675. maps:merge(
  676. LogMeta#{msg => "postgresql_parse_failed"},
  677. translate_to_log_context(Error)
  678. ),
  679. ?SLOG(error, LogMsg),
  680. {error, undefined_table};
  681. {error, #error{severity = error, codename = duplicate_prepared_statement}} = Error ->
  682. ?tp(pgsql_prepared_statement_exists, #{}),
  683. LogMsg =
  684. maps:merge(
  685. LogMeta#{
  686. msg => "postgresql_prepared_statment_with_same_name_already_exists",
  687. explain => <<
  688. "A prepared statement with the same name already "
  689. "exists in the driver. Will attempt to remove the "
  690. "previous prepared statement with the name and then "
  691. "try again."
  692. >>
  693. },
  694. translate_to_log_context(Error)
  695. ),
  696. ?SLOG(warning, LogMsg),
  697. case epgsql:close(Conn, statement, Key) of
  698. ok ->
  699. ?SLOG(info, #{msg => "pqsql_closed_statement_successfully"}),
  700. prepare_sql_to_conn(Conn, ToPrepare, Statements, Attempts + 1);
  701. {error, CloseError} ->
  702. ?SLOG(error, #{msg => "pqsql_close_statement_failed", cause => CloseError}),
  703. failed_to_remove_prev_prepared_statement_error()
  704. end;
  705. {error, Error} ->
  706. TranslatedError = translate_to_log_context(Error),
  707. LogMsg =
  708. maps:merge(
  709. LogMeta#{msg => "postgresql_parse_failed"},
  710. TranslatedError
  711. ),
  712. ?SLOG(error, LogMsg),
  713. {error, export_error(TranslatedError)}
  714. end.
  715. failed_to_remove_prev_prepared_statement_error() ->
  716. Msg =
  717. ("A previous prepared statement for the action already exists "
  718. "but cannot be closed. Please, try to disable and then enable "
  719. "the connector to resolve this issue."),
  720. {error, unicode:characters_to_binary(Msg)}.
  721. to_bin(Bin) when is_binary(Bin) ->
  722. Bin;
  723. to_bin(Atom) when is_atom(Atom) ->
  724. erlang:atom_to_binary(Atom).
  725. handle_result({error, {recoverable_error, _Error}} = Res) ->
  726. Res;
  727. handle_result({error, {unrecoverable_error, _Error}} = Res) ->
  728. Res;
  729. handle_result({error, disconnected}) ->
  730. {error, {recoverable_error, disconnected}};
  731. handle_result({error, Error}) ->
  732. TranslatedError = translate_to_log_context(Error),
  733. {error, {unrecoverable_error, export_error(TranslatedError)}};
  734. handle_result(Res) ->
  735. Res.
  736. on_format_query_result({ok, Cnt}) when is_integer(Cnt) ->
  737. #{result => ok, affected_rows => Cnt};
  738. on_format_query_result(Res) ->
  739. Res.
  740. handle_batch_result([{ok, Count} | Rest], Acc) ->
  741. handle_batch_result(Rest, Acc + Count);
  742. handle_batch_result([{error, Error} | _Rest], _Acc) ->
  743. TranslatedError = translate_to_log_context(Error),
  744. {error, {unrecoverable_error, export_error(TranslatedError)}};
  745. handle_batch_result([], Acc) ->
  746. ?tp("postgres_success_batch_result", #{row_count => Acc}),
  747. {ok, Acc}.
  748. translate_to_log_context({error, Reason}) ->
  749. translate_to_log_context(Reason);
  750. translate_to_log_context(#error{} = Reason) ->
  751. #error{
  752. severity = Severity,
  753. code = Code,
  754. codename = Codename,
  755. message = Message,
  756. extra = Extra
  757. } = Reason,
  758. #{
  759. driver_severity => Severity,
  760. driver_error_codename => Codename,
  761. driver_error_code => Code,
  762. driver_error_message => emqx_logger_textfmt:try_format_unicode(Message),
  763. driver_error_extra => Extra
  764. };
  765. translate_to_log_context(Reason) ->
  766. #{reason => Reason}.
  767. export_error(#{
  768. driver_severity := Severity,
  769. driver_error_codename := Codename,
  770. driver_error_code := Code
  771. }) ->
  772. %% Extra information has already been logged.
  773. #{
  774. error_code => Code,
  775. error_codename => Codename,
  776. severity => Severity
  777. };
  778. export_error(#{reason := Reason}) ->
  779. Reason;
  780. export_error(Error) ->
  781. Error.