How can I transform a UUID to a UUID5 during select (or insert) queries in postgres?

早过忘川 提交于 2019-12-11 15:57:26

问题


Is there a way to convert a UUID to a UUID5 in an existing postgres data set?

I have a master/slave setup with pglogical replicating specific tables & columns. I need to give access to this replicated data to a third party, but I want to anonymise it as much as possible.

Every row in a table uses a UUID (generated in code) as primary key, but in order to ensure anonymity I need to ensure that the primary keys of the master & replicated tables are one-way transformed to make back-tracing harder. Some of the replicated tables refer to each other as well, meaning I can't just exclude those columns - values need to match between tables. Hopefully this makes sense so far...

Is there a way of using uuid-ossp and uuid_generate_v5() to essentially use the existing UUIDs as the namespace for a v5 UUID, returning those in the view to the third party (or even to use to transform the specific columns during replication)?

I'm a little stumped. If I can get this sorted entirely in postgres I won't have to muck about with additional tools to transform the data before allowing the third party access, I can just give them real-time data instead of batched. But if I need to involve some additional code then I guess that's not the end of the world...

Thanks in advance!


回答1:


Simply provide read-only views to a third-party (in a separate schema) and limit data access to them to those views (or schema).

A view can then be defined with a query such as:

SELECT uuid_generate_v5(t.uuid, 'custom-third-party'), t.a, t.b, ...
FROM ...

This way, you can also provide different UUID's to different third-parties as the original UUID's should be unique across data entries.

Rewriting data strategies are not going to scale and would just add overhead where it is not needed.



来源:https://stackoverflow.com/questions/55938941/how-can-i-transform-a-uuid-to-a-uuid5-during-select-or-insert-queries-in-postg

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!