timescale / timescaledb-toolkit

Extension for more hyperfunctions, fully compatible with TimescaleDB and PostgreSQL 📈
https://www.timescale.com
Other
364 stars 46 forks source link

Generating a docker image for pg14 results into a dynamic load error #344

Open flixman opened 2 years ago

flixman commented 2 years ago

Relevant system information:

Describe the bug Trying to build timescaledb-toolkit for docker image timescale/timescaledb:latest-pg14 is giving trouble. Using this dockerfile, generated from the CD pipeline of timescaledb-toolkit,

FROM timescale/timescaledb:latest-pg14 AS toolkit-tools

RUN apk add --no-cache clang pkgconfig openssl-dev git gcc postgresql14-dev curl clang-static

USER postgres
ENV PATH="/var/lib/postgresql/.cargo/bin:${PATH}" RUSTUP_INIT_SKIP_PATH_CHECK=yes
WORKDIR /var/lib/postgresql

RUN curl https://sh.rustup.rs -sSf | bash -s -- -y --profile=minimal -c rustfmt && \
         cargo install --version 0.2.4 cargo-pgx && \
         cargo pgx init --pg14 `which pg_config`

RUN git clone https://github.com/timescale/timescaledb-toolkit && \
         cd timescaledb-toolkit/extension && \
         cargo pgx install --release && \
         cargo run --manifest-path ../tools/post-install/Cargo.toml -- pg_config

Results in the following error when compiling counter-agg:

thread 'main' panicked at 'Unable to find libclang: "the `libclang` shared library at /usr/lib/libclang.so.12 could not be opened: Dynamic loading not supported"', /var/lib/postgresql/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.59.1/src/lib.rs:2117:31

I am not an expert on rust, so I do not know how to proceed to fix the issue. However, I have noted that there is a library /usr/lib/libclang.a, so... in case the dynamic library is not supported, shouldn't be the static?

To Reproduce Steps to reproduce the behavior:

  1. Copy the snippet into a Dockerfile
  2. execute docker build
flixman commented 2 years ago

Found the issue. This docker file successfully builds the toolkit:

FROM timescale/timescaledb:latest-pg14 AS toolkit-tools

RUN apk add --no-cache clang pkgconfig openssl-dev gcc postgresql14-dev curl jq make musl-dev

RUN chown postgres /usr/local/share/postgresql/extension /usr/local/lib/postgresql

USER postgres
ENV PATH="/var/lib/postgresql/.cargo/bin:${PATH}" RUSTFLAGS='-C target-feature=-crt-static'
WORKDIR /var/lib/postgresql

RUN curl https://sh.rustup.rs -sSf | bash -s -- -y --profile=minimal -c rustfmt && \
    cargo install --version 0.2.4 cargo-pgx && \
    cargo pgx init --pg14 `which pg_config`

RUN mkdir timescaledb-toolkit && \
    curl -s -L `curl -s https://api.github.com/repos/timescale/timescaledb-toolkit/releases/latest | jq -r ".tarball_url"` | tar -zx -C timescaledb-toolkit --strip-components 1

RUN cd timescaledb-toolkit/extension && \
    cargo pgx install --release && \
    cargo run --manifest-path ../tools/post-install/Cargo.toml -- pg_config

FROM timescale/timescaledb:latest-pg14 AS final

COPY --from=toolkit-tools /usr/local/share/postgresql/extension/timescaledb_toolkit* /usr/local/share/postgresql/extension/
COPY --from=toolkit-tools /usr/local/lib/postgresql/timescaledb_toolkit* /usr/local/lib/postgresql/

Now, the next problem: when timescaledb gets started I execute the following statement (which succeeds):

CREATE EXTENSION timescaledb_toolkit;

However, if I try to use any function from the library, I get back an error saying the function cannot be found:

db=# SELECT time_bucket('5m'::interval, time) AS five_min, delta(counter_agg(time, energy_t1_in)) AS net_in FROM electricity GROUP BY five_min;
ERROR:  function counter_agg(timestamp without time zone, numeric) does not exist
LINE 1: ...e_bucket('5m'::interval, time) AS five_min, delta(counter_ag...
                                                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
Tomcat-Engineering commented 2 years ago

Thanks for figuring out the above! I got to the same point and then compared my docker image to the timescaledb-ha images that they have just started publishing, which include the extension.

It looks like the SQL files generated by pgx in /usr/local/share/postgresql/extension/timescaledb_toolkit* are tiny compared to the ones in /usr/share/postgresql/13/extension in the HA image.

As a temporary hack I overwrote these files with the ones from the HA image and the functions now seem to work.