Working with PostgreSQL databases

Warning

External connectors are an experimental feature of YDB. To work with external DBMS through connectors, you need to deploy fq-connector-go and explicitly enable the corresponding sources in the cluster configuration YDB. The functionality may change, so use in production is not recommended without thorough testing.

This section describes the basic information about working with an external PostgreSQL database.

To work with an external PostgreSQL database, you need to perform the following steps:

  1. Create a secret containing the password for connecting to the database.

    CREATE SECRET postgresql_datasource_user_password WITH (value = "<password>");
    
  2. Create an external data source that describes a specific database within a PostgreSQL cluster. By default, the namespace public is used for reading, but this value can be changed using the optional parameter SCHEMA. The network connection is established via the standard (Frontend/Backend Protocol) over TCP transport (PROTOCOL="NATIVE"). Encryption of connections to the external database can be enabled using the parameter USE_TLS="TRUE".

    CREATE EXTERNAL DATA SOURCE postgresql_datasource WITH (
        SOURCE_TYPE="PostgreSQL",
        LOCATION="<host>:<port>",
        DATABASE_NAME="<database>",
        AUTH_METHOD="BASIC",
        LOGIN="user",
        PASSWORD_SECRET_PATH="postgresql_datasource_user_password",
        PROTOCOL="NATIVE",
        USE_TLS="TRUE",
        SCHEMA="<schema>"
    );
    
  3. Deploy the connector and configure the YDB dynamic nodes to interact with it. Additionally, ensure network access from the YDB dynamic nodes to the external data source (at the address specified in the LOCATION parameter of the CREATE EXTERNAL DATA SOURCE request). If network connection encryption to the external source was enabled in the previous step, the connector will use the system's root certificates. More details on TLS configuration can be found in the guide on deploying the connector.

  4. Execute a query to the database.

Query syntax

The following SQL query form is used to work with PostgreSQL:

SELECT * FROM postgresql_datasource.<table_name>

where:

  • postgresql_datasource: external data source identifier
  • <table_name> is the name of the table inside the external data source.

Limitations

When working with PostgreSQL clusters, there are a number of limitations:

  1. External sources are available only for reading data through SELECT queries. The federated query processing engine currently does not support queries that modify tables in external sources.

  2. If the date value stored in the external data source is outside the allowed range for YDB (all dates used must be later than 1970-01-01 but earlier than 2105-12-31), such a value in YDB will be converted to NULL.

  3. The YDB federated query processing system is capable of delegating the execution of certain parts of a query to the system acting as the data source. Query fragments are passed through YDB directly to the external system and processed by them. This optimization, known as "predicate pushdown", significantly reduces the amount of data transferred from the source to the federated query processing engine. This reduces network load and saves computational resources for the federated YDB.

    A specific case of predicate pushdown, when the filtering expressions are specified after the WHERE keyword, are passed to the data source, is called "filter pushdown". Filter pushdown is possible when using:

    Description Example
    NULL checks WHERE column1 IS NULL or WHERE column1 IS NOT NULL
    Logical conditions OR, NOT, AND and parentheses for controlling calculation priority. WHERE column1 IS NULL OR (column2 IS NOT NULL AND column3 > 10).
    Comparison operators with other columns or constants. WHERE column1 > column2 OR column3 <= 10, WHERE column1 + column2 > 10, WHERE column1 = (10 + 10)

    When using other types of filters, pushdown to the data source is not performed: filtering of the external table rows will be executed by the federated YDB, which means that YDB will perform a full scan of the external table when processing the query.

    Supported data types for filter pushdown:

    Data type YDB
    Bool
    Int8
    Int16
    Int32
    Int64
    Float
    Double
    Decimal

Supported data types

In a PostgreSQL database, the optionality of column values (whether a column is allowed or not allowed to contain NULL values) is not part of the data type system. The constraint NOT NULL for each column is implemented as an attribute attnotnull in the system catalog pg_attribute, that is, at the table metadata level. Consequently, all basic PostgreSQL types can by default contain NULL values, and in the YDB type system they must be mapped to optional types.

Below is a table of correspondence between PostgreSQL types and YDB. All other data types, except those listed, are not supported.

PostgreSQL data type Data type YDB Notes
boolean Optional<Bool>
smallint Optional<Int16>
int2 Optional<Int16>
integer Optional<Int32>
int Optional<Int32>
int4 Optional<Int32>
serial Optional<Int32>
serial4 Optional<Int32>
bigint Optional<Int64>
int8 Optional<Int64>
bigserial Optional<Int64>
serial8 Optional<Int64>
real Optional<Float>
float4 Optional<Float>
double precision Optional<Double>
float8 Optional<Double>
date Optional<Date> Valid date range from 1970-01-01 to 2105-12-31. If the value goes outside the range, NULL is returned.
timestamp Optional<Timestamp> Valid time range from 1970-01-01 00:00:00 to 2105-12-31 23:59:59. If the value goes outside the range, the value NULL is returned.
bytea Optional<String>
character Optional<Utf8> Default collation rules, the string is padded with spaces to the required length.
character varying Optional<Utf8> Default collation rules.
text Optional<Utf8> Default collation rules.
json Optional<Json>
numeric(p,s) Optional<Decimal(p,s)> p (precision) - the total number of digits in the number, s (scale) - the number of digits after the decimal point. Types numeric without parameters (so-called "unconstrained") are converted to Optional<Decimal(35, 0)>. Types numeric with p > 35 or s < 0 are not supported.