transactions-multi-conn.td 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. #
  10. # Test that transactions work properly
  11. #
  12. > CREATE SECRET pgpass AS 'postgres'
  13. > CREATE CONNECTION pg TO POSTGRES (
  14. HOST postgres,
  15. DATABASE postgres,
  16. USER postgres,
  17. PASSWORD SECRET pgpass
  18. )
  19. $ postgres-execute connection=postgres://postgres:postgres@postgres
  20. $ postgres-connect name=conn1 url=postgres://postgres:postgres@postgres
  21. $ postgres-connect name=conn2 url=postgres://postgres:postgres@postgres
  22. $ postgres-connect name=conn3 url=postgres://postgres:postgres@postgres
  23. $ postgres-execute connection=conn1
  24. ALTER USER postgres WITH replication;
  25. DROP SCHEMA IF EXISTS public CASCADE;
  26. DROP PUBLICATION IF EXISTS mz_source;
  27. CREATE SCHEMA public;
  28. CREATE TABLE t1 (a INT);
  29. CREATE TABLE t2 (a INT);
  30. CREATE TABLE t3 (a INT);
  31. ALTER TABLE t1 REPLICA IDENTITY FULL;
  32. ALTER TABLE t2 REPLICA IDENTITY FULL;
  33. ALTER TABLE t3 REPLICA IDENTITY FULL;
  34. CREATE PUBLICATION mz_source FOR ALL TABLES;
  35. > CREATE SOURCE mz_source FROM POSTGRES CONNECTION pg (PUBLICATION 'mz_source');
  36. > CREATE TABLE t1 FROM SOURCE mz_source (REFERENCE t1);
  37. > CREATE TABLE t2 FROM SOURCE mz_source (REFERENCE t2);
  38. > CREATE TABLE t3 FROM SOURCE mz_source (REFERENCE t3);
  39. > SELECT count(*) FROM t1;
  40. 0
  41. > SELECT count(*) FROM t2;
  42. 0
  43. > SELECT count(*) FROM t3;
  44. 0
  45. $ postgres-execute connection=conn1
  46. BEGIN;
  47. INSERT INTO t1 VALUES (1000);
  48. INSERT INTO t2 VALUES (1000);
  49. INSERT INTO t3 VALUES (1000);
  50. $ postgres-execute connection=conn2
  51. BEGIN;
  52. INSERT INTO t1 VALUES (2000);
  53. INSERT INTO t2 VALUES (2000);
  54. INSERT INTO t3 VALUES (2000);
  55. $ postgres-execute connection=conn3
  56. BEGIN;
  57. INSERT INTO t1 VALUES (3000);
  58. INSERT INTO t2 VALUES (3000);
  59. INSERT INTO t3 VALUES (3000);
  60. $ postgres-execute connection=conn1
  61. COMMIT;
  62. BEGIN;
  63. $ postgres-execute connection=conn3
  64. COMMIT;
  65. BEGIN;
  66. > SELECT * FROM t1;
  67. 1000
  68. 3000
  69. > SELECT * FROM t2;
  70. 1000
  71. 3000
  72. > SELECT * FROM t3;
  73. 1000
  74. 3000
  75. $ postgres-execute connection=conn2
  76. COMMIT;
  77. BEGIN;
  78. > SELECT * FROM t1;
  79. 1000
  80. 2000
  81. 3000
  82. # delete and insert statements cannot be done in multiple transactions on the same table even with fine-grained where condition
  83. $ postgres-execute connection=conn1
  84. INSERT INTO t1 VALUES (1001);
  85. INSERT INTO t2 VALUES (1001);
  86. INSERT INTO t3 VALUES (1001);
  87. $ postgres-execute connection=conn2
  88. INSERT INTO t1 VALUES (2001);
  89. INSERT INTO t2 VALUES (2001);
  90. INSERT INTO t3 VALUES (2001);
  91. $ postgres-execute connection=conn3
  92. INSERT INTO t1 VALUES (3001);
  93. INSERT INTO t2 VALUES (3001);
  94. INSERT INTO t3 VALUES (3001);
  95. $ postgres-execute connection=conn1
  96. COMMIT;
  97. BEGIN;
  98. $ postgres-execute connection=conn3
  99. COMMIT;
  100. BEGIN;
  101. > SELECT * FROM t1;
  102. 1000
  103. 1001
  104. 2000
  105. 3000
  106. 3001
  107. > SELECT * FROM t2;
  108. 1000
  109. 1001
  110. 2000
  111. 3000
  112. 3001
  113. > SELECT * FROM t3;
  114. 1000
  115. 1001
  116. 2000
  117. 3000
  118. 3001
  119. $ postgres-execute connection=conn2
  120. COMMIT;
  121. BEGIN;
  122. INSERT INTO t1 VALUES (2002);
  123. $ postgres-execute connection=conn1
  124. INSERT INTO t1 VALUES (1002);
  125. DELETE FROM t2 WHERE a = 2000;
  126. $ postgres-execute connection=conn2
  127. DELETE FROM t3 WHERE a = 2000;
  128. $ postgres-execute connection=conn1
  129. COMMIT;
  130. BEGIN;
  131. > SELECT * FROM t1;
  132. 1000
  133. 1001
  134. 1002
  135. 2000
  136. 2001
  137. 3000
  138. 3001
  139. > SELECT * FROM t2;
  140. 1000
  141. 1001
  142. 2001
  143. 3000
  144. 3001
  145. > SELECT * FROM t3;
  146. 1000
  147. 1001
  148. 2000
  149. 2001
  150. 3000
  151. 3001
  152. $ postgres-execute connection=conn2
  153. COMMIT;
  154. BEGIN;
  155. > SELECT * FROM t1;
  156. 1000
  157. 1001
  158. 1002
  159. 2000
  160. 2001
  161. 2002
  162. 3000
  163. 3001
  164. > SELECT * FROM t2;
  165. 1000
  166. 1001
  167. 2001
  168. 3000
  169. 3001
  170. > SELECT * FROM t3;
  171. 1000
  172. 1001
  173. 2001
  174. 3000
  175. 3001