pgjdbc/r2dbc-postgresql

Introduce connection factory option for statementTimeout

alampada opened this issue · 15 comments

Feature Request

Is your feature request related to a problem? Please describe

I'd like to be able to set the statement timeout on connection-level.

This code below achieves the desired behavior

	return  ConnectionFactories.get(ConnectionFactoryOptions.builder()
				.option(DRIVER, "postgresql")
				.option(HOST, "172.17.0.2")
				.option(USER, "x")
				.option(PASSWORD, "x")
				.option(Option.valueOf("options"), Map.of("statement_timeout", "1000"))
				.build());

Describe the solution you'd like

As per the discussion in r2dbc/r2dbc-spi#173 (comment), it would be nice to be able to set the statement_timeout option through the connection url.

Describe alternatives you've considered

  • Workaround above.
  • Handling timeouts with Flux/Mono timeouts instead of statement_timeout, but as far as I understand the Mono/Flux timeout approach doesn't cancel the query.

Teachability, Documentation, Adoption, Migration Strategy

Users will be able to set the statement_timeout in the connection URL, for example in the spring configuration files instead of having to write code. Readme needs to update to reflect the change.

I am willing to work on this.

As part of this feature, are we planning to introduce 2 things: PostgresqlConnectionFactory#statementTimeout and PostgresqlConnectionFactory.option(STATEMENT_TIMEOUT, ...)?

or only via option?

On PostgresqlConnectionFactory, we don't configure the timeout. Do you rather mean PostgresqlConnectionConfiguration? Other than that, any help is welcome. Let us know if you have any questions.

Ah! yes PostgresqlConnectionConfiguration

If I'm not mistaken, we need to add a builder method PostgresqlConnectionConfiguratio#statementTimeout(...) right?

Actually, it's the builder for the config class.

Thanks, Mark. I have a couple of questions.

  • Should the method accept Duration or String type ?
  • After creating the builder method, do I need to configure/set statement_timeout somewhere? I am unable to figure out where it's configured.

We should define the property as Option<Duration>, similar to connection timeout. On the side that accepts the value, we could allow for Duration and String values (PT1S), we have already converters for other data types in place.

I'm not sure whether we need to issue a SET statement_timeout = … statement or whether we can pass on the option in the startup packet along with other options. Paging @davecramer.

Hi @davecramer, could you please advise me on the above queries.

hi, @mp911de could you please review the PR?!

looks like we need to send these parameters in StartUpMessage itself Jdbc-Connect.md

Thanks for submitting a PR. I've been on vacation recently and didn't get to review recent pull requests. AFAIK, Postgres allows setting quite a few variables in the startup so that it doesn't require any subsequent SET … interactions.

While already at it, care to add also methods to PostgresqlConnection to set lock/statement timeouts through Duration? These would translate to set statement_timeout=… calls. There's already a specification request in the SPI (r2dbc/r2dbc-spi#173) and we would basically implement what's planned anyway.

Hi,

Thank you for looking into this.

I'd like to share that after further investigation, we were able to set the statement_timeout in the url using this format: r2dbc:postgresql://172.17.0.2:5432/postgres?options=statement_timeout=1s. Based on this, what are your thoughts in terms of documenting the current behaviour instead of doing code changes?

In terms of the connection factory option statement_timeout, it always makes sense to align with PGJDBC so URLs remain interchangeable where possible.

We document connection factory options in the readme, see https://github.com/pgjdbc/r2dbc-postgresql#getting-started

Thanks for submitting a PR. I've been on vacation recently and didn't get to review recent pull requests. AFAIK, Postgres allows setting quite a few variables in the startup so that it doesn't require any subsequent SET … interactions.

While already at it, care to add also methods to PostgresqlConnection to set lock/statement timeouts through Duration? These would translate to set statement_timeout=… calls. There's already a specification request in the SPI (r2dbc/r2dbc-spi#173) and we would basically implement what's planned anyway.

Sure. Let me add these 2 methods to PostgresqlConnection. Please correct me If I am wrong, should these two APIs return Mono<Void>?

Mono<Void> lockTimeout(Duration lockTimeout)
Mono<Void> statementTimeout(Duration statementTimeout)

Exactly. We just want to await completion.

Added these 2 APIs!