diff --git a/README.md b/README.md index 4540a2f..199132f 100644 --- a/README.md +++ b/README.md @@ -4,35 +4,58 @@ This PostgreSQL extension exposes foreign tables like a normal table with rewrit ## Installation - $ make - $ sudo make install +Add a directory of `pg_config` to PATH and build and install it. + +```sh +make USE_PGXS=1 +make install USE_PGXS=1 +``` + +If you want to build it in a source tree of PostgreSQL, use + +```sh +make +make install +``` or - $ pgxnclient install foreign_table_exposer +```bash + pgxnclient install foreign_table_exposer +``` ## Setup + Write this line in your `postgresql.conf` and then restart PostgreSQL. - shared_preload_libraries = 'foreign_table_exposer' +``` +shared_preload_libraries = 'foreign_table_exposer' +``` Execute this statement when you want to enable this feature. - CREATE EXTENSION foreign_table_exposer; - +```sql +CREATE EXTENSION foreign_table_exposer; +``` + ## Usage + When you scan `pg_catalog.pg_class` with a predicate like `relkind in ('r', 'v')`, this extension automatically rewrites the query to include `'f'` (foreign table). - postgres=# - select - relname, nspname, relkind - from - pg_catalog.pg_class c, - pg_catalog.pg_namespace n - where relkind in ('r', 'v') and - nspname not in ('pg_catalog', 'information_schema', 'pg_toast', 'pg_temp_1') and - n.oid = relnamespace order by nspname, relname; +```sql +select + relname, nspname, relkind +from + pg_catalog.pg_class c, + pg_catalog.pg_namespace n +where relkind in ('r', 'v') and + nspname not in ('pg_catalog', 'information_schema', 'pg_toast', 'pg_temp_1') and + n.oid = relnamespace order by nspname, relname; +``` + +returns +``` relname | nspname | relkind ------------------+---------+--------- normal_tbl | public | r @@ -40,3 +63,4 @@ When you scan `pg_catalog.pg_class` with a predicate like `relkind in ('r', 'v') example_view | public | v (3 rows) +``` diff --git a/foreign_table_exposer.c b/foreign_table_exposer.c index 8914dbf..d032be2 100644 --- a/foreign_table_exposer.c +++ b/foreign_table_exposer.c @@ -44,7 +44,11 @@ static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL; void _PG_init(void); void _PG_fini(void); +#if PG14_LT static void fte_post_parse_analyse(ParseState *pstate, Query *query); +#else +static void fte_post_parse_analyse(ParseState *pstate, Query *query,JumbleState *jstate); +#endif static void rewrite_query(Query *query); static bool walker(Node *node, List *relkinds); @@ -75,11 +79,20 @@ _PG_fini(void) * Post-parse-analysis hook: mark query with a queryId */ static void -fte_post_parse_analyse(ParseState *pstate, Query *query) + +#if PG14_LT + fte_post_parse_analyse(ParseState *pstate, Query *query) +#else + fte_post_parse_analyse(ParseState *pstate, Query *query,JumbleState *jstate) +#endif { if (prev_post_parse_analyze_hook) { +#if PG14_LT prev_post_parse_analyze_hook(pstate, query); +#else + prev_post_parse_analyze_hook(pstate, query, jstate); +#endif } rewrite_query(query); @@ -135,13 +148,15 @@ rewrite_query(Query *query) if (is_pg_class && rte->eref != NULL) { int varattrno_index = 1; ListCell *lc_colname; + PgClassRelKindPos *relkindPos; foreach(lc_colname, rte->eref->colnames) { char *colname = strVal(lfirst(lc_colname)); if (strcmp(colname, COL_RELKIND) == 0) { ereport(DEBUG1, (errmsg("Found pg_catalog.pg_class.relkind: [varno:%d, varattno:%d]", varno_index, varattrno_index))); - PgClassRelKindPos *relkindPos = (PgClassRelKindPos*) palloc(sizeof(PgClassRelKindPos)); + + relkindPos = (PgClassRelKindPos*) palloc(sizeof(PgClassRelKindPos)); relkindPos->varno = varno_index; relkindPos->varattno = varattrno_index; relkinds = lappend(relkinds, relkindPos); diff --git a/foreign_table_exposer.dylib b/foreign_table_exposer.dylib new file mode 100755 index 0000000..14ef425 Binary files /dev/null and b/foreign_table_exposer.dylib differ