{"record":{"id":"c5b15e0f395ce919","repo":"multica-ai/multica","slug":"connect-to-database-w-c5b15e","errorCode":null,"errorMessage":"connect to database: %w","messagePattern":"connect to database: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/cmd/backfill_task_usage_hourly/main.go","lineNumber":88,"sourceCode":"\t\tsleep        = flag.Duration(\"sleep-between-slices\", 0, \"pause this long between monthly slices to throttle source-table read pressure on a busy DB (e.g. 2s)\")\n\t)\n\tflag.Parse()\n\n\tdbURL := os.Getenv(\"DATABASE_URL\")\n\tif dbURL == \"\" {\n\t\tdbURL = \"postgres://multica:multica@localhost:5432/multica?sslmode=disable\"\n\t}\n\n\t// SIGINT/SIGTERM cancels ctx so an in-flight slice stops cleanly —\n\t// each slice runs in its own transaction (the window function), so\n\t// Postgres rolls back the interrupted one and the idempotent design\n\t// lets a later run resume from where this one stopped.\n\tctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)\n\tdefer stop()\n\n\tpool, err := pgxpool.New(ctx, dbURL)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"connect to database: %w\", err)\n\t}\n\tdefer pool.Close()\n\n\tif err := pool.Ping(ctx); err != nil {\n\t\treturn fmt.Errorf(\"ping database: %w\", err)\n\t}\n\n\t// Serialise against the cron rollup and any other backfill run via\n\t// advisory lock 4246 — the same id the cron entry checks with\n\t// pg_try_advisory_lock. While this backfill holds it, the cron tick\n\t// no-ops instead of racing on task_usage_hourly row locks; a second\n\t// concurrent backfill blocks here until this one finishes. The lock\n\t// is held on a dedicated session connection for the whole run.\n\tlockConn, err := pool.Acquire(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"acquire advisory-lock connection: %w\", err)\n\t}\n\tdefer lockConn.Release()","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/multica-ai/multica/blob/2c0912b6ec764b373d44eeea1e80f0d9f11ab417/server/cmd/backfill_task_usage_hourly/main.go#L70-L106","documentation":"pgxpool.New failed while parsing the DSN or initializing the connection pool for the task_usage_hourly backfill command. pgxpool.New only validates the config (connection string syntax, pool sizes, timeouts) — it does not contact the server, so this error almost always means the DSN itself is malformed or contains unsupported parameters. The default DSN is postgres://multica:multica@localhost:5432/multica?sslmode=disable, overridden by the command's -db flag/env.","triggerScenarios":"Passing a DSN with a syntax error (e.g. missing scheme, bad query params), an unknown runtime parameter in the query string, or invalid pool settings (min_size > max_size). Also using a URL with special characters in the password that are not percent-encoded.","commonSituations":"Local dev without Postgres running is NOT this error (that surfaces at Ping); this is typos in DATABASE_URL, copy-pasting a libpq key=value string where a URL is expected, or %-unencoded passwords like p@ssw0rd.","solutions":["Print/inspect the exact DSN being passed (flag or env) and validate its syntax with psql \"<dsn>\" -c 'select 1'","Percent-encode reserved characters in the password portion of the URL","Confirm every query parameter (sslmode, pool_max_conns, etc.) is spelled correctly and supported by pgxpool","If the error text mentions parse failure on a key=value string, switch to URL form or a fully qualified libpq string pgx accepts"],"exampleFix":"// before\nexport DATABASE_URL='postgres://multica:p@ss@localhost:5432/multica'\n\n// after\nexport DATABASE_URL='postgres://multica:p%40ss@localhost:5432/multica'","handlingStrategy":"validation","validationCode":"if _, err := pgxpool.ParseConfig(dsn); err != nil {\n    log.Fatalf(\"invalid DSN: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"if err != nil {\n    return fmt.Errorf(\"connect to database: %w\", err)\n}","preventionTips":["Centralize the DSN in one env var and validate it in CI with pgxpool.ParseConfig","Percent-encode passwords embedded in URL-form DSNs","Fail fast at startup rather than deep in a backfill run"],"tags":["go","postgres","pgx","config","dsn"],"backgroundTag":null,"analyzedSha":"2c0912b6ec764b373d44eeea1e80f0d9f11ab417","analyzedAt":"2026-08-15T13:25:18.241Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}