From: Timo Myyrä <timo.myyra@bittivirhe.fi>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: pgsql-general@postgresql.org >> PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Migrating to postgresql from oracle
Date: Sun, 24 Dec 2017 00:38:42 +0200
Message-ID: <1514068722.1617355.1214555136.33C1A3FE@webmail.messagingengine.com> (raw)
In-Reply-To: <CAFj8pRCiBw2st8NvmJs14VGXq0r+EFTjET+nmMSyq2ZOPo-9Ug@mail.gmail.com>
References: <1514055195.1570504.1214442624.3DB93FFD@webmail.messagingengine.com>
<CAFj8pRCiBw2st8NvmJs14VGXq0r+EFTjET+nmMSyq2ZOPo-9Ug@mail.gmail.com>
On Sat, Dec 23, 2017, at 21:05, Pavel Stehule wrote:
> Hi
>
> 2017-12-23 19:53 GMT+01:00 Timo Myyrä <timo.myyra@bittivirhe.fi>:
>> Hi,
>>
>> I'm preparing migration of our asset management system database from
>> Oracle 12c to>> PostgreSQL 10. I'm using ora2pg and a bit of sed to mangle the SQL
>> ready for>> import to pg but I've hit first problem:
>> ERROR: referenced relation "..." is not a table
>>
>> Our Oracle databases uses single 'admin' schema and dedicated schema
>> for each>> customer. For example usergroup mappings are held in admin.usergroup
>> table. Then>> admin schema has customer-specific view on this table
>> admin.usergroup_customer1>> view which limits the full view to just those of that customer. And
>> then each>> customers own schema has synonym to that view like
>> customer1.usergroup. The>> applications queries use the "usergroup" table to query the group
>> mappings.>>
>> Here's hopefully a bit more detailed description of the database
>> structure:>>
>> ADMIN SCHEMA:
>> TABLES:
>> usergroup
>> unit
>> user
>> ...
>> VIEWS:
>> usergroup_customer1
>> unit_customer1
>> user_customer1
>> ...
>> CUSTOMER1 SCHEMA:
>> TABLES:
>> resource
>> ...
>> SYNONYM:
>> usergroup (refers to admin.usergroup_customer1)
>> user (refers to admin.user_customer1)
>> unit (refers to admin.unit_customer1)
>> ...
>>
>> So it seems that postgresql doesn't support foreign keys in views
>> like Oracle.>> Would you have any suggestions how the above Oracle structure would
>> best be>> handled in PostgreSQL? I'm pretty new to PostgreSQL so I might
>> overlook>> something if I try to solve this by myself.
>
> try to ask on https://github.com/darold/ora2pg/issues
>
> Regard
>
> Pavel
>
>
>>
>> Timo
Well, I doubt postgresql limitations apply for ora2pg.
I looked this a bit more closely. Postgresql seems to have nice feature
which would apply to this quite nicely called row-level security. I
like to limit the 'admin' schema table rows so that each user sees only
their own rows.
So a bit more about the table structure is needed to understand the
situation.Lets take admin.unit table, this holds the business units of each
customer. The import rows are id and parent_id. Parent_id field might
refer to unit id in the same unit table so units can be nested. The top-
level unit doesn't have parent_id. The admin.unit.id field gives the top-
level id, then theres the admin.project_unit table which has 2 fields,
project_id, unit_id which gives mapping to project. Finally we have
project table which has the actual project id and name which map to
logged in project.
I was testing the row-level security with following query but it gives
syntax error for some reason:
CREATE POLICY unit_customer1 ON admin.unit
FOR ALL
TO customer1
USING ( with recursive e(id,parent_id) as (
select id, parent_id
from admin.bg_unit
where id in (select unit_id from admin.project_unit where
project_id = 'customer1') union all
select f.id, f.parent_id
from admin.bg_unit f, e
where e.id = f.parent_id
) select id from e order by id asc
);
When I run the above query I get just:
ERROR: syntax error at or near "with"
LINE 4: USING ( with recursive e(id,parent_id) as (
Can the above policy made to work for my use-case or am I doing
something fundamentally wrong?
Timo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: timo.myyra@bittivirhe.fi, pavel.stehule@gmail.com
Subject: Re: Migrating to postgresql from oracle
In-Reply-To: <1514068722.1617355.1214555136.33C1A3FE@webmail.messagingengine.com>
References: <1514055195.1570504.1214442624.3DB93FFD@webmail.messagingengine.com> <CAFj8pRCiBw2st8NvmJs14VGXq0r+EFTjET+nmMSyq2ZOPo-9Ug@mail.gmail.com> <1514068722.1617355.1214555136.33C1A3FE@webmail.messagingengine.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox