sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Can't read pg stats

Open nexovec opened this issue 2 weeks ago • 0 comments

Version

1.30.0

What happened?

I tried to sqlc generate based on an otherwise working sql select.

Relevant log output

relation "pg_stat_activity" does not exist

Database schema

you already have everything.

SQL queries

-- name: PostgresHealthcheck :one
SELECT
    jsonb_build_object(
    -- 1. General Server/Database Info
    'server_version', version(),
    'db_size_bytes', pg_database_size(current_database()),
    
    -- 2. Connection and Load Metrics
    'total_connections', (SELECT count(*) FROM pg_stat_activity),
    'active_queries', (SELECT count(*) FROM pg_stat_activity WHERE state = 'active'),
    'idle_in_txn', (SELECT count(*) FROM pg_stat_activity WHERE state = 'idle in transaction'),
    
    -- 3. Potential Problem Indicators: Long-running Queries
    'max_query_duration_seconds', (SELECT
        COALESCE(MAX(EXTRACT(EPOCH FROM (NOW() - query_start))), 0)
     FROM
        pg_stat_activity
     WHERE
        state != 'idle' AND backend_type = 'client backend'
    ),
    
    -- 4. Potential Problem Indicators: Locks
    'waiting_locks', (SELECT count(*) FROM pg_locks WHERE NOT granted),
    
    -- 5. System/Storage Information (Requires superuser or specific permissions)
    'max_connections_limit', (SELECT setting FROM pg_settings WHERE name = 'max_connections'),
    'shared_buffers_setting', (SELECT setting FROM pg_settings WHERE name = 'shared_buffers')
);

Configuration

version: "2"
plugins:
  - name: py
    wasm:
      url: https://downloads.sqlc.dev/plugin/sqlc-gen-python_1.3.0.wasm
      sha256: fbedae96b5ecae2380a70fb5b925fd4bff58a6cfb1f3140375d098fbab7b3a3c
sql:
  - schema: "migrate.sql"
    queries: "queries.sql"
    engine: postgresql
    codegen:
      - out: app/db
        plugin: py
        options:
          package: db
          emit_async_querier: true
          emit_sync_querier: false
          emit_pydantic_models: true

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Python

nexovec avatar Dec 08 '25 10:12 nexovec