{"id":"660d817bbf64f6b6","repo":"go-sql-driver/mysql","slug":"packet-for-query-is-too-large-try-adjusting-the","errorCode":null,"errorMessage":"packet for query is too large. Try adjusting the `Config.MaxAllowedPacket`","messagePattern":"packet for query is too large\\. Try adjusting the `Config\\.MaxAllowedPacket`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":30,"sourceCode":"\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n)\n\n// Various errors the driver might return. Can change between driver versions.\nvar (\n\tErrInvalidConn       = errors.New(\"invalid connection\")\n\tErrMalformPkt        = errors.New(\"malformed packet\")\n\tErrNoTLS             = errors.New(\"TLS requested but server does not support TLS\")\n\tErrCleartextPassword = errors.New(\"this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN\")\n\tErrNativePassword    = errors.New(\"this user requires mysql native password authentication\")\n\tErrOldPassword       = errors.New(\"this user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords\")\n\tErrUnknownPlugin     = errors.New(\"this authentication plugin is not supported\")\n\tErrOldProtocol       = errors.New(\"MySQL server does not support required protocol 41+\")\n\tErrPktSync           = errors.New(\"commands out of sync. You can't run this command now\")\n\tErrPktSyncMul        = errors.New(\"commands out of sync. Did you run multiple statements at once?\")\n\tErrPktTooLarge       = errors.New(\"packet for query is too large. Try adjusting the `Config.MaxAllowedPacket`\")\n\tErrBusyBuffer        = errors.New(\"busy buffer\")\n\n\t// errBadConnNoWrite is used for connection errors where nothing was sent to the database yet.\n\t// If this happens first in a function starting a database interaction, it should be replaced by driver.ErrBadConn\n\t// to trigger a resend. Use mc.markBadConn(err) to do this.\n\t// See https://github.com/go-sql-driver/mysql/pull/302\n\terrBadConnNoWrite = errors.New(\"bad connection\")\n)\n\nvar defaultLogger = Logger(log.New(os.Stderr, \"[mysql] \", log.Ldate|log.Ltime))\n\n// Logger is used to log critical error messages.\ntype Logger interface {\n\tPrint(v ...any)\n}\n\n// NopLogger is a nop implementation of the Logger interface.\ntype NopLogger struct{}","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/errors.go#L12-L48","documentation":"ErrPktTooLarge is returned by writePacket (packets.go:128-129) when the data to be sent exceeds mc.maxAllowedPacket, the value negotiated from the server's max_allowed_packet. This protects against the server rejecting an oversized packet; the driver fails fast on the client side. The message points at Config.MaxAllowedPacket.","triggerScenarios":"Issuing a query/Exec whose serialized payload (query text + parameters/binds) is larger than maxAllowedPacket. The check at packets.go:127 `if pktLen > mc.maxAllowedPacket` triggers. Common with bulk INSERTs, large BLOB/TEXT writes, or huge IN(...) lists.","commonSituations":"Bulk-loading large rows; inserting big binary blobs; generating huge single statements from code; default max_allowed_packet (often 4MB or 64MB) too small for the workload; DSN maxAllowedPacket not matching the server.","solutions":["Raise max_allowed_packet on the MySQL server (my.cnf + SET GLOBAL) AND set maxAllowedPacket in the DSN to match: `?maxAllowedPacket=67108864`.","Chunk the data: batch INSERTs into smaller groups, or stream large blobs in pieces.","Reduce per-statement size (e.g. split a giant IN list into multiple queries)."],"exampleFix":"// before\ndb.Exec(\"INSERT INTO t(b) VALUES(?)\", hugeBlob) // > maxAllowedPacket\n// after\n// server: set global max_allowed_packet = 256*1024*1024;\ndsn := \"u:p@tcp(host:3306)/db?maxAllowedPacket=268435456\"\n// and/or split the blob into chunked writes","handlingStrategy":"validation","validationCode":"max := len(serializedPayload)\nif int64(max) > int64(cfg.MaxAllowedPacket) {\n    return fmt.Errorf(\"payload %d exceeds maxAllowedPacket %d\", max, cfg.MaxAllowedPacket)\n}","typeGuard":"func withinLimit(size, max int64) bool { return size <= max }","tryCatchPattern":"if errors.Is(err, mysql.ErrPktTooLarge) {\n    // split the batch and retry in smaller chunks\n}","preventionTips":["Match DSN maxAllowedPacket to the server's max_allowed_packet.","Batch large writes in bounded sizes.","Stream or chunk large BLOBs."],"tags":["limits","data","config"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}