@@ -39,10 +39,6 @@ select compress_chunk(show_chunks('readings'), hypercore_use_access_method => tr
39
39
_timescaledb_internal._hyper_1_4_chunk
40
40
(4 rows)
41
41
42
- -- Insert some extra data to get some non-compressed data as well.
43
- insert into readings (time, location, device, temp, humidity, jdata)
44
- select t, ceil(random()*10), ceil(random()*30), random()*40, random()*100, '{"a":1,"b":2}'::jsonb
45
- from generate_series('2022-06-01 00:01:00'::timestamptz, '2022-06-04'::timestamptz, '5m') t;
46
42
select chunk, amname from chunk_info where hypertable = 'readings'::regclass;
47
43
chunk | amname
48
44
----------------------------------------+-----------
@@ -52,10 +48,144 @@ select chunk, amname from chunk_info where hypertable = 'readings'::regclass;
52
48
_timescaledb_internal._hyper_1_4_chunk | hypercore
53
49
(4 rows)
54
50
55
- -- Pick a chunk to truncate that is not the first chunk. This is
51
+ ----------------------------------------------------------------
52
+ -- Test ALTER TABLE .... ALTER COLUMN commands
53
+ -- This should fail since "location" is NOT NULL
54
+ \set ON_ERROR_STOP 0
55
+ insert into readings(time,device,temp,humidity,jdata)
56
+ values ('2024-01-01 00:00:00', 1, 99.0, 99.0, '{"magic": "yes"}'::jsonb);
57
+ ERROR: null value in column "location" of relation "_hyper_1_9_chunk" violates not-null constraint
58
+ \set ON_ERROR_STOP 1
59
+ -- Test altering column definitions to drop NOT NULL
60
+ alter table readings alter column location drop not null;
61
+ -- This should now work since we allow NULL values
62
+ insert into readings(time,device,temp,humidity,jdata)
63
+ values ('2024-01-01 00:00:00', 1, 99.0, 99.0, '{"magic": "yes"}'::jsonb);
64
+ select count(*) from readings where location is null;
65
+ count
66
+ -------
67
+ 1
68
+ (1 row)
69
+
70
+ select compress_chunk(show_chunks('readings'), hypercore_use_access_method => true);
71
+ NOTICE: chunk "_hyper_1_1_chunk" is already compressed
72
+ NOTICE: chunk "_hyper_1_2_chunk" is already compressed
73
+ NOTICE: chunk "_hyper_1_3_chunk" is already compressed
74
+ NOTICE: chunk "_hyper_1_4_chunk" is already compressed
75
+ compress_chunk
76
+ -----------------------------------------
77
+ _timescaledb_internal._hyper_1_1_chunk
78
+ _timescaledb_internal._hyper_1_2_chunk
79
+ _timescaledb_internal._hyper_1_3_chunk
80
+ _timescaledb_internal._hyper_1_4_chunk
81
+ _timescaledb_internal._hyper_1_10_chunk
82
+ (5 rows)
83
+
84
+ select count(*) from readings where location is null;
85
+ count
86
+ -------
87
+ 1
88
+ (1 row)
89
+
90
+ -- Pick a chunk to play with that is not the first chunk. This is
56
91
-- mostly a precaution to make sure that there is no bias towards the
57
92
-- first chunk and we could just as well pick the first chunk.
58
93
select chunk from show_chunks('readings') x(chunk) limit 1 offset 3 \gset
94
+ -- We should not be able to set the not null before we have removed
95
+ -- the null rows in the table. This works for hypercore-compressed
96
+ -- chunks but not for heap-compressed chunks.
97
+ \set ON_ERROR_STOP 0
98
+ alter table readings alter column location set not null;
99
+ ERROR: column "location" of relation "_hyper_1_10_chunk" contains null values
100
+ \set ON_ERROR_STOP 1
101
+ delete from readings where location is null;
102
+ select count(*) from readings where location is null;
103
+ count
104
+ -------
105
+ 0
106
+ (1 row)
107
+
108
+ \d readings
109
+ Table "public.readings"
110
+ Column | Type | Collation | Nullable | Default
111
+ ----------+--------------------------+-----------+----------+---------
112
+ time | timestamp with time zone | | not null |
113
+ location | integer | | |
114
+ device | integer | | not null |
115
+ temp | numeric(4,1) | | |
116
+ humidity | double precision | | |
117
+ jdata | jsonb | | |
118
+ Indexes:
119
+ "readings_time_key" UNIQUE CONSTRAINT, btree ("time")
120
+ Triggers:
121
+ ts_insert_blocker BEFORE INSERT ON readings FOR EACH ROW EXECUTE FUNCTION _timescaledb_functions.insert_blocker()
122
+ Number of child tables: 5 (Use \d+ to list them.)
123
+
124
+ \d :chunk
125
+ Table "_timescaledb_internal._hyper_1_4_chunk"
126
+ Column | Type | Collation | Nullable | Default
127
+ ----------+--------------------------+-----------+----------+---------
128
+ time | timestamp with time zone | | not null |
129
+ location | integer | | |
130
+ device | integer | | not null |
131
+ temp | numeric(4,1) | | |
132
+ humidity | double precision | | |
133
+ jdata | jsonb | | |
134
+ Indexes:
135
+ "4_4_readings_time_key" UNIQUE CONSTRAINT, btree ("time")
136
+ Check constraints:
137
+ "constraint_4" CHECK ("time" >= 'Fri Jun 03 17:00:00 2022 PDT'::timestamp with time zone AND "time" < 'Sat Jun 04 17:00:00 2022 PDT'::timestamp with time zone)
138
+ Inherits: readings
139
+
140
+ alter table readings alter column location set not null;
141
+ \d readings
142
+ Table "public.readings"
143
+ Column | Type | Collation | Nullable | Default
144
+ ----------+--------------------------+-----------+----------+---------
145
+ time | timestamp with time zone | | not null |
146
+ location | integer | | not null |
147
+ device | integer | | not null |
148
+ temp | numeric(4,1) | | |
149
+ humidity | double precision | | |
150
+ jdata | jsonb | | |
151
+ Indexes:
152
+ "readings_time_key" UNIQUE CONSTRAINT, btree ("time")
153
+ Triggers:
154
+ ts_insert_blocker BEFORE INSERT ON readings FOR EACH ROW EXECUTE FUNCTION _timescaledb_functions.insert_blocker()
155
+ Number of child tables: 5 (Use \d+ to list them.)
156
+
157
+ \d :chunk
158
+ Table "_timescaledb_internal._hyper_1_4_chunk"
159
+ Column | Type | Collation | Nullable | Default
160
+ ----------+--------------------------+-----------+----------+---------
161
+ time | timestamp with time zone | | not null |
162
+ location | integer | | not null |
163
+ device | integer | | not null |
164
+ temp | numeric(4,1) | | |
165
+ humidity | double precision | | |
166
+ jdata | jsonb | | |
167
+ Indexes:
168
+ "4_4_readings_time_key" UNIQUE CONSTRAINT, btree ("time")
169
+ Check constraints:
170
+ "constraint_4" CHECK ("time" >= 'Fri Jun 03 17:00:00 2022 PDT'::timestamp with time zone AND "time" < 'Sat Jun 04 17:00:00 2022 PDT'::timestamp with time zone)
171
+ Inherits: readings
172
+
173
+ select count(*) from readings where location is null;
174
+ count
175
+ -------
176
+ 0
177
+ (1 row)
178
+
179
+ ----------------------------------------------------------------
180
+ -- TRUNCATE test
181
+ -- We keep the truncate test last in the file to avoid having to
182
+ -- re-populate it.
183
+ -- Insert some extra data to get some non-compressed data as
184
+ -- well. This checks that truncate will deal with with write-store
185
+ -- (WS) and read-store (RS)
186
+ insert into readings (time, location, device, temp, humidity, jdata)
187
+ select t, ceil(random()*10), ceil(random()*30), random()*40, random()*100, '{"a":1,"b":2}'::jsonb
188
+ from generate_series('2022-06-01 00:01:00'::timestamptz, '2022-06-04'::timestamptz, '5m') t;
59
189
-- Check that the number of bytes in the table before and after the
60
190
-- truncate.
61
191
--
@@ -68,7 +198,7 @@ select pg_table_size(chunk) as chunk_size,
68
198
where chunk = :'chunk'::regclass;
69
199
chunk_size | compressed_chunk_size
70
200
------------+-----------------------
71
- 40960 | 57344
201
+ 49152 | 57344
72
202
(1 row)
73
203
74
204
truncate :chunk;
@@ -88,7 +218,7 @@ select (select count(*) from readings) tuples,
88
218
(select count(*) from show_chunks('readings')) chunks;
89
219
tuples | chunks
90
220
--------+--------
91
- 1560 | 4
221
+ 1560 | 5
92
222
(1 row)
93
223
94
224
truncate readings;
@@ -99,32 +229,3 @@ select (select count(*) from readings) tuples,
99
229
0 | 0
100
230
(1 row)
101
231
102
- \set ON_ERROR_STOP 0
103
- insert into readings(time,device,temp,humidity,jdata)
104
- values ('2024-01-01 00:00:00', 1, 99.0, 99.0, '{"magic": "yes"}'::jsonb);
105
- ERROR: null value in column "location" of relation "_hyper_1_9_chunk" violates not-null constraint
106
- \set ON_ERROR_STOP 1
107
- -- Test altering column definitions
108
- alter table readings
109
- alter column location drop not null;
110
- -- This should now work.
111
- insert into readings(time,device,temp,humidity,jdata)
112
- values ('2024-01-01 00:00:00', 1, 99.0, 99.0, '{"magic": "yes"}'::jsonb);
113
- select count(*) from readings where location is null;
114
- count
115
- -------
116
- 1
117
- (1 row)
118
-
119
- select compress_chunk(show_chunks('readings'), hypercore_use_access_method => true);
120
- compress_chunk
121
- -----------------------------------------
122
- _timescaledb_internal._hyper_1_10_chunk
123
- (1 row)
124
-
125
- select count(*) from readings where location is null;
126
- count
127
- -------
128
- 1
129
- (1 row)
130
-
0 commit comments