13

I'm currently trying to store images in a psql table and was following this guide here using a bytea for the image. Problem is that the image I'm trying to insert is ~24kb and I keep getting an error that the maximum size is 8191, though I've read in other places that a bytea should be able to store up to 1gb. Surely I should be able to raise this max limit somehow?

Code:

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;
}

The database table only consists of a bytea at the moment.

Error message:

org.postgresql.util.PSQLException: ERROR: index row requires 23888 bytes, maximum size is 8191
2
  • Please show us the code and the exact error message (ideally including the stacktrace). There is no problem with storing (a lot) more than 8KB in a bytea column Commented Apr 3, 2012 at 13:07
  • Who throws the error? I guess the error comes from the web server where the 8k limit is common for uploads. Commented Apr 3, 2012 at 13:10

3 Answers 3

13

Apparently you have an index on that column (to be honest I'm surprised that you could create it - I would have expected Postgres to reject that).

An index on a bytea column does not really make sense. If you remove that index, you should be fine.

The real question is: why did you create an index on a column that stores binary data?

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think I want to answer that question :(
binary data isn't necessarily large. It is very reasonable to use a bytea column with an index if you only store a few bytes in the bytea.
Probably more than 99% of all computer data as of 2021 can be expressed as binary data (maybe excluding quantum computing??). Haven't you ever saved any data that isn't based on one of the preset patterns? XOXOXOOXOOXOOOXOOXOOXOOO
13

Max size of bytea

According to this old thread, maximum size for a field in Postgres is 1 GB.

The PostgreSQL version 12 protocol limits row size to 2 GiB minus message header when it is sent to the client (SELECTed). (The protocol uses 32-bit signed integers to denote message size.) No other limits found (another topic).

But largeobjects are stored as multiple bytea records so they not limited on such way. See this docs for them.

1 Comment

PostgreSQL protocol limits message size to 2 GiB. The server sends one message for each row, so maximum size of row transferred to the client is slightly below 2 GiB.
9

If you need to ensure that you don't upload the same image twice, you can create a unique index on the md5 (or some other hash) of the bytea:

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.