|
| 1 | +--- |
| 2 | +title: PostgreSQL |
| 3 | +parent: SQL Dialects |
| 4 | +nav_order: 4 |
| 5 | +permalink: /sql-dialects/postgresql/ |
| 6 | +description: "PostgreSQL dialect — double-quote quoting, ILIKE operators, and RETURNING on DELETE" |
| 7 | +--- |
| 8 | + |
| 9 | +# PostgreSQL Dialect |
| 10 | +{: .no_toc } |
| 11 | + |
| 12 | +## Table of contents |
| 13 | +{: .no_toc .text-delta } |
| 14 | + |
| 15 | +1. TOC |
| 16 | +{:toc} |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Overview |
| 21 | + |
| 22 | +`SqlDialect.POSTGRESQL` wraps all table and column identifiers in double-quotes |
| 23 | +and adds two PostgreSQL-specific features: case-insensitive `ILIKE` / `NOT ILIKE` |
| 24 | +operators on SELECT queries, and a `RETURNING` clause on `DELETE` statements. |
| 25 | + |
| 26 | +| Feature | Value | |
| 27 | +|---------|-------| |
| 28 | +| Identifier quoting | Double-quote `"` | |
| 29 | +| DELETE LIMIT | Not supported | |
| 30 | +| ILIKE / NOT ILIKE | Supported | |
| 31 | +| RETURNING on DELETE | Supported | |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## SELECT with double-quote quoting |
| 36 | + |
| 37 | +```java |
| 38 | +SqlResult r = new QueryBuilder() |
| 39 | + .from("users") |
| 40 | + .select("id", "name", "email") |
| 41 | + .whereEquals("status", "active") |
| 42 | + .orderBy("name", true) |
| 43 | + .buildSql(SqlDialect.POSTGRESQL); |
| 44 | +// → SELECT "id", "name", "email" FROM "users" WHERE "status" = ? ORDER BY "name" ASC |
| 45 | +// Parameters: ["active"] |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## ILIKE — case-insensitive LIKE |
| 51 | + |
| 52 | +`ILIKE` is a PostgreSQL extension for case-insensitive pattern matching. |
| 53 | +Use `whereILike` / `orWhereILike` on `QueryBuilder` or `SelectBuilder`: |
| 54 | + |
| 55 | +```java |
| 56 | +// Match email containing "alice" (any case) |
| 57 | +SqlResult r = new QueryBuilder() |
| 58 | + .from("users") |
| 59 | + .whereILike("email", "alice") |
| 60 | + .buildSql(SqlDialect.POSTGRESQL); |
| 61 | +// → SELECT * FROM "users" WHERE "email" ILIKE ? |
| 62 | +// Parameters: ["%alice%"] |
| 63 | + |
| 64 | +// Combine with other conditions |
| 65 | +SqlResult r2 = new QueryBuilder() |
| 66 | + .from("articles") |
| 67 | + .whereEquals("published", true) |
| 68 | + .whereILike("title", "java") |
| 69 | + .buildSql(SqlDialect.POSTGRESQL); |
| 70 | +// → SELECT * FROM "articles" WHERE "published" = ? AND "title" ILIKE ? |
| 71 | + |
| 72 | +// OR variant |
| 73 | +SqlResult r3 = new QueryBuilder() |
| 74 | + .from("users") |
| 75 | + .whereEquals("role", "admin") |
| 76 | + .orWhereILike("name", "bot") |
| 77 | + .buildSql(SqlDialect.POSTGRESQL); |
| 78 | +// → SELECT * FROM "users" WHERE "role" = ? OR "name" ILIKE ? |
| 79 | +``` |
| 80 | + |
| 81 | +{: .note } |
| 82 | +> `whereILike` and `orWhereILike` use the `ILIKE` operator which is only |
| 83 | +> rendered correctly by `SqlDialect.POSTGRESQL`. Passing a different dialect |
| 84 | +> will render the operator as-is without case-insensitive behaviour. |
| 85 | +
|
| 86 | +--- |
| 87 | + |
| 88 | +## DELETE with RETURNING |
| 89 | + |
| 90 | +`RETURNING` on DELETE is rendered by the dialect — it is appended only when |
| 91 | +`SqlDialect.POSTGRESQL` is active: |
| 92 | + |
| 93 | +```java |
| 94 | +SqlResult result = QueryBuilder.deleteFrom("users") |
| 95 | + .whereEquals("id", 99) |
| 96 | + .returning("id", "email") |
| 97 | + .build(SqlDialect.POSTGRESQL); |
| 98 | + |
| 99 | +// → DELETE FROM "users" WHERE "id" = ? RETURNING id, email |
| 100 | +// Parameters: [99] |
| 101 | +``` |
| 102 | + |
| 103 | +Without the PostgreSQL dialect the `RETURNING` columns are ignored even if set: |
| 104 | + |
| 105 | +```java |
| 106 | +// RETURNING is silently dropped for non-supporting dialects |
| 107 | +SqlResult result = QueryBuilder.deleteFrom("users") |
| 108 | + .whereEquals("id", 99) |
| 109 | + .returning("id", "email") |
| 110 | + .build(SqlDialect.STANDARD); |
| 111 | +// → DELETE FROM users WHERE id = ? |
| 112 | +``` |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## INSERT with RETURNING |
| 117 | + |
| 118 | +For INSERT, `RETURNING` is appended inline regardless of dialect — the caller |
| 119 | +is responsible for using a PostgreSQL connection: |
| 120 | + |
| 121 | +```java |
| 122 | +SqlResult result = QueryBuilder.insertInto("users") |
| 123 | + .value("name", "Alice") |
| 124 | + .value("email", "alice@example.com") |
| 125 | + .returning("id", "created_at") |
| 126 | + .build(); |
| 127 | +// → INSERT INTO users (name, email) VALUES (?, ?) RETURNING id, created_at |
| 128 | +// Parameters: ["Alice", "alice@example.com"] |
| 129 | +``` |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +## UPDATE with RETURNING |
| 134 | + |
| 135 | +Same inline behaviour as INSERT: |
| 136 | + |
| 137 | +```java |
| 138 | +SqlResult result = QueryBuilder.update("users") |
| 139 | + .set("status", "active") |
| 140 | + .whereEquals("id", 7) |
| 141 | + .returning("id", "updated_at") |
| 142 | + .build(); |
| 143 | +// → UPDATE users SET status = ? WHERE id = ? RETURNING id, updated_at |
| 144 | +// Parameters: ["active", 7] |
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## Identifier quoting coverage |
| 150 | + |
| 151 | +Double-quote quoting is applied by the dialect to identifiers in SELECT and |
| 152 | +DELETE statements. INSERT and UPDATE builders render their own SQL and do not |
| 153 | +apply dialect quoting to column or table names. |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## When to use |
| 158 | + |
| 159 | +- PostgreSQL databases |
| 160 | +- When you need case-insensitive LIKE matching (`ILIKE`) |
| 161 | +- When you need to retrieve affected row values from a DELETE in a single |
| 162 | + round-trip (`RETURNING`) |
0 commit comments