{"record":{"id":"ef56157028206f5d","repo":"risingwavelabs/risingwave","slug":"postgres-sink-does-not-support-target-table","errorCode":null,"errorMessage":"Postgres sink does not support target table \"{}\".\"{}\" with foreign key constraints. Please remove foreign key constraints from the target table or choose a different sink table.","messagePattern":"Postgres sink does not support target table \"(.+?)\"\\.\"(.+?)\" with foreign key constraints\\. Please remove foreign key constraints from the target table or choose a different sink table\\.","errorType":"validation","errorClass":"SinkError::Config","httpStatus":null,"severity":"error","filePath":"src/connector/src/sink/postgres.rs","lineNumber":140,"sourceCode":"    )\n    .await?;\n\n    ensure_no_foreign_key_with_client(&client, &config.schema, &config.table).await\n}\n\nasync fn ensure_no_foreign_key_with_client(\n    client: &tokio_postgres::Client,\n    schema: &str,\n    table: &str,\n) -> Result<()> {\n    let has_foreign_key = client\n        .query_one(CHECK_FOREIGN_KEY_SQL, &[&schema, &table])\n        .await\n        .context(\"failed to check foreign key constraints\")?\n        .get::<_, bool>(0);\n\n    if has_foreign_key {\n        return Err(SinkError::Config(anyhow!(\n            \"Postgres sink does not support target table \\\"{}\\\".\\\"{}\\\" with foreign key constraints. Please remove foreign key constraints from the target table or choose a different sink table.\",\n            schema,\n            table,\n        )));\n    }\n\n    Ok(())\n}\n\nimpl PostgresConfig {\n    pub fn from_btreemap(properties: BTreeMap<String, String>) -> Result<Self> {\n        let config =\n            serde_json::from_value::<PostgresConfig>(serde_json::to_value(properties).unwrap())\n                .map_err(|e| SinkError::Config(anyhow!(e)))?;\n        if config.r#type != SINK_TYPE_APPEND_ONLY && config.r#type != SINK_TYPE_UPSERT {\n            return Err(SinkError::Config(anyhow!(\n                \"`{}` must be {}, or {}\",\n                SINK_TYPE_OPTION,","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/risingwavelabs/risingwave/blob/6469eb736d691e8e9b8a419a57edd6429ca77417/src/connector/src/sink/postgres.rs#L122-L158","documentation":"`ensure_no_foreign_key_with_client` queries the target Postgres database for foreign key constraints on the sink target table and refuses to proceed if any exist, because RisingWave cannot maintain referential constraints on rows it upserts. The error names the offending `schema.table`.","triggerScenarios":"Creating a Postgres sink with `type='upsert'` (or sink type requiring direct table writes) where the destination table `schema.table` has FK constraints referencing or referenced by other tables; raised from `ensure_no_foreign_key` during writer `new`/validation.","commonSituations":"Sinking into an existing Postgres table that is part of a normalized relational schema with FKs; DBA-managed schemas where FKs are standard practice.","solutions":["Drop the foreign key constraints on the target table before creating the sink: `ALTER TABLE schema.table DROP CONSTRAINT <fk_name>;`","Sink into a different, constraint-free staging table instead","Sink into a fresh table dedicated to RisingWave output without FKs"],"exampleFix":"// before\nALTER TABLE public.target ADD CONSTRAINT fk_parent FOREIGN KEY (pid) REFERENCES parents(id);\n// after (remove FK so the sink can target the table)\nALTER TABLE public.target DROP CONSTRAINT fk_parent;","handlingStrategy":"validation","validationCode":"-- run against the target database before creating the sink\nSELECT COUNT(*) > 0 AS has_fk\nFROM information_schema.table_constraints tc\nJOIN information_schema.constraint_column_usage ccu\n  ON tc.constraint_name = ccu.constraint_name\nWHERE tc.table_schema = $1 AND tc.table_name = $2\n  AND tc.constraint_type = 'FOREIGN KEY';\n-- expect 0 rows / false","typeGuard":null,"tryCatchPattern":"match ensure_no_foreign_key(&client, &schema, &table).await {\n    Ok(()) => proceed(),\n    Err(e) => plan_fk_removal(&schema, &table, e),\n}","preventionTips":["Check FKs with information_schema before pointing a sink at an existing table","Use dedicated staging tables without FK constraints for RisingWave output","Coordinate with the DBA before dropping constraints"],"tags":["postgres","sink","schema-constraint","upsert"],"backgroundTag":"invalid-config-value","analyzedSha":"6469eb736d691e8e9b8a419a57edd6429ca77417","analyzedAt":"2026-09-11T21:06:21.487Z","contentChangedAt":"2026-09-11T21:06:21.487Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}