无缝更新 postgres 数据库 - 模式、重命名,如何?

发布于 2024-10-06 00:24:42 字数 325 浏览 3 评论 0原文

其实是一个简单的问题,但我找不到任何好的结论性答案。

假设有一个生产数据库 foo_prd,以及同一 foo_new 的新版本(在同一服务器上),该版本应该替换旧版本。从 _prd 无缝切换到 _new 的最简洁方法是什么?

重命名数据库需要通过 pid 断开当前用户的连接。这将减少一些请求,新用户可能会在此过程中连接。我正在考虑将新数据库的表创建为不同的架构,然后更改 search_path,例如从 "$user",prd 更改为 "$user",new,prd

可能会出现什么问题?您有更好的建议吗?我完全采取了错误的方法吗?

Actually a simple question, but I wasn't able to find any good conclusive answer.

Assuming a production database foo_prd, and a newer version of the same foo_new (on the same server) that is supposed to replace the old one. What is the cleanest way to seamlessly switch from _prd to _new?

RENAME-ing the databases would require to disconnect the current users via their pid. That would take down some requests, and new users might connect during the process. I was thinking of creating the tables of the new database as different SCHEMA and then change the search_path, e.g. from "$user",prd to "$user",new,prd.

What could possibly go wrong? Do you have any better suggestions? Am I taking the wrong approach altogether?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2024-10-13 00:24:42

按照您的建议进行操作:将新数据库的表创建为不同的架构,然后更改 search_path。

但还要创建一个与新架构同名的用户,并在更改 search_path 之前通过在每个应用程序中以该用户身份登录来测试所有内容 - 默认情况下,新架构将位于该用户的 search_path 中,因为名称匹配。

最后,当您删除旧模式时要小心 - 我建议首先重命名,以防任何内容使用限定引用(例如 prd.tableprd.function)。几天/几周后,就可以放心地放弃它了。

Do as you suggest: create the tables of the new database as different schema and then change the search_path.

But also create a user with the same name as the new schema and test everything before changing the search_path by logging in as this user with each of your apps - the new schema will be first in that user's search_path by default because the name matches.

Finally, take care when you come to drop the old schema - I suggest renaming first in case anything refers to it's objects using a qualified reference (eg prd.table or prd.function). After a few days/weeks it can then be dropped with confidence.

梦断已成空 2024-10-13 00:24:42

我会对我的架构进行版本控制,并在准备好后将我的应用程序更改为指向新架构。

I would version my schema, and change my app to point to the new schema when ready.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文