登录
首页 >  Golang >  Go问答

在 PostgreSQL 中使用 jackc/pgx 插入大对象导致内存不足问题(SQLSTATE 54000)

来源:stackoverflow

时间:2024-03-26 10:54:43 209浏览 收藏

在使用 jackc/pgx 库向 PostgreSQL 中插入大型对象时,可能会遇到“内存不足(SQLSTATE 54000)”错误,尤其是当对象大小接近 1.8 GB 时。这种错误是由一次性写入大量数据造成的。解决此问题的最佳实践是将文件分块读取,然后逐块写入 blob。通过分块,可以避免一次性加载大量数据到内存中,从而防止内存不足错误。

问题内容

我正在使用 jackc/pgx 库将大对象插入 postgres。 当大物体很小时它工作得很好。然而,在一种情况下,该大型对象的大小几乎为 1.8 gb。结果在执行写操作时,出现“内存不足(sqlstate 54000)”错误。

这是我如何插入 blob 的代码片段

import (
    "github.com/jackc/pgx/v4"
    "github.com/jackc/pgx/v4/pgxpool"
)

// Read bytes from the file to be imported as large object
b, err := ioutil.ReadFile(pathToLargeObjectFile)
txWrite, err := dbPool.Begin(ctx)
loWrite := txWrite.LargeObjects()

fmt.Printf("Creating new blob with ID : %d", ID)
id, err := loWrite.Create(ctx, ID)
// open blob with ID
obj, err := loWrite.Open(ctx, id, pgx.LargeObjectModeRead|pgx.LargeObjectModeWrite)
n, err := obj.Write(b)
fmt.printf("Written %d byte to blob %d\n", n, id)

我在这一行遇到错误

n, err := obj.write(b)

如何防止错误并成功导入大对象?

我阅读了这篇文章“将大对象插入 postgresql 返回 53200 内存不足错误”,该错误尝试以块的形式写入字节。

jackc/pgx 是否可以实现类似的功能?


正确答案


同样的解决方案是我们需要分块读取文件并将其分块写入 blob。

这里需要注意的重要一点是,当使用 obj.write(b) 时,obj 对象维护着指向上一次写入末尾的指针。因此,如果我们连续进行写入,则每次写入后都会附加 blob

这是我解决这个问题的方法,

// Open blob with ID
obj, err := loWrite.Open(ctx, id, pgx.LargeObjectModeRead|pgx.LargeObjectModeWrite)
if err != nil {
    fmt.Printf("Error opening blob. Error: %s",  err.Error())
    return err
}

importFile:= 
// reader for the blob backup file
reader := bufio.NewReader(blobBackupFile)
chunk:=0
id:= 
// Start reading from blob in chunks and writing it to blob
for {
    buf := make([]byte, bufferSize)    // Initializing the buffer
    bytesRead, err := reader.Read(buf) // Loading chunk into buffer
    buf = buf[:bytesRead]              // slicing it to the number of the bytes actually read

    

    if bytesRead == 0 {
        if err == io.EOF {
            fmt.Printf("Reached end of file %s", file.Name())
            break
        }
        if err != nil {
            fmt.Printf("Error reading chunks %d from file %s. Error: %s", chunk, importFile, err.Error())
            break
        }

        return err
    }
    loc, err := obj.Tell()
    if err != nil {
        fmt.Printf("Error in getting the current pointer location %s", err.Error())
    }
    fmt.Printf("BlobID: %d. Pointer at %#v ", id, loc)
    fmt.Printf("Writing chunk %d of %d bytes at address %#v of the blob %d", chunk, bytesRead, loc, id)

    bytesWritten, err := obj.Write(buf)
    if err != nil {
        fmt.Printf("Error writing bytes to blob %s", err.Error())
        return err
    }
    fmt.Printf("Written %d byte to blob %d.", bytesWritten, id)
    endLoc, err := obj.Tell()
    if err != nil {
        fmt.Printf("Error getting the pointer location after writing %s", err.Error())
    }
    fmt.Printf("Written chunk %d of %d bytes from address %#v to address %#v of the blob %d", chunk, bytesWritten, loc, endLoc, id)

    if int64(totalBytesRead) == file.Size() {
        fmt.Printf("Chunk %d was last chunk written at address %#v to address %#v of the blob %d", chunk, loc, endLoc, id)
        break
    }

    //next chunk
    chunk++
}

写入完整的blob后关闭blob和obj

以上就是《在 PostgreSQL 中使用 jackc/pgx 插入大对象导致内存不足问题(SQLSTATE 54000)》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>