public PqsqlLargeObject(PqsqlConnection conn)
        {
#if CODECONTRACTS
            Contract.Requires <ArgumentNullException>(conn != null);
#else
            if (conn == null)
            {
                throw new ArgumentNullException(nameof(conn));
            }
#endif

            // All large object manipulation using these functions must take place within an SQL transaction block,
            // since large object file descriptors are only valid for the duration of a transaction.
            // https://www.postgresql.org/docs/current/static/lo-interfaces.html
            PGTransactionStatusType transactionStatus = conn.TransactionStatus;
            if (transactionStatus != PGTransactionStatusType.PQTRANS_INTRANS &&
                transactionStatus != PGTransactionStatusType.PQTRANS_ACTIVE)
            {
                throw new PqsqlException("PqsqlLargeObject manipulation must take place within an SQL transaction");
            }

            mConn   = conn;
            mPGConn = conn.PGConnection;
            if (mPGConn == IntPtr.Zero)
            {
                throw new ArgumentNullException("PqsqlConnection is closed: " + mConn.GetErrorMessage());
            }

            mOid  = 0;
            mFd   = -1;
            mMode = 0;
            mPos  = -1;
        }
        public override void Close()
        {
            if (mFd < 0)
            {
                return;
            }

            if (PqsqlWrapper.lo_close(mPGConn, mFd) == -1)
            {
                // ignore error
            }

            mFd   = -1;
            mMode = 0;
            mPos  = -1;
        }
        public int Open(uint oid, LoOpen mode)
        {
#if CODECONTRACTS
            Contract.Requires <ArgumentException>(oid != 0, "Cannot open large object with InvalidOid (0)");
#else
            if (oid == 0)
            {
                throw new ArgumentException("Cannot open large object with InvalidOid (0)");
            }
#endif

            mFd = PqsqlWrapper.lo_open(mPGConn, oid, (int)mode);

            if (mFd < 0)
            {
                throw new PqsqlException(string.Format(CultureInfo.InvariantCulture, "Cannot open large object {0}: {1}", oid, mConn.GetErrorMessage()));
            }

            mOid  = oid;
            mMode = mode;
            mPos  = 0;
            return(mFd);
        }