You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
90
94
91
95
```sql
92
96
CREATETABLEorders (
@@ -100,47 +104,51 @@ CREATE TABLE orders (
100
104
);
101
105
```
102
106
103
-
You'll see a NOTICE suggesting the optimal order:
104
-
105
107
```
106
108
NOTICE: pg_column_tetris: suboptimal column alignment — 19 bytes of fixed-width padding wasted per row
107
109
```
108
110
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:
110
116
111
117
```sql
112
118
SELECTcolumn_tetris.set_mode('strict');
119
+
120
+
CREATETABLEorders ( ... );
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
+
-- );
113
132
```
114
133
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.
116
135
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
130
137
131
-
## Auditing Existing Tables
138
+
Use `check()` to inspect any table's current layout and see where padding is wasted:
132
139
133
140
```sql
134
-
-- Detailed layout report for a single table
135
141
SELECT*FROMcolumn_tetris.check('orders');
142
+
```
143
+
144
+
Use `suggest_rewrite()` to generate a complete migration script that reorders the columns optimally:
136
145
137
-
-- Generate migration DDL to fix a table
146
+
```sql
138
147
SELECTcolumn_tetris.suggest_rewrite('orders');
139
148
```
140
149
141
-
The `suggest_rewrite` function generates a complete migration script:
142
-
143
150
```sql
151
+
-- Generated output:
144
152
BEGIN;
145
153
ALTERTABLEpublic.orders RENAME TO orders_old;
146
154
CREATETABLEpublic.orders ( ...optimal order... );
@@ -149,20 +157,19 @@ DROP TABLE public.orders_old;
149
157
COMMIT;
150
158
```
151
159
152
-
## Configuration
160
+
You can use `off` mode if you want to disable the event trigger entirely and just use the analysis functions.
153
161
154
-
```sql
155
-
-- Set mode: 'strict' (block), 'warn' (notice only), 'off' (disable)
156
-
SELECTcolumn_tetris.set_mode('strict');
162
+
### Other configuration
157
163
164
+
```sql
158
165
-- Check current mode
159
166
SELECTcolumn_tetris.mode();
160
167
161
168
-- Exclude a table from validation (e.g., matching an external schema)
162
169
SELECTcolumn_tetris.exclude('legacy_imports');
163
170
```
164
171
165
-
## What Gets Checked
172
+
###What gets checked
166
173
167
174
-**CREATE TABLE** statements are validated by the event trigger
168
175
-**ALTER TABLE** is deliberately skipped — you can't reorder existing columns, so warning would be noise
0 commit comments