Skip to content

[DNM] postgres_fdw arbitrary queries #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: REL_11_CARTO
Choose a base branch
from

Conversation

rafatower
Copy link

A PoC of a function to execute arbitrary queries through a foreign server.

Signature:

CREATE FUNCTION postgres_fdw_query(server name, sql text)
RETURNS SETOF record

Examples of usage:

SELECT * FROM postgres_fdw_query('cdb_fdw_local_pg11', 'SELECT nspname, nspowner from pg_namespace') AS schemas(schema_name name, owner oid);
    schema_name     | owner 
--------------------+-------
 pg_toast           |    10
 pg_temp_1          |    10
 pg_toast_temp_1    |    10
 pg_catalog         |    10
 public             |    10
 information_schema |    10
 carto_lite         | 16384
(7 rows)
SELECT * FROM postgres_fdw_query('cdb_fdw_local_pg11', 'SELECT 1') ;
ERROR:  a column definition list is required for functions returning "record"
LINE 1: SELECT * FROM postgres_fdw_query('cdb_fdw_local_pg11', 'SELE...
SELECT * FROM postgres_fdw_query('cdb_fdw_local_pg11', 'SELECT 1') AS t1(i int);
 i 
---
 1
(1 row)

Rafa de la Torre added 10 commits October 24, 2019 10:45
Remove SRF calls, which make actually harder to manage memory
contexts, depending on the approach we take.

Copy some patterns and code from dblink to achieve what we want: get
an appropriate TupleDesc and TupleStore to store what we get from a
sync call through libpq.
I managed to reproduce the test failure in our CI:

```
$ make check-world

...

============== running regression test queries        ==============
test postgres_fdw                 ... FAILED
============== shutting down postmaster               ==============

======================
 1 of 1 tests failed.
======================

The differences that caused some tests to fail can be viewed in the
file "/home/rtorre/src/postgres/pg11_build/contrib/postgres_fdw/regression.diffs".  A copy of the test summary that you see
above is saved in the file "/home/rtorre/src/postgres/pg11_build/contrib/postgres_fdw/regression.out".

/home/rtorre/src/postgres/pg11_build/../src/makefiles/pgxs.mk:412: recipe for target 'check' failed
make[2]: *** [check] Error 1
make[2]: Leaving directory '/home/rtorre/src/postgres/pg11_build/contrib/postgres_fdw'
Makefile:93: recipe for target 'check-postgres_fdw-recurse' failed
make[1]: *** [check-postgres_fdw-recurse] Error 2
make[1]: Leaving directory '/home/rtorre/src/postgres/pg11_build/contrib'
GNUmakefile:70: recipe for target 'check-world-contrib-recurse' failed
make: *** [check-world-contrib-recurse] Error 2
```

By looking at the diffs:

```
$ cat /home/rtorre/src/postgres/pg11_build/contrib/postgres_fdw/regression.diffs
*** /home/rtorre/src/postgres/pg11_build/../contrib/postgres_fdw/expected/postgres_fdw.out      2019-10-25 15:12:57.866375535 +0200
--- /home/rtorre/src/postgres/pg11_build/contrib/postgres_fdw/results/postgres_fdw.out  2019-10-28 12:41:04.876038961 +0100
***************
*** 8816,8822 ****
    AS t(role_name name);
   role_name
  -----------
!  postgres
  (1 row)

  -- Select schemas owned by the role configured in the user mapping
--- 8816,8822 ----
    AS t(role_name name);
   role_name
  -----------
!  rtorre
  (1 row)

  -- Select schemas owned by the role configured in the user mapping

======================================================================

```

but when it is executed from the contrib/postgres_fdw dir, it works as
expeted (note in my env there's a PGUSER=postgres variable):

```
$ pwd
/home/rtorre/src/postgres/pg11_build/contrib/postgres_fdw
$ make clean all install installcheck

...

============== dropping database "contrib_regression" ==============
DROP DATABASE
============== creating database "contrib_regression" ==============
CREATE DATABASE
ALTER DATABASE
============== running regression test queries        ==============
test postgres_fdw                 ... ok

=====================
 All 1 tests passed.
=====================

```

I'd rather remove this environment-dependent test (on CURRENT_USER)
than review all makefiles and possible configurations.
Previously it could actually send commands, but since it wasn't really
getting tuples, it failed with an empty error message.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant