|
| 1 | +# ETL Adapter: PostgreSQL |
| 2 | + |
| 3 | +- [⬅️️ Back](/documentation/introduction.md) |
| 4 | +- [📦Packagist](https://packagist.org/packages/flow-php/etl-adapter-postgresql) |
| 5 | +- [🐙GitHub](https://github.com/flow-php/etl-adapter-postgresql) |
| 6 | +- [📚API Reference](/documentation/api/adapter/postgresql) |
| 7 | +- [📁Files](/documentation/api/adapter/postgresql/indices/files.html) |
| 8 | + |
| 9 | +[TOC] |
| 10 | + |
| 11 | +Flow PHP's Adapter PostgreSQL is designed to seamlessly integrate PostgreSQL within your ETL (Extract, Transform, Load) workflows. This adapter is built on top of the [PostgreSQL library](/documentation/components/libs/postgresql/client-connection.md), providing efficient data extraction capabilities with built-in pagination support. By harnessing the Adapter PostgreSQL library, developers can tap into robust features for precise database interaction, simplifying complex data transformations and enhancing data processing efficiency. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +``` |
| 16 | +composer require flow-php/etl-adapter-postgresql:~--FLOW_PHP_VERSION-- |
| 17 | +``` |
| 18 | + |
| 19 | +## Requirements |
| 20 | + |
| 21 | +- PHP 8.3+ |
| 22 | +- ext-pgsql |
| 23 | +- ext-pg_query (optional, for query builder support) |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +This adapter provides two extraction strategies optimized for different use cases: |
| 28 | + |
| 29 | +- **LIMIT/OFFSET Pagination**: Simple pagination suitable for smaller datasets |
| 30 | +- **Keyset (Cursor) Pagination**: Efficient pagination for large datasets with consistent performance |
| 31 | + |
| 32 | +Both extractors support: |
| 33 | +- Raw SQL strings or Query Builder objects |
| 34 | +- Configurable page sizes |
| 35 | +- Maximum row limits |
| 36 | +- Custom schema definitions |
| 37 | + |
| 38 | +## Extractor - LIMIT/OFFSET Pagination |
| 39 | + |
| 40 | +The `from_pgsql_limit_offset` extractor uses traditional LIMIT/OFFSET pagination. This is simple to use but may have performance degradation on very large datasets with high offsets. |
| 41 | + |
| 42 | +### Basic Usage |
| 43 | + |
| 44 | +```php |
| 45 | +use function Flow\ETL\Adapter\PostgreSql\from_pgsql_limit_offset; |
| 46 | +use function Flow\PostgreSql\DSL\{pgsql_client, pgsql_connection_dsn}; |
| 47 | + |
| 48 | +$client = pgsql_client(pgsql_connection_dsn('pgsql://user:pass@localhost:5432/database')); |
| 49 | + |
| 50 | +data_frame() |
| 51 | + ->read(from_pgsql_limit_offset( |
| 52 | + $client, |
| 53 | + "SELECT id, name, email FROM users ORDER BY id", |
| 54 | + pageSize: 1000 |
| 55 | + )) |
| 56 | + ->write(to_output()) |
| 57 | + ->run(); |
| 58 | +``` |
| 59 | + |
| 60 | +### With Query Builder |
| 61 | + |
| 62 | +```php |
| 63 | +use function Flow\ETL\Adapter\PostgreSql\from_pgsql_limit_offset; |
| 64 | +use function Flow\PostgreSql\DSL\{asc, col, select, table}; |
| 65 | + |
| 66 | +data_frame() |
| 67 | + ->read(from_pgsql_limit_offset( |
| 68 | + $client, |
| 69 | + select(col('id'), col('name'), col('email')) |
| 70 | + ->from(table('users')) |
| 71 | + ->orderBy(asc(col('id'))), |
| 72 | + pageSize: 500 |
| 73 | + )) |
| 74 | + ->write(to_output()) |
| 75 | + ->run(); |
| 76 | +``` |
| 77 | + |
| 78 | +### With Maximum Row Limit |
| 79 | + |
| 80 | +```php |
| 81 | +use function Flow\ETL\Adapter\PostgreSql\from_pgsql_limit_offset; |
| 82 | + |
| 83 | +data_frame() |
| 84 | + ->read(from_pgsql_limit_offset( |
| 85 | + $client, |
| 86 | + "SELECT * FROM large_table ORDER BY id", |
| 87 | + pageSize: 1000, |
| 88 | + maximum: 10000 // Only extract first 10,000 rows |
| 89 | + )) |
| 90 | + ->write(to_output()) |
| 91 | + ->run(); |
| 92 | +``` |
| 93 | + |
| 94 | +## Extractor - Keyset (Cursor) Pagination |
| 95 | + |
| 96 | +The `from_pgsql_key_set` extractor uses keyset pagination (also known as cursor-based pagination). This provides consistent performance regardless of how deep you paginate, making it ideal for large datasets. |
| 97 | + |
| 98 | +### Basic Usage |
| 99 | + |
| 100 | +```php |
| 101 | +use function Flow\ETL\Adapter\PostgreSql\{from_pgsql_key_set, pgsql_pagination_key_asc, pgsql_pagination_key_set}; |
| 102 | + |
| 103 | +data_frame() |
| 104 | + ->read(from_pgsql_key_set( |
| 105 | + $client, |
| 106 | + "SELECT id, name, email FROM users", |
| 107 | + pgsql_pagination_key_set(pgsql_pagination_key_asc('id')), |
| 108 | + pageSize: 1000 |
| 109 | + )) |
| 110 | + ->write(to_output()) |
| 111 | + ->run(); |
| 112 | +``` |
| 113 | + |
| 114 | +### Descending Order |
| 115 | + |
| 116 | +```php |
| 117 | +use function Flow\ETL\Adapter\PostgreSql\{from_pgsql_key_set, pgsql_pagination_key_desc, pgsql_pagination_key_set}; |
| 118 | + |
| 119 | +data_frame() |
| 120 | + ->read(from_pgsql_key_set( |
| 121 | + $client, |
| 122 | + "SELECT id, name, created_at FROM orders", |
| 123 | + pgsql_pagination_key_set(pgsql_pagination_key_desc('id')), // Newest first |
| 124 | + pageSize: 500 |
| 125 | + )) |
| 126 | + ->write(to_output()) |
| 127 | + ->run(); |
| 128 | +``` |
| 129 | + |
| 130 | +### Composite Keys |
| 131 | + |
| 132 | +For tables with composite ordering, you can specify multiple keys: |
| 133 | + |
| 134 | +```php |
| 135 | +use function Flow\ETL\Adapter\PostgreSql\{from_pgsql_key_set, pgsql_pagination_key_asc, pgsql_pagination_key_desc, pgsql_pagination_key_set}; |
| 136 | + |
| 137 | +data_frame() |
| 138 | + ->read(from_pgsql_key_set( |
| 139 | + $client, |
| 140 | + "SELECT * FROM events", |
| 141 | + pgsql_pagination_key_set( |
| 142 | + pgsql_pagination_key_desc('created_at'), // First by date descending |
| 143 | + pgsql_pagination_key_asc('id') // Then by ID ascending |
| 144 | + ), |
| 145 | + pageSize: 1000 |
| 146 | + )) |
| 147 | + ->write(to_output()) |
| 148 | + ->run(); |
| 149 | +``` |
| 150 | + |
| 151 | +### With Query Builder |
| 152 | + |
| 153 | +```php |
| 154 | +use function Flow\ETL\Adapter\PostgreSql\{from_pgsql_key_set, pgsql_pagination_key_asc, pgsql_pagination_key_set}; |
| 155 | +use function Flow\PostgreSql\DSL\{col, select, star, table}; |
| 156 | + |
| 157 | +data_frame() |
| 158 | + ->read(from_pgsql_key_set( |
| 159 | + $client, |
| 160 | + select(star())->from(table('products')), |
| 161 | + pgsql_pagination_key_set(pgsql_pagination_key_asc('product_id')), |
| 162 | + pageSize: 500 |
| 163 | + )) |
| 164 | + ->write(to_output()) |
| 165 | + ->run(); |
| 166 | +``` |
| 167 | + |
| 168 | +## DSL Functions Reference |
| 169 | + |
| 170 | +### Extractor Functions |
| 171 | + |
| 172 | +| Function | Description | |
| 173 | +|----------|-------------| |
| 174 | +| `from_pgsql_limit_offset($client, $query, $pageSize, $maximum)` | Extract using LIMIT/OFFSET pagination | |
| 175 | +| `from_pgsql_key_set($client, $query, $keySet, $pageSize, $maximum)` | Extract using keyset pagination | |
| 176 | + |
| 177 | +### Key Functions |
| 178 | + |
| 179 | +| Function | Description | |
| 180 | +|----------|-------------| |
| 181 | +| `pgsql_pagination_key_asc($column)` | Create an ascending key for keyset pagination | |
| 182 | +| `pgsql_pagination_key_desc($column)` | Create a descending key for keyset pagination | |
| 183 | +| `pgsql_pagination_key_set(...$keys)` | Create a keyset from one or more keys | |
| 184 | + |
| 185 | +## Choosing Between Extractors |
| 186 | + |
| 187 | +### Use LIMIT/OFFSET when: |
| 188 | +- Working with smaller datasets (< 100k rows) |
| 189 | +- You need simple, straightforward pagination |
| 190 | +- Random page access is required |
| 191 | +- The offset values remain relatively small |
| 192 | + |
| 193 | +### Use Keyset Pagination when: |
| 194 | +- Working with large datasets (100k+ rows) |
| 195 | +- Performance consistency is critical |
| 196 | +- You're doing sequential/forward pagination |
| 197 | +- Your table has suitable indexed columns for the keyset |
| 198 | + |
| 199 | +## Performance Considerations |
| 200 | + |
| 201 | +### LIMIT/OFFSET |
| 202 | + |
| 203 | +- Simple to understand and implement |
| 204 | +- Performance degrades as offset increases (PostgreSQL must skip all previous rows) |
| 205 | +- Memory usage increases with larger offsets |
| 206 | + |
| 207 | +### Keyset Pagination |
| 208 | + |
| 209 | +- Consistent O(1) performance regardless of position |
| 210 | +- Requires indexed columns in the keyset |
| 211 | +- Cannot jump to arbitrary pages (sequential access only) |
| 212 | +- Handles concurrent modifications more gracefully |
0 commit comments