0

SQL standard provides large object data types for character data (clob) and binary data(blob), and in an example, I found them being declared as:

attribute_name clob(20KB)
attribute_name blob(10GB)

so, I tried the following:

create table mydata(
my_movie blob(2GB)
);

but, it gave me an error. How is this large object created?

5
  • Use text instead of clob. bytea (the natural choice for "blobs") is limited to 1GB but "large objects" are very complicated to handle. For a complete movie with 10GB I would recommend to store them in the filesystem, not in the database. Commented Sep 19, 2018 at 13:19
  • @a_horse_with_no_name, isn't there something larger than bytea? Commented Sep 19, 2018 at 13:51
  • Not something that's as easy to handle as bytea Commented Sep 19, 2018 at 13:51
  • @a_horse_with_no_name, still doesn't work: pasteboard.co/HECimJ5.png Commented Sep 19, 2018 at 13:54
  • Please see the manual for the correct syntax Commented Sep 19, 2018 at 13:55

1 Answer 1

1

PostgreSQL supports large objects as related chunks in a pg_largeobject table. You create a large object (separately) then insert a reference to it into your table. Large objects are more difficult to deal with than bytea and require an API beyond pure SQL.

The SQL syntax for creating a table is simple. Assuming you want a primary key named "id", you can create one like:

CREATE TABLE mydata (id INTEGER, my_movie OID);

The OID is a reference to a large object.

Because a large object is potentially too large to hold in memory, you'll probably want to read and write them using a streaming interface. Below is an untested example that uses Java's JDBC interface to create a large object and associate it with an existing record in mydata. Be aware that deleting the record in mydata won't delete the large object.

Path moviePath  = ...
int  primaryKey = ...

connection.setAutoCommit(false); // start a transaction (required for large objects)

try (var inputStream = Files.newInputStream(moviePath)) { // Open the file for reading
    // Insert the movie into a mydata record as a Blob
    try (PreparedStatement ps = connection.prepareStatement(
            "UPDATE mydata SET my_movie = ? WHERE id = ?")) {
        ps.setBlob(1, inputStream);
        ps.setInt(2, primaryKey);
        ps.executeUpdate();
    }
}

connection.commit();

Note that TEXT is not appropriate for movie clips, since PostgreSQL disallows the use of codepoint=0 in TEXT and there may be null bytes in binary data.

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

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.