Importing tables from foreign schema matching specific conditions - postgresql

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).

Related

PostgreSQL: 'original' name (and schema) of FOREIGN TABLE

I would like to obtain from database B, by querying its database tables, the original schemas and table names of its foreign tables (i.e. those in the original database A where they actually reside, as their name can be changed when created with the available fdw).
Any hint greatly appreciated!
Currently I am only able to find columns names and datatypes of the foreign tables (in database B) ...

Is there any way to create foreign key from different schemas?

I have an application where I am separating data with schemas where I have one table common in all schemas. lets say I have one public schema which is an admin schema and other schemas such as a,b,c,...,etc which are user schema. And all schemas have one common table T and all users can create entries in this T table and there are some entries which can only be created by admin user and all other users will use that rows.
so how I will achieve this.
I have two onptions in my mind
I will create T table in every schema and when ever admin adds new entry I will add that in all other schemas.
I will create T table in public schema and will add foreign key from that schema in other schema.
note: Schemas are dynamic and they are created run time and I am using postgres, Nestjs and sequelize-typescript

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.

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

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;

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.