transactions.slt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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. mode cockroach
  10. # Start from a pristine server
  11. reset-server
  12. statement ok
  13. CREATE TABLE t (a int)
  14. statement ok
  15. INSERT INTO t (a) VALUES (1)
  16. #### next transaction
  17. statement ok
  18. BEGIN
  19. query I rowsort
  20. SELECT * FROM t
  21. ----
  22. 1
  23. statement ok
  24. COMMIT
  25. #### next transaction
  26. statement ok
  27. BEGIN
  28. query I rowsort
  29. SELECT * FROM t
  30. ----
  31. 1
  32. statement ok
  33. ROLLBACK
  34. #### next transaction
  35. statement ok
  36. START TRANSACTION
  37. query I rowsort
  38. SELECT * FROM t
  39. ----
  40. 1
  41. statement ok
  42. COMMIT
  43. #### next transaction
  44. statement ok
  45. START TRANSACTION
  46. query I rowsort
  47. SELECT * FROM t
  48. ----
  49. 1
  50. statement ok
  51. ROLLBACK
  52. # Multiple INSERTs.
  53. simple
  54. INSERT INTO t VALUES (2);
  55. INSERT INTO t VALUES (3);
  56. ----
  57. COMPLETE 1
  58. COMPLETE 1
  59. # INSERT in explicit transactions.
  60. statement ok
  61. BEGIN
  62. simple
  63. INSERT INTO t VALUES (4);
  64. ----
  65. COMPLETE 1
  66. # Verify ROLLBACK works by not expecting 4 below.
  67. statement ok
  68. ROLLBACK
  69. # INSERT rolled up from implicit txn into explicit not ok because mixed
  70. # with a read.
  71. simple
  72. INSERT INTO t VALUES (5);
  73. BEGIN;
  74. SELECT * FROM t;
  75. ----
  76. db error: ERROR: transaction in write-only mode
  77. # This COMMIT should be ignored due to the failure above.
  78. statement ok
  79. COMMIT
  80. # INSERT allowed in explicit transactions.
  81. simple
  82. BEGIN; INSERT INTO t VALUES (6);
  83. ----
  84. COMPLETE 0
  85. COMPLETE 1
  86. # Verify that the to-be-inserted data is not readable by another connection.
  87. simple conn=read
  88. SELECT * FROM t WHERE a=6
  89. ----
  90. COMPLETE 0
  91. statement ok
  92. COMMIT
  93. simple
  94. INSERT INTO t VALUES (7), (8)
  95. ----
  96. COMPLETE 2
  97. # Verify contents of table at the end.
  98. query I
  99. SELECT * FROM t ORDER BY a
  100. ----
  101. 1
  102. 2
  103. 3
  104. 6
  105. 7
  106. 8
  107. # The only thing we support multiple of in an implicit transaction
  108. # (multiple statements in the same query string) is row-returning
  109. # statements.
  110. simple
  111. CREATE TABLE u (i INT); SELECT 1;
  112. ----
  113. db error: ERROR: CREATE TABLE u (i int4) cannot be run inside a transaction block
  114. # Multiple reads in the same query string are ok.
  115. simple
  116. SELECT 1; SELECT 2
  117. ----
  118. 1
  119. COMPLETE 1
  120. 2
  121. COMPLETE 1
  122. # Verify that `SHOW` queries work in transactions.
  123. simple
  124. BEGIN
  125. ----
  126. COMPLETE 0
  127. query TT rowsort
  128. SHOW TABLES
  129. ----
  130. t (empty)
  131. simple
  132. COMMIT
  133. ----
  134. COMPLETE 0
  135. # Regression for database-issues#1768
  136. statement ok
  137. CREATE TABLE t5727 (i INT)
  138. simple
  139. BEGIN;
  140. INSERT INTO t VALUES (1);
  141. ----
  142. COMPLETE 0
  143. COMPLETE 1
  144. simple conn=drop
  145. DROP TABLE t
  146. ----
  147. COMPLETE 0
  148. simple
  149. COMMIT
  150. ----
  151. db error: ERROR: unknown catalog item 'u1'
  152. # Verify SUBSCRIBE must be only read statement in a transaction.
  153. # We use FETCH 0 with SUBSCRIBE below so that we don't need to worry about
  154. # timestamps in this slt file.
  155. statement ok
  156. CREATE TABLE t (a int)
  157. statement ok
  158. INSERT INTO t VALUES (1)
  159. simple
  160. DECLARE c CURSOR FOR SUBSCRIBE t;
  161. FETCH 0 c;
  162. DECLARE d CURSOR FOR SUBSCRIBE t;
  163. FETCH 0 d;
  164. ----
  165. db error: ERROR: SUBSCRIBE in transactions must be the only read statement
  166. simple
  167. DECLARE c CURSOR FOR SUBSCRIBE t;
  168. FETCH 0 c;
  169. SELECT * FROM t LIMIT 0;
  170. ----
  171. db error: ERROR: SUBSCRIBE in transactions must be the only read statement
  172. simple
  173. SELECT * FROM t LIMIT 0;
  174. DECLARE c CURSOR FOR SUBSCRIBE t;
  175. FETCH 0 c;
  176. ----
  177. db error: ERROR: transaction in read-only mode
  178. DETAIL: SELECT queries cannot be combined with other query types, including SUBSCRIBE.
  179. # Using an AS OF in the SELECT or SUBSCRIBE allows lifting that restriction.
  180. simple
  181. DECLARE c CURSOR FOR SUBSCRIBE t AS OF 18446744073709551615;
  182. FETCH 0 c;
  183. DECLARE d CURSOR FOR SUBSCRIBE t;
  184. FETCH 0 d;
  185. ----
  186. COMPLETE 0
  187. COMPLETE 0
  188. COMPLETE 0
  189. COMPLETE 0
  190. simple
  191. DECLARE c CURSOR FOR SUBSCRIBE t AS OF 18446744073709551615;
  192. FETCH 0 c;
  193. SELECT * FROM t LIMIT 0;
  194. ----
  195. COMPLETE 0
  196. COMPLETE 0
  197. COMPLETE 0
  198. simple
  199. SELECT * FROM t LIMIT 0;
  200. DECLARE c CURSOR FOR SUBSCRIBE t AS OF 18446744073709551615;
  201. FETCH 0 c;
  202. ----
  203. COMPLETE 0
  204. COMPLETE 0
  205. COMPLETE 0
  206. simple
  207. DECLARE c CURSOR FOR SUBSCRIBE t;
  208. FETCH 0 c;
  209. SELECT * FROM t LIMIT 0 AS OF AT LEAST 0;
  210. ----
  211. COMPLETE 0
  212. COMPLETE 0
  213. COMPLETE 0
  214. simple
  215. SELECT * FROM t LIMIT 0 AS OF AT LEAST 0;
  216. DECLARE c CURSOR FOR SUBSCRIBE t;
  217. FETCH 0 c;
  218. ----
  219. COMPLETE 0
  220. COMPLETE 0
  221. COMPLETE 0
  222. simple
  223. SELECT 1;
  224. SELECT * FROM t;
  225. ----
  226. db error: ERROR: querying the following items "materialize.public.t" is not allowed from the "mz_catalog_server" cluster
  227. DETAIL: The transaction is executing on the mz_catalog_server cluster, maybe having been routed there by the first statement in the transaction.
  228. statement ok
  229. CREATE SCHEMA other
  230. statement ok
  231. CREATE TABLE other.t (i INT)
  232. simple
  233. SELECT * FROM t;
  234. SELECT * FROM other.t;
  235. ----
  236. db error: ERROR: Transactions can only reference objects in the same timedomain. See https://materialize.com/docs/sql/begin/#same-timedomain-error
  237. DETAIL: The following relations in the query are outside the transaction's time domain:
  238. "materialize.other.t"
  239. Only the following relations are available:
  240. "materialize.public.t"
  241. "materialize.public.t5727"
  242. "mz_catalog.mz_array_types"
  243. "mz_catalog.mz_base_types"
  244. "mz_catalog.mz_columns"
  245. "mz_catalog.mz_connections"
  246. "mz_catalog.mz_databases"
  247. "mz_catalog.mz_functions"
  248. "mz_catalog.mz_index_columns"
  249. "mz_catalog.mz_indexes"
  250. "mz_catalog.mz_list_types"
  251. "mz_catalog.mz_map_types"
  252. "mz_catalog.mz_materialized_views"
  253. "mz_catalog.mz_operators"
  254. "mz_catalog.mz_pseudo_types"
  255. "mz_catalog.mz_role_members"
  256. "mz_catalog.mz_role_parameters"
  257. "mz_catalog.mz_roles"
  258. "mz_catalog.mz_schemas"
  259. "mz_catalog.mz_secrets"
  260. "mz_catalog.mz_sinks"
  261. "mz_catalog.mz_sources"
  262. "mz_catalog.mz_system_privileges"
  263. "mz_catalog.mz_tables"
  264. "mz_catalog.mz_types"
  265. "mz_catalog.mz_views"
  266. "mz_internal.mz_aggregates"
  267. "mz_internal.mz_comments"
  268. "mz_internal.mz_continual_tasks"
  269. "mz_internal.mz_object_dependencies"
  270. "mz_internal.mz_type_pg_metadata"
  271. # Verify that changed tables and views don't change during a transaction.
  272. statement ok
  273. CREATE MATERIALIZED VIEW v AS SELECT COUNT(*) FROM T
  274. simple conn=read
  275. BEGIN;
  276. SELECT * FROM t;
  277. SELECT * FROM v;
  278. ----
  279. COMPLETE 0
  280. 1
  281. COMPLETE 1
  282. 1
  283. COMPLETE 1
  284. simple conn=write
  285. INSERT INTO t VALUES (3)
  286. ----
  287. COMPLETE 1
  288. simple conn=write
  289. SELECT * FROM t;
  290. SELECT * FROM v;
  291. ----
  292. 1
  293. 3
  294. COMPLETE 2
  295. 2
  296. COMPLETE 1
  297. simple conn=read
  298. SELECT * FROM t;
  299. SELECT * FROM v;
  300. COMMIT;
  301. ----
  302. 1
  303. COMPLETE 1
  304. 1
  305. COMPLETE 1
  306. COMPLETE 0
  307. # Test replacing a non-materialized view in a different transaction.
  308. statement ok
  309. CREATE VIEW v1 AS SELECT 1
  310. simple conn=t1
  311. BEGIN;
  312. SELECT * FROM v1;
  313. ----
  314. COMPLETE 0
  315. 1
  316. COMPLETE 1
  317. simple conn=t2
  318. CREATE OR REPLACE VIEW v1 AS SELECT 2;
  319. ----
  320. COMPLETE 0
  321. simple conn=t2
  322. SELECT * FROM v1;
  323. ----
  324. 2
  325. COMPLETE 1
  326. # Our catalog doesn't respect SQL transactions, so we see the new v1.
  327. # Unmaterialized views with no dependencies exist outside of any particular
  328. # timedomain.
  329. simple conn=t1
  330. SELECT * FROM v1;
  331. COMMIT;
  332. ----
  333. 2
  334. COMPLETE 1
  335. COMPLETE 0
  336. simple conn=t1
  337. ROLLBACK;
  338. ----
  339. COMPLETE 0
  340. # Verify an error is produce during write transactions if the commit fails.
  341. statement ok
  342. CREATE TABLE insert_fail (i int)
  343. statement ok
  344. INSERT INTO insert_fail VALUES (1)
  345. simple conn=t1
  346. BEGIN;
  347. INSERT into insert_fail VALUES (2);
  348. ----
  349. COMPLETE 0
  350. COMPLETE 1
  351. simple conn=t2
  352. DROP table insert_fail;
  353. ----
  354. COMPLETE 0
  355. simple conn=t1
  356. COMMIT;
  357. ----
  358. db error: ERROR: unknown catalog item 'u8'
  359. # Test transaction syntax that we don't support.
  360. statement ok
  361. BEGIN ISOLATION LEVEL SERIALIZABLE
  362. statement error CHAIN not yet supported
  363. COMMIT AND CHAIN
  364. statement error CHAIN not yet supported
  365. ROLLBACK AND CHAIN
  366. statement ok
  367. ROLLBACK
  368. # This is a noop, but is supported syntax.
  369. statement ok
  370. BEGIN ISOLATION LEVEL REPEATABLE READ
  371. statement ok
  372. COMMIT
  373. # Access modes.
  374. statement ok
  375. BEGIN TRANSACTION READ WRITE
  376. statement ok
  377. COMMIT
  378. statement ok
  379. BEGIN TRANSACTION READ ONLY
  380. query I
  381. SELECT 1
  382. ----
  383. 1
  384. statement ok
  385. COMMIT
  386. statement ok
  387. BEGIN TRANSACTION READ ONLY
  388. statement error transaction in read-only mode
  389. INSERT INTO t (a) VALUES (1)
  390. statement ok
  391. ROLLBACK
  392. ## BEGIN does not lose READ ONLY bit
  393. statement ok
  394. BEGIN READ ONLY
  395. statement ok
  396. BEGIN
  397. statement error transaction in read-only mode
  398. INSERT INTO t (a) VALUES (1)
  399. statement ok
  400. ROLLBACK
  401. ## READ ONLY -> READ WRITE valid only if no queries issued yet
  402. statement ok
  403. BEGIN READ ONLY
  404. statement ok
  405. BEGIN READ WRITE
  406. statement ok
  407. INSERT INTO t (a) VALUES (1)
  408. statement ok
  409. ROLLBACK
  410. statement ok
  411. BEGIN READ ONLY
  412. query I
  413. SELECT 1
  414. ----
  415. 1
  416. statement error transaction read-write mode must be set before any query
  417. BEGIN READ WRITE
  418. statement ok
  419. COMMIT
  420. ## READ WRITE -> READ ONLY valid, but cannot switch back if any queries issued
  421. statement ok
  422. BEGIN READ WRITE
  423. query I
  424. SELECT 1
  425. ----
  426. 1
  427. statement ok
  428. BEGIN READ ONLY
  429. statement error transaction read-write mode must be set before any query
  430. BEGIN READ WRITE
  431. statement ok
  432. COMMIT
  433. # Test read-only -> read-write with subscribe
  434. statement ok
  435. BEGIN READ ONLY
  436. simple
  437. DECLARE c CURSOR FOR SUBSCRIBE t;
  438. FETCH 0 c;
  439. ----
  440. COMPLETE 0
  441. COMPLETE 0
  442. statement error transaction read-write mode must be set before any query
  443. BEGIN READ WRITE
  444. statement ok
  445. COMMIT
  446. # Test that multi-table write transactions aren't supported
  447. statement ok
  448. CREATE TABLE foo(a int)
  449. statement ok
  450. CREATE TABLE bar(a int)
  451. statement ok
  452. BEGIN
  453. statement ok
  454. INSERT INTO foo VALUES (42)
  455. statement ok
  456. INSERT INTO bar VALUES (43)
  457. statement ok
  458. COMMIT
  459. query I
  460. SELECT a FROM foo
  461. ----
  462. 42
  463. query I
  464. SELECT a FROM bar
  465. ----
  466. 43
  467. # Test that constant reads are allowed in write-only transactions
  468. statement ok
  469. BEGIN
  470. query I
  471. SELECT 1
  472. ----
  473. 1
  474. statement ok
  475. SELECT now()
  476. query T
  477. SELECT INTERVAL '1 day'
  478. ----
  479. 1 day
  480. statement ok
  481. INSERT INTO t VALUES (1), (3)
  482. query I
  483. SELECT 1
  484. ----
  485. 1
  486. statement ok
  487. SELECT now()
  488. query T
  489. SELECT INTERVAL '1 day'
  490. ----
  491. 1 day
  492. statement ok
  493. COMMIT
  494. # Verify that different kinds of INSERT INTO ... statements are correctly
  495. # rejected when a transaction is in read-only mode.
  496. #
  497. # At least inserts of non-constant values internally get translated to
  498. # read-then write statements, which has caused trouble with maintaining the
  499. # read-only bit in the past.
  500. statement ok
  501. CREATE TABLE baz(a text)
  502. statement ok
  503. BEGIN
  504. # A constant value
  505. statement ok
  506. INSERT INTO baz VALUES ('hello')
  507. statement ok
  508. COMMIT
  509. statement ok
  510. BEGIN
  511. # A non-constant value
  512. statement ok
  513. INSERT INTO baz VALUES (now()::text)
  514. statement ok
  515. COMMIT
  516. statement ok
  517. BEGIN
  518. statement ok
  519. SELECT * FROM baz
  520. # A constant value, should be rejected
  521. statement error transaction in read-only mode
  522. INSERT INTO baz VALUES ('ciao')
  523. statement ok
  524. ROLLBACK
  525. statement ok
  526. BEGIN
  527. statement ok
  528. SELECT * FROM baz
  529. # A non-constant value, should be rejected as well
  530. statement error transaction in read-only mode
  531. INSERT INTO baz VALUES (now()::text)
  532. statement ok
  533. ROLLBACK
  534. simple conn=mz_system,user=mz_system
  535. ALTER SYSTEM SET allow_real_time_recency = true
  536. ----
  537. COMPLETE 0
  538. statement ok
  539. SET REAL_TIME_RECENCY TO TRUE
  540. statement ok
  541. CREATE TABLE rtr (a INT)
  542. statement ok
  543. INSERT INTO rtr VALUES (1)
  544. query I
  545. SELECT * FROM rtr
  546. ----
  547. 1
  548. statement ok
  549. INSERT INTO rtr SELECT * FROM rtr
  550. query I
  551. SELECT * FROM rtr
  552. ----
  553. 1
  554. 1
  555. statement ok
  556. DROP TABLE t CASCADE;
  557. statement ok
  558. CREATE TABLE t (a INT);
  559. # AS OF should work in the middle of a transaction
  560. statement ok
  561. BEGIN
  562. query T
  563. SELECT * FROM t AS OF AT LEAST 1683131452106;
  564. ----
  565. # Give t a chance to advance.
  566. statement ok
  567. SELECT mz_unsafe.mz_sleep(2)
  568. query T
  569. SELECT * FROM t AS OF AT LEAST 1683131452106;
  570. ----
  571. query T
  572. SELECT * FROM t;
  573. ----
  574. # Give t a chance to advance.
  575. statement ok
  576. SELECT mz_unsafe.mz_sleep(2)
  577. query T
  578. SELECT * FROM t AS OF AT LEAST 1683131452106;
  579. ----
  580. statement ok
  581. COMMIT
  582. statement ok
  583. BEGIN
  584. statement ok
  585. SELECT * FROM t
  586. statement error SET TRANSACTION ISOLATION LEVEL must be called before any query
  587. SET TRANSACTION_ISOLATION TO serializable
  588. statement ok
  589. ROLLBACK
  590. statement ok
  591. BEGIN
  592. statement ok
  593. SELECT * FROM t
  594. statement error SET TRANSACTION ISOLATION LEVEL must be called before any query
  595. SET TRANSACTION ISOLATION LEVEL serializable
  596. statement ok
  597. ROLLBACK
  598. statement ok
  599. BEGIN
  600. statement ok
  601. SET TRANSACTION_ISOLATION TO serializable
  602. statement ok
  603. SELECT * FROM t
  604. statement error SET TRANSACTION ISOLATION LEVEL must be called before any query
  605. RESET TRANSACTION_ISOLATION
  606. statement ok
  607. ROLLBACK
  608. # Test explicit single statement transactions.
  609. reset-server
  610. statement ok
  611. BEGIN
  612. statement ok
  613. CREATE TABLE t (i INT)
  614. # Should not have executed.
  615. simple conn=c1
  616. SHOW tables
  617. ----
  618. COMPLETE 0
  619. statement ok
  620. COMMIT
  621. simple conn=c1
  622. SHOW tables
  623. ----
  624. t,
  625. COMPLETE 1
  626. statement ok
  627. BEGIN
  628. # No error yet because we didn't try to execute.
  629. statement ok
  630. CREATE TABLE t (i INT)
  631. statement error db error: ERROR: table "materialize\.public\.t" already exists
  632. COMMIT
  633. statement ok
  634. BEGIN
  635. # No error yet because we didn't try to execute.
  636. statement ok
  637. CREATE TABLE t (i INT)
  638. statement error db error: ERROR: this transaction can only execute a single statement
  639. SELECT 1
  640. statement ok
  641. ROLLBACK
  642. # Test CREATE SOURCE for its off thread purify.
  643. statement ok
  644. BEGIN
  645. statement ok
  646. CREATE SOURCE s FROM LOAD GENERATOR COUNTER
  647. statement ok
  648. COMMIT
  649. simple
  650. SHOW SOURCES
  651. ----
  652. s,load-generator,quickstart,
  653. s_progress,progress,NULL,
  654. COMPLETE 2
  655. # Test a statement that doesn't work even in this mode because of ambiguous responses.
  656. statement ok
  657. CREATE DEFAULT INDEX ON s
  658. statement ok
  659. BEGIN
  660. statement error db error: ERROR: ALTER INDEX s_primary_idx SET \(RETAIN HISTORY = FOR '1000 hours'\) cannot be run inside a transaction block
  661. ALTER INDEX s_primary_idx SET (RETAIN HISTORY = FOR '1000 hours')
  662. query error db error: ERROR: current transaction is aborted, commands ignored until end of transaction block
  663. SELECT 1
  664. statement ok
  665. ROLLBACK
  666. # Test ALTER and DROP which go through separate paths to determine the correct response.
  667. statement ok
  668. BEGIN
  669. statement ok
  670. ALTER SOURCE s RENAME TO v
  671. statement ok
  672. COMMIT
  673. statement ok
  674. BEGIN
  675. statement ok
  676. DROP SOURCE v
  677. statement ok
  678. COMMIT
  679. simple
  680. SHOW SOURCES
  681. ----
  682. COMPLETE 0
  683. # Test that the cluster cannot change mid-transaction.
  684. statement ok
  685. DROP TABLE t CASCADE
  686. statement ok
  687. CREATE TABLE t (a INT)
  688. statement ok
  689. CREATE CLUSTER c REPLICAS (r1 (SIZE '1'))
  690. statement ok
  691. BEGIN
  692. statement ok
  693. SELECT * FROM t
  694. statement error SET cluster cannot be called in an active transaction
  695. SET CLUSTER TO c
  696. statement ok
  697. ROLLBACK
  698. statement ok
  699. BEGIN
  700. statement ok
  701. SELECT * FROM t
  702. statement error SET cluster cannot be called in an active transaction
  703. SET LOCAL CLUSTER TO c
  704. statement ok
  705. ROLLBACK
  706. # Test that the cluster can change at the start of a transaction.
  707. statement ok
  708. BEGIN
  709. statement ok
  710. SET CLUSTER TO c
  711. statement ok
  712. SELECT * FROM t
  713. statement ok
  714. COMMIT
  715. statement ok
  716. SET CLUSTER TO default
  717. statement ok
  718. BEGIN
  719. statement ok
  720. SET LOCAL CLUSTER TO c
  721. statement ok
  722. SELECT * FROM t
  723. statement ok
  724. COMMIT
  725. statement ok
  726. SET CLUSTER TO default
  727. # Test that the cluster is selected at the start of a transaction and doesn't change.
  728. ## Auto-routing selects mz_catalog_server at the start of transaction.
  729. statement ok
  730. BEGIN
  731. statement ok
  732. SHOW VIEWS
  733. query T multiline
  734. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT *, generate_series(1, 100) FROM mz_views
  735. ----
  736. Explained Query:
  737. CrossJoin type=differential
  738. ArrangeBy keys=[[]]
  739. ReadIndex on=mz_views mz_views_ind=[*** full scan ***]
  740. ArrangeBy keys=[[]]
  741. Constant
  742. total_rows (diffs absed): 100
  743. first_rows:
  744. - (1)
  745. - (2)
  746. - (3)
  747. - (4)
  748. - (5)
  749. - (6)
  750. - (7)
  751. - (8)
  752. - (9)
  753. - (10)
  754. - (11)
  755. - (12)
  756. - (13)
  757. - (14)
  758. - (15)
  759. - (16)
  760. - (17)
  761. - (18)
  762. - (19)
  763. - (20)
  764. Used Indexes:
  765. - mz_catalog.mz_views_ind (*** full scan ***)
  766. Target cluster: mz_catalog_server
  767. EOF
  768. statement ok
  769. COMMIT
  770. ## Auto-routing doesn't select mz_catalog_server in the middle of a transaction.
  771. # Since mz_views uses custom types, the postgres client will look it up in the catalog on
  772. # first use. If the first use happens to be in a transaction, then we can get unexpected time
  773. # domain errors. This is an annoying hack to load the information in the postgres client before
  774. # we start any transactions.
  775. statement ok
  776. SELECT * FROM mz_views LIMIT 0
  777. ----
  778. statement ok
  779. BEGIN
  780. statement ok
  781. SELECT * FROM t, mz_views
  782. query T multiline
  783. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SHOW VIEWS
  784. ----
  785. Explained Query:
  786. With
  787. cte l0 =
  788. Project (#0{id}..=#2{name}, #4{comment})
  789. Join on=(#0{id} = #3{id}) type=differential
  790. ArrangeBy keys=[[#0{id}]]
  791. Project (#0{id}, #2{schema_id}, #3{name})
  792. ReadStorage mz_catalog.mz_views
  793. ArrangeBy keys=[[#0{id}]]
  794. Project (#0{id}, #3{comment})
  795. Filter (#2{object_sub_id}) IS NULL AND (#1{object_type} = "view")
  796. ReadStorage mz_internal.mz_comments
  797. cte l1 =
  798. Project (#0{id}, #3{name})
  799. Filter (#2{schema_id} = "u3")
  800. ReadStorage mz_catalog.mz_views
  801. Return
  802. Project (#0{name}, #2)
  803. Map (coalesce(#1{comment}, ""))
  804. Union
  805. Map (null)
  806. Union
  807. Negate
  808. Project (#1{name})
  809. Join on=(#0{id} = #2{id}) type=differential
  810. ArrangeBy keys=[[#0{id}]]
  811. Get l1
  812. ArrangeBy keys=[[#0{id}]]
  813. Distinct project=[#0{id}]
  814. Project (#0{id})
  815. Get l0
  816. Project (#1{name})
  817. Get l1
  818. Project (#2{name}, #3{comment})
  819. Filter (#1{schema_id} = "u3")
  820. Get l0
  821. Source mz_catalog.mz_views
  822. Source mz_internal.mz_comments
  823. filter=((#1{object_type} = "view") AND (#2{object_sub_id}) IS NULL)
  824. Target cluster: quickstart
  825. EOF
  826. statement ok
  827. COMMIT
  828. simple conn=mz_system,user=mz_system
  829. ALTER SYSTEM SET enable_alter_swap = true;
  830. ----
  831. COMPLETE 0
  832. statement ok
  833. CREATE SCHEMA blue;
  834. statement ok
  835. CREATE SCHEMA green;
  836. query TT
  837. SELECT name, id FROM mz_schemas WHERE id LIKE 'u%' ORDER BY name;
  838. ----
  839. blue u9
  840. green u10
  841. public u3
  842. statement ok
  843. BEGIN;
  844. statement ok
  845. ALTER SCHEMA blue SWAP WITH green;
  846. statement ok
  847. COMMIT;
  848. query TT
  849. SELECT name, id FROM mz_schemas WHERE id LIKE 'u%' ORDER BY name;
  850. ----
  851. blue u10
  852. green u9
  853. public u3
  854. statement ok
  855. BEGIN;
  856. statement ok
  857. ALTER SCHEMA blue SWAP WITH green;
  858. statement ok
  859. ALTER SCHEMA green RENAME TO purple;
  860. statement ok
  861. ROLLBACK;
  862. query TT
  863. SELECT name, id FROM mz_schemas WHERE id LIKE 'u%' ORDER BY name;
  864. ----
  865. blue u10
  866. green u9
  867. public u3
  868. statement ok
  869. BEGIN;
  870. statement ok
  871. ALTER SCHEMA green RENAME TO purple;
  872. # Modify the Catalog from a different session while a transaction is open.
  873. simple conn=mz_system,user=mz_system
  874. CREATE TABLE yellow_t1 (x int);
  875. ----
  876. COMPLETE 0
  877. statement error db error: ERROR: object state changed while transaction was in progress
  878. ALTER SCHEMA blue RENAME to pink;
  879. statement ok
  880. ROLLBACK;
  881. query TT
  882. SELECT name, id FROM mz_schemas WHERE id LIKE 'u%' ORDER BY name;
  883. ----
  884. blue u10
  885. green u9
  886. public u3
  887. statement ok
  888. BEGIN;
  889. statement ok
  890. ALTER SCHEMA blue RENAME TO pink;
  891. statement ok
  892. ALTER SCHEMA pink RENAME TO purple;
  893. statement ok
  894. ALTER SCHEMA purple RENAME TO orange;
  895. statement ok
  896. ALTER SCHEMA orange RENAME TO red;
  897. statement ok
  898. ALTER SCHEMA red RENAME TO orange;
  899. statement ok
  900. COMMIT;
  901. query TT
  902. SELECT name, id FROM mz_schemas WHERE id LIKE 'u%' ORDER BY name;
  903. ----
  904. green u9
  905. orange u10
  906. public u3
  907. statement ok
  908. BEGIN;
  909. statement error db error: ERROR: schema 'green' already exists
  910. ALTER SCHEMA orange RENAME TO green;
  911. statement ok
  912. COMMIT;
  913. query TT
  914. SELECT name, id FROM mz_schemas WHERE id LIKE 'u%' ORDER BY name;
  915. ----
  916. green u9
  917. orange u10
  918. public u3
  919. statement ok
  920. BEGIN;
  921. statement ok
  922. ALTER SCHEMA green RENAME TO red;
  923. statement error db error: ERROR: schema 'red' already exists
  924. ALTER SCHEMA orange RENAME TO red;
  925. statement ok
  926. COMMIT;
  927. # Transaction should be rolled back and nothing should change.
  928. query TT
  929. SELECT name, id FROM mz_schemas WHERE id LIKE 'u%' ORDER BY name;
  930. ----
  931. green u9
  932. orange u10
  933. public u3
  934. statement ok
  935. CREATE TABLE green_t1 (x int);
  936. statement ok
  937. CREATE VIEW green_v1 AS ( SELECT SUM(x) FROM green_t1 );
  938. statement ok
  939. CREATE CLUSTER green_compute SIZE '1';
  940. statement ok
  941. CREATE MATERIALIZED VIEW green_mv1 IN CLUSTER green_compute AS ( SELECT AVG(x) FROM green_t1 );
  942. statement ok
  943. BEGIN;
  944. statement ok
  945. ALTER TABLE green_t1 RENAME TO blue_t1;
  946. statement ok
  947. ALTER VIEW green_v1 RENAME TO blue_v1;
  948. statement ok
  949. ALTER MATERIALIZED VIEW green_mv1 RENAME TO blue_mv1;
  950. statement ok
  951. ALTER CLUSTER green_compute RENAME TO blue_compute;
  952. statement ok
  953. COMMIT;
  954. statement ok
  955. INSERT INTO blue_t1 VALUES (10), (20), (30);
  956. query I
  957. SELECT * FROM blue_v1;
  958. ----
  959. 60
  960. query I
  961. SELECT * FROM blue_mv1;
  962. ----
  963. 20
  964. query TT
  965. SELECT name, id FROM mz_clusters WHERE id LIKE 'u%' AND name != 'quickstart' ORDER BY name;
  966. ----
  967. blue_compute u3
  968. c u2
  969. statement ok
  970. BEGIN;
  971. statement ok
  972. INSERT INTO blue_t1 VALUES (40), (50), (60);
  973. statement error db error: ERROR: transaction in write-only mode
  974. ALTER TABLE blue_t1 RENAME TO red_t1;
  975. statement ok
  976. COMMIT;
  977. query I
  978. SELECT * FROM blue_t1 LIMIT 1;
  979. ----
  980. 10
  981. statement ok
  982. BEGIN;
  983. query I
  984. SELECT * FROM blue_v1;
  985. ----
  986. 60
  987. statement error db error: ERROR: transaction in read-only mode
  988. ALTER TABLE blue_t1 RENAME TO red_t1;
  989. statement ok
  990. COMMIT;
  991. query I
  992. SELECT * FROM blue_t1 LIMIT 1;
  993. ----
  994. 10
  995. statement ok
  996. BEGIN;
  997. statement ok
  998. ALTER TABLE blue_t1 RENAME TO purple_t1;
  999. statement error db error: ERROR: transactions which modify objects are restricted to just modifying objects
  1000. SELECT * FROM blue_mv1;
  1001. statement ok
  1002. COMMIT;
  1003. query I
  1004. SELECT * FROM blue_t1 LIMIT 1;
  1005. ----
  1006. 10
  1007. # Make sure SHOW COLUMNS does not use a different cluster during a transaction.
  1008. #
  1009. # SHOW COLUMNS is planned separately from other SHOW statements, and previously it would ignore
  1010. # the mz_catalog_server auto-routing. This resulted in its dependents being outside the timedomain
  1011. # of the transaction.
  1012. statement ok
  1013. BEGIN;
  1014. query T
  1015. SELECT name FROM mz_columns WHERE name = 'foobar';
  1016. ----
  1017. query TTTT
  1018. SHOW COLUMNS IN mz_columns
  1019. ----
  1020. id false text The␠unique␠ID␠of␠the␠table,␠source,␠or␠view␠containing␠the␠column.
  1021. name false text The␠name␠of␠the␠column.
  1022. position false uint8 The␠1-indexed␠position␠of␠the␠column␠in␠its␠containing␠table,␠source,␠or␠view.
  1023. nullable false boolean Can␠the␠column␠contain␠a␠`NULL`␠value?
  1024. type false text The␠data␠type␠of␠the␠column.
  1025. default true text The␠default␠expression␠of␠the␠column.
  1026. type_oid false oid The␠OID␠of␠the␠type␠of␠the␠column␠(references␠`mz_types`).
  1027. type_mod false integer The␠packed␠type␠identifier␠of␠the␠column.
  1028. statement ok
  1029. COMMIT;
  1030. # Cleanup.
  1031. statement ok
  1032. DROP CLUSTER blue_compute CASCADE;