Convert a bytea column to OID while retaining values

前端 未结 5 935
情话喂你
情话喂你 2021-01-18 16:20

I\'m trying to alter a bytea column to have type oid and still retain the values.

I have tried using queries like:

ALTER TA         


        
5条回答
  •  情歌与酒
    2021-01-18 17:02

    A column of type Oid is just a reference to the binary contents which are actually stored in the system's pg_largeobject table. In terms of storage, an Oid a 4 byte integer. On the other hand, a column of type bytea is the actual contents.

    To transfer a bytea into a large object, a new large object should be created with the file-like API of large objects: lo_create() to get a new OID, then lo_open() in write mode, then writes with lo_write() or lowrite(), and then lo_close().

    This can't reasonably be done with just a cast.

    Basically, you would need to write a ~10 lines piece of code in the language of your choice (at least one that supports the large object API, including plpgsql) to do this conversion.

提交回复
热议问题