How come PostgreSQL 9.2.1 can store Large Object bigger than 2GB?
我是PostgreSQL和数据库的新手,并尝试对大对象进行一些测试。
我刚发现8GB文件可以保存到Postgres。
但是文档说大对象(
http://www.postgresql.org/docs/9.2/static/lo-intro.html
我在这里错过了什么吗?
选择
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
这是我的代码,如果你感兴趣:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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; } |
更新:
1 2 3 | SELECT SUM(LENGTH(lo.data)) FROM pg_largeobject lo WHERE lo.loid=1497980; |
返回
更新:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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"函数的随机咆哮。