Skip to content

Commit 2574ef2

Browse files
ff137cursoragent
andcommitted
feat(postgres): tag connections with application_name
Set application_name on every Postgres connection so each session is identifiable in pg_stat_activity. No behaviour change. Naming: - Shared Knex pool: odota-<APP_NAME> - runReliableQueue dedicated Client: odota-<APP_NAME>-queue - Explorer readonly Pool: odota-<APP_NAME>-explorer - archivePostgresStream Client: odota-<APP_NAME>-archive APP_NAME is set per process by ecosystem.config.js (e.g. inserter, scanner, web, parser); falls back to "unknown" when run outside PM2. With ~30 backend roles plus a clustered web role, this makes it possible to answer "which service is holding connections?" with a single query: SELECT application_name, state, count(*) FROM pg_stat_activity GROUP BY 1, 2 ORDER BY 3 DESC; Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 04c0b54 commit 2574ef2

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

svc/api/spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import { matchupToString } from "../util/matchups.ts";
9393

9494
const pool = new Pool({
9595
connectionString: config.READONLY_POSTGRES_URL,
96+
application_name: `odota-${config.APP_NAME || "unknown"}-explorer`,
9697
statement_timeout: 15000,
9798
query_timeout: 15000,
9899
lock_timeout: 15000,

svc/store/db.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ console.log(
1414

1515
export const db = knex({
1616
client: "pg",
17-
connection: config.POSTGRES_URL,
17+
connection: {
18+
connectionString: config.POSTGRES_URL,
19+
application_name: `odota-${config.APP_NAME || "unknown"}`,
20+
},
1821
pool: {
1922
min: 0,
2023
max: Number(config.POSTGRES_MAX_CONNECTIONS),

svc/store/queue.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ export async function runReliableQueue(
5555
getCapacity?: () => Promise<number>,
5656
) {
5757
const executor = async (i: number) => {
58-
const consumer = new Client(config.POSTGRES_URL);
58+
const consumer = new Client({
59+
connectionString: config.POSTGRES_URL,
60+
application_name: `odota-${config.APP_NAME || "unknown"}-queue`,
61+
});
5962
await consumer.connect();
6063
while (true) {
6164
// If we have a way to measure capacity, throttle the processing speed based on capacity

svc/util/archiveUtil.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ export async function archivePostgresStream() {
4343
ORDER BY match_id asc`,
4444
[],
4545
);
46-
const pg = new Client(config.POSTGRES_URL);
46+
const pg = new Client({
47+
connectionString: config.POSTGRES_URL,
48+
application_name: `odota-${config.APP_NAME || "unknown"}-archive`,
49+
});
4750
await pg.connect();
4851
const stream = pg.query(query);
4952
let i = 0;

0 commit comments

Comments
 (0)