Skip to content

Commit ea22597

Browse files
preserve ordinality in postgres when checking existence of documents (#5533)
1 parent 4ad6aa7 commit ea22597

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

libs/langchain-community/src/indexes/postgres.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ export class PostgresRecordManager implements RecordManagerInterface {
136136
.join(", ");
137137

138138
const query = `
139-
SELECT k, (key is not null) ex from unnest(ARRAY[${arrayPlaceholders}]) k left join ${this.finalTableName} on k=key and namespace = $1;
139+
WITH ordered_keys AS (
140+
SELECT * FROM unnest(ARRAY[${arrayPlaceholders}]) WITH ORDINALITY as t(key, o)
141+
)
142+
SELECT ok.key, (r.key IS NOT NULL) ex
143+
FROM ordered_keys ok
144+
LEFT JOIN ${this.finalTableName} r
145+
ON r.key = ok.key
146+
AND namespace = $1
147+
ORDER BY ok.o;
140148
`;
141149
const res = await this.pool.query(query, [this.namespace, ...keys.flat()]);
142150
return res.rows.map((row: { ex: boolean }) => row.ex);

0 commit comments

Comments
 (0)