Skip to content

Commit 60d6af4

Browse files
committed
tweak readme
1 parent 2c90887 commit 60d6af4

1 file changed

Lines changed: 36 additions & 29 deletions

File tree

README.md

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ Since there's no C code, the extension runs anywhere PostgreSQL does:
8484
psql -d your_database -f pg_column_tetris--0.1.0.sql
8585
```
8686

87-
## Quick Start
87+
## Usage
8888

89-
The extension installs in **warn** mode by default. Create a table with suboptimal column order:
89+
The extension has three modes (`warn`, `strict`, `off`) that cover different workflows.
90+
91+
### Warn mode (default) — catch bad ordering during development
92+
93+
The extension installs in `warn` mode. Any `CREATE TABLE` with suboptimal column order emits a NOTICE but still succeeds:
9094

9195
```sql
9296
CREATE TABLE orders (
@@ -100,47 +104,51 @@ CREATE TABLE orders (
100104
);
101105
```
102106

103-
You'll see a NOTICE suggesting the optimal order:
104-
105107
```
106108
NOTICE: pg_column_tetris: suboptimal column alignment — 19 bytes of fixed-width padding wasted per row
107109
```
108110

109-
Switch to **strict** mode to block suboptimal tables entirely:
111+
Good for development — you see the problem without breaking anything.
112+
113+
### Strict mode — enforce alignment in CI/migrations
114+
115+
In strict mode, `CREATE TABLE` with suboptimal column order is **blocked** and rolled back. The error message includes the optimal column order so you can fix it immediately:
110116

111117
```sql
112118
SELECT column_tetris.set_mode('strict');
119+
120+
CREATE TABLE orders ( ... );
121+
-- ERROR: suboptimal column alignment — 19 bytes of fixed-width padding wasted per row
122+
-- HINT: Suggested order:
123+
-- CREATE TABLE orders (
124+
-- user_id bigint, -- 8-byte aligned
125+
-- order_dt timestamptz, -- 8-byte aligned
126+
-- ship_dt timestamptz, -- 8-byte aligned
127+
-- item_ct smallint, -- 2-byte aligned
128+
-- status smallint, -- 2-byte aligned
129+
-- is_shipped boolean, -- 1-byte aligned
130+
-- order_total numeric -- varlena (last)
131+
-- );
113132
```
114133

115-
Now the same CREATE TABLE will fail with an error and a hint showing the optimal column order:
134+
Use this in staging/production databases or CI pipelines to guarantee every new table has optimal alignment.
116135

117-
```
118-
ERROR: suboptimal column alignment — 19 bytes of fixed-width padding wasted per row
119-
HINT: Suggested order:
120-
CREATE TABLE orders (
121-
user_id bigint, -- 8-byte aligned
122-
order_dt timestamptz, -- 8-byte aligned
123-
ship_dt timestamptz, -- 8-byte aligned
124-
item_ct smallint, -- 2-byte aligned
125-
status smallint, -- 2-byte aligned
126-
is_shipped boolean, -- 1-byte aligned
127-
order_total numeric -- varlena (last)
128-
);
129-
```
136+
### As an analysis tool — audit existing tables
130137

131-
## Auditing Existing Tables
138+
Use `check()` to inspect any table's current layout and see where padding is wasted:
132139

133140
```sql
134-
-- Detailed layout report for a single table
135141
SELECT * FROM column_tetris.check('orders');
142+
```
143+
144+
Use `suggest_rewrite()` to generate a complete migration script that reorders the columns optimally:
136145

137-
-- Generate migration DDL to fix a table
146+
```sql
138147
SELECT column_tetris.suggest_rewrite('orders');
139148
```
140149

141-
The `suggest_rewrite` function generates a complete migration script:
142-
143150
```sql
151+
-- Generated output:
144152
BEGIN;
145153
ALTER TABLE public.orders RENAME TO orders_old;
146154
CREATE TABLE public.orders ( ...optimal order... );
@@ -149,20 +157,19 @@ DROP TABLE public.orders_old;
149157
COMMIT;
150158
```
151159

152-
## Configuration
160+
You can use `off` mode if you want to disable the event trigger entirely and just use the analysis functions.
153161

154-
```sql
155-
-- Set mode: 'strict' (block), 'warn' (notice only), 'off' (disable)
156-
SELECT column_tetris.set_mode('strict');
162+
### Other configuration
157163

164+
```sql
158165
-- Check current mode
159166
SELECT column_tetris.mode();
160167

161168
-- Exclude a table from validation (e.g., matching an external schema)
162169
SELECT column_tetris.exclude('legacy_imports');
163170
```
164171

165-
## What Gets Checked
172+
### What gets checked
166173

167174
- **CREATE TABLE** statements are validated by the event trigger
168175
- **ALTER TABLE** is deliberately skipped — you can't reorder existing columns, so warning would be noise

0 commit comments

Comments
 (0)