---
apiVersion: v1
kind: ConfigMap
metadata:
  name: timescaledb-rmdc-config
  namespace: cmii-rmdc
data:
  postgresql.conf: |
    # ========== 内存核心参数优化(32GB) ==========
    # shared_buffers: 32GB以下推荐25%总内存
    shared_buffers = 8GB
    
    # effective_cache_size: 推荐50-75%总内存
    effective_cache_size = 24GB
    
    # work_mem: (Total RAM * 0.25) / max_connections
    # (32GB * 0.25) / 150 ≈ 54MB,向上调整至128MB
    work_mem = 128MB
    
    # maintenance_work_mem: 建议1-2GB,32GB内存可提升至2GB
    maintenance_work_mem = 2GB
    
    # ========== WAL相关参数 ==========
    wal_buffers = 32MB
    min_wal_size = 4GB
    max_wal_size = 16GB
    checkpoint_completion_target = 0.9
    wal_compression = on
    
    # ========== 查询规划器参数 ==========
    default_statistics_target = 100
    random_page_cost = 1.1
    effective_io_concurrency = 200
    
    # ========== 并行查询参数 ==========
    max_worker_processes = 16
    max_parallel_workers_per_gather = 4
    max_parallel_workers = 8
    max_parallel_maintenance_workers = 4
    
    # ========== 连接相关 ==========
    max_connections = 150
    
    # ========== TimescaleDB专用优化 ==========
    timescaledb.max_background_workers = 8
    shared_preload_libraries = 'timescaledb'
    
    # TimescaleDB压缩优化(针对时序数据)
    timescaledb.max_background_workers = 8
    
    # ========== 日志配置 ==========
    logging_collector = on
    log_min_duration_statement = 1000
    log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '
    log_checkpoints = on
    
    # ========== 自动清理优化 ==========
    autovacuum_max_workers = 4
    autovacuum_naptime = 10s
    autovacuum_vacuum_scale_factor = 0.05
    autovacuum_analyze_scale_factor = 0.02
    
    # ========== 其他性能优化 ==========
    checkpoint_timeout = 15min
    bgwriter_delay = 200ms
    bgwriter_lru_maxpages = 100
    bgwriter_lru_multiplier = 2.0

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: timescaledb-rmdc
  namespace: cmii-rmdc
spec:
  serviceName: timescaledb-rmdc-svc
  replicas: 1
  selector:
    matchLabels:
      app: timescaledb-rmdc
  template:
    metadata:
      labels:
        app: timescaledb-rmdc
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: In
                    values:
                      - worker-1-192.168.40.61
      imagePullSecrets:
        - name: harborsecret
      containers:
        - name: timescaledb
          image: harbor.wdd.io:8033/rmdc/timescaledb:2.24.0-pg17
          ports:
            - containerPort: 5432
              name: postgresql
          env:
            - name: POSTGRES_PASSWORD
              value: "Super@Boge.123"
            - name: POSTGRES_USER
              value: "postgres"
            - name: POSTGRES_DB
              value: "timescaledb"
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
            - name: TS_TUNE_MEMORY
              value: "32GB"
            - name: TS_TUNE_NUM_CPUS
              value: "16"
          resources:
            requests:
              memory: "16Gi"
              cpu: "8"
            limits:
              memory: "32Gi"
              cpu: "16"
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
            - name: config
              mountPath: /etc/postgresql/postgresql.conf
              subPath: postgresql.conf
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - pg_isready -U postgres
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - pg_isready -U postgres
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 3
      volumes:
        - name: config
          configMap:
            name: timescaledb-rmdc-config
        - name: data
          hostPath:
            path: /var/lib/docker/postgresql_data
---
---
apiVersion: v1
kind: Service
metadata:
  name: timescaledb-rmdc-svc
  namespace: cmii-rmdc
spec:
  type: NodePort
  ports:
    - port: 5432
      targetPort: 5432
      nodePort: 35435
      protocol: TCP
      name: postgresql
  selector:
    app: timescaledb-rmdc
---
apiVersion: v1
kind: Service
metadata:
  name: timescaledb-rmdc-svc-headless
  namespace: cmii-rmdc
spec:
  clusterIP: None
  ports:
    - port: 5432
      targetPort: 5432
      protocol: TCP
      name: postgresql
  selector:
    app: timescaledb-rmdc