// 临时诊断:对照测试列元信息查询的"参数版"与"内联版",定位 42601。 package main import ( "database/sql" "fmt" "log" "strings" _ "github.com/jackc/pgx/v5/stdlib" "fashionapi/internal/config" ) func main() { cfg, err := config.Load("") if err != nil { log.Fatalf("load config: %v", err) } db, err := sql.Open("pgx", cfg.Database.DSN()) if err != nil { log.Fatalf("open: %v", err) } defer db.Close() if err := db.Ping(); err != nil { log.Fatalf("ping: %v", err) } base := ` SELECT c.column_name, c.udt_name, (c.is_nullable = 'YES'), COALESCE(c.column_default, ''), (c.column_default LIKE 'nextval(%') FROM information_schema.columns c WHERE c.table_schema = 'public' AND c.table_name = %s ORDER BY c.ordinal_position` // (a) 参数版:用 $1 paramQ := strings.Replace(base, "%s", "$1", 1) fmt.Println("=== (a) 参数版 $1 ===") testQ(db, paramQ, "brand") // (b) 内联版:把 'brand' 直接拼进 SQL(模拟 simple-protocol 内联) inlineQ := strings.Replace(base, "%s", "'brand'", 1) fmt.Println("=== (b) 内联版 'brand' ===") testQ(db, inlineQ) // (c) 去掉 LIKE 表达式的版本(用 is_identity 代替) noLike := ` SELECT c.column_name, c.udt_name, (c.is_nullable = 'YES'), COALESCE(c.column_default, ''), (c.is_identity = 'YES') FROM information_schema.columns c WHERE c.table_schema = 'public' AND c.table_name = 'brand' ORDER BY c.ordinal_position` fmt.Println("=== (c) 去掉 LIKE 表达式版 ===") testQ(db, noLike) } func testQ(db *sql.DB, q string, args ...any) { rows, err := db.Query(q, args...) if err != nil { fmt.Printf(" ERROR: %v\n", err) // 尝试用 pgconn 解析 position var pgErr interface{ Error() string } _ = pgErr fmt.Printf(" raw err: %s\n", err.Error()) return } cnt := 0 for rows.Next() { cnt++ } fmt.Printf(" OK rows=%d err=%v\n", cnt, rows.Err()) rows.Close() }