Skip to content

Commit afaa366

Browse files
committed
fix: duplicate column results
1 parent 659f5dc commit afaa366

1 file changed

Lines changed: 29 additions & 10 deletions

File tree

src/sync/pg-connector.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type ColumnMetadata = {
3232
};
3333
export type TableMetadata = {
3434
tableName: string;
35+
schemaName: string;
3536
rowCountEstimate: number;
3637
pageCount: number;
3738
columns: ColumnMetadata[];
@@ -145,6 +146,8 @@ LEFT JOIN LATERAL (
145146
ON ref_ns.oid = ref_cl.relnamespace
146147
WHERE
147148
pc.contype = 'f' -- 'f' stands for foreign key
149+
AND pa.attnum > 0 -- Skip system columns
150+
AND pa.attisdropped = false -- Skip dropped columns
148151
AND pgn.nspname = ${schema}
149152
AND pgc.relname = pg_tables.tablename
150153
GROUP BY
@@ -263,12 +266,15 @@ ORDER BY
263266
options: DependencyAnalyzerOptions
264267
): Promise<SerializeResult> {
265268
const schema = await this.getSchema(schemaName);
266-
const mkKey = (table: string, column: string) =>
267-
`${table.toLowerCase()}:${column}`;
269+
const mkKey = (schema: string, table: string, column: string) =>
270+
`${schema.toLowerCase()}:${table.toLowerCase()}:${column}`;
268271
const schemaMap = new Map<string, ColumnMetadata>();
269272
for (const table of schema) {
270273
for (const column of table.columns) {
271-
schemaMap.set(mkKey(table.tableName, column.columnName), column);
274+
schemaMap.set(
275+
mkKey(table.schemaName, table.tableName, column.columnName),
276+
column
277+
);
272278
}
273279
}
274280
const comments = [
@@ -297,7 +303,9 @@ ORDER BY
297303

298304
// TODO: batch this into a single query
299305
for (const [table, rows] of Object.entries(tables)) {
300-
const tableSchema = schema.find((s) => s.tableName === table);
306+
const tableSchema = schema.find(
307+
(s) => s.tableName === table && s.schemaName === schemaName
308+
);
301309
const allCtids = rows.map((row) => row[ctidSymbol]);
302310
if (!tableSchema) {
303311
console.warn(`No schema found for ${table}. Skipping.`);
@@ -368,6 +376,7 @@ ORDER BY
368376
const results = await this.sql<TableMetadata[]>`
369377
SELECT
370378
c.table_name as "tableName",
379+
n.nspname as "schemaName",
371380
cl.reltuples as "rowCountEstimate",
372381
cl.relpages as "pageCount",
373382
json_agg(
@@ -384,25 +393,35 @@ ORDER BY
384393
'histogramBounds', s.histogram_bounds
385394
)
386395
from pg_stats s
387-
where s.schemaname = ${schemaName}
396+
where
397+
s.schemaname = n.nspname
388398
and s.tablename = c.table_name
389399
and s.attname = c.column_name
400+
-- Skip dropped columns (they still exist in the schema)
401+
and a.attisdropped = false
402+
-- Skip system columns
403+
and a.attnum > 0
390404
)
391405
)
392406
ORDER BY c.ordinal_position) as columns
393407
FROM
394408
information_schema.columns c
409+
JOIN
410+
pg_namespace n
411+
ON n.nspname = c.table_schema
395412
JOIN
396413
pg_class cl
397-
ON cl.relname = c.table_name
414+
ON cl.relname = c.table_name and cl.relnamespace = n.oid
398415
JOIN
399-
pg_namespace n
400-
ON n.oid = cl.relnamespace
416+
pg_attribute a
417+
ON a.attrelid = (quote_ident(c.table_schema) || '.' || quote_ident(c.table_name))::regclass
418+
AND a.attname = c.column_name
401419
WHERE
402-
c.table_schema = ${schemaName}
420+
n.nspname = ${schemaName}
421+
and c.table_name not like 'pg_%'
403422
and c.table_name not in ('pg_stat_statements', 'pg_stat_statements_info')
404423
GROUP BY
405-
c.table_name, cl.reltuples, cl.relpages; -- @qd_introspection
424+
n.nspname, c.table_name, cl.reltuples, cl.relpages; -- @qd_introspection
406425
`;
407426
return results;
408427
}

0 commit comments

Comments
 (0)