Re: random access - bytea

From: Joe Conway <mail(at)joeconway(dot)com>
To: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: random access - bytea
Date: 2003-10-26 05:22:17
Message-ID: 3F9B5A09.7070709@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Dennis Bjorklund wrote:
> There have been (in the past at least) plans on adding a random access
> interface to bytea fields. I don't find it in the todo, maybe it should be
> added?

First do this:
ALTER TABLE foo ALTER COLUMN foo_bytea SET STORAGE EXTERNAL;

see:
http://www.postgresql.org/docs/view.php?version=7.3&idoc=0&file=sql-altertable.html

Then do:
UPDATE foo SET foo_bytea = foo_bytea || '';

That will modify the bytea column so that it is stored uncompressed in
the TOAST table.

Now, simply use substr() to grab any random chunk:
SELECT substr(foo_bytea, 20000, 100) from foo where foo_id = 42;

When TOASTed columns are stored uncompressed, substring will grab just
the needed chunks from the TOAST table directly. In contrast, when the
field is stored compressed, it grabs the entire thing, uncompresses it,
then gets the piece you asked for.

HTH,

Joe

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Dennis Bjorklund 2003-10-26 05:30:23 Re: random access - bytea
Previous Message Bruce Momjian 2003-10-26 05:08:21 Re: 7.4 compatibility question