Postgres serializable transactions not enforcing isolation when joins are involved Announcing...

How to install press fit bottom bracket into new frame

How to write the following sign?

How to react to hostile behavior from a senior developer?

A term for a woman complaining about things/begging in a cute/childish way

What does it mean that physics no longer uses mechanical models to describe phenomena?

How to compare two different files line by line in unix?

Why is it faster to reheat something than it is to cook it?

Do I really need to have a message in a novel to appeal to readers?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

Why is Nikon 1.4g better when Nikon 1.8g is sharper?

What is the font for "b" letter?

How to tell that you are a giant?

Is there a kind of relay only consumes power when switching?

Trademark violation for app?

What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?

Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode

An adverb for when you're not exaggerating

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

NumericArray versus PackedArray in MMA12

Project Euler #1 in C++

SF book about people trapped in a series of worlds they imagine

Performance gap between vector<bool> and array

How fail-safe is nr as stop bytes?



Postgres serializable transactions not enforcing isolation when joins are involved



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)How do I implement insert-if-not-found for transactions at serializable isolation level?SELECT INTO OUTFILE vs INSERT INTO … SELECTExclusively access a table and prevent other queries, even read queriespostgresql insert from select query, plus static valuesUnexpected failure in transactions with isolation level serializableIsolation level issueIn PSQL, is it possible to insert rows in two different tables with not-nullable foreign key columns referencing each other?Serializable range deadlocksIs it possible to override a ReadCommittedSnapshot isolation level with ReadCommitted isolation level for a particular transaction?Understanding repeatable read isolation level





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















The Postgres manual says that transactions in SERIALIZABLE mode behave as though they were executed serially. However, this example seems to refute that. Am I missing something?



Suppose the application using transaction 1 wants to insert a row into B referring to the row in A with id 1, but only if no other rows in B already refer to that row in A. (I know you can do this properly with just UPDATE ... WHERE, but in more complicated applications, that's hard to deal with.)



% setup
CREATE TABLE a (id serial primary key);
CREATE TABLE b (a int REFERENCES a(id), val text);
INSERT INTO a(id) VALUES (1);


% transaction 1:
BEGIN;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1;
% Leaves transaction open...

% transaction 2:
BEGIN;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
INSERT INTO b(a, val) VALUES (1, 'foo'); % Shouldn't succeed but does.
COMMIT;

% transaction 1:
SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 0 rows. Ok, let's insert...
INSERT INTO b(a, val) VALUES (1, 'foo');
SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 1 rows. As expected.
COMMIT;

% no longer in a transaction:
SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 2 rows?!


If I instead do in transaction 1 SELECT... FOR UPDATE, I get the expected behavior. Session 2 blocks when it tries to insert. But strangely, this doesn't happen if I leave out the FK in table B (REFERENCES a(id)), which is bad because I can imagine cases where there'd be no FK.









share







New contributor




sudont is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    0















    The Postgres manual says that transactions in SERIALIZABLE mode behave as though they were executed serially. However, this example seems to refute that. Am I missing something?



    Suppose the application using transaction 1 wants to insert a row into B referring to the row in A with id 1, but only if no other rows in B already refer to that row in A. (I know you can do this properly with just UPDATE ... WHERE, but in more complicated applications, that's hard to deal with.)



    % setup
    CREATE TABLE a (id serial primary key);
    CREATE TABLE b (a int REFERENCES a(id), val text);
    INSERT INTO a(id) VALUES (1);


    % transaction 1:
    BEGIN;
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
    SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1;
    % Leaves transaction open...

    % transaction 2:
    BEGIN;
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
    INSERT INTO b(a, val) VALUES (1, 'foo'); % Shouldn't succeed but does.
    COMMIT;

    % transaction 1:
    SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 0 rows. Ok, let's insert...
    INSERT INTO b(a, val) VALUES (1, 'foo');
    SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 1 rows. As expected.
    COMMIT;

    % no longer in a transaction:
    SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 2 rows?!


    If I instead do in transaction 1 SELECT... FOR UPDATE, I get the expected behavior. Session 2 blocks when it tries to insert. But strangely, this doesn't happen if I leave out the FK in table B (REFERENCES a(id)), which is bad because I can imagine cases where there'd be no FK.









    share







    New contributor




    sudont is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      The Postgres manual says that transactions in SERIALIZABLE mode behave as though they were executed serially. However, this example seems to refute that. Am I missing something?



      Suppose the application using transaction 1 wants to insert a row into B referring to the row in A with id 1, but only if no other rows in B already refer to that row in A. (I know you can do this properly with just UPDATE ... WHERE, but in more complicated applications, that's hard to deal with.)



      % setup
      CREATE TABLE a (id serial primary key);
      CREATE TABLE b (a int REFERENCES a(id), val text);
      INSERT INTO a(id) VALUES (1);


      % transaction 1:
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
      SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1;
      % Leaves transaction open...

      % transaction 2:
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
      INSERT INTO b(a, val) VALUES (1, 'foo'); % Shouldn't succeed but does.
      COMMIT;

      % transaction 1:
      SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 0 rows. Ok, let's insert...
      INSERT INTO b(a, val) VALUES (1, 'foo');
      SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 1 rows. As expected.
      COMMIT;

      % no longer in a transaction:
      SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 2 rows?!


      If I instead do in transaction 1 SELECT... FOR UPDATE, I get the expected behavior. Session 2 blocks when it tries to insert. But strangely, this doesn't happen if I leave out the FK in table B (REFERENCES a(id)), which is bad because I can imagine cases where there'd be no FK.









      share







      New contributor




      sudont is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      The Postgres manual says that transactions in SERIALIZABLE mode behave as though they were executed serially. However, this example seems to refute that. Am I missing something?



      Suppose the application using transaction 1 wants to insert a row into B referring to the row in A with id 1, but only if no other rows in B already refer to that row in A. (I know you can do this properly with just UPDATE ... WHERE, but in more complicated applications, that's hard to deal with.)



      % setup
      CREATE TABLE a (id serial primary key);
      CREATE TABLE b (a int REFERENCES a(id), val text);
      INSERT INTO a(id) VALUES (1);


      % transaction 1:
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
      SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1;
      % Leaves transaction open...

      % transaction 2:
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
      INSERT INTO b(a, val) VALUES (1, 'foo'); % Shouldn't succeed but does.
      COMMIT;

      % transaction 1:
      SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 0 rows. Ok, let's insert...
      INSERT INTO b(a, val) VALUES (1, 'foo');
      SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 1 rows. As expected.
      COMMIT;

      % no longer in a transaction:
      SELECT a.* FROM a, b WHERE b.a = a.id AND a.id = 1; % 2 rows?!


      If I instead do in transaction 1 SELECT... FOR UPDATE, I get the expected behavior. Session 2 blocks when it tries to insert. But strangely, this doesn't happen if I leave out the FK in table B (REFERENCES a(id)), which is bad because I can imagine cases where there'd be no FK.







      postgresql isolation-level





      share







      New contributor




      sudont is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      sudont is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      sudont is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 mins ago









      sudontsudont

      1




      1




      New contributor




      sudont is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      sudont is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      sudont is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          sudont is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f235237%2fpostgres-serializable-transactions-not-enforcing-isolation-when-joins-are-involv%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          sudont is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          sudont is a new contributor. Be nice, and check out our Code of Conduct.













          sudont is a new contributor. Be nice, and check out our Code of Conduct.












          sudont is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f235237%2fpostgres-serializable-transactions-not-enforcing-isolation-when-joins-are-involv%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Parapolítica Índice Antecedentes El escándalo Proceso judicial Consecuencias Véase...

          How to remove border from elements in the last row?Targeting flex items on the last rowHow to vertically wrap...

          Tecnologías entrañables Índice Antecedentes Desarrollo Tecnologías Entrañables en la...