overwriting large objects bug? - Mailing list pgsql-general

From Stéphane Popinet
Subject overwriting large objects bug?
Date
Msg-id 373BE096.8B517D06@lmm.jussieu.fr
Whole thread Raw
Responses Re: [GENERAL] overwriting large objects bug?  (Bruce Momjian <maillist@candle.pha.pa.us>)
List pgsql-general
Hi,

I have some problems when trying to overwrite large objects (I use
postgres 6.3.2-10). Apparently, everything looks allright when
overwriting existing data but it fails when trying to write after the
end of the existing large object.

Before describing in more detail what happens, I'd like to ask to
questions:

- is that a known problem with large objects?
_ is there a function which allows to import a file in an already
existing large object (i.e. overwriting its contents)? (from what I saw
in libpq-fe.h probably not).

Actually the problem looks a bit more complex than what described above.
I use the function overwrite() given below.

Let's take an example to make the problem clearer

Given two files f1 and f2

f1 contains "abcdefghijklmnop"
f2 contains "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

a program looking like

    opening connection ...

    lobjid = lo_import(conn, "f1");
    overwrite (conn, lobjid, "f2");
    lo_export (conn, lobjid, "f3");

    closing connection ...

works fine and f3 contains the same thing as f2, but if I divide this
single program into three separate programs looking like

        program1:     opening connection ...

            lobjid = lo_import(conn, "f1");
            printf("%d\n", lobjid);

            closing connection ...

    program2:     opening connection ...

            overwrite(conn, atoi(argv[1]), "f2");

            closing connection ...

    program3:     opening connection ...

            lo_export(conn, atoi(argv[1]), "f3");

            closing connection ...

and runs the script

    #!/bin/bash
    id=`program1`
    program2 $id
    program3 $id

then f3 contains "abcdefghijklmnop\nRSTUVWXYZ".

Everything works fine if f2 is shorter or equal to f1

f1="abcdefghijklmnop", f2="ABCDEFGHIJKLMNOP" then
f3="ABCDEFGHIJKLMNOP"

Any idea to what is wrong? Am I wrong or is there a bug?

Thanks a lot

Stéphane

--------------------------------------------------------------------------

int overwrite (PGconn * conn,
           Oid lobjId,
           const char * filename)
{
  int lobj_fd;
  char buf[BUFSIZE];
  int nbytes;
  int fd;
  int status = -1;

  lobj_fd = lo_open(conn, lobjId, INV_WRITE);
  if (lobj_fd >= 0) {
    if (lo_lseek(conn, lobj_fd, 0, SEEK_SET) >= 0) {
      fd = open(filename, O_RDONLY, 0666);
      if (fd >= 0) {
    int n, nwritten;
    status = 0;
    while ((nbytes = read(fd, buf, BUFSIZE)) > 0) {
      nwritten = 0;
      while (nbytes - nwritten > 0) {
        n = lo_write(conn, lobj_fd, buf + nwritten, nbytes - nwritten);
        nwritten += n;
      }
    }
    close(fd);
      }
    }
    lo_close(conn, lobj_fd);
  }
  return status;
}

pgsql-general by date:

Previous
From: Chris Bitmead
Date:
Subject: Re: [GENERAL] How to express categorized items in relational tables
Next
From: "Brett W. McCoy"
Date:
Subject: Re: [GENERAL] postgres getting slow