{"record":{"id":"32e1630a34b27109","repo":"jackc/pgx","slug":"afterconnect-error-w","errorCode":null,"errorMessage":"AfterConnect error: %w","messagePattern":"AfterConnect error: %w","errorType":"exception","errorClass":"ConnectError","httpStatus":null,"severity":"error","filePath":"pgconn/pgconn.go","lineNumber":171,"sourceCode":"\tif len(errs) > 0 {\n\t\tallErrors = append(allErrors, errs...)\n\t}\n\n\tif len(connectConfigs) == 0 {\n\t\treturn nil, &ConnectError{Config: config, err: fmt.Errorf(\"hostname resolving error: %w\", errors.Join(allErrors...))}\n\t}\n\n\tpgConn, errs := connectPreferred(ctx, config, connectConfigs)\n\tif len(errs) > 0 {\n\t\tallErrors = append(allErrors, errs...)\n\t\treturn nil, &ConnectError{Config: config, err: errors.Join(allErrors...)}\n\t}\n\n\tif config.AfterConnect != nil {\n\t\terr := config.AfterConnect(ctx, pgConn)\n\t\tif err != nil {\n\t\t\tpgConn.conn.Close()\n\t\t\treturn nil, &ConnectError{Config: config, err: fmt.Errorf(\"AfterConnect error: %w\", err)}\n\t\t}\n\t}\n\n\treturn pgConn, nil\n}\n\n// buildConnectOneConfigs resolves hostnames and builds a list of connectOneConfigs to try connecting to. It returns a\n// slice of successfully resolved connectOneConfigs and a slice of errors. It is possible for both slices to contain\n// values if some hosts were successfully resolved and others were not.\nfunc buildConnectOneConfigs(ctx context.Context, config *Config) ([]*connectOneConfig, []error) {\n\t// Simplify usage by treating primary config and fallbacks the same.\n\tfallbackConfigs := []*FallbackConfig{\n\t\t{\n\t\t\tHost:      config.Host,\n\t\t\tPort:      config.Port,\n\t\t\tTLSConfig: config.TLSConfig,\n\t\t},\n\t}","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgconn/pgconn.go#L153-L189","documentation":"The connection was successfully established but the user-supplied AfterConnect hook returned an error. The connection is closed and the hook's error is wrapped and surfaced inside a ConnectError.","triggerScenarios":"Thrown at pgconn/pgconn.go:171 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect the wrapped error from your AfterConnect callback","Add logging inside the hook to find the failing statement","Ensure the hook's SQL is valid for the target database","Check the hook does not assume a specific server version"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}