我对 PostgreSQL 和数据库完全陌生,正在尝试对大对象进行一些测试。
我刚刚发现 8GB 的文件可以保存到 Postgres。
但是文档说大对象(pg_largeobject
存储)的最大值是 2GB。
http://www.postgresql.org/docs/9.2/static/lo-intro.html
我是不是漏掉了什么?
选择 version()
显示:
PostgreSQL 9.2.1 on x86_64-unknow-linux-gnu, compiled by gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4), 64-bit
如果您有兴趣,这是我的代码:
private long insertLargeObject(UsSqlSession session, FileEntity fileEntity) throws SQLException, FileNotFoundException, IOException{
LargeObjectManager lobj = getLargeObjectAPI(session);
long oid = lobj.createLO();
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
try(FileInputStream fis = new FileInputStream(fileEntity.getFile())){
int bufSize = 0x8FFFFFF;
byte buf[] = new byte[bufSize];
int s = 0;
int tl = 0;
while( (s = fis.read(buf, 0, bufSize)) > 0 ) {
obj.write(buf, 0, s);
tl += s;
}
}
obj.close();
return oid;
}
更新:
pg_largeobject
的大小是 11GB,pg_largeobject_metadata
说有一行,这意味着只有一个大对象存在。
select sum(length(lo.data))
from pg_largeobject lo
where lo.loid=1497980;
返回 4378853347
。
更新:
public File findLargeObject(UsSqlSession session, long oid) throws SQLException, FileNotFoundException, IOException{
LargeObjectManager lobj = getLargeObjectAPI(session);
LargeObject obj = lobj.open(oid, LargeObjectManager.READ);
int bufSize = 0x8FFFFFF;
byte buf[] = new byte[bufSize];
int s = 0;
int tl=0;
File file = new File("e:/target-file");
try(FileOutputStream output = new FileOutputStream(file)){
while( (s = obj.read(buf, 0, bufSize)) > 0 ){
output.write(buf, 0, s);
tl += s;
}
output.flush();
}
obj.close();
return file;
}
最佳答案
我认为正确的答案是:“你的 PostgreSQL 是用 int64 支持构建的,因此使你能够在一个 LO 中写入超过 2GB 的数据。不过你在读取它时可能会遇到问题。”
尝试阅读 Tom Lane 的回复:http://postgresql.1045698.n5.nabble.com/Large-objects-td2852592.html 注意关于“lo_seek64”和“lo_tell64”函数的随机咆哮。
关于postgresql - 为什么 PostgreSQL 9.2.1 可以存储大于 2GB 的大对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17039576/