transactions-multi-conn.td 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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
  36. FROM POSTGRES CONNECTION pg (PUBLICATION 'mz_source')
  37. FOR ALL TABLES;
  38. > SELECT count(*) FROM t1;
  39. 0
  40. > SELECT count(*) FROM t2;
  41. 0
  42. > SELECT count(*) FROM t3;
  43. 0
  44. $ postgres-execute connection=conn1
  45. BEGIN;
  46. INSERT INTO t1 VALUES (1000);
  47. INSERT INTO t2 VALUES (1000);
  48. INSERT INTO t3 VALUES (1000);
  49. $ postgres-execute connection=conn2
  50. BEGIN;
  51. INSERT INTO t1 VALUES (2000);
  52. INSERT INTO t2 VALUES (2000);
  53. INSERT INTO t3 VALUES (2000);
  54. $ postgres-execute connection=conn3
  55. BEGIN;
  56. INSERT INTO t1 VALUES (3000);
  57. INSERT INTO t2 VALUES (3000);
  58. INSERT INTO t3 VALUES (3000);
  59. $ postgres-execute connection=conn1
  60. COMMIT;
  61. BEGIN;
  62. $ postgres-execute connection=conn3
  63. COMMIT;
  64. BEGIN;
  65. > SELECT * FROM t1;
  66. 1000
  67. 3000
  68. > SELECT * FROM t2;
  69. 1000
  70. 3000
  71. > SELECT * FROM t3;
  72. 1000
  73. 3000
  74. $ postgres-execute connection=conn2
  75. COMMIT;
  76. BEGIN;
  77. > SELECT * FROM t1;
  78. 1000
  79. 2000
  80. 3000
  81. # delete and insert statements cannot be done in multiple transactions on the same table even with fine-grained where condition
  82. $ postgres-execute connection=conn1
  83. INSERT INTO t1 VALUES (1001);
  84. INSERT INTO t2 VALUES (1001);
  85. INSERT INTO t3 VALUES (1001);
  86. $ postgres-execute connection=conn2
  87. INSERT INTO t1 VALUES (2001);
  88. INSERT INTO t2 VALUES (2001);
  89. INSERT INTO t3 VALUES (2001);
  90. $ postgres-execute connection=conn3
  91. INSERT INTO t1 VALUES (3001);
  92. INSERT INTO t2 VALUES (3001);
  93. INSERT INTO t3 VALUES (3001);
  94. $ postgres-execute connection=conn1
  95. COMMIT;
  96. BEGIN;
  97. $ postgres-execute connection=conn3
  98. COMMIT;
  99. BEGIN;
  100. > SELECT * FROM t1;
  101. 1000
  102. 1001
  103. 2000
  104. 3000
  105. 3001
  106. > SELECT * FROM t2;
  107. 1000
  108. 1001
  109. 2000
  110. 3000
  111. 3001
  112. > SELECT * FROM t3;
  113. 1000
  114. 1001
  115. 2000
  116. 3000
  117. 3001
  118. $ postgres-execute connection=conn2
  119. COMMIT;
  120. BEGIN;
  121. INSERT INTO t1 VALUES (2002);
  122. $ postgres-execute connection=conn1
  123. INSERT INTO t1 VALUES (1002);
  124. DELETE FROM t2 WHERE a = 2000;
  125. $ postgres-execute connection=conn2
  126. DELETE FROM t3 WHERE a = 2000;
  127. $ postgres-execute connection=conn1
  128. COMMIT;
  129. BEGIN;
  130. > SELECT * FROM t1;
  131. 1000
  132. 1001
  133. 1002
  134. 2000
  135. 2001
  136. 3000
  137. 3001
  138. > SELECT * FROM t2;
  139. 1000
  140. 1001
  141. 2001
  142. 3000
  143. 3001
  144. > SELECT * FROM t3;
  145. 1000
  146. 1001
  147. 2000
  148. 2001
  149. 3000
  150. 3001
  151. $ postgres-execute connection=conn2
  152. COMMIT;
  153. BEGIN;
  154. > SELECT * FROM t1;
  155. 1000
  156. 1001
  157. 1002
  158. 2000
  159. 2001
  160. 2002
  161. 3000
  162. 3001
  163. > SELECT * FROM t2;
  164. 1000
  165. 1001
  166. 2001
  167. 3000
  168. 3001
  169. > SELECT * FROM t3;
  170. 1000
  171. 1001
  172. 2001
  173. 3000
  174. 3001