{"record":{"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":29,"sourceCode":"import (\n\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.","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/03d76c7e07908e255ce62d126d07ede3f2365d86/errors.go#L11-L47","documentation":"ErrPktTooLarge is returned from writePacket (packets.go:129) when the payload length (len(data)-4) exceeds mc.maxAllowedPacket before anything is written. mc.maxAllowedPacket comes from Config.MaxAllowedPacket (default 64 MiB, const.go:17) or the server's max_allowed_packet system variable read at connect (connector.go:180).","triggerScenarios":"Sending a single statement, prepared-parameter, or COM_QUERY whose serialized size exceeds max_allowed_packet — e.g. a huge IN(...) list, a very large bound []byte/string parameter, or a multi-row bulk INSERT in one statement.","commonSituations":"Bulk-loading large blobs; generating INSERT ... VALUES with thousands of rows; a query string built by string concatenation growing past the limit.","solutions":["Raise max_allowed_packet on the server (SET GLOBAL max_allowed_packet=...) and restart/for new conns.","Set Config.MaxAllowedPacket / DSN maxAllowedPacket to match so the client cap agrees with the server.","Chunk the work: page the IN-list, batch INSERTs into smaller statements, or stream via LOAD DATA LOCAL INFILE.","Prefer prepared statements + chunked parameters over one giant interpolated query."],"exampleFix":"// before: one giant interpolated INSERT -> ErrPktTooLarge\nq := \"INSERT INTO t(v) VALUES \" + strings.Join(values, \",\") // too big\n\n// after: batch in chunks the server can accept\ndsn := \"...&maxAllowedPacket=67108864\"\nconst batch = 1000\nfor i := 0; i < len(values); i += batch {\n    end := min(i+batch, len(values))\n    q := \"INSERT INTO t(v) VALUES \" + strings.Join(values[i:end], \",\")\n    db.Exec(q)\n}","handlingStrategy":"validation","validationCode":"// Bound your payloads against the negotiated limit before sending.\nconst maxPkt = 64 << 20 // keep in sync with server max_allowed_packet\nif len(blob) > maxPkt-1024 {\n    return fmt.Errorf(\"payload %d exceeds max_allowed_packet\", len(blob))\n}","typeGuard":"func isPktTooLarge(err error) bool {\n    return errors.Is(err, mysql.ErrPktTooLarge)\n}","tryCatchPattern":"if errors.Is(err, mysql.ErrPktTooLarge) {\n    // either raise server + client max_allowed_packet, or chunk the work.\n}","preventionTips":["Keep Config.MaxAllowedPacket aligned with the server's max_allowed_packet.","Batch large INSERTs and IN-lists into page-sized chunks.","Prefer prepared statements over one huge interpolated query.","Use LOAD DATA LOCAL INFILE for bulk loads instead of giant packets."],"tags":["packet-size","config","bulk-load"],"backgroundTag":null,"analyzedSha":"03d76c7e07908e255ce62d126d07ede3f2365d86","analyzedAt":"2026-08-07T10:39:17.340Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}