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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:55:06+00:00 2026-06-09T21:55:06+00:00

I have a table foo_bar and another table spam_eggs with a fb foreign key

  • 0

I have a table foo_bar and another table spam_eggs with a fb foreign key pointing to foo_bar. spam_eggs rows are cascade deleted when their related spam_eggs.fb are deleted.

I’m working with PostgreSQL.

In a transaction I have used SELECT... FOR UPDATE to lock a spam_eggs row. In the duration of this transaction, another transaction has attempted to DELETE FROM... the related foo_bar of my locked row. Will this trigger an error, or will my locked row cause the query to block until the end of my original update transaction?

  • 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-09T21:55:07+00:00Added an answer on June 9, 2026 at 9:55 pm

    Try it and see. Open psql and do some setup:

    CREATE TABLE foo_bar(id integer primary key);
    CREATE TABLE spam_eggs(
         foo_bar_id integer not null references foo_bar(id) on delete cascade
    );
    INSERT INTO foo_bar (id) VALUES (1),(2),(3),(4);
    INSERT INTO spam_eggs(foo_bar_id) VALUES (1),(2),(3),(4);
    

    then open another psql connection. BEGIN a transaction in both of them.

    1. In the first (old) session, run SELECT 1 FROM spam_eggs WHERE foo_bar_id = 4 FOR UPDATE;
    2. In the second (new) session, run DELETE FROM foo_bar WHERE id = 4;

    You will see that the second statement blocks on the first. That’s because the DELETE on foo_bar cascades to spam_eggs and attempts to lock the row with the foreign key reference so it can delete it. That lock blocks on the lock held by the SELECT ... FOR SHARE.

    In general, try to test in all these circumstances:

    • tx’s are BEGIN ISOLATION LEVEL READ COMMITTED and first issues a ROLLBACK
    • tx’s are BEGIN ISOLATION LEVEL READ COMMITTED and first issues a COMMIT
    • tx’s are BEGIN ISOLATION LEVEL SERIALIZABLE and first issues a ROLLBACK
    • tx’s are BEGIN ISOLATION LEVEL SERIALIZABLE and first issues a COMMIT

    to make sure you know what to expect. It’s also good for your learning if you reason through what you expect to happen before testing it.

    In this case the READ COMMITTED and SERIALIZABLE isolation levels will behave the same. If you actually do an UPDATE after your SELECT ... FOR UPDATE and then COMMIT then they’ll behave differently, though; the READ COMMITTED version will DELETE successfully, while the SERIALIZABLE version will fail with:

    regress=# BEGIN ISOLATION LEVEL SERIALIZABLE;
    regress=# DELETE FROM foo_bar WHERE id = 4;                                                                                                                                                                                                    
    ERROR:  could not serialize access due to concurrent update                                                                                                                                                        
    CONTEXT:  SQL statement "DELETE FROM ONLY "public"."spam_eggs" WHERE $1 OPERATOR(pg_catalog.=) "foo_bar_id""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that is linked to an another table with a foreign
I have an MS Access table that has a many-to-one key to another one,
Suppose we have two tables Foo and Bar. I have an association table Foo_Bar
I have a table of information in a SQL Server database and another table
I have a bag whose structure looks like this: <bag name=foo fetch=select table=table_of_foos> <key
We have a large MyISAM table to which rows get inserted to the bottom
I have a table with a composite key like this: ========TABLE======== key_a_col | key_b_col
I have a MySQL table like so; foo bar 1 21 23 17 31
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
Let's say I have a table Foo . This table has the columns ID

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.