Thread: bytea internal encoding
How are bytea values encoded internally? Or maybe a better question would be what is the proper way to access bytea data from within a C function? Are there utility functions for reading the bytea data as a stream of scalar values, for example? -- Ron Peterson Network & Systems Manager Mount Holyoke College http://www.mtholyoke.edu/~rpeterso
On Wed, Dec 15, 2004 at 11:08:29PM -0500, Ron Peterson wrote: > How are bytea values encoded internally? > > Or maybe a better question would be what is the proper way to access > bytea data from within a C function? Are there utility functions for > reading the bytea data as a stream of scalar values, for example? If I do something like: bytea* bindat = PG_GETARG_BYTEA_P(0); int32 total_data_bytes = VARSIZE( bindat ) - VARHDRSZ; char* test_cp = (char*)palloc( total_data_bytes ); memcpy( test_cp, VARDATA( bindat ), total_data_bytes ); fwrite( test_cp, 1, total_data_bytes, test_fp ); I get the exactly the same binary file out that I stuffed into PostgreSQL by octal escaping it and doing an insert. Am I asking for trouble writing code like this? -- Ron Peterson Network & Systems Manager Mount Holyoke College http://www.mtholyoke.edu/~rpeterso
On Wed, Dec 15, 2004 at 11:08:29PM -0500, Ron Peterson wrote: > How are bytea values encoded internally? > > Or maybe a better question would be what is the proper way to access > bytea data from within a C function? Are there utility functions for > reading the bytea data as a stream of scalar values, for example? Do you mean a C function on the server side or on the client side? For examples of server-side code, take a look at functions in the PostgreSQL source that handle BYTEA, e.g., byteacat() in src/backend/utils/adt/varlena.c. See also the comments concerning variable-length data and the relevant examples in the "C-Language Functions" section of the "Extending SQL" chapter in the documentation. -- Michael Fuhr http://www.fuhr.org/~mfuhr/
On Wed, Dec 15, 2004 at 10:22:07PM -0700, Michael Fuhr wrote: > On Wed, Dec 15, 2004 at 11:08:29PM -0500, Ron Peterson wrote: > > > How are bytea values encoded internally? > > > > Or maybe a better question would be what is the proper way to access > > bytea data from within a C function? Are there utility functions for > > reading the bytea data as a stream of scalar values, for example? > > Do you mean a C function on the server side or on the client side? > > For examples of server-side code, take a look at functions in the > PostgreSQL source that handle BYTEA, e.g., byteacat() in > src/backend/utils/adt/varlena.c. See also the comments concerning > variable-length data and the relevant examples in the "C-Language > Functions" section of the "Extending SQL" chapter in the documentation. Thanks. I was just looking at byteacat. Because byteacat concatenates two bytea arguments, it then seems safe enough to use memcpy in conjunction with VARSIZE to move the data around (they're both the same type, so they'll map one to one). I guess maybe what I'm asking is whether or not it's safe to assume that bytea data will always consist of a string of VARSIZE-VARHDRSZ eight bit bytes, i.e. it's safe to assume 'byte' will always refer to 8 bit values in the range 0..255. -- Ron Peterson Network & Systems Manager Mount Holyoke College http://www.mtholyoke.edu/~rpeterso
Does anyone know if it is possible to use system catalogs in foreign key constraints in Postgres 7.4.1? My sample table: * CREATE TABLE tbl_reference ( reference_id SERIAL PRIMARY KEY, from_table NAME NOT NULL REFERENCES pg_catalog.pg_class(relname), from_id INTEGER, to_table NAME NOT NULL REFERENCES pg_catalog.pg_class(relname), to_id INTEGER );* The error I get when I try to create the table: *ERROR: permission denied for relation pg_class* If this is a permission issue, I already have SELECT permissions on pg_class...any ideas as to what sort of permissions need to be granted to accomplish this?
Attachment
Jonathan Hedstrom <jhedstrom@desc.org> writes: > Does anyone know if it is possible to use system catalogs in foreign key > constraints in Postgres 7.4.1? It's not. The system wouldn't enforce the constraint even if you overrode the permission check, because internal backend catalog updates don't fire triggers. regards, tom lane