summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2020-08-17 03:39:58 -0300
committerGitHub <noreply@github.com>2020-08-17 08:39:58 +0200
commitc9fdad2c21a6394daec70e9d0e215176b69783ac (patch)
treec6b784193eefa1f684399be4ca43132573cb3a0f
parentba042af9cc6194cd3c3e67e5daf0165b4b3630c0 (diff)
downloadNim-c9fdad2c21a6394daec70e9d0e215176b69783ac.tar.gz
db_postgres document how to use it with unix socket (#15187)
-rw-r--r--lib/impure/db_postgres.nim24
1 files changed, 22 insertions, 2 deletions
diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim
index 9e8075450..f9f584663 100644
--- a/lib/impure/db_postgres.nim
+++ b/lib/impure/db_postgres.nim
@@ -37,6 +37,26 @@
 ##                 VALUES ($1, $2, $3)""",
 ##           3)
 ##
+##
+## Unix Socket
+## ===========
+##
+## Using Unix sockets instead of TCP connection can
+## `improve performance up to 30% ~ 175% for some operations <https://momjian.us/main/blogs/pgblog/2012.html#June_6_2012>`_.
+##
+## To use Unix sockets with `db_postgres`, change the server address to the socket file path:
+##
+## .. code-block:: Nim
+##   import db_postgres ## Change "localhost" or "127.0.0.1" to the socket file path
+##   let db = db_postgres.open("/run/postgresql", "user", "password", "database")
+##   echo db.getAllRows(sql"SELECT version();")
+##   db.close()
+##
+## The socket file path is operating system specific and distribution specific,
+## additional configuration may or may not be needed on your `postgresql.conf`.
+## The Postgres server must be on the same computer and only works for Unix-like operating systems.
+##
+##
 ## Examples
 ## ========
 ##
@@ -491,7 +511,7 @@ proc tryInsert*(db: DbConn, query: SqlQuery,pkName: string,
                 args: varargs[string, `$`]): int64
                {.tags: [WriteDbEffect], since: (1, 3).}=
   ## executes the query (typically "INSERT") and returns the
-  ## generated ID for the row or -1 in case of an error. 
+  ## generated ID for the row or -1 in case of an error.
   var x = pqgetvalue(setupQuery(db, SqlQuery(string(query) & " RETURNING " & pkName),
     args), 0, 0)
   if not isNil(x):
@@ -503,7 +523,7 @@ proc insert*(db: DbConn, query: SqlQuery, pkName: string,
              args: varargs[string, `$`]): int64
             {.tags: [WriteDbEffect], since: (1, 3).} =
   ## executes the query (typically "INSERT") and returns the
-  ## generated ID 
+  ## generated ID
   result = tryInsertID(db, query, args)
   if result < 0: dbError(db)