Postgres: Oracle_fdw: Import foreign schema does not support if not exists - postgresql

When we use import foreign schema in oracle_fdw, there is no option for IF NOT EXISTS.
Due to which if we re-execute the import foreign schema command to import the newly added tables/view we get the error that relation already exists.
As we are not aware of the table/view which were not added in the previous execution it becomes difficult to specify them in LIMIT/EXCEPT clause
Is there any work around available to achieve the IF NOT EXISTS functionality

There is no direct way to do that, but you can first find the foreign tables that already exist in the target schema and list them in the EXCEPT clause of IMPORT FOREIGN SCHEMA.
To find all foreign tables in a schema:
SELECT relname
FROM pg_class
WHERE relkind = 'f'
AND relnamespace = 'schemaname'::regnamespace;
Then
IMPORT FOREIGN SCHEMA "XYZ"
EXCEPT (/* list from above */)
FROM SERVER oraserver
INTO schemaname;

Related

Question. oracle_fdw - Import foreign schema limit to synonym?

oracle_fdw - β€ŽIs it possible in PostgreSQL to import foreign schema with a limit to synonym - as in the attached image?
γ…€
For example, this not working (limit to synonym):
IMPORT FOREIGN SCHEMA "UserA" LIMIT TO (A_SYNONYM) FROM SERVER oracledb INTO public;
but works when I try to import foreign schemat limit to view or table:
IMPORT FOREIGN SCHEMA "UserB" LIMIT TO (B_VIEW) FROM SERVER oracledb INTO public;
β€Ž
So, it is possible to use synonyms in import foreign schema "LIMIT TO ()"?
β€Ž
When processing IMPORT FOREIGN SCHEMA, oracle_fdw searches through the Oracle view ALL_TAB_COLUMNS in Oracle. According to the Oracle documentation
ALL_TAB_COLUMNS describes the columns of the tables, views, and clusters accessible to the current user.
That sounds like synonyms won't be listed. You can easily test that by omitting the LIMIT TO clause. If foreign tables for synonyms are not created, you have a proof.
As a workaround, you could import those tables from the schemas where they really reside.

Importing tables from foreign schema matching specific conditions

IMPORT FOREIGN SCHEMA myforeignschema LIMIT TO (tables_that_match_a_specific_condition)
FROM SERVER myserver INTO myschema;
I want to import a limited amount of tables from a foreign schema, but I don't want to list them seperately, rather I want a list of all tables in the foreign schema that match specific conditions (In my case, I want to import only those tables which are foreign tables themselves).

Foreign Table that references ALL columns of the origin table

Is it possible to create a foreign table that lists all columns of the original table, like this somehow?
CREATE FOREIGN TABLE localschema.localtable (/* list all columns of foreign_table here */)
SERVER foreignserver
OPTIONS (schema_name 'foreign_schema', table_name 'foreign_table');
Use IMPORT FOREIGN SCHEMA:
IMPORT FOREIGN SCHEMA foreign_schema LIMIT TO (foreign_table)
FROM SERVER foreignserver INTO localschema;
If you need the foreign table name to be different, rename it.

Pre-fix the foreign table with the schema - postgres_pwd

If I follow: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.PostgreSQL.CommonDBATasks.html#postgresql-commondbatasks-fdw, how can I pre-fix the tables with the schema I am retrieving tables from, e.g.
IMPORT FOREIGN SCHEMA lands
LIMIT TO (land, land2)
FROM SERVER foreign_server INTO public;
The created tables are named land and land2. Is it possible to prefix land and land2 with 'lands', e.g. 'lands_land' and 'lands_land2'?
With psql and recent PostgreSQL versions, you could simply run (after the IMPORT FOREIGN SCHEMA):
SELECT format(
'ALTER FOREIGN TABLE public.%I RENAME TO %I;',
relname,
'lands_' || relname
)
FROM pg_class
WHERE relkind = 'f' -- foreign table
AND relnamespace = 'public'::regnamespace \gexec
The \gexec will interpret each result row as an SQL stateent and execute it.
Another option that I'd like better is to keep the original names, but use a different schema for the foreign tables:
IMPORT FOREIGN SCHEMA lands
LIMIT TO (land, land2)
FROM SERVER foreign_server INTO lands;
Then all foreign tables will be in a schema lands, and you have the same effect in a more natural fashion. You can adjust search_path to include the lands schema.

ORACLE 10g : How to import without foreign key constraint error?

I have a data dump then I wanna import it to another DB (Oracle 10g). The destination DB has already tables (no data) and some foreign key constraints. I will import the data by tables in order to avoid constraint errors. If does anyone know any easier ways, please teach me ?. (Oracle's import tool does not have functions to recognize relations between tables automatically, does it ?)
You can disable all foreign keys first:
begin
for cnst in (SELECT constraint_name, table_name FROM user_constraints WHERE constraint_type ='R') loop
execute immediate 'alter table '|| cnst.table_name||' disable constraint ' || cnst.constraint_name ;
end loop;
end;
After loading your data do the same to enable them back (just change the alter table command to enable instead of disable
But this is risky, because if the data won't meet your contraints - you'll have a problem ...
Assuming that you have a schema-level export, the source schema had those same foreign key constraints, and all the foreign key constraints are between tables in the same schema, the import utility should automatically take care of the foreign key constraints. You shouldn't need to do anything for that (though, of course, you'll have to ignore errors when you do the import because it will try to create the tables and get an error that they already exist).