pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #737: Bulk Upsert into PGVector Database
4+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #737: Bulk Upsert into PGVector Database
@ 2024-12-23 05:02 "svim-ig (@svim-ig)" <noreply+svim-ig@github.com>
  0 siblings, 0 replies; 4+ messages in thread

From: svim-ig (@svim-ig) @ 2024-12-23 05:02 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I am unable to Upsert data having more than 4000 embeddings into PGVector Database in Azure from Azure databricks  using psycopg2....is there any best practices for bulk upsert into PGVector database or Postgress from databricks using parallelization of nodes in databricks without breaking data? I am having 1M records                                                     
import psycopg2
from psycopg2.extras import execute_values
### Typical Code is as below:
  def upsert_partition(partition, table_name):
        conn = psycopg2.connect(
                    host="myhost.postgres.database.azure.com",
                   database="postgres",
                    port=5432, 
                   user="admin", 
                   password="password"
                    )
    cur = conn.cursor()

    values = [
        (row['col1'],  
         row['col2'], 
         row['col3'], 
         row['col4'], # product description
         row['col5']) # Product embeddings
        for row in partition
    ]
    
    # Loop through the values and execute the upsert one at a time 
    insert_query = f"""
    INSERT INTO {table_name} 
    (col1, col2, col3, col4, col5)
    VALUES %s
    ON CONFLICT (col1, col2, col3) ...key column combination
    DO UPDATE
    SET
        col4 = EXCLUDED.col4,
        col5 = EXCLUDED.col5;
    """
    
    try:
        # Insert all values at once using execute_values for efficiency (no batching)
        execute_values(cur, insert_query, values)
        conn.commit()
    except Exception as e:
        conn.rollback()
        print(f"Error executing upsert: {e}")
        raise e
    finally:
        cur.close()
        conn.close()
upsert_df = upsert_df.repartition(80) (dataset with 1M records)
 df1_upsert.rdd.foreachPartition(lambda partition: upsert_partition(partition, table_name))

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #737: Bulk Upsert into PGVector Database
@ 2024-12-23 14:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2024-12-23 14:39 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @svim-ig, I'm not sure what error you're seeing, but two options that come to mind are:

1. Upsert in batches
2. `COPY` into a new/temporary table, then upsert from there

(in any case, I don't think this is specific to pgvector)

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #737: Bulk Upsert into PGVector Database
@ 2024-12-23 14:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2024-12-23 14:39 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #737: Bulk Upsert into PGVector Database
@ 2024-12-26 05:42 ` "svim-ig (@svim-ig)" <noreply+svim-ig@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: svim-ig (@svim-ig) @ 2024-12-26 05:42 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks for response and clarifying...already tried with batching too...all records not getting upserted even without showing error where gone wrong. will check with second method of COPY

^ permalink  raw  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2024-12-26 05:42 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-12-23 05:02 [pgvector/pgvector] issue #737: Bulk Upsert into PGVector Database "svim-ig (@svim-ig)" <noreply+svim-ig@github.com>
2024-12-23 14:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-12-23 14:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-12-26 05:42 ` "svim-ig (@svim-ig)" <noreply+svim-ig@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox