README 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. Erlang PostgreSQL Database Client
  2. Asynchronous fork of https://github.com/wg/epgsql originally here:
  3. https://github.com/mabrek/epgsql and subsequently forked in order to
  4. provide a common fork for community development.
  5. * Motivation
  6. When you need to execute several queries, it involves a number network
  7. round-trips between the application and the database.
  8. The PostgreSQL frontend/backend protocol supports request pipelining.
  9. This means that you don't need to wait for the previous command to finish
  10. before sending the next command. This version of the driver makes full use
  11. of the protocol feature that allows faster execution.
  12. * Difference highlights
  13. + 3 API sets: pgsql, apgsql and ipgsql:
  14. pgsql maintains backwards compatibility with the original driver API,
  15. apgsql delivers complete results as regular erlang messages,
  16. ipgsql delivers results as messages incrementally (row by row)
  17. + internal queue of client requests, so you don't need to wait for the
  18. response to send the next request
  19. + single process to hold driver state and receive socket data
  20. + execution of several parsed statements as a batch
  21. + binding timestamps in erlang:now() format
  22. see CHANGES for full list.
  23. * Differences between devel branch and mabrek's original async fork:
  24. + Unnamed statements are used unless specified otherwise. This may
  25. cause problems for people attempting to use the same connection
  26. concurrently, which will no longer work.
  27. * Known problems
  28. A timeout supplied at connect time works as a socket connect timeout,
  29. not a query timeout. It passes all tests from original driver except for
  30. the 3 failing timeout tests.
  31. SSL performance can degrade if the driver process has a large inbox
  32. (thousands of messages).
  33. Usage of unnamed prepared statement and portals leads to unpredicted results
  34. in case of concurrent access to same connection.
  35. * Connect
  36. {ok, C} = pgsql:connect(Host, [Username], [Password], Opts).
  37. Host - host to connect to.
  38. Username - username to connect as, defaults to $USER.
  39. Password - optional password to authenticate with.
  40. Opts - property list of extra options. Supported properties:
  41. + {database, String}
  42. + {port, Integer}
  43. + {ssl, Atom} true | false | required
  44. + {ssl_opts, List} see ssl application docs in OTP
  45. + {timeout, Integer} milliseconds, defaults to 5000
  46. + {async, Pid} see Server Notifications section
  47. {ok, C} = pgsql:connect("localhost", "username", [{database, "test_db"}]).
  48. ok = pgsql:close(C).
  49. The timeout parameter will trigger an {error, timeout} result when the
  50. socket fails to connect within Timeout milliseconds.
  51. Asynchronous connect example (applies to ipgsql too):
  52. {ok, C} = apgsql:start_link(),
  53. Ref = apgsql:connect(C, "localhost", "username", [{database, "test_db"}]),
  54. receive
  55. {C, Ref, connected} ->
  56. {ok, C};
  57. {C, Ref, Error = {error, _}} ->
  58. Error;
  59. {'EXIT', C, _Reason} ->
  60. {error, closed}
  61. end.
  62. * Simple Query
  63. {ok, Columns, Rows} = pgsql:squery(C, "select ...").
  64. {ok, Count} = pgsql:squery(C, "update ...").
  65. {ok, Count, Columns, Rows} = pgsql:squery(C, "insert ... returning ...").
  66. {error, Error} = pgsql:squery(C, "invalid SQL").
  67. Columns - list of column records, see pgsql.hrl for definition.
  68. Rows - list of tuples, one for each row.
  69. Count - integer count of rows inserted/updated/etc
  70. The simple query protocol returns all columns as text (Erlang binaries)
  71. and does not support binding parameters.
  72. Several queries separated by semicolon can be executed by squery.
  73. [{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}] =
  74. pgsql:squery(C, "select 1; select 2").
  75. apgsql:squery returns result as a single message:
  76. Ref = apgsql:squery(C, Sql),
  77. receive
  78. {C, Ref, Result} -> Result
  79. end.
  80. Result has same format as return value of pgsql:squery.
  81. ipgsql:squery returns results incrementally for each query inside Sql and
  82. for each row:
  83. Ref = ipgsql:squery(C, Sql),
  84. receive
  85. {C, Ref, {columns, Columns}} ->
  86. %% columns description
  87. Columns;
  88. {C, Ref, {data, Row}} ->
  89. %% single data row
  90. Row;
  91. {C, Ref, {error, _E} = Error} ->
  92. Error;
  93. {C, Ref, {complete, {_Type, Count}}} ->
  94. %% execution of one insert/update/delete has finished
  95. {ok, Count}; % affected rows count
  96. {C, Ref, {complete, _Type}} ->
  97. %% execution of one select has finished
  98. ok;
  99. {C, Ref, done} ->
  100. %% execution of all queries from Sql has finished
  101. done;
  102. end.
  103. * Extended Query
  104. {ok, Columns, Rows} = pgsql:equery(C, "select ...", [Parameters]).
  105. {ok, Count} = pgsql:equery(C, "update ...", [Parameters]).
  106. {ok, Count, Columns, Rows} = pgsql:equery(C, "insert ... returning ...", [Parameters]).
  107. {error, Error} = pgsql:equery(C, "invalid SQL", [Parameters]).
  108. Parameters - optional list of values to be bound to $1, $2, $3, etc.
  109. The extended query protocol combines parse, bind, and execute using
  110. the unnamed prepared statement and portal. A "select" statement returns
  111. {ok, Columns, Rows}, "insert/update/delete" returns {ok, Count} or
  112. {ok, Count, Columns, Rows} when a "returning" clause is present. When
  113. an error occurs, all statements result in {error, #error{}}.
  114. PostgreSQL's binary format is used to return integers as Erlang
  115. integers, floats as floats, bytea/text/varchar columns as binaries,
  116. bools as true/false, etc. For details see pgsql_binary.erl and the
  117. Data Representation section below.
  118. Asynchronous api equery requires you to parse statement beforehand
  119. Ref = apgsql:equery(C, Statement, [Parameters]),
  120. receive
  121. {C, Ref, Res} -> Res
  122. end.
  123. Statement - parsed statement (see parse below)
  124. Res has same format as return value of pgsql:equery.
  125. ipgsql:equery(C, Statement, [Parameters]) sends same set of messages as
  126. squery including final {C, Ref, done}.
  127. * Parse/Bind/Execute
  128. {ok, Statement} = pgsql:parse(C, [StatementName], Sql, [ParameterTypes]).
  129. StatementName - optional, reusable, name for the prepared statement.
  130. ParameterTypes - optional list of PostgreSQL types for each parameter.
  131. For valid type names see pgsql_types.erl.
  132. apgsql:parse sends {C, Ref, {ok, Statement} | {error, Reason}}.
  133. ipgsql:parse sends:
  134. {C, Ref, {types, Types}}
  135. {C, Ref, {columns, Columns}}
  136. {C, Ref, no_data} if statement will not return rows
  137. {C, Ref, {error, Reason}}
  138. ok = pgsql:bind(C, Statement, [PortalName], ParameterValues).
  139. PortalName - optional name for the result portal.
  140. both apgsql:bind and ipgsql:bind send {C, Ref, ok | {error, Reason}}
  141. {ok | partial, Rows} = pgsql:execute(C, Statement, [PortalName], [MaxRows]).
  142. {ok, Count} = pgsql:execute(C, Statement, [PortalName]).
  143. {ok, Count, Rows} = pgsql:execute(C, Statement, [PortalName]).
  144. PortalName - optional portal name used in bind/4.
  145. MaxRows - maximum number of rows to return (0 for all rows).
  146. execute returns {partial, Rows} when more rows are available.
  147. apgsql:execute sends {C, Ref, Result} where Result has same format as
  148. return value of pgsql:execute.
  149. ipgsql:execute sends
  150. {C, Ref, {data, Row}}
  151. {C, Ref, {error, Reason}}
  152. {C, Ref, suspended} partial result was sent, more rows are available
  153. {C, Ref, {complete, {_Type, Count}}}
  154. {C, Ref, {complete, _Type}}
  155. ok = pgsql:close(C, Statement).
  156. ok = pgsql:close(C, statement | portal, Name).
  157. ok = pgsql:sync(C).
  158. All pgsql functions return {error, Error} when an error occurs.
  159. apgsql and ipgsql close and sync functions send {C, Ref, ok}.
  160. * Batch execution
  161. Batch execution is bind + execute for several prepared statements.
  162. It uses unnamed portals and MaxRows = 0.
  163. Results = pgsql:execute_batch(C, Batch).
  164. Batch - list of {Statement, ParameterValues}
  165. Results - list of {ok, Count} or {ok, Count, Rows}
  166. Example
  167. {ok, S1} = pgsql:parse(C, "one", "select $1", [int4]),
  168. {ok, S2} = pgsql:parse(C, "two", "select $1 + $2", [int4, int4]),
  169. [{ok, [{1}]}, {ok, [{3}]}] =
  170. pgsql:execute_batch(C, [{S1, [1]}, {S2, [1, 2]}]).
  171. apgsql:execute_batch sends {C, Ref, Results}
  172. ipgsql:execute_batch sends
  173. {C, Ref, {data, Row}}
  174. {C, Ref, {error, Reason}}
  175. {C, Ref, {complete, {_Type, Count}}}
  176. {C, Ref, {complete, _Type}}
  177. {C, Ref, done} - execution of all queries from Batch has finished
  178. * Data Representation
  179. null = null
  180. bool = true | false
  181. char = $A | binary
  182. intX = 1
  183. floatX = 1.0
  184. date = {Year, Month, Day}
  185. time = {Hour, Minute, Second.Microsecond}
  186. timetz = {time, Timezone}
  187. timestamp = {date, time}
  188. timestamptz = {date, time}
  189. interval = {time, Days, Months}
  190. text = <<"a">>
  191. varchar = <<"a">>
  192. bytea = <<1, 2>>
  193. array = [1, 2, 3]
  194. point = {10.1, 100.0}
  195. record = {int2, time, text, ...} (decode only)
  196. timestamp and timestamptz parameters can take erlang:now() format {MegaSeconds, Seconds, MicroSeconds}
  197. * Errors
  198. Errors originating from the PostgreSQL backend are returned as {error, #error{}},
  199. see pgsql.hrl for the record definition. epgsql functions may also return
  200. {error, What} where What is one of the following:
  201. {unsupported_auth_method, Method} - required auth method is unsupported
  202. timeout - request timed out
  203. closed - connection was closed
  204. sync_required - error occured and pgsql:sync must be called
  205. * Server Notifications
  206. PostgreSQL may deliver two types of asynchronous message: "notices" in response
  207. to notice and warning messages generated by the server, and "notifications" which
  208. are generated by the LISTEN/NOTIFY mechanism.
  209. Passing the {async, Pid} option to pgsql:connect will result in these async
  210. messages being sent to the specified process, otherwise they will be dropped.
  211. Message formats:
  212. {pgsql, Connection, {notification, Channel, Pid, Payload}}
  213. Connection - connection the notification occurred on
  214. Channel - channel the notification occurred on
  215. Pid - database session pid that sent notification
  216. Payload - optional payload, only available from PostgreSQL >= 9.0
  217. {pgsql, Connection, {notice, Error}}
  218. Connection - connection the notice occurred on
  219. Error - an #error{} record, see pgsql.hrl
  220. * Mailing list
  221. https://groups.google.com/forum/#!forum/epgsql