README 9.6 KB

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