# PostgreSQL Service
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: __NAMESPACE__
  labels:
    app: postgres
spec:
  type: ClusterIP
  selector:
    app: postgres
  ports:
  - name: tcp
    port: 5432
    targetPort: 5432
---
# PostgreSQL StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
  namespace: __NAMESPACE__
  labels:
    app: postgres
spec:
  serviceName: postgres
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
        # Disable Istio injection for database
        sidecar.istio.io/inject: "false"
    spec:
      containers:
      - name: postgres
        # pgvector/pgvector:pg16-trixie is postgres:16 plus the pgvector extension.
        # file_search runs `CREATE EXTENSION IF NOT EXISTS vector` at startup;
        # without this image it fails closed (logs and disables the feature,
        # never throws).
        #
        # Pinned to "-trixie", not plain "pg16": plain `postgres:16` and
        # `pgvector/pgvector:pg16` currently build on different Debian releases
        # (trixie/glibc 2.41 vs bookworm/glibc 2.36). An existing PVC upgrading
        # in place — same volume, image swap only — would keep postgres
        # text-collation data (including admin's CAP schema, which shares this
        # database) built under one glibc and read under another: a silent
        # wrong-results/index-corruption hazard, not a startup failure.
        # "-trixie" matches plain postgres:16's base exactly, so an in-place
        # upgrade never crosses a glibc boundary. See
        # docs/developer/release.md for the remediation if a
        # differently-based pgvector image is ever substituted here.
        image: pgvector/pgvector:pg16-trixie
        envFrom:
        - secretRef:
            name: postgres-env
        ports:
        - containerPort: 5432
        volumeMounts:
        - name: data
          mountPath: /var/lib/postgresql/data
          subPath: postgres-data
        startupProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U $POSTGRES_USER -d $POSTGRES_DB
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 30
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U $POSTGRES_USER -d $POSTGRES_DB
          initialDelaySeconds: 15
          periodSeconds: 5
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U $POSTGRES_USER -d $POSTGRES_DB
          initialDelaySeconds: 30
          periodSeconds: 10
        resources:
          requests:
            memory: "256Mi"
            cpu: "250m"
          limits:
            memory: "1Gi"
            cpu: "500m"
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ReadWriteOnce]
      resources:
        requests:
          storage: 20Gi