async Task Write(byte[] buffer, int offset, int count, bool async)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException("Invalid offset or count for this buffer");
            }

            CheckDisposed();

            if (!_writeable)
            {
                throw new NotSupportedException("Write cannot be called on a stream opened with no write permissions");
            }

            var totalWritten = 0;

            while (totalWritten < count)
            {
                var chunkSize    = Math.Min(count - totalWritten, _manager.MaxTransferBlockSize);
                var bytesWritten = await _manager.ExecuteFunction <int>("lowrite", async, _fd, new ArraySegment <byte>(buffer, offset + totalWritten, chunkSize));

                totalWritten += bytesWritten;

                if (bytesWritten != chunkSize)
                {
                    throw new InvalidOperationException($"Internal Npgsql bug, please report");
                }

                _pos += bytesWritten;
            }
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException("Invalid offset or count for this buffer");
            }
            Contract.EndContractBlock();

            CheckDisposed();

            if (!_writeable)
            {
                throw new NotSupportedException("Write cannot be called on a stream opened with no write permissions");
            }

            int totalWritten = 0;

            while (totalWritten < count)
            {
                var chunkSize    = Math.Min(count - totalWritten, _manager.MaxTransferBlockSize);
                var bytesWritten = _manager.ExecuteFunction <int>("lowrite", _fd, new ArraySegment <byte>(buffer, offset + totalWritten, chunkSize));
                totalWritten += bytesWritten;

                if (bytesWritten != chunkSize)
                {
                    throw PGUtil.ThrowIfReached();
                }

                _pos += bytesWritten;
            }
        }