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 9204515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:43:40+00:00 2026-06-17T23:43:40+00:00

I am seeking clarification of how to ensure an atomic transaction in a plpgsql

  • 0

I am seeking clarification of how to ensure an atomic transaction in a plpgsql function, and where the isolation level is set for this particular change to the database.

In the plpgsql function shown below, I want to make sure that BOTH the deletion AND the insertion succeed. I am getting an error when I try to wrap them in a single transaction:

ERROR:  cannot begin/end transactions in PL/pgSQL

What happens during execution of the function below if another user has added a default behavior for circumstances (‘RAIN’, ‘NIGHT’, ’45MPH’) after this function has deleted the custom row but before it has had a chance to insert the custom row? Is there an implicit transaction wrapping the insert and delete so that both are rolled back if another user has changed either of the rows referenced by this function? Can I set the isolation level for this function?

create function foo(v_weather varchar(10), v_timeofday varchar(10), v_speed varchar(10),
   v_behavior varchar(10))
   returns setof CUSTOMBEHAVIOR
   as $body$
   begin
      -- run-time error if either of these lines is un-commented

      -- start transaction ISOLATION LEVEL READ COMMITTED;
      -- or, alternatively, set transaction ISOLATION LEVEL READ COMMITTED;

      delete from CUSTOMBEHAVIOR 
      where weather = 'RAIN' and timeofday = 'NIGHT' and speed= '45MPH' ;

      -- if there is no default behavior insert a custom behavior

      if not exists
        (select id from DEFAULTBEHAVIOR where a = 'RAIN' and b = 'NIGHT' and c= '45MPH') then
         insert into CUSTOMBEHAVIOR
           (weather, timeofday, speed, behavior)
         values
           (v_weather, v_timeofday, v_speed, v_behavior);
      end if;

      return QUERY
      select * from CUSTOMBEHAVIOR where ...   ;

      -- commit;
   end
   $body$  LANGUAGE plpgsql;
  • 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-17T23:43:41+00:00Added an answer on June 17, 2026 at 11:43 pm

    A PL/pgSQL function automatically runs inside a transaction. It all succeeds or it all fails. See:

    • Do stored procedures run in database transaction in Postgres?

    If you need to, you can catch an exception that theoretically might occur (but is very unlikely).
    Details on trapping errors in the manual.

    Your function reviewed and simplified:

    CREATE FUNCTION foo(v_weather text
                      , v_timeofday text
                      , v_speed text
                      , v_behavior text)
      RETURNS SETOF custombehavior
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       DELETE FROM custombehavior
       WHERE  weather   = 'RAIN'
       AND    timeofday = 'NIGHT'
       AND    speed     = '45MPH';
    
       INSERT INTO custombehavior
             (  weather,   timeofday,   speed,   behavior)
       SELECT v_weather, v_timeofday, v_speed, v_behavior
       WHERE  NOT EXISTS (
          SELECT FROM defaultbehavior
          WHERE  a = 'RAIN'
          AND    b = 'NIGHT'
          AND    c = '45MPH'
          );
    
       RETURN QUERY
       SELECT * FROM custombehavior WHERE ... ;
    END
    $func$;
    

    If you actually need to begin/end transactions like indicated in the title look to SQL procedures in Postgres 11 or later (CREATE PROCEDURE). (But procedures cannot currently return a set.) See:

    • In PostgreSQL, what is the difference between a “Stored Procedure” and other types of functions?

    Also, you may want to replace your INSERT & DELETE with "UPSERT" to safely handle race conditions. See:

    • Insert, on duplicate update in PostgreSQL?
    • How to use RETURNING with ON CONFLICT in PostgreSQL?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeking further clarification after seeing What is responsible for releasing NSWindowController objects? I'm
Seeking help to design a layout as shown here: http://docs.google.com/Doc?docid=0AQhgDtGvE2HgZGZ6cmtua185MTd0eGdyZmc&hl=en The major challenge I
Seeking clarification/confirmation: if I understand correctly, the following considerations about orphaned users apply only
This may be a very basic question but it is one I am seeking
Seeking a more elegant solution I have this piece of code, I just use
i was seeking for a solution like this in stackoverflow and in google but
Seeking help from friends I have loaded 3 tables from database to DataSet using
Seeking feedback on best to approach this: I am building a study guide BBapp.
I'm seeking clarification on whether to put code in a controller, an entity or
Seeking to create an event handler that registers a callback and iterates based on

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.