Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eSsRV-0006yD-DH for pgsql-general@arkaria.postgresql.org; Sat, 23 Dec 2017 22:38:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eSsRU-0004Kc-MC for pgsql-general@arkaria.postgresql.org; Sat, 23 Dec 2017 22:38:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eSsRT-0004KQ-WC for pgsql-general@lists.postgresql.org; Sat, 23 Dec 2017 22:38:56 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eSsRJ-0004ye-GU for pgsql-general@postgresql.org; Sat, 23 Dec 2017 22:38:55 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 00E8E209C7; Sat, 23 Dec 2017 17:38:43 -0500 (EST) Received: from web2 ([10.202.2.212]) by compute6.internal (MEProxy); Sat, 23 Dec 2017 17:38:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bittivirhe.fi; h=cc:content-transfer-encoding:content-type:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=ai/6ky1hKDV3ttmOm t3rIYu+zeYbRN0i1jUAoXtVHPI=; b=bFBjg2WLOG4DsFJ7/4+Bm8zrfp214ruTV hveWEtO+QDxTdR9lEnJtDgMZaK5fuam9fcGGpS2oPUYPxF7ro288vkE7gInUvXnK q6nGLlfaCFTNclYUz9qgP+S6ZTmBNPkD8skebkG4xMvYbNcrNDf2lU2nwsBFaJgc 7JlMVeM8NL5NC9IdZEfl+0WYxdkexQt5XjfH/WvFJRj9eIW6wOu50zSL7Y3D3BZw BEZyeOsQE4ovcRyI1k7vnX/rdrklx+9C/df9JfR1SupwS/b8WDgkhu0FydZVA538 foLarJAZ0CGqFmrOjqlW8paSPoghgdGOB+S2hbrXoRWLirnsEeGKw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=ai/6ky 1hKDV3ttmOmt3rIYu+zeYbRN0i1jUAoXtVHPI=; b=i8tJQyI8BSgctivIzeJp4B PjsTAca/I8iEP2h9cie9ADd0WQLeS8ZPIXYAI5ecZdOt3Ai+LbnEOO7+1p8MrYmD Xep5wSV+ALFlZ2Nl/DN/xNvu0O10S2xygzqLockqvFVYdoGYH0Q6Lwh2Z178Zwl6 5MBpEI7vgDpDVUo0to9oTNVLsMIG4wNppi+hOwnrU/L+s/7pR52S9SRDMXMEm5DL f5PkwaJMeEJyrtaidHCtFjCWnnf/BlVhXM5B2Y1XaAWenTWIT7VAlftbnYXZ7zMS m33jhjpGNUMqaWlLU9PoThdZmJHrJxcFyzDj8nzhHvKL4bpbSNEXYXTtPYhR4Sgw == X-ME-Sender: Received: by mailuser.nyi.internal (Postfix, from userid 99) id CA6CB62B9E; Sat, 23 Dec 2017 17:38:42 -0500 (EST) Message-Id: <1514068722.1617355.1214555136.33C1A3FE@webmail.messagingengine.com> From: =?utf-8?Q?Timo=20Myyr=C3=A4?= To: Pavel Stehule Cc: "pgsql-general@postgresql.org >> PG-General Mailing List" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: multipart/alternative; boundary="_----------=_151406872216173550" X-Mailer: MessagingEngine.com Webmail Interface - ajax-bb99f02b References: <1514055195.1570504.1214442624.3DB93FFD@webmail.messagingengine.com> In-Reply-To: Date: Sun, 24 Dec 2017 00:38:42 +0200 Subject: Re: Migrating to postgresql from oracle List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --_----------=_151406872216173550 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" On Sat, Dec 23, 2017, at 21:05, Pavel Stehule wrote: > Hi >=20 > 2017-12-23 19:53 GMT+01:00 Timo Myyr=C3=A4 : >> Hi, >>=20 >> 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 m= angle the SQL >> ready for>> import to pg but I've hit first problem: >> ERROR: referenced relation "..." is not a table >>=20 >> 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 th= ose of that customer. And >> then each>> customers own schema has synonym to that view like >> customer1.usergroup. The>> applications queries use the "usergroup" ta= ble to query the group >> mappings.>>=20 >> Here's hopefully a bit more detailed description of the database >> structure:>>=20 >> 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) >> ... >>=20 >> So it seems that postgresql doesn't support foreign keys in views >> like Oracle.>> Would you have any suggestions how the above Oracle str= ucture would >> best be>> handled in PostgreSQL? I'm pretty new to PostgreSQL so I mig= ht >> overlook>> something if I try to solve this by myself. >=20 > try to ask on https://github.com/darold/ora2pg/issues >=20 > Regard >=20 > Pavel >=20 >=20 >>=20 >> 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 =3D 'customer1') union all select f.id, f.parent_id from admin.bg_unit f, e where e.id =3D 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 --_----------=_151406872216173550 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="utf-8"
On Sat, Dec 23, 2017, at 21:05, Pavel Stehule wrote:
Hi

2017-12-23 19:53 GMT+01:00 Timo My= yr=C3=A4 <= 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 t= hen each
customers own schema has synonym to that view like customer1.usergrou= p. The
applications queries use the "usergroup" table to query the group map= pings.

Here's hopefully a bit more detailed description of the database stru= cture:

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 lik= e Oracle.
Would you have any suggestions how the above Oracle structure would b= est be
handled in PostgreSQL? I'm pretty new to PostgreSQL so I might overlo= ok
something if I try to solve this by myself.


Regard

Pavel



Timo
<= /span>

Well, I doubt postgresql limitations apply for ora2pg.

I looked this a bit more closely. Postgresql seems to have nice featur= e 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 ow= n rows.

So a bit more about the table structure is needed to understand the si= tuation.
Lets take admin.unit table, this holds the business units of each cust= omer. 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 d= oesn'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_i= d which gives mapping to project. Finally we have project table which has t= he 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 (
           &nbs= p;  select id, parent_id
           &nbs= p;  from admin.bg_unit
           &nbs= p;  where id in (select unit_id from admin.project_unit where project_= id =3D 'customer1')
            uni= on all
           &nbs= p;  select f.id, f.parent_id
           &nbs= p;  from admin.bg_unit f, e
           &nbs= p;  where e.id =3D f.parent_id
            ) s= elect 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 someth= ing fundamentally wrong?

Timo

--_----------=_151406872216173550--