Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7763117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:35:04+00:00 2026-06-01T14:35:04+00:00

While creating complex procedures the whole function is treated as a transaction or If

  • 0

While creating complex procedures the whole function is treated as a transaction or If I just put the whole function in a begin ... end. so If any of the intermediate queries fail there will be no problem in system. So do I need any error reporting ? or IF ... ELSE checking for error reporting ? (assuming foreign key/unique/etc.. constraints are properly done)

Do/should I need to raise an error by myself if duplicate key value violates unique constraint or I should rely on database engine ?

are there any patterns to follow while writing stored procedures while developing Business logic into it ?

I am using PostgreSql and PL/PgSQL and I used to do ORM before

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:35:05+00:00Added an answer on June 1, 2026 at 2:35 pm

    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 BEGIN a transaction, although you can use subtransactions in plpgsql by coding a BEGIN/END block with an EXCEPTION clause. Such subtransactions are not free, so you want to use them only when really needed.

    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. CHECK constraints, PRIMARY KEY constraints, FOREIGN KEY constraints, UNIQUE constraints, and (a feature unique to PostgreSQL) EXCLUSION constraints should all be used instead of trigger code when they cover the use case.

    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: http://www.postgresql.org/files/developer/concurrency.pdf while links regarding the latter are:

    http://www.postgresql.org/docs/current/interactive/mvcc.html

    http://wiki.postgresql.org/wiki/SSI

    If you use SERIALIZABLE or even REPEATABLE READ 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).

    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.

    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 reason I worked on the feature rather than a result of having worked on the feature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While creating function imports in Model Browser, Get Column Information does not returns any
While creating the setup for VB.net application I am getting the following warning: Warning
while creating a thread in java, there is two ways such as Extending threads
While creating JavaScript with ASP.NET MVC I noticed several scope warnings and realized that
While creating a command line tool (Mac OS X) project in XCode, one has
While creating an online shop application using play-1.2.4 ,I ran into some problems with
While creating sharing links for facebook, you can personalize a sharing link by doing
While creating my horizontal menu using the <ul><li>....</li></ul> I came across a few behaviors
I am facing a problem while creating a static cursor in DB2. This is
Hi I am getting the following erro while creating new Account in CRM 2011.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.