Problem with Large Object Interface of pgsql_perl5

Started by Ken Causeyabout 26 years ago6 messagesgeneral
Jump to latest
#1Ken Causey
ken@premiernet.net

I'm using PostgreSQL 6.5.3, Perl 5.005_03, and pgsql_perl5 1.8.0 on
Linux (intel).

Running this program:

#!/usr/bin/perl
use strict;
package Main;

use Pg;

my $db_name='excised';
my $db_user='excised';
my $db_passwd='excised';

# Connect to the database server
my $dbconn = Pg::connectdb("dbname=$db_name user=$db_user
password=$db_passwd");
if($dbconn->status ne PGRES_CONNECTION_OK){
# We failed to get a connection, exit with error
my $error=$dbconn->errorMessage;
print "Error on connection: $error\n";
exit;
}

print "Database connection opened\n";

my $newoid = $dbconn->lo_creat(PGRES_INV_WRITE|PGRES_INV_READ);

if($newoid == PGRES_InvalidOid){
my $error = $dbconn->errorMessage;
print "Error on lo_creat: $error\n";
exit;
}

print "LO created\n";

my $fd = $dbconn->lo_open($newoid,PGRES_INV_WRITE);

if($fd == -1){
# Error on opening new oid
my $error = $dbconn->errorMessage;
print "Error on lo_open (oid = $newoid): $error\n";
# Try to unlink the oid
$dbconn->lo_unlink($newoid);
exit;
}

print "LO opened\n";

my $content = 'This is a test of the emergency broadcasting system, in the
event of a real emergency...';

my $numbytes=$dbconn->lo_write($fd,$content,length($content));

if($numbytes != length($content)){
my $rightnum=length($content);
my $error = $dbconn->errorMessage;
print "Error on lo_write ($numbytes != $rightnum): $error\n";
# Try to unlink the oid
$dbconn->lo_close($fd);
$dbconn->lo_unlink($newoid);
exit;
}

print "LO written\n";

$dbconn->lo_close($fd);

$dbconn->lo_unlink($newoid);

I get this output:

Database connection opened
LO created
Error on lo_open (oid = 46401): ERROR: lo_lseek: invalid large obj
descriptor (0)

Can anyone tell me what I'm doing wrong?

All help is greatly appreciated.

Ken Causey
PremierNET, Inc.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ken Causey (#1)
Re: Problem with Large Object Interface of pgsql_perl5

Ken Causey <ken@premiernet.net> writes:

Can anyone tell me what I'm doing wrong?

Not doing the lo_open inside a transaction. You need begin ... end
around any use of an open large object. (This has always been true,
but older versions of Postgres didn't enforce it --- they'd just die
occasionally if you broke the rule. Now, open LO handles are auto-
closed at transaction commit, which is to say at end of statement
if you're not inside a BEGIN block.)

regards, tom lane

#3Ken Causey
ken@premiernet.net
In reply to: Tom Lane (#2)
Re: [INTERFACES] Problem with Large Object Interface of pgsql_perl5

Edmund Mergl was kind enough to point out my error and now
that I look back at the Programmer's documentation I see it. Someone
might want to update the Pg man page to reflect this. Thanks!

Ken Causey
PremierNET, Inc.

At 01:11 PM 3/31/00 -0500, Tom Lane wrote:

Show quoted text

Ken Causey <ken@premiernet.net> writes:

Can anyone tell me what I'm doing wrong?

Not doing the lo_open inside a transaction. You need begin ... end
around any use of an open large object. (This has always been true,
but older versions of Postgres didn't enforce it --- they'd just die
occasionally if you broke the rule. Now, open LO handles are auto-
closed at transaction commit, which is to say at end of statement
if you're not inside a BEGIN block.)

regards, tom lane

#4Thomas Lockhart
lockhart@alumni.caltech.edu
In reply to: Ken Causey (#1)
Re: [INTERFACES] Problem with Large Object Interface ofpgsql_perl5

Edmund Mergl was kind enough to point out my error and now
that I look back at the Programmer's documentation I see it. Someone
might want to update the Pg man page to reflect this. Thanks!

What "Pg man page"? Where? Can't fix what I can't find...

- Thomas

--
Thomas Lockhart lockhart@alumni.caltech.edu
South Pasadena, California

#5Lamar Owen
lamar.owen@wgcr.org
In reply to: Thomas Lockhart (#4)
Re: [INTERFACES] Problem with Large Object Interface ofpgsql_perl5

On Fri, 31 Mar 2000, Thomas Lockhart wrote:

Edmund Mergl was kind enough to point out my error and now
that I look back at the Programmer's documentation I see it. Someone
might want to update the Pg man page to reflect this. Thanks!

What "Pg man page"? Where? Can't fix what I can't find...

Use perldoc Pg to read it. It's packaged as /usr/lib/perl5/man/man3/Pg.3 in
the RPM. It is located at src/interfaces/perl5/blib/man3/Pg.3 in the tarball.

--
Lamar Owen
WGCR Internet Radio
1 Peter 4:11

#6Thomas Lockhart
lockhart@alumni.caltech.edu
In reply to: Ken Causey (#1)
Re: [INTERFACES] Problem with Large Object Interface ofpgsql_perl5

What "Pg man page"? Where? Can't fix what I can't find...

Use perldoc Pg to read it. It's packaged as /usr/lib/perl5/man/man3/Pg.3 in
the RPM. It is located at src/interfaces/perl5/blib/man3/Pg.3 in the tarball.

Sheesh, a whole 'nother set of docs to look at. Hopefully someone else
will pick it up...

- Thomas

--
Thomas Lockhart lockhart@alumni.caltech.edu
South Pasadena, California