39
39
import static io .airbyte .integrations .source .postgres .xmin .XminCtidUtils .reclassifyCategorisedCtidStreams ;
40
40
import static java .util .stream .Collectors .toList ;
41
41
import static java .util .stream .Collectors .toSet ;
42
+ import static org .postgresql .PGProperty .ADAPTIVE_FETCH ;
43
+ import static org .postgresql .PGProperty .CURRENT_SCHEMA ;
44
+ import static org .postgresql .PGProperty .DEFAULT_ROW_FETCH_SIZE ;
45
+ import static org .postgresql .PGProperty .MAX_RESULT_BUFFER ;
46
+ import static org .postgresql .PGProperty .PREPARE_THRESHOLD ;
42
47
43
48
import com .fasterxml .jackson .databind .JsonNode ;
44
49
import com .fasterxml .jackson .databind .node .ObjectNode ;
52
57
import io .airbyte .cdk .db .jdbc .JdbcDatabase ;
53
58
import io .airbyte .cdk .db .jdbc .JdbcUtils ;
54
59
import io .airbyte .cdk .db .jdbc .StreamingJdbcDatabase ;
55
- import io .airbyte .cdk .db .jdbc .streaming .AdaptiveStreamingQueryConfig ;
56
60
import io .airbyte .cdk .integrations .base .AirbyteTraceMessageUtility ;
57
61
import io .airbyte .cdk .integrations .base .IntegrationRunner ;
58
62
import io .airbyte .cdk .integrations .base .Source ;
122
126
import java .util .stream .Stream ;
123
127
import javax .sql .DataSource ;
124
128
import org .apache .commons .lang3 .StringUtils ;
129
+ import org .postgresql .PGProperty ;
125
130
import org .slf4j .Logger ;
126
131
import org .slf4j .LoggerFactory ;
127
132
@@ -146,6 +151,14 @@ public class PostgresSource extends AbstractJdbcSource<PostgresType> implements
146
151
public static final String SSL_MODE_DISABLE = "disable" ;
147
152
public static final String SSL_MODE_REQUIRE = "require" ;
148
153
154
+ public static final Map <PGProperty , String > JDBC_CONNECTION_PARAMS = ImmutableMap .of (
155
+ // Initialize parameters with prepareThreshold=0 to mitigate pgbouncer errors
156
+ // https://github.com/airbytehq/airbyte/issues/24796
157
+ PREPARE_THRESHOLD , "0" ,
158
+ DEFAULT_ROW_FETCH_SIZE , "1" ,
159
+ ADAPTIVE_FETCH , "true" ,
160
+ MAX_RESULT_BUFFER , "10percent" );
161
+
149
162
private List <String > schemas ;
150
163
151
164
private Set <AirbyteStreamNameNamespacePair > publicizedTablesInCdc ;
@@ -157,7 +170,7 @@ public static Source sshWrappedSource(PostgresSource source) {
157
170
}
158
171
159
172
PostgresSource () {
160
- super (DRIVER_CLASS , AdaptiveStreamingQueryConfig ::new , new PostgresSourceOperations ());
173
+ super (DRIVER_CLASS , PostgresStreamingQueryConfig ::new , new PostgresSourceOperations ());
161
174
this .stateEmissionFrequency = INTERMEDIATE_STATE_EMISSION_FREQUENCY ;
162
175
}
163
176
@@ -176,9 +189,9 @@ public ConnectorSpecification spec() throws Exception {
176
189
@ Override
177
190
public JsonNode toDatabaseConfig (final JsonNode config ) {
178
191
final List <String > additionalParameters = new ArrayList <>();
179
- // Initialize parameters with prepareThreshold=0 to mitigate pgbouncer errors
180
- // https://github.com/airbytehq/airbyte/issues/24796
181
- additionalParameters . add ( "prepareThreshold=0" );
192
+ for ( var e : JDBC_CONNECTION_PARAMS . entrySet ()) {
193
+ additionalParameters . add ( e . getKey (). getName () + EQUALS + e . getValue ());
194
+ }
182
195
183
196
final String encodedDatabaseName = URLEncoder .encode (config .get (JdbcUtils .DATABASE_KEY ).asText (), StandardCharsets .UTF_8 );
184
197
@@ -188,7 +201,7 @@ public JsonNode toDatabaseConfig(final JsonNode config) {
188
201
encodedDatabaseName ));
189
202
190
203
if (config .get (JdbcUtils .JDBC_URL_PARAMS_KEY ) != null && !config .get (JdbcUtils .JDBC_URL_PARAMS_KEY ).asText ().isEmpty ()) {
191
- jdbcUrl . append (config .get (JdbcUtils .JDBC_URL_PARAMS_KEY ).asText ()). append ( AMPERSAND );
204
+ additionalParameters . add (config .get (JdbcUtils .JDBC_URL_PARAMS_KEY ).asText ());
192
205
}
193
206
194
207
final Map <String , String > sslParameters = parseSSLConfig (config );
@@ -206,12 +219,10 @@ public JsonNode toDatabaseConfig(final JsonNode config) {
206
219
}
207
220
208
221
if (schemas != null && !schemas .isEmpty ()) {
209
- additionalParameters .add ("currentSchema=" + String .join ("," , schemas ));
222
+ additionalParameters .add (CURRENT_SCHEMA . getName () + EQUALS + String .join ("," , schemas ));
210
223
}
211
-
212
- additionalParameters .forEach (x -> jdbcUrl .append (x ).append ("&" ));
213
-
214
- jdbcUrl .append (toJDBCQueryParams (sslParameters ));
224
+ additionalParameters .addAll (toJDBCQueryParams (sslParameters ));
225
+ jdbcUrl .append (String .join (AMPERSAND , additionalParameters ));
215
226
LOGGER .debug ("jdbc url: {}" , jdbcUrl );
216
227
final ImmutableMap .Builder <Object , Object > configBuilder = ImmutableMap .builder ()
217
228
.put (JdbcUtils .USERNAME_KEY , config .get (JdbcUtils .USERNAME_KEY ).asText ())
@@ -225,8 +236,9 @@ public JsonNode toDatabaseConfig(final JsonNode config) {
225
236
return Jsons .jsonNode (configBuilder .build ());
226
237
}
227
238
228
- public String toJDBCQueryParams (final Map <String , String > sslParams ) {
229
- return Objects .isNull (sslParams ) ? ""
239
+ public List <String > toJDBCQueryParams (final Map <String , String > sslParams ) {
240
+ return Objects .isNull (sslParams )
241
+ ? List .of ()
230
242
: sslParams .entrySet ()
231
243
.stream ()
232
244
.map ((entry ) -> {
@@ -243,7 +255,7 @@ public String toJDBCQueryParams(final Map<String, String> sslParams) {
243
255
}
244
256
})
245
257
.filter (s -> Objects .nonNull (s ) && !s .isEmpty ())
246
- .collect ( Collectors . joining ( JdbcUtils . AMPERSAND ) );
258
+ .toList ( );
247
259
}
248
260
249
261
@ Override
0 commit comments