alter-table-irrelevant.td 3.2 KB

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