Skip to content

Conversation

thisisharsh7
Copy link
Contributor

fixes #722

feature implemented ->

  • Added schema field to spec for Postgres target
  • When schema is specified, table_name must also be explicitly specified
  • automatically create the schema if not exists
  • it doesn't drop the schema
  • targets from different flows can share the same schema
  • Schema is created but not owned, users can add their own tables

@thisisharsh7
Copy link
Contributor Author

please let me know for any changes thanks!!

Copy link
Member

@georgeh0 georgeh0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn qualified_table_name(table_id: &TableId) -> String {
match &table_id.schema {
Some(schema) => format!("\"{}\".\"{}\"", schema, table_id.table_name),
None => format!("\"{}\"", table_id.table_name),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave table name unquoted for this case for now. Only double-qutes schema names.

We considered adding double-quotes for table names before, but didn't proceed because it'll break existing pipelines. We'll make this change in our next major version upgrade (e.g. v1) which is considered to be backward imcompatible.

Comment on lines 574 to 579
// Create schema if specified
if let Some(schema) = &table_id.schema {
let sql = format!("CREATE SCHEMA IF NOT EXISTS \"{}\"", schema);
sqlx::query(&sql).execute(db_pool).await?;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need to do it when we need to run CREATE TABLE .... We can move it before executing that statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay got it

@georgeh0
Copy link
Member

georgeh0 commented Oct 4, 2025

Can you also describe how you test it end to end? Thanks!

@thisisharsh7
Copy link
Contributor Author

Can you also describe how you test it end to end? Thanks!

thanks! I tested this feature with local postgres server:

  • with both schema and table name -> it works
  • with just schema no table name -> it works too and without schema at all

for database verification:

  • created a schema using CREATE SCHEMA IF NOT EXISTS test_schema
  • verified the schema in the database
  • created a table inside the schema
  • inserted and queried data successfully

@thisisharsh7
Copy link
Contributor Author

image

@georgeh0
Copy link
Member

georgeh0 commented Oct 5, 2025

image

Seems these tests happen on single statements, instead of as part of cocoindex flow?
Did you test it end to end with CocoIndex? For example, pick any existing example with Postgres target, and set the schema, and run setup and update.

pub struct Spec {
database: Option<spec::AuthEntryReference<DatabaseConnectionSpec>>,
table_name: Option<String>,
schema: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at least the field also needs to be added to this dataclass in Python SDK:

class Postgres(op.TargetSpec):
"""Target powered by Postgres and pgvector."""
database: AuthEntryReference[DatabaseConnectionSpec] | None = None
table_name: str | None = None

So that users will be able to use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    database: AuthEntryReference[DatabaseConnectionSpec] | None = None
    table_name: str | None = None
    schema: str | None = None

@thisisharsh7
Copy link
Contributor Author

image

can you please check this if this is correct thanks!

@thisisharsh7
Copy link
Contributor Author

I picked text-embedding example and put the schema then ran the setup nd update cmd

@georgeh0
Copy link
Member

georgeh0 commented Oct 6, 2025

Can you also help with updating this doc: https://github.com/cocoindex-io/cocoindex/blob/main/docs/docs/sources/index.md?plain=1

Thanks!

Thanks! Looks good! Can you also help update the doc in the location above? Thanks!

@thisisharsh7
Copy link
Contributor Author

yes

@thisisharsh7
Copy link
Contributor Author

what should I include also do I need to update target file? some pointers

@georgeh0
Copy link
Member

georgeh0 commented Oct 6, 2025

what should I include also do I need to update target file? some pointers

Sorry I gave the wrong link. It should be for the target, not for the source: https://github.com/cocoindex-io/cocoindex/blob/main/docs/docs/targets/postgres.md?plain=1

The description for new schema field in the spec should be quite similar to table_name.

@thisisharsh7
Copy link
Contributor Author

image

please check this is this right

@georgeh0
Copy link
Member

georgeh0 commented Oct 6, 2025

image please check this is this right

Looks good. Please push the change. Thanks!

@georgeh0
Copy link
Member

georgeh0 commented Oct 6, 2025

Looks good! Thanks for the PR. Merging.

@georgeh0 georgeh0 merged commit c0827ca into cocoindex-io:main Oct 6, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Allow users to specify target schema for Postgres targets
3 participants