alter-table-irrelevant.td 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 ALTER TABLE -- tables that are created after the source are irrelevant
  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. ALTER USER postgres WITH replication;
  21. DROP SCHEMA IF EXISTS public CASCADE;
  22. DROP PUBLICATION IF EXISTS mz_source;
  23. CREATE SCHEMA public;
  24. CREATE TABLE base_table (f1 INTEGER);
  25. ALTER TABLE base_table REPLICA IDENTITY FULL;
  26. INSERT INTO base_table VALUES (1);
  27. CREATE PUBLICATION mz_source FOR ALL TABLES;
  28. > CREATE SOURCE mz_source
  29. FROM POSTGRES CONNECTION pg (PUBLICATION 'mz_source')
  30. FOR ALL TABLES;
  31. > SELECT * FROM base_table;
  32. 1
  33. # Create "irrelevant" table after the materialized source took a snapshot of the publication
  34. $ postgres-execute connection=postgres://postgres:postgres@postgres
  35. CREATE TABLE irrelevant_table (f1 INTEGER);
  36. ALTER TABLE irrelevant_table REPLICA IDENTITY FULL;
  37. INSERT INTO irrelevant_table VALUES (1);
  38. INSERT INTO base_table VALUES (2);
  39. # A new table arriving does not prevent queries on existing views for this materialized source
  40. > SELECT * FROM base_table;
  41. 1
  42. 2
  43. # Alter the irrelevant table and insert a row to force a second relation message that would be incompatible
  44. $ postgres-execute connection=postgres://postgres:postgres@postgres
  45. ALTER TABLE irrelevant_table ADD COLUMN f2 varchar(2);
  46. ALTER TABLE irrelevant_table DROP COLUMN f1;
  47. INSERT INTO irrelevant_table VALUES ('ab');
  48. # Query still works because the relation was ignored for being irrelevant
  49. > SELECT * FROM base_table;
  50. 1
  51. 2
  52. # Recreate the source and views to verify the irrelevant_table is part of the publication
  53. > DROP SOURCE mz_source CASCADE;
  54. > CREATE SOURCE mz_source
  55. FROM POSTGRES CONNECTION pg (PUBLICATION 'mz_source')
  56. FOR ALL TABLES;
  57. # Check the first view still works
  58. > SELECT * FROM base_table;
  59. 1
  60. 2
  61. # Confirm the second table now has a corresponding view and it has the expected data
  62. > SELECT * FROM irrelevant_table
  63. <null>
  64. ab
  65. # Alter the irrelevant_table now that it is relevant
  66. $ postgres-execute connection=postgres://postgres:postgres@postgres
  67. ALTER TABLE irrelevant_table ADD COLUMN f3 char(2);
  68. INSERT INTO irrelevant_table VALUES ('bc', 'de');
  69. > SELECT * FROM base_table;
  70. 1
  71. 2
  72. > SELECT * FROM irrelevant_table
  73. <null>
  74. ab
  75. bc
  76. # Alter in an incompatible way and ensure replication error does not occur
  77. $ postgres-execute connection=postgres://postgres:postgres@postgres
  78. ALTER TABLE irrelevant_table DROP COLUMN f2;
  79. INSERT INTO irrelevant_table VALUES ('gh');
  80. > SELECT * FROM base_table;
  81. 1
  82. 2
  83. > DROP SOURCE irrelevant_table CASCADE;