我目前正在尝试将图像存储在psql表中,并在此处使用bytea作为图像.问题是,我试图插入图像〜24KB和我不断收到一个错误,最大尺寸为8191,虽然我在其他地方,一个BYTEA应该能够存储多达1GB读过.当然我应该能够以某种方式提高这个最大限度?
码:
String query = "INSERT INTO " + tableName + " VALUES(?);";
try {
PreparedStatement stmt = conn.prepareStatement(query);
File file = new File(location);
FileInputStream fi = new FileInputStream(file);
stmt.setBinaryStream(1, fi, (int)file.length());
boolean res = stmt.execute();
stmt.close();
fi.close
return res;
}
Run Code Online (Sandbox Code Playgroud)
数据库表目前只包含一个bytea.
错误信息:
org.postgresql.util.PSQLException: ERROR: index row requires 23888 bytes, maximum size is 8191
Run Code Online (Sandbox Code Playgroud)
如果您需要确保不会两次上传相同的图像,您可以在 bytea 的 md5(或其他哈希值)上创建唯一索引:
create table a(a bytea);
create unique index a_bytea_unique_hash on a (md5(a));
insert into a values ('abc');
INSERT 0 1
insert into a values ('abc');
ERROR: duplicate key value violates unique constraint "a_bytea_unique_hash"
DETAIL: Key (md5(a))=(900150983cd24fb0d6963f7d28e17f72) already exists.
Run Code Online (Sandbox Code Playgroud)
显然你有一个关于该列的索引(说实话,我很惊讶你可以创建它 - 我希望Postgres拒绝这一点).
bytea列上的索引实际上没有意义.如果删除该索引,则应该没问题.
真正的问题是:为什么要在存储二进制数据的列上创建索引?
| 归档时间: |
|
| 查看次数: |
6845 次 |
| 最近记录: |