0

My application receives JPG images as byte arrays from a device connected to the system. These need to be written to the PostgreSql 9.1 database's Large Objects table. I'm currently using C# and the Devart docConnect for PostgreSql library to access my database, and everything is working fine. The problem is that writing my data to the database (I need to update 2 tables plus write the image bytes to the large objects table) takes several trips to the database. I want to reduce the number of trips my code makes to the database to speed things up.

I have written a stored function which performs an UPSERT on the two tables. I want to pass the image bytes as a byte array parameter to the stored function and have the stored function write the image bytes to the large objects table. Is this possible? The documentation on server side functions in the PostgreSql 9.1 manual is skimpy and I'm not sure what function to call to read & write the data.

Thanks

Tony

4
  • Wouldn't using bytea instead make things a lot easier? Commented Jan 11, 2012 at 21:52
  • Please see my comment to Twelfth's answer. Commented Jan 12, 2012 at 0:40
  • I still don't get it. If you already have a bytea, why do you need the LargeObject then? Commented Jan 12, 2012 at 8:02
  • The Large Objects table is being used because it's a requirement. I really can't get into it more than that. It's also a requirement I can't change. Commented Jan 12, 2012 at 15:54

1 Answer 1

1

Unlike Oracle or Microsoft, Postgres does not differentiate between functions and stored procedures...it has only functions (look up in the postgres manual about functions...very flexible as well, you can call c or java libraries and other fun stuffs). So you'll create a function (probably in PLPGSQL) to accept a few parameters, then call the function using a select statement as function(arguement1,arg2,etc...). Here is an example if you want a template:

CREATE OR REPLACE FUNCTION deleteme
(deleteid integer)
RETURNS integer AS
$BODY$
BEGIN
delete from mytable_attrib where id = $1;
delete from mytable where id = $1;
return 0;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;

Little different in it's language and syntax then you might be used to...do not name a variable the same name as a column name for example...or be prepared to shift to dynamic SQL to build a statement in a variable using plpgsql and execute the variable as sql.

Execute it like a function, not the exec syntax oracle or MS would like. To delete ID row 8 I can now use

select deleteme(8)

Extra points for apple spell check changing postgres to post grease

I should flag that my knowledge is for 8.4 not 9.1

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

2 Comments

I'm aware of that. My apologies for not being specific, but my question has to do with the large object functions built into Postgres (the functions that start with "lo_"). I want to pass a bytea parameter to the function; I'm not sure what "lo_" function to call to write the bytea to the large object table, as the functions I'm aware of take a file name as parameters. I don't want to write the data to the file system just to get it into the Large Objects table.
Oh sorry...I was a bit off with your intent here. I've actually avoided using large objects within any DB i've created, so I don't have any direct experience for you here...sorry. I assume you've installed the contrib file. postgresql.org/docs/9.1/static/lo-interfaces.html#LO-WRITE is the best I can show you from there

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.