Re: [ADMIN] Reg:lo_open error..pls help me

Lists: pgsql-adminpgsql-hackers
From: "sandhya" <sandhyar(at)amiindia(dot)co(dot)in>
To: "Postgres" <pgsql-admin(at)postgresql(dot)org>
Subject: Fw: pls help me
Date: 2006-03-01 11:56:38
Message-ID: 018201c63d27$34f26ec0$cd00000a@sandhyar
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-hackers

Hi,

In my application i need to create a file dynamically and load it into DB.

So.....I have created one object at run time .....and trying to open it inorder to write the content into it.....
But lo_open is returning -1(-ve value).
where i am doing mistake pls tell me.

lobjId = lo_creat(conn, INV_READ | INV_WRITE);
if (lobjId == 0)
fprintf(stderr, "can't create large object\n");

lobj_fd = lo_open(conn, lobjId, INV_WRITE);
//My application failed at this point..what i am missing here ?
if (lobj_fd < 0)
{
fprintf(stderr, "can't open large object %d\n",lobjId);
}
lo_lseek(conn, lobj_fd, 0, SEEK_SET);
nwritten = 0;
len = strlen(szContent);
while (len - nwritten > 0)
{
nbytes = lo_write(conn, lobj_fd, szContent, len - nwritten);
nwritten += nbytes;
}


From: "sandhya" <sandhyar(at)amiindia(dot)co(dot)in>
To: "Postgres" <pgsql-admin(at)postgresql(dot)org>
Subject: Reg:lo_open error..pls help me
Date: 2006-03-01 12:18:51
Message-ID: 01b701c63d2a$4d640880$cd00000a@sandhyar
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-hackers

Is it possible to use lo_open after creating the object while the application is running?
Hi,
In my application i need to create a file dynamically and load it into DB.

So.....I have created one object at run time .....and trying to open it inorder to write the content into it.....
But lo_open is returning -1(-ve value).
where i am doing mistake pls tell me.

lobjId = lo_creat(conn, INV_READ | INV_WRITE);
if (lobjId == 0)
fprintf(stderr, "can't create large object\n");

lobj_fd = lo_open(conn, lobjId, INV_WRITE);
//My application failed at this point..what i am missing here ?
if (lobj_fd < 0)
{
fprintf(stderr, "can't open large object %d\n",lobjId);
}
lo_lseek(conn, lobj_fd, 0, SEEK_SET);
nwritten = 0;
len = strlen(szContent);
while (len - nwritten > 0)
{
nbytes = lo_write(conn, lobj_fd, szContent, len - nwritten);
nwritten += nbytes;
}


From: Michael Fuhr <mike(at)fuhr(dot)org>
To: sandhya <sandhyar(at)amiindia(dot)co(dot)in>
Cc: Postgres <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Reg:lo_open error..pls help me
Date: 2006-03-01 15:26:20
Message-ID: 20060301152619.GA42224@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-hackers

On Wed, Mar 01, 2006 at 05:48:51PM +0530, sandhya wrote:
> Is it possible to use lo_open after creating the object while
> the application is running?

Yes, but that's off-topic for pgsql-admin. Try pgsql-interfaces
or pgsql-general.

--
Michael Fuhr


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "sandhya" <sandhyar(at)amiindia(dot)co(dot)in>
Cc: "Postgres" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Reg:lo_open error..pls help me
Date: 2006-03-01 16:03:12
Message-ID: 11079.1141228992@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-hackers

"sandhya" <sandhyar(at)amiindia(dot)co(dot)in> writes:
> But lo_open is returning -1(-ve value).

Are you sure that's where it's failing? The fragment you showed looks
fine as far as it goes. The most likely bet is you forgot to wrap it in
a transaction (BEGIN/COMMIT commands), but that would result in a
failure at the seek/write commands because the object wouldn't be open
anymore.

You might want to look at src/test/examples/testlo.c in the PG distribution.

regards, tom lane


From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: sandhya <sandhyar(at)amiindia(dot)co(dot)in>, Postgres <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Reg:lo_open error..pls help me
Date: 2006-03-01 16:10:59
Message-ID: 20060301161059.GA80123@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-hackers

On Wed, Mar 01, 2006 at 11:03:12AM -0500, Tom Lane wrote:
> "sandhya" <sandhyar(at)amiindia(dot)co(dot)in> writes:
> > But lo_open is returning -1(-ve value).
>
> Are you sure that's where it's failing? The fragment you showed looks
> fine as far as it goes. The most likely bet is you forgot to wrap it in
> a transaction (BEGIN/COMMIT commands), but that would result in a
> failure at the seek/write commands because the object wouldn't be open
> anymore.

lo_open() fails if it's not in a transaction. The error from
PQerrorMessage is:

ERROR: invalid large-object descriptor: 0

--
Michael Fuhr


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: pgsql-hackers(at)postgreSQL(dot)org, sandhya <sandhyar(at)amiindia(dot)co(dot)in>
Subject: Re: [ADMIN] Reg:lo_open error..pls help me
Date: 2006-03-01 16:30:10
Message-ID: 11346.1141230610@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-hackers

Michael Fuhr <mike(at)fuhr(dot)org> writes:
> On Wed, Mar 01, 2006 at 11:03:12AM -0500, Tom Lane wrote:
>> Are you sure that's where it's failing? The fragment you showed looks
>> fine as far as it goes. The most likely bet is you forgot to wrap it in
>> a transaction (BEGIN/COMMIT commands), but that would result in a
>> failure at the seek/write commands because the object wouldn't be open
>> anymore.

> lo_open() fails if it's not in a transaction. The error from
> PQerrorMessage is:
> ERROR: invalid large-object descriptor: 0

Hmm, I wonder why that is [ looks at code ... ]

The culprit seems to be this little bit in libpq's lo_open() function:

/* have to do this to reset offset in shared fd cache */
/* but only if fd is valid */
if (fd >= 0 && lo_lseek(conn, fd, 0L, SEEK_SET) < 0)
return -1;
return fd;

Outside a transaction block, this fails since the LO FD is already
closed by the time the lo_lseek request is run.

This hack goes all the way back --- it's in our original CVS version,
and there is equivalent code in Postgres v4r2 --- but it sure looks to
me like a workaround for a long-forgotten bug. It's forcing an extra
network round trip for every lo_open, so I'm very strongly tempted to
remove it. Comments?

regards, tom lane