replica-expiration.td 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. # Test replica expiration.
  10. ## No retractions: expiration=30d, temporal filter width=20d
  11. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  12. ALTER SYSTEM SET compute_replica_expiration_offset = '30d'
  13. > CREATE CLUSTER test (SIZE = '1')
  14. > SET CLUSTER TO test
  15. > CREATE TABLE events (
  16. content TEXT,
  17. event_ts TIMESTAMP
  18. );
  19. > CREATE VIEW events_view AS
  20. SELECT event_ts, content
  21. FROM events
  22. WHERE mz_now() <= event_ts + INTERVAL '20 days';
  23. > CREATE DEFAULT INDEX ON events_view;
  24. > INSERT INTO events SELECT x::text, now() FROM generate_series(1, 1000) AS x;
  25. # TODO: The following query should return 2000, but it returns 1000 because the
  26. # arrangement sizes does not account for the temporal bucket. It is part of
  27. # a different operator, and we only reveal counts associated with arrangement
  28. # operators.
  29. > SELECT records FROM mz_introspection.mz_dataflow_arrangement_sizes
  30. WHERE name LIKE '%events_view_primary_idx';
  31. 1000
  32. > DROP TABLE events CASCADE;
  33. > DROP CLUSTER test;
  34. ## Does retractions: expiration=20d, temporal filter width=30d
  35. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  36. ALTER SYSTEM SET compute_replica_expiration_offset = '20d'
  37. > CREATE CLUSTER test (SIZE = '1')
  38. > SET CLUSTER TO test
  39. > CREATE TABLE events (
  40. content TEXT,
  41. event_ts TIMESTAMP
  42. );
  43. > CREATE VIEW events_view AS
  44. SELECT event_ts, content
  45. FROM events
  46. WHERE mz_now() <= event_ts + INTERVAL '30 days';
  47. > CREATE DEFAULT INDEX ON events_view;
  48. > INSERT INTO events SELECT x::text, now() FROM generate_series(1, 1000) AS x;
  49. > SELECT records FROM mz_introspection.mz_dataflow_arrangement_sizes
  50. WHERE name LIKE '%events_view_primary_idx';
  51. 1000
  52. > DROP TABLE events CASCADE;
  53. > DROP CLUSTER test;
  54. # Test materialize views. `mz_introspection` does not report number of records, but we just make
  55. # sure that the queries work.
  56. ## No retractions: expiration=30d, temporal filter width=20d
  57. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  58. ALTER SYSTEM SET compute_replica_expiration_offset = '30d'
  59. > CREATE CLUSTER test (SIZE = '1')
  60. > SET CLUSTER TO test
  61. > CREATE TABLE events (
  62. content TEXT,
  63. event_ts TIMESTAMP
  64. );
  65. > INSERT INTO events SELECT x::text, now() FROM generate_series(1, 1000) AS x;
  66. > CREATE MATERIALIZED VIEW events_mv AS
  67. SELECT event_ts, content
  68. FROM events
  69. WHERE mz_now() <= event_ts + INTERVAL '20 days';
  70. > SELECT count(*) FROM events_mv where content like '1%';
  71. 112
  72. > DROP TABLE events CASCADE;
  73. > DROP CLUSTER test;
  74. ## Does retractions: expiration=20d, temporal filter width=30d
  75. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  76. ALTER SYSTEM SET compute_replica_expiration_offset = '20d'
  77. > CREATE CLUSTER test (SIZE = '1')
  78. > SET CLUSTER TO test
  79. > CREATE TABLE events (
  80. content TEXT,
  81. event_ts TIMESTAMP
  82. );
  83. > INSERT INTO events SELECT x::text, now() FROM generate_series(1, 1000) AS x;
  84. > CREATE MATERIALIZED VIEW events_mv AS
  85. SELECT event_ts, content
  86. FROM events
  87. WHERE mz_now() <= event_ts + INTERVAL '30 days';
  88. > SELECT count(*) FROM events_mv where content like '1%';
  89. 112
  90. > DROP TABLE events CASCADE;
  91. > DROP CLUSTER test;
  92. # Check that transitive refresh disables expiration even when enabled: expiration=20d, temporal filter width=30d
  93. ## No transitive refresh enables expiration, thus no retractions
  94. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  95. ALTER SYSTEM SET compute_replica_expiration_offset = '20d'
  96. > CREATE CLUSTER test (SIZE = '1')
  97. > SET CLUSTER TO test
  98. > CREATE TABLE events (
  99. id INT,
  100. content TEXT,
  101. event_ts TIMESTAMP
  102. );
  103. > INSERT INTO events SELECT x, x::text, now() FROM generate_series(1, 1000) AS x;
  104. > CREATE MATERIALIZED VIEW mv AS
  105. SELECT id, content, event_ts from events WHERE id < 100;
  106. > CREATE VIEW view1 AS
  107. SELECT id, content, event_ts
  108. FROM mv
  109. WHERE content like '1%';
  110. > CREATE VIEW view2 AS
  111. SELECT id, content, event_ts
  112. FROM view1
  113. WHERE content like '__';
  114. > CREATE VIEW view3 AS
  115. SELECT id, content, event_ts
  116. FROM view2
  117. WHERE mz_now() <= event_ts + INTERVAL '30 days';
  118. > CREATE DEFAULT INDEX ON view3;
  119. > SELECT records FROM mz_introspection.mz_dataflow_arrangement_sizes
  120. WHERE name LIKE '%view3_primary_idx';
  121. 10
  122. > DROP TABLE events CASCADE;
  123. > DROP MATERIALIZED VIEW if exists mv CASCADE;
  124. > DROP VIEW if exists view1 CASCADE;
  125. > DROP VIEW if exists view2 CASCADE;
  126. > DROP VIEW if exists view3 CASCADE;
  127. > DROP CLUSTER test;
  128. ## Transitive refresh disables expiration, resulting in retractions
  129. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  130. ALTER SYSTEM SET compute_replica_expiration_offset = '20d'
  131. > CREATE CLUSTER test (SIZE = '1');
  132. > SET CLUSTER TO test;
  133. > CREATE TABLE events (
  134. id INT,
  135. content TEXT,
  136. event_ts TIMESTAMP
  137. );
  138. > INSERT INTO events SELECT x, x::text, now() FROM generate_series(1, 1000) AS x;
  139. > CREATE MATERIALIZED VIEW mv
  140. WITH (REFRESH AT CREATION, REFRESH AT '3000-01-01 23:59') AS
  141. SELECT id, content, event_ts from events WHERE id < 100;
  142. > CREATE VIEW view1 AS
  143. SELECT id, content, event_ts
  144. FROM mv
  145. WHERE content like '1%';
  146. > CREATE VIEW view2 AS
  147. SELECT id, content, event_ts
  148. FROM view1
  149. WHERE content like '__';
  150. > CREATE VIEW view3 AS
  151. SELECT id, content, event_ts
  152. FROM view2
  153. WHERE mz_now() <= event_ts + INTERVAL '30 days';
  154. > CREATE DEFAULT INDEX ON view3;
  155. > SELECT records FROM mz_introspection.mz_dataflow_arrangement_sizes
  156. WHERE name LIKE '%view3_primary_idx';
  157. 20
  158. > DROP TABLE events CASCADE;
  159. > DROP MATERIALIZED VIEW if exists mv CASCADE;
  160. > DROP VIEW if exists view1 CASCADE;
  161. > DROP VIEW if exists view2 CASCADE;
  162. > DROP VIEW if exists view3 CASCADE;
  163. > DROP CLUSTER test;
  164. # Views with constant values disable expiration even when enabled: expiration=20d, temporal filter width=30d
  165. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  166. ALTER SYSTEM SET compute_replica_expiration_offset = '20d';
  167. > CREATE CLUSTER test (SIZE = '1');
  168. > SET CLUSTER TO test;
  169. > CREATE VIEW events_over_time AS VALUES ('joe', 100), ('mike', 101), ('sam', 200), ('end', 18446144073709551615);
  170. > CREATE VIEW events AS SELECT * FROM events_over_time WHERE mz_now() <= column2 + 2592000000; -- 30d in ms
  171. > CREATE DEFAULT INDEX ON events;
  172. > SUBSCRIBE events WITH (progress) AS OF 0;
  173. mz_timestamp mz_progressed mz_diff column1 column2
  174. ----
  175. 0 false 1 end 18446144073709551615
  176. 0 false 1 joe 100
  177. 0 false 1 mike 101
  178. 0 false 1 sam 200
  179. 0 true <null> <null> <null>
  180. 18446144076301551616 false -1 end 18446144073709551615
  181. 2592000101 false -1 joe 100
  182. 2592000102 false -1 mike 101
  183. 2592000201 false -1 sam 200
  184. > DROP VIEW if exists events_over_time CASCADE;
  185. > DROP VIEW if exists events CASCADE;
  186. > DROP CLUSTER test;
  187. # Ensure disabling expiration results in retractions
  188. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  189. ALTER SYSTEM SET compute_replica_expiration_offset = 0;
  190. > CREATE CLUSTER test (SIZE = '1')
  191. > SET CLUSTER TO test
  192. > CREATE TABLE events (
  193. content TEXT,
  194. event_ts TIMESTAMP
  195. );
  196. > CREATE VIEW events_view AS
  197. SELECT event_ts, content
  198. FROM events
  199. WHERE mz_now() <= event_ts + INTERVAL '30 days';
  200. > CREATE DEFAULT INDEX ON events_view;
  201. > INSERT INTO events SELECT x::text, now() FROM generate_series(1, 1000) AS x;
  202. # TODO: The following query should return 2000, but it returns 1000 because the
  203. # arrangement sizes does not account for the temporal bucket. It is part of
  204. # a different operator.
  205. > SELECT records FROM mz_introspection.mz_dataflow_arrangement_sizes
  206. WHERE name LIKE '%events_view_primary_idx';
  207. 1000
  208. > DROP TABLE events CASCADE;
  209. > DROP CLUSTER test;
  210. # Test that a constant collection is not expired on a cluster with replication factor 0
  211. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  212. ALTER SYSTEM SET compute_replica_expiration_offset = '30d';
  213. > CREATE CLUSTER test (SIZE = '1', REPLICATION FACTOR = 0);
  214. > SET CLUSTER TO test;
  215. # `now()` cannot be materialized.
  216. > CREATE MATERIALIZED VIEW events_mv AS
  217. SELECT x::text AS content, '2024-10-09 07:05:10.318+00'::timestamptz AS event_ts
  218. FROM generate_series(1, 1000) AS x;
  219. > CREATE VIEW events_view AS
  220. SELECT event_ts, content
  221. FROM events_mv
  222. WHERE mz_now() <= event_ts + INTERVAL '30 years';
  223. > CREATE DEFAULT INDEX ON events_view;
  224. > ALTER CLUSTER test SET (REPLICATION FACTOR = 1);
  225. > SELECT count(*) FROM events_view;
  226. 1000
  227. > DROP MATERIALIZED VIEW events_mv CASCADE;
  228. > DROP CLUSTER test CASCADE;