Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I understand it you are writing functions to execute from your client code. The entirety of a function execution will always be within one transaction, whether or not you explicitly <code>BEGIN</code> a transaction, although you can use subtransactions in plpgsql by coding a <code>BEGIN</code>/<code>END</code> block with an <code>EXCEPTION</code> clause. Such subtransactions are not free, so you want to use them only when really needed.</p> <p>Where invariants necessary to enforce business rules can be stated declaratively, that is almost always better than enforcing them with imperative code in a trigger. <code>CHECK</code> constraints, <code>PRIMARY KEY</code> constraints, <code>FOREIGN KEY</code> constraints, <code>UNIQUE</code> constraints, and (a feature unique to PostgreSQL) <code>EXCLUSION</code> constraints should all be used instead of trigger code when they cover the use case.</p> <p>If you are using PostgreSQL version 9.1 or later, the biggest decision to make before you get started is what strategy you will use for dealing with race conditions between concurrent transactions. The two main choices are whether to try to track all possible conflicts and deal with the issues in application code, or use just serializable transactions (you can make that default in your postgresql.conf file) and let PostgreSQL sort things out. The best write-up I've seen on the former is here: <a href="http://www.postgresql.org/files/developer/concurrency.pdf" rel="nofollow">http://www.postgresql.org/files/developer/concurrency.pdf</a> while links regarding the latter are:</p> <p><a href="http://www.postgresql.org/docs/current/interactive/mvcc.html" rel="nofollow">http://www.postgresql.org/docs/current/interactive/mvcc.html</a></p> <p><a href="http://wiki.postgresql.org/wiki/SSI" rel="nofollow">http://wiki.postgresql.org/wiki/SSI</a></p> <p>If you use <code>SERIALIZABLE</code> or even <code>REPEATABLE READ</code> transactions you should run queries in some way that provides generalized handling of serialization failures; that is, if the transaction throws an exception with SQLSTATE set to '40001' or '40P01' you should rollback the failed transaction and retry it from the start. You don't want to embed the logic for that in application code each place you run a query. Many development frameworks these days allow transactional annotations which make this easy to implement. We only use serializable transactions at our shop, and it really does simplify things to know that if you can show that a transaction does the right thing by itself, it will do the right thing in any mix of transactions without any special coding to ensure that (beyond handling serialization failures as mentioned above).</p> <p>Note that due to the use of Serializable Snapshot Isolation the performance of PostgreSQL using serializable transactions is very close to less strict isolation levels for many workloads; if you've been shying away from the idea because of fears about blocking and deadlocks -- don't. Serializable transactions in PostgreSQL 9.1 don't have those problems like databases using strict two-phase locking (S2PL) do.</p> <p>Full disclosure: In a joint effort with Dan R.K. Ports of MIT, and based on papers by Michael J. Cahill et all of Sidney University, I helped implement Serializable Snapshot Isolation for PostgreSQL version 9.1, because we needed the functionality at my workplace. I don't get any financial benefit from anyone else using it, but I personally feel this is generally the way to go. That is more, I hope, the <em>reason</em> I worked on the feature rather than a <em>result</em> of having worked on the feature.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload