README 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. Erlang PostgreSQL Database Client
  2. Asynchronous fork of https://github.com/wg/epgsql
  3. It passes all tests from original driver except 3 timeout tests.
  4. Backward compatibility is preserved by module pgsql.
  5. Difference highlights (see CHANGES for full list):
  6. + internal queue of client requests, so you don't need to wait for response to send next request
  7. + results can be delivered as regular erlang messages, either complete or row by row
  8. + single process to hold driver state and receive socket data
  9. + execute several prepared statements as a batch
  10. + bind timestamps in erlang:now() format
  11. * Known problems
  12. Timeout supplied at connect time will work only as connect timeout not query timeout.
  13. SSL performance degrades if driver process has large inbox (thousands of messages).
  14. * Connect
  15. {ok, C} = pgsql:connect(Host, [Username], [Password], Opts).
  16. Host - host to connect to.
  17. Username - username to connect as, defaults to $USER.
  18. Password - optional password to authenticate with.
  19. Opts - property list of extra options. Supported properties:
  20. + {database, String}
  21. + {port, Integer}
  22. + {ssl, Atom} true | false | required
  23. + {ssl_opts, List} see ssl application docs in OTP
  24. + {timeout, Integer} milliseconds, defaults to 5000
  25. + {async, Pid} see Asynchronous Messages section
  26. {ok, C} = pgsql:connect("localhost", "username", [{database, "test_db"}]).
  27. ok = pgsql:close(C).
  28. The timeout parameter will trigger an {error, timeout} result when the
  29. server fails to respond within Timeout milliseconds.
  30. * Simple Query
  31. {ok, Columns, Rows} = pgsql:squery(C, "select ...").
  32. {ok, Count} = pgsql:squery(C, "update ...").
  33. {ok, Count, Columns, Rows} = pgsql:squery(C, "insert ... returning ...").
  34. {error, Error} = pgsql:squery(C, "invalid SQL").
  35. Columns - list of column records, see pgsql.hrl for definition.
  36. Rows - list of tuples, one for each row.
  37. Count - integer count of rows inserted/updated/etc
  38. The simple query protocol returns all columns as text (Erlang binaries)
  39. and does not support binding parameters.
  40. * Extended Query
  41. {ok, Columns, Rows} = pgsql:equery(C, "select ...", [Parameters]).
  42. {ok, Count} = pgsql:equery(C, "update ...", [Parameters]).
  43. {ok, Count, Columns, Rows} = pgsql:equery(C, "insert ... returning ...", [Parameters]).
  44. {error, Error} = pgsql:equery(C, "invalid SQL", [Parameters]).
  45. Parameters - optional list of values to be bound to $1, $2, $3, etc.
  46. The extended query protocol combines parse, bind, and execute using
  47. the unnamed prepared statement and portal. A "select" statement returns
  48. {ok, Columns, Rows}, "insert/update/delete" returns {ok, Count} or
  49. {ok, Count, Columns, Rows} when a "returning" clause is present. When
  50. an error occurs, all statements result in {error, #error{}}.
  51. PostgreSQL's binary format is used to return integers as Erlang
  52. integers, floats as floats, bytea/text/varchar columns as binaries,
  53. bools as true/false, etc. For details see pgsql_binary.erl and the
  54. Data Representation section below.
  55. * Parse/Bind/Execute
  56. {ok, Statement} = pgsql:parse(C, [StatementName], Sql, [ParameterTypes]).
  57. StatementName - optional, reusable, name for the prepared statement.
  58. ParameterTypes - optional list of PostgreSQL types for each parameter.
  59. For valid type names see pgsql_types.erl.
  60. ok = pgsql:bind(C, Statement, [PortalName], ParameterValues).
  61. PortalName - optional name for the result portal.
  62. {ok | partial, Rows} = pgsql:execute(C, Statement, [PortalName], [MaxRows]).
  63. {ok, Count} = pgsql:execute(C, Statement, [PortalName]).
  64. {ok, Count, Rows} = pgsql:execute(C, Statement, [PortalName]).
  65. PortalName - optional portal name used in bind/4.
  66. MaxRows - maximum number of rows to return (0 for all rows).
  67. execute returns {partial, Rows} when more rows are available.
  68. ok = pgsql:close(C, Statement).
  69. ok = pgsql:close(C, statement | portal, Name).
  70. ok = pgsql:sync(C).
  71. All functions return {error, Error} when an error occurs.
  72. * Data Representation
  73. null = null
  74. bool = true | false
  75. char = $A | binary
  76. intX = 1
  77. floatX = 1.0
  78. date = {Year, Month, Day}
  79. time = {Hour, Minute, Second.Microsecond}
  80. timetz = {time, Timezone}
  81. timestamp = {date, time}
  82. timestamptz = {date, time}
  83. interval = {time, Days, Months}
  84. text = <<"a">>
  85. varchar = <<"a">>
  86. bytea = <<1, 2>>
  87. array = [1, 2, 3]
  88. record = {int2, time, text, ...} (decode only)
  89. timestamp and timestamptz parameters can take erlang:now() format {MegaSeconds, Seconds, MicroSeconds}
  90. * Errors
  91. Errors originating from the PostgreSQL backend are returned as {error, #error{}},
  92. see pgsql.hrl for the record definition. epgsql functions may also return
  93. {error, What} where What is one of the following:
  94. {unsupported_auth_method, Method} - required auth method is unsupported
  95. timeout - request timed out
  96. closed - connection was closed
  97. sync_required - error occured and pgsql:sync must be called
  98. * Asynchronous Messages
  99. PostgreSQL may deliver two types of asynchronous message: "notices" in response
  100. to notice and warning messages generated by the server, and "notifications" which
  101. are generated by the LISTEN/NOTIFY mechanism.
  102. Passing the {async, Pid} option to pgsql:connect will result in these async
  103. messages being sent to the specified process, otherwise they will be dropped.
  104. Message formats:
  105. {pgsql, Connection, {notification, Channel, Pid, Payload}}
  106. Connection - connection the notification occured on
  107. Channel - channel the notification occured on
  108. Pid - database session pid that sent notification
  109. Payload - optional payload, only available from PostgreSQL >= 9.0
  110. {pgsql, Connection, {notice, Error}}
  111. Connection - connection the notice occured on
  112. Error - an #error{} record, see pgsql.hrl