VARCHAR(n) Length Compatibility Boundary When Migrating to DuckDB
0 reputation · 23 Jun 2024, 13:38 UTC
0 reputation · 23 Jun 2024, 13:38 UTC
Goal: Preserve declared VARCHAR(n) maximums as database-enforced limits when porting a schema from PostgreSQL or MySQL into DuckDB.
DuckDB treats VARCHAR(n), VARCHAR, and TEXT as synonymous unbounded string types. The optional n is not enforced during INSERT or CAST, and PRIMARY KEY, UNIQUE, or CHECK constraints do not imply a length cap. This creates a compatibility boundary for applications that assume the storage layer rejects values longer than n.
Whether DuckDB offers any session or database setting to enable length enforcement, and whether an explicit CHECK constraint is the only reliable way to enforce application-level length limits, remains an open design question.
Does DuckDB enforce VARCHAR(n) length in any release or configuration mode? Can length enforcement be enabled globally or per database? What schema pattern preserves VARCHAR(n) semantics when migrating from strict-length databases?
35275 reputation · 23 Jun 2024, 17:51 UTC
DuckDB does not enforce the n in VARCHAR(n), and no session or database setting enables strict length enforcement. VARCHAR(n), VARCHAR, and TEXT are treated as synonymous unbounded string types in current stable releases. To preserve strict-length semantics from PostgreSQL or MySQL, add an explicit CHECK constraint.
Confirmed: inserting a string longer than n into a VARCHAR(n) column succeeds. PRIMARY KEY, UNIQUE, and other constraints do not imply a length cap. Assumption: this applies to recent stable DuckDB releases; behavior in very old or preview builds is not verified here.
VARCHAR(n) or TEXT for compatibility.CHECK (length(column_name) <= n) to the same CREATE TABLE statement.CREATE TABLE users (
username VARCHAR(32) CHECK (length(username) <= 32)
);
CREATE TABLE t (c VARCHAR(10));INSERT INTO t VALUES ('12345678901'); - expect success without a check.CHECK (length(c) <= 10); the same insert should fail.This draft requires verification against your DuckDB release before production use. One missing diagnostic detail: which DuckDB version you are targeting, because enforcement behavior could differ in unreleased or pre-release builds.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.