0

Search on Postgres with pg_pinecone

Published 13 days ago

pg_pinecone is a Postgres extension for vector & full-text search. It runs inside the database you already have, and you query it with the same SQL as pgvector. A filter, such as a tenant id, is applied while the search runs, and the rows stay in Postgres. This script connects through a PostgreSQL resource. It creates the `vector` and `pg_pinecone` extensions, loads a small demo table, builds an index, and returns the two closest rows. The database user needs permission to create those extensions. The `pineconeteam/pg_pinecone` image is Postgres with both of them already installed. View the documentation here: https://docs.pinecone.io/guides/pg-pinecone/overview

Script pg_pinecone Verified
Search on Postgres with pg_pinecone

The script

Submitted by anthony761 Bun
Verified 14 days ago
1
import { Client } from "pg";
2

3
type Postgresql = {
4
  host: string;
5
  port: number;
6
  user: string;
7
  dbname: string;
8
  sslmode: string;
9
  password: string;
10
};
11

12
// Point the PostgreSQL resource at a database that has the vector and
13
// pg_pinecone extensions. The database user needs to be allowed to create
14
// them. This script replaces the table windmill_pg_pinecone_docs, builds a
15
// native index, and returns the two closest rows. The query is the same SQL
16
// as pgvector, and the rows stay in Postgres.
17
export async function main(db: Postgresql) {
18
  const client = new Client({
19
    host: db.host,
20
    port: db.port,
21
    user: db.user,
22
    password: db.password,
23
    database: db.dbname,
24
    ssl: sslOption(db.sslmode),
25
  });
26
  await client.connect();
27
  try {
28
    await client.query("CREATE EXTENSION IF NOT EXISTS vector");
29
    await client.query("CREATE EXTENSION IF NOT EXISTS pg_pinecone");
30
    await client.query("DROP TABLE IF EXISTS windmill_pg_pinecone_docs");
31
    await client.query(`
32
      CREATE TABLE windmill_pg_pinecone_docs (
33
        id bigint PRIMARY KEY,
34
        body text,
35
        embedding vector(3)
36
      )
37
    `);
38
    await client.query(`
39
      INSERT INTO windmill_pg_pinecone_docs (id, body, embedding) VALUES
40
        (1, 'The Eiffel Tower was completed in 1889.', '[0.1, 0.1, 0.9]'),
41
        (2, 'Photosynthesis converts sunlight into glucose.', '[0.05, 0.9, 0.1]'),
42
        (4, 'Espresso is brewed by forcing hot water through ground coffee.', '[0.95, 0.05, 0.05]'),
43
        (7, 'A croissant is a flaky pastry made from buttered dough.', '[0.95, 0.05, 0.1]')
44
    `);
45
    await client.query(
46
      "SELECT pinecone.create_index('windmill_pg_pinecone_docs', 'embedding', engine => 'native')",
47
    );
48
    await client.query("SET enable_seqscan = off");
49
    const { rows } = await client.query(
50
      "SELECT id::int AS id, body FROM windmill_pg_pinecone_docs ORDER BY embedding <=> '[1, 0, 0]' LIMIT 2",
51
    );
52
    return rows;
53
  } finally {
54
    await client.end();
55
  }
56
}
57

58
function sslOption(sslmode: string | undefined) {
59
  const mode = (sslmode ?? "").toLowerCase();
60
  if (mode === "" || mode === "disable") return false;
61
  if (mode === "verify-ca" || mode === "verify-full") {
62
    return { rejectUnauthorized: true };
63
  }
64
  return { rejectUnauthorized: false };
65
}