postgres.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. import os
  10. from materialize import MZ_ROOT
  11. from materialize.mzcompose import loader
  12. from materialize.mzcompose.service import (
  13. Service,
  14. ServiceConfig,
  15. )
  16. from materialize.mzcompose.services.cockroach import Cockroach
  17. class Postgres(Service):
  18. def __init__(
  19. self,
  20. name: str = "postgres",
  21. mzbuild: str = "postgres",
  22. image: str | None = None,
  23. ports: list[str] = ["5432"],
  24. extra_command: list[str] = [],
  25. environment: list[str] = [
  26. "POSTGRESDB=postgres",
  27. "POSTGRES_PASSWORD=postgres",
  28. "LD_PRELOAD=libeatmydata.so",
  29. ],
  30. volumes: list[str] = [],
  31. max_wal_senders: int = 100,
  32. max_replication_slots: int = 100,
  33. setup_materialize: bool = False,
  34. restart: str = "no",
  35. ) -> None:
  36. command: list[str] = [
  37. "postgres",
  38. "-c",
  39. "wal_level=logical",
  40. "-c",
  41. f"max_wal_senders={max_wal_senders}",
  42. "-c",
  43. f"max_replication_slots={max_replication_slots}",
  44. "-c",
  45. "max_connections=5000",
  46. ] + extra_command
  47. if setup_materialize:
  48. path = os.path.relpath(
  49. MZ_ROOT / "misc" / "postgres" / "setup_materialize.sql",
  50. loader.composition_path,
  51. )
  52. volumes = volumes + [
  53. f"{path}:/docker-entrypoint-initdb.d/z_setup_materialize.sql"
  54. ]
  55. environment = environment + ["PGPORT=26257"]
  56. config: ServiceConfig = {"image": image} if image else {"mzbuild": mzbuild}
  57. config.update(
  58. {
  59. "command": command,
  60. "allow_host_ports": True,
  61. "ports": ports,
  62. "environment": environment,
  63. "healthcheck": {
  64. "test": ["CMD", "pg_isready"],
  65. "interval": "1s",
  66. "start_period": "30s",
  67. },
  68. "restart": restart,
  69. "volumes": volumes,
  70. }
  71. )
  72. super().__init__(name=name, config=config)
  73. class PostgresMetadata(Postgres):
  74. def __init__(self, restart: str = "no") -> None:
  75. super().__init__(
  76. name="postgres-metadata",
  77. setup_materialize=True,
  78. ports=["26257"],
  79. restart=restart,
  80. )
  81. CockroachOrPostgresMetadata = (
  82. Cockroach if os.getenv("BUILDKITE_TAG", "") != "" else PostgresMetadata
  83. )
  84. METADATA_STORE: str = (
  85. "cockroach" if CockroachOrPostgresMetadata == Cockroach else "postgres-metadata"
  86. )