Skip to content

Commit 080275f

Browse files
author
James William Pye
committed
Add the v1.0 docs.
1 parent c909e14 commit 080275f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+16196
-0
lines changed

frontend/static/docs/1.0/.buildinfo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Sphinx build info version 1
2+
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3+
config: a85cd22e0e01cc81ce9f28fa3c0b8d86
4+
tags: fbb0d17656682115ca4d033fb2f83ba1
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Administration
2+
==============
3+
4+
This chapter covers the administration of py-postgresql. This includes
5+
installation and other aspects of working with py-postgresql such as
6+
environment variables and configuration files.
7+
8+
9+
Installation
10+
------------
11+
12+
py-postgresql uses Python's distutils package to manage the build and
13+
installation process of the package. The normal entry point for
14+
this is the ``setup.py`` script contained in the root project directory.
15+
16+
After extracting the archive and changing the into the project's directory,
17+
installation is normally as simple as::
18+
19+
$ python3 ./setup.py install
20+
21+
However, if you need to install for use with a particular version of python,
22+
just use the path of the executable that should be used::
23+
24+
$ /usr/opt/bin/python3 ./setup.py install
25+
26+
27+
Environment
28+
-----------
29+
30+
These environment variables effect the operation of the package:
31+
32+
============== ===============================================================================
33+
PGINSTALLATION The path to the ``pg_config`` executable of the installation to use by default.
34+
============== ===============================================================================
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
.. _alock:
2+
3+
**************
4+
Advisory Locks
5+
**************
6+
7+
.. warning:: `postgresql.alock` is a new feature in v1.0.
8+
9+
`Explicit Locking in PostgreSQL <http://www.postgresql.org/docs/current/static/explicit-locking.html#ADVISORY-LOCKS>`_.
10+
11+
PostgreSQL's advisory locks offer a cooperative synchronization primitive.
12+
These are used in cases where an application needs access to a resource, but
13+
using table locks may cause interference with other operations that can be
14+
safely performed alongside the application-level, exclusive operation.
15+
16+
Advisory locks can be used by directly executing the stored procedures in the
17+
database or by using the :class:`postgresql.alock.ALock` subclasses, which
18+
provides a context manager that uses those stored procedures.
19+
20+
Currently, only two subclasses exist. Each represents the lock mode
21+
supported by PostgreSQL's advisory locks:
22+
23+
* :class:`postgresql.alock.ShareLock`
24+
* :class:`postgresql.alock.ExclusiveLock`
25+
26+
27+
Acquiring ALocks
28+
================
29+
30+
An ALock instance represents a sequence of advisory locks. A single ALock can
31+
acquire and release multiple advisory locks by creating the instance with
32+
multiple lock identifiers::
33+
34+
>>> from postgresql import alock
35+
>>> table1_oid = 192842
36+
>>> table2_oid = 192849
37+
>>> l = alock.ExclusiveLock(db, (table1_oid, 0), (table2_oid, 0))
38+
>>> l.acquire()
39+
>>> ...
40+
>>> l.release()
41+
42+
:class:`postgresql.alock.ALock` is similar to :class:`threading.RLock`; in
43+
order for an ALock to be released, it must be released the number of times it
44+
has been acquired. ALocks are associated with and survived by their session.
45+
Much like how RLocks are associated with the thread they are acquired in:
46+
acquiring an ALock again will merely increment its count.
47+
48+
PostgreSQL allows advisory locks to be identified using a pair of `int4` or a
49+
single `int8`. ALock instances represent a *sequence* of those identifiers::
50+
51+
>>> from postgresql import alock
52+
>>> ids = [(0,0), 0, 1]
53+
>>> with alock.ShareLock(db, *ids):
54+
... ...
55+
56+
Both types of identifiers may be used within the same ALock, and, regardless of
57+
their type, will be aquired in the order that they were given to the class'
58+
constructor. In the above example, ``(0,0)`` is acquired first, then ``0``, and
59+
lastly ``1``.
60+
61+
62+
ALocks
63+
======
64+
65+
`postgresql.alock.ALock` is abstract; it defines the interface and some common
66+
functionality. The lock mode is selected by choosing the appropriate subclass.
67+
68+
There are two:
69+
70+
``postgresql.alock.ExclusiveLock(database, *identifiers)``
71+
Instantiate an ALock object representing the `identifiers` for use with the
72+
`database`. Exclusive locks will conflict with other exclusive locks and share
73+
locks.
74+
75+
``postgresql.alock.ShareLock(database, *identifiers)``
76+
Instantiate an ALock object representing the `identifiers` for use with the
77+
`database`. Share locks can be acquired when a share lock with the same
78+
identifier has been acquired by another backend. However, an exclusive lock
79+
with the same identifier will conflict.
80+
81+
82+
ALock Interface Points
83+
----------------------
84+
85+
Methods and properties available on :class:`postgresql.alock.ALock` instances:
86+
87+
``alock.acquire(blocking = True)``
88+
Acquire the advisory locks represented by the ``alock`` object. If blocking is
89+
`True`, the default, the method will block until locks on *all* the
90+
identifiers have been acquired.
91+
92+
If blocking is `False`, acquisition may not block, and success will be
93+
indicated by the returned object: `True` if *all* lock identifiers were
94+
acquired and `False` if any of the lock identifiers could not be acquired.
95+
96+
``alock.release()``
97+
Release the advisory locks represented by the ``alock`` object. If the lock
98+
has not been acquired, a `RuntimeError` will be raised.
99+
100+
``alock.locked()``
101+
Returns a boolean describing whether the locks are held or not. This will
102+
return `False` if the lock connection has been closed.
103+
104+
``alock.__enter__()``
105+
Alias to ``acquire``; context manager protocol. Always blocking.
106+
107+
``alock.__exit__(typ, val, tb)``
108+
Alias to ``release``; context manager protocol.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
Commands
2+
********
3+
4+
This chapter discusses the usage of the available console scripts.
5+
6+
7+
postgresql.bin.pg_python
8+
========================
9+
10+
The ``pg_python`` command provides a simple way to write Python scripts against a
11+
single target database. It acts like the regular Python console command, but
12+
takes standard PostgreSQL options as well to specify the client parameters
13+
to make establish connection with. The Python environment is then augmented
14+
with the following built-ins:
15+
16+
``db``
17+
The PG-API connection object.
18+
19+
``xact``
20+
``db.xact``, the transaction creator.
21+
22+
``settings``
23+
``db.settings``
24+
25+
``prepare``
26+
``db.prepare``, the statement creator.
27+
28+
``proc``
29+
``db.proc``
30+
31+
``do``
32+
``db.do``, execute a single DO statement.
33+
34+
``sqlexec``
35+
``db.execute``, execute multiple SQL statements (``None`` is always returned)
36+
37+
pg_python Usage
38+
---------------
39+
40+
Usage: postgresql.bin.pg_python [connection options] [script] ...
41+
42+
Options:
43+
--unix=UNIX path to filesystem socket
44+
--ssl-mode=SSLMODE SSL requirement for connectivity: require, prefer,
45+
allow, disable
46+
-s SETTINGS, --setting=SETTINGS
47+
run-time parameters to set upon connecting
48+
-I PQ_IRI, --iri=PQ_IRI
49+
database locator string
50+
[pq://user:password@host:port/database?setting=value]
51+
-h HOST, --host=HOST database server host
52+
-p PORT, --port=PORT database server port
53+
-U USER, --username=USER
54+
user name to connect as
55+
-W, --password prompt for password
56+
-d DATABASE, --database=DATABASE
57+
database's name
58+
--pq-trace=PQ_TRACE trace PQ protocol transmissions
59+
-C PYTHON_CONTEXT, --context=PYTHON_CONTEXT
60+
Python context code to run[file://,module:,<code>]
61+
-m PYTHON_MAIN Python module to run as script(__main__)
62+
-c PYTHON_MAIN Python expression to run(__main__)
63+
--version show program's version number and exit
64+
--help show this help message and exit
65+
66+
67+
Interactive Console Backslash Commands
68+
--------------------------------------
69+
70+
Inspired by ``psql``::
71+
72+
>>> \?
73+
Backslash Commands:
74+
75+
\? Show this help message.
76+
\E Edit a file or a temporary script.
77+
\e Edit and Execute the file directly in the context.
78+
\i Execute a Python script within the interpreter's context.
79+
\set Configure environment variables. \set without arguments to show all
80+
\x Execute the Python command within this process.
81+
82+
83+
pg_python Examples
84+
------------------
85+
86+
Module execution taking advantage of the new built-ins::
87+
88+
$ python3 -m postgresql.bin.pg_python -h localhost -W -m timeit "prepare('SELECT 1').first()"
89+
Password for pg_python[pq://jwp@localhost:5432]:
90+
1000 loops, best of 3: 1.35 msec per loop
91+
92+
$ python3 -m postgresql.bin.pg_python -h localhost -W -m timeit -s "ps=prepare('SELECT 1')" "ps.first()"
93+
Password for pg_python[pq://jwp@localhost:5432]:
94+
1000 loops, best of 3: 442 usec per loop
95+
96+
Simple interactive usage::
97+
98+
$ python3 -m postgresql.bin.pg_python -h localhost -W
99+
Password for pg_python[pq://jwp@localhost:5432]:
100+
>>> ps = prepare('select 1')
101+
>>> ps.first()
102+
1
103+
>>> c = ps()
104+
>>> c.read()
105+
[(1,)]
106+
>>> ps.close()
107+
>>> import sys
108+
>>> sys.exit(0)
109+
110+
111+
postgresql.bin.pg_dotconf
112+
=========================
113+
114+
pg_dotconf is used to modify a PostgreSQL cluster's configuration file.
115+
It provides a means to apply settings specified from the command line and from a
116+
file referenced using the ``-f`` option.
117+
118+
.. warning::
119+
``include`` directives in configuration files are *completely* ignored. If
120+
modification of an included file is desired, the command must be applied to
121+
that specific file.
122+
123+
124+
pg_dotconf Usage
125+
----------------
126+
127+
Usage: postgresql.bin.pg_dotconf [--stdout] [-f filepath] postgresql.conf ([param=val]|[param])*
128+
129+
Options:
130+
--version show program's version number and exit
131+
-h, --help show this help message and exit
132+
-f SETTINGS, --file=SETTINGS
133+
A file of settings to *apply* to the given
134+
"postgresql.conf"
135+
--stdout Redirect the product to standard output instead of
136+
writing back to the "postgresql.conf" file
137+
138+
139+
Examples
140+
--------
141+
142+
Modifying a simple configuration file::
143+
144+
$ echo "setting = value" >pg.conf
145+
146+
# change 'setting'
147+
$ python3 -m postgresql.bin.pg_dotconf pg.conf setting=newvalue
148+
149+
$ cat pg.conf
150+
setting = 'newvalue'
151+
152+
# new settings are appended to the file
153+
$ python3 -m postgresql.bin.pg_dotconf pg.conf another_setting=value
154+
$ cat pg.conf
155+
setting = 'newvalue'
156+
another_setting = 'value'
157+
158+
# comment a setting
159+
$ python3 -m postgresql.bin.pg_dotconf pg.conf another_setting
160+
161+
$ cat pg.conf
162+
setting = 'newvalue'
163+
#another_setting = 'value'
164+
165+
When a setting is given on the command line, it must been seen as one argument
166+
to the command, so it's *very* important to avoid invocations like::
167+
168+
$ python3 -m postgresql.bin.pg_dotconf pg.conf setting = value
169+
ERROR: invalid setting, '=' after 'setting'
170+
HINT: Settings must take the form 'setting=value' or 'setting_name_to_comment'. Settings must also be received as a single argument.

0 commit comments

Comments
 (0)