I am creating extension to postgres in C (c++). It is new data type that behave like text but it is encrypted by HSM device. But I have problem to use more then one key to protect data. My idea is to get original sql query and process it to choose what key I should use. But I don't know how to do that or if it is even possible?
My goal is to change some existing text fields in database to encrypted ones. And that's why I can't provide key number to my type in direct way. Type must be seen by external app as text.
Normally there is userID field and single query always use that id to get or set encrypted data. Base on that field I want to chose key. HSM can have billions of keys in itself and that's mean every user can have it's own key. It's not a problem if I need to parse string by myself, I am more then capable of doing that. Performance is not issue too, HSM is so slow that I can encode or decode only couple fields in one second.
longjmpbased error handling that doesn't work well with C++ exceptions? You'll need to useextern "C"interfaces at boundaries between Pg and the C++, and catch C++ exceptions and turn them into PostgreSQL errors (ereport) at each boundary. You also cannot call any PostgreSQL function that might callelogorereportwithERRORseverity or above from anywhere that needs stack unwinding. In practice that means writing a C++ lib that doesn't uselibpqand exposes onlyCinterfaces, then a wrapper PostgreSQL type for it.