Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,63 @@ 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
foreign_tbl | public | f
example_view | public | v
(3 rows)

```
19 changes: 17 additions & 2 deletions foreign_table_exposer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fte_post_parse_analyse(ParseState *pstate, Query *query,JumbleState *jstate)
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);
Expand Down Expand Up @@ -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));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed?

relkindPos->varno = varno_index;
relkindPos->varattno = varattrno_index;
relkinds = lappend(relkinds, relkindPos);
Expand Down
Binary file added foreign_table_exposer.dylib
Binary file not shown.