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

  • Home
  • SEARCH
  • 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 9096249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:47:55+00:00 2026-06-16T23:47:55+00:00

Suppose I have this foo table in a PostgreSQL 9.1 database: CREATE TABLE foo

  • 0

Suppose I have this foo table in a PostgreSQL 9.1 database:

CREATE TABLE foo
(
  bar integer,
  flg_deleted boolean
);

Along with this vwfoo view:

CREATE VIEW vwfoo AS
  SELECT bar
  FROM foo
  WHERE flg_deleted = false;

Also assume I have an application running a couple of short-lived transactions per second, using vwfoo.

Now, I want to add a column baz to foo and I want baz to be in vwfoo too. But, of course, I don’t want my application to get any errors because of those changes.

If I execute the following steps (in a single transaction) to perform the desired change:

  1. Drop vwfoo.
  2. Add column baz to foo.
  3. Create vwfoo again (now including baz).

Do I get the desired behavior (no errors in the application)?

Will there be an exclusive lock being held on vwfoo during the whole transaction (that’s what I want)?

Is it possible that any transaction will try to use the view between steps 1 and 3 and then fail (instead of just blocking, waiting for a lock)?

Does the “identity” of vwfoo change when it’s recreated? In other words: is it possible that any transaction will try to use the view between steps 1 and 3, block, resume after step 3 and then fail because the view was recreated?

Thanks.

  • 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-16T23:47:56+00:00Added an answer on June 16, 2026 at 11:47 pm

    A quick test suggests that this will cause problems with your application. To recreate, create two connections (A and B), then run:

    A: BEGIN;
    A: DROP VIEW vwfoo;
    B: SELECT * FROM vwfoo;
    (B blocks… )
    A: CREATE VIEW vwfoo AS SELECT * FROM foo;
    A: COMMIT;
    (B yields:
        ERROR:  could not open relation with OID 326418
        LINE 1: SELECT * FROM vwfoo
    )
    

    Instead, you should do this kind of atomic swapping by renaming the views:

    A: CREATE VIEW vwfoo_new AS SELECT * FROM foo;
    A: BEGIN;
    A: ALTER VIEW vwfoo RENAME TO vwfoo_old;
    B: SELECT * FROM vwfoo;
    (B blocks…)
    A: ALTER VIEW vwfoo_new RENAME TO vwfoo;
    A: COMMIT;
    (B completes as expected)
    A: DROP TABLE vwfoo_old;
    

    This will work as expected (and you won’t need to run a (comparatively) expensive DROP TABLE inside the transaction!)

    Edit: You can use the same strategy to solve your “real” problem, too:

    ALTER TABLE foo ADD COLUMN bar_new TEXT;
    UPDATE foo SET bar_new = bar;
    CREATE VIEW vwfoo_new AS SELECT bar_new AS bar FROM foo;
    … do the view switcheroo …
    DROP VIEW vwfoo_old;
    BEGIN;
    ALTER TABLE foo RENAME bar TO bar_old;
    ALTER TABLE foo RENAME bar_new TO bar;
    COMMIT;
    ALTER TABLE foo DROP COLUMN bar_old;
    

    The view referencing bar_new will be correctly updated, too:

    # \d vwfoo
    View definition:
     SELECT foo.a
       FROM foo
      WHERE foo.b = false;
    # ALTER foo RENAME a TO new_a;
    ALTER TABLE
    # \d vwfoo
    View definition:
     SELECT foo.newa AS a
       FROM foo
      WHERE foo.b = false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a database table with two fields, foo and bar. Neither of
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
Suppose I have a table, Foo, that looks like this: ID | Name |
Suppose I have this directory: foo.c // last changed rev = 4800 bar.c //
Suppose I have this folder structure: /projectroot/foo/bar/subfoo/foo.txt Now I change something in foo.txt a
Suppose i have this class: class Foo Public Property a() As Integer Private _l
Suppose we have this environment: mkdir -p /tmp/foo/bar/ export TEST=/tmp/foo Out of the box,
Suppose you have this SVN repository structure Root Foo Project1 Project2 Bar Project3 And
Suppose that I have table create table foo ( insert_current timestamp default now(), insert_previous
Suppose I have this feature branch foo. Now I want to merge it back

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.