HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

What is the 131072 in the call to lo_open()

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thewhat131072lo_opencall

Problem

ByteA to BLOB conversions typically make a call to lo_open(),

CREATE OR REPLACE FUNCTION make_lo(bytea)
RETURNS oid AS $
DECLARE
  loid oid;
  fd integer;
  bytes integer;
BEGIN
  loid := lo_creat(-1);
  fd := lo_open(loid, 131072); -- <<< --==HERE==--
  bytes := lowrite(fd, $1);
  IF (bytes != LENGTH($1)) THEN
    RAISE EXCEPTION 'Not all data copied to blob';
  END IF;
  PERFORM lo_close(fd);
  RETURN loid;
END;
$ LANGUAGE plpgsql STRICT;


In the above what is

fd := lo_open(loid, 131072);


You can see others asking this same question here,


The number 131072 is some flag I couldn't find what means, and 999999999 is the max read size, but I could read the value anyway.

Solution

This variable is called the "mode parameter" and it's the int representation of INV_READ (x'20000')

-- lo_open(lobjId oid, mode integer) returns integer
-- The mode parameter to lo_open uses two constants:
--   INV_READ  = 0x20000
--   INV_WRITE = 0x40000


You can see the integer representation like this,

SELECT CAST( x'20000' AS integer);
  int4  
--------
 131072
(1 row)


Personally, I would rather use x'20000' which I find to be more explicit. You can see INV_READ mentioned in a comment in largeobject.source, and you can see it defined it in the code at libpq-fs.h.

In the event you ever see 262144 that's the numified representation of INV_WRITE (0x40000).

Code Snippets

-- lo_open(lobjId oid, mode integer) returns integer
-- The mode parameter to lo_open uses two constants:
--   INV_READ  = 0x20000
--   INV_WRITE = 0x40000
SELECT CAST( x'20000' AS integer);
  int4  
--------
 131072
(1 row)

Context

StackExchange Database Administrators Q#189717, answer score: 4

Revisions (0)

No revisions yet.