
= PL/Proxy Language Syntax =

The language is similar to plpgsql - string quoting, comments,
semicolon at the statements end.

It contains only 4 statements: `CONNECT`, `CLUSTER`, `RUN` and `SELECT`.

Each function needs to have either `CONNECT` or pair of `CLUSTER` + `RUN` statements
to specify where to run the function.

The `SELECT` statement is optional, if it is missing, there will be default
query generated based on proxy function signature.

The `RUN` statment is also optional, it defaults to `RUN ON ANY`
which means the query will be run random partition.

== CONNECT ==

  CONNECT 'libpq connstr';

Specifies exact location where to connect and execute the query.
If several functions have same connstr, they will use same connection.

  CONNECT connect_func(...);
  CONNECT argname;
  CONNECT $argnr;

Connect string is taken from function result or directly from argument.
If several functions have same connstr, they will use same connection.

NB: giving untrusted users ability to specify full connect string creates
security hole.  Eg it can used to read cleartext passwords from `~/.pgpass`
or `pg_service.conf`.  If such function cannot be avoided, it's access rights
need to be restricted.


== CLUSTER ==

  CLUSTER 'cluster_name';

Specifies exact cluster name to be run on.  The cluster name will
be passed to plproxy.get_cluster_* functions.

  CLUSTER cluster_func(..);

Cluster name can be dynamically decided upon proxy function arguments.
`cluster_func` should return text value of final cluster name.

== RUN ON ==

  RUN ON ALL;

Query will be run on all partitions in cluster in parallel.

  RUN ON ANY;

Query will be run on random partition.

  RUN ON <NR>;

Run on partition number `<NR>`.

  RUN ON partition_func(..);

Run partition_func() which should return one or more hash values.  (int4)
query will be run on tagged partitions.  If more than one partition was
tagged, query will be sent in parallel to them.

  RUN ON argname;  RUN ON $1;

Take hash value directly from function argument.

== SELECT ==

  SELECT .... ;

By default, PL/Proxy generates query based on its own signature.
But this can be overrided by giving explicit `SELECT` statement to run.

Everything after `SELECT` until semicolon is taken as SQL to be passed on.
Only argument substitution is done on the contents, otherwise the text
is unparsed.  To avoid a table column to be parsed as function argument,
table aliases should be used.

Query result should have same number of columns as function result
and same names too.

== Argument substitution ==

Proxy function arguments can be referenced using name or `$n` syntax.
Everything that is not argument reference is just passed on.

== Dynamic records ==

PL/Proxy supports function returning plain RECORD type.  Such functions
need the result type specified at call site.  Main use-case is to run
random queries on partitions.  Very simple example:

    CREATE OR REPLACE FUNCTION dynamic_query(q text)
    RETURNS SETOF RECORD AS $$
        CLUSTER 'mycluster';
        RUN ON ALL;
    $$ LANGUAGE plproxy;

Corresponding function in partitions:

    CREATE OR REPLACE FUNCTION dynamic_query(sql text)
    RETURNS SETOF RECORD AS $$
    DECLARE
        rec RECORD;
    BEGIN
        FOR rec IN EXECUTE sql
        LOOP
            RETURN NEXT rec;
        END LOOP;
        RETURN;
    END;
    $$ LANGUAGE plpgsql;

Sample request:

    SELECT * FROM dynamic_query('SELECT id, username FROM sometable')
                  AS (id integer, username text);

The types given in AS clause must match actual types from query.

