Re: Seek within Large Object, within PL/* function?

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "Adam Seering" <aseering(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Seek within Large Object, within PL/* function?
Date: 2008-11-16 02:07:46
Message-ID: 89397c96-ecf3-4695-b287-4dd73e7e7e6e@mm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Adam Seering wrote:

> Thanks for the link; still trying to figure out how to use these
> functions, though.
> The link that you sent suggests that these commands are really only
> useful for writing out data to server-side files, which isn't so much

> what I want. What I really want is to lo_open a file, then lo_lseek
to
> a particular address and loread a constant number of bytes (to be
> returned as the query result), then lo_close the file.

Out of curiosity, I've just quickly tried this:

CREATE OR REPLACE FUNCTION lo_part(oid,integer,integer) RETURNS bytea
AS $$
declare
fd integer;
c bytea;
i integer;
begin
fd = lo_open($1, 262144); -- INV_READ
i=lo_lseek(fd,$2,0); --arg 2 is offset
c = loread(fd, $3); -- arg 3 is size
i=lo_close(fd);
return c;
end;
$$ LANGUAGE 'plpgsql';

...and it appears to work, isn't it what you say you need?

> I'm currently stuck on the easy part: lo_open always returns 0,
which
> gives me "ERROR: invalid large-object descriptor: 0" when I try to
use
> it with loread.

Generally it's because you've forgotten to start a transaction. lo_*
functions need to be executed inside a transaction, otherwise you get
that error (the message is not exactly helpful in this particular case,
admittedly!)

Best regards,
--
Daniel
PostgreSQL-powered mail user agent and storage:
http://www.manitou-mail.org

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tony Caduto 2008-11-16 09:51:57 Re: 8.3 libpq.dll not working on some versions of windows
Previous Message Glyn Astill 2008-11-16 00:45:17 Re: 8.3 libpq.dll not working on some versions of windows