{"record":{"id":"f71e9c356a45029d","repo":"cocoindex-io/cocoindex","slug":"column-column-has-postgresql-type-pg-type","errorCode":null,"errorMessage":"Column '{column}' has PostgreSQL type '{pg_type}', which is not a pgvector type.","messagePattern":"Column '(.+?)' has PostgreSQL type '(.+?)', which is not a pgvector type\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/connectors/postgres/_target.py","lineNumber":469,"sourceCode":"\n_PGVECTOR_OP_CLASS: dict[str, dict[str, str]] = {\n    \"vector\": {\n        \"cosine\": \"vector_cosine_ops\",\n        \"l2\": \"vector_l2_ops\",\n        \"ip\": \"vector_ip_ops\",\n    },\n    \"halfvec\": {\n        \"cosine\": \"halfvec_cosine_ops\",\n        \"l2\": \"halfvec_l2_ops\",\n        \"ip\": \"halfvec_ip_ops\",\n    },\n}\n\n\ndef _pgvector_op_class(column: str, pg_type: str, metric: str) -> str:\n    type_base = _pgvector_type_base(pg_type)\n    if type_base is None:\n        raise ValueError(\n            f\"Column '{column}' has PostgreSQL type '{pg_type}', which is not a pgvector type.\"\n        )\n\n    try:\n        return _PGVECTOR_OP_CLASS[type_base][metric]\n    except KeyError as e:\n        raise ValueError(\n            f\"Unsupported pgvector metric '{metric}' for PostgreSQL type '{pg_type}'.\"\n        ) from e\n\n\nclass _VectorIndexSpec(NamedTuple):\n    column: str\n    metric: str\n    op_class: str\n    method: str\n    lists: int | None\n    m: int | None","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/connectors/postgres/_target.py#L451-L487","documentation":"Raised by the Postgres connector when declaring a vector index on a column whose PostgreSQL type is not a pgvector type. The library only supports HNSW/IVFFlat indexes on `vector` and `halfvec` columns (checked via `_pgvector_type_base` against `_PGVECTOR_TYPE_BASES`). Any other column type (text, bytea, plain arrays, etc.) cannot carry a pgvector op class, so indexing it as a vector index fails fast.","triggerScenarios":"Calling `table.declare_vector_index(column=..., metric=...)` where the column's ColumnDef.type (from the table schema) is not `vector` or `halfvec` — e.g. the column was declared as `text[]`, `jsonb`, `bytea`, or an annotated PgType that maps to a non-pgvector type.","commonSituations":"Pointing declare_vector_index at the wrong column name (a typo selects an id/text column); embedding a column stored as JSON instead of a typed vector column; forgetting the vector type annotation on the field so it maps to a default Postgres type; upgrading from an older schema where the column type changed.","solutions":["Check the column name passed to declare_vector_index — verify it in the declared table schema against `self._table_schema.columns`.","Annotate the field with a vector type (e.g. via PgType.Vector or a vector schema annotation) so it maps to the PostgreSQL `vector` type.","If the column is `halfvec`-intended, ensure the type annotation maps to `halfvec` (both are supported).","Remove the declare_vector_index call for columns that genuinely hold non-vector data."],"exampleFix":"// before\ntable.declare_vector_index(column=\"description\", metric=\"cosine\")  # description is text\n// after\ntable.declare_vector_index(column=\"description_embedding\", metric=\"cosine\")  # vector column","handlingStrategy":"validation","validationCode":"def is_pgvector_column(col_type: str) -> bool:\n    base = col_type.strip().split(\"(\")[0]\n    return base in {\"vector\", \"halfvec\"}\n\n# before calling:\n# assert is_pgvector_column(schema.columns[col].type)","typeGuard":"def _is_pgvector_type(pg_type: str) -> bool:\n    return pg_type.strip().split('(')[0] in {'vector', 'halfvec'}","tryCatchPattern":"try:\n    table.declare_vector_index(column=col, metric=\"cosine\")\nexcept ValueError as e:\n    if \"not a pgvector type\" in str(e):\n        logger.error(\"Column %s must be vector/halfvec type\", col)\n    else:\n        raise","preventionTips":["Keep embedding columns explicitly typed as vector via the vector/PgType annotations.","Centralize column names in constants to avoid typos in index declarations.","Assert column types right after building the table schema."],"tags":["postgres","pgvector","vector-index","schema"],"backgroundTag":"incompatible-source-type","analyzedSha":"e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b","analyzedAt":"2026-09-08T15:59:19.997Z","contentChangedAt":"2026-09-08T15:59:19.997Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}