statement_logging.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. from textwrap import dedent
  10. from materialize.checks.actions import Testdrive
  11. from materialize.checks.checks import Check
  12. class StatementLogging(Check):
  13. def initialize(self) -> Testdrive:
  14. return Testdrive(
  15. dedent(
  16. """
  17. $ postgres-execute connection=postgres://mz_system@${testdrive.materialize-internal-sql-addr}
  18. ALTER SYSTEM SET statement_logging_max_sample_rate TO 1.0
  19. """
  20. )
  21. )
  22. def manipulate(self) -> list[Testdrive]:
  23. return [
  24. Testdrive(dedent(s))
  25. for s in [
  26. """
  27. > SET statement_logging_sample_rate TO 1.0
  28. > SELECT 'hello' /* Btv was here */;
  29. hello
  30. $ sleep-is-probably-flaky-i-have-justified-my-need-with-a-comment duration="5s"
  31. """,
  32. """
  33. > SET statement_logging_sample_rate TO 1.0
  34. > SELECT 'goodbye' /* Btv was here */;
  35. goodbye
  36. $ sleep-is-probably-flaky-i-have-justified-my-need-with-a-comment duration="5s"
  37. """,
  38. ]
  39. ]
  40. def validate(self) -> Testdrive:
  41. if self.base_version == self.current_version:
  42. return Testdrive(
  43. dedent(
  44. """
  45. > SELECT sql, finished_status FROM mz_internal.mz_statement_execution_history mseh, mz_internal.mz_prepared_statement_history mpsh, (SELECT DISTINCT sql, sql_hash, redacted_sql FROM mz_internal.mz_sql_text) mst WHERE mseh.prepared_statement_id = mpsh.id AND mst.sql_hash = mpsh.sql_hash AND sql LIKE '%/* Btv was here */' ORDER BY mseh.began_at;
  46. "SELECT 'hello' /* Btv was here */" success
  47. "SELECT 'goodbye' /* Btv was here */" success
  48. """
  49. )
  50. )
  51. else:
  52. # Rows are expected to maybe disappear across versions
  53. return Testdrive(
  54. dedent(
  55. """
  56. > SELECT count(*) <= 2 FROM mz_internal.mz_statement_execution_history mseh, mz_internal.mz_prepared_statement_history mpsh, (SELECT DISTINCT sql, sql_hash, redacted_sql FROM mz_internal.mz_sql_text) mst WHERE mseh.prepared_statement_id = mpsh.id AND mpsh.sql_hash = mst.sql_hash AND sql LIKE '%/* Btv was here */'
  57. true
  58. """
  59. )
  60. )