在PostgreSQL中使用架构 [英] using schemas in postgresql

查看:112
本文介绍了在PostgreSQL中使用架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用postgresql开发了一个应用程序,并且运行良好。

I have developed an application using postgresql and it works well.

现在我需要创建同一应用程序的多个实例,但是我只有一个数据库。因此,我正在考虑使用架构,以便可以将每个实例表分组为不同的架构。

Now I need to create several instances of the same application but I have only one database. So I am thinking about using schemas, so that I can group each instance tables in a different schema.

现在,我不想重写所有函数和脚本,因此我想知道是否可以仅使用某些指令来指示数据库在特定架构上运行。只是为了使其更清楚一点,您是否知道在c ++中使用

Now, I wouldn't like to rewrite all my functions and script, thus I am wondering if I can just use some directive to instruct the database to operate on a specific schema. Just to try to make it clearer, do you know when in c++ you do

using namespace std;

以便您可以使用 cout 代替 std :: cout ?我想在可能的情况下使用类似的东西。

so that you can use cout instead of std::cout ? I would like to use someting similar if possible.

推荐答案

您要查找的参数是 search_path -列出查询将要查看的架构。因此,您可以执行以下操作:

The parameter you are looking for is search_path - that lists the schemas a query will look in. So, you can do something like:

CREATE TABLE schema1.tt ...
CREATE TABLE schema2.tt ...
CREATE FUNCTION schema1.foo() ...
CREATE FUNCTION schema2.foo() ...
SET search_path = schema1, something_else;
SELECT * FROM tt;        -- schema1.tt
SELECT * FROM schema2.tt -- schema2.tt
SELECT foo();            -- calls schema1.foo
SELECT schema2.foo();    -- calls schema2.foo

请注意,如果查询的计划保存在foo()的正文中那么您可能会得到意想不到的结果。如果使用重复表,我建议您始终在plpgsql函数中显式列出引用表的模式。如果没有,请确保已进行适当的测试,以通过更改search_path的方式检查行为。

Note that if a query's plan gets saved inside the body of foo() then you may get an unexpected results. I would recommend you always explicitly list schemas for referenced tables in plpgsql functions if you are using duplicated tables. If not, make sure you have testing in place to check behaviour with a chaning search_path.

哦-您也可以为函数主体明确设置search_path-请参见手册的CREATE有关详细信息,请参见功能参考。

Oh - you can explicitly set search_path for a function's body too - see the manual's CREATE FUNCTION reference for details.

这篇关于在PostgreSQL中使用架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆