|
140 | 140 | output = pathman_range_out
|
141 | 141 | );
|
142 | 142 |
|
143 |
| -/* |
144 |
| - * Copy rows to partitions |
145 |
| - */ |
146 |
| --- CREATE OR REPLACE FUNCTION @[email protected]_data( |
147 |
| --- p_relation regclass |
148 |
| --- , p_min ANYELEMENT DEFAULT NULL::text |
149 |
| --- , p_max ANYELEMENT DEFAULT NULL::text |
150 |
| --- , p_limit INT DEFAULT NULL |
151 |
| --- , OUT p_total BIGINT) |
152 |
| --- AS |
153 |
| --- $$ |
154 |
| --- DECLARE |
155 |
| --- v_attr TEXT; |
156 |
| --- v_limit_clause TEXT := ''; |
157 |
| --- v_where_clause TEXT := ''; |
158 |
| --- BEGIN |
159 |
| --- SELECT attname INTO v_attr |
160 |
| --- FROM @[email protected]_config WHERE partrel = p_relation; |
161 |
| - |
162 |
| --- PERFORM @[email protected]_capture(); |
163 |
| - |
164 |
| --- p_total := 0; |
165 |
| - |
166 |
| --- /* Format LIMIT clause if needed */ |
167 |
| --- IF NOT p_limit IS NULL THEN |
168 |
| --- v_limit_clause := format('LIMIT %s', p_limit); |
169 |
| --- END IF; |
170 |
| - |
171 |
| --- /* Format WHERE clause if needed */ |
172 |
| --- IF NOT p_min IS NULL THEN |
173 |
| --- v_where_clause := format('%1$s >= $1', v_attr); |
174 |
| --- END IF; |
175 |
| - |
176 |
| --- IF NOT p_max IS NULL THEN |
177 |
| --- IF NOT p_min IS NULL THEN |
178 |
| --- v_where_clause := v_where_clause || ' AND '; |
179 |
| --- END IF; |
180 |
| --- v_where_clause := v_where_clause || format('%1$s < $2', v_attr); |
181 |
| --- END IF; |
182 |
| - |
183 |
| --- IF v_where_clause != '' THEN |
184 |
| --- v_where_clause := 'WHERE ' || v_where_clause; |
185 |
| --- END IF; |
186 |
| - |
187 |
| --- /* Lock rows and copy data */ |
188 |
| --- RAISE NOTICE 'Copying data to partitions...'; |
189 |
| --- EXECUTE format(' |
190 |
| --- WITH data AS ( |
191 |
| --- DELETE FROM ONLY %1$s WHERE ctid IN ( |
192 |
| --- SELECT ctid FROM ONLY %1$s %2$s %3$s FOR UPDATE NOWAIT |
193 |
| --- ) RETURNING *) |
194 |
| --- INSERT INTO %1$s SELECT * FROM data' |
195 |
| --- , p_relation, v_where_clause, v_limit_clause) |
196 |
| --- USING p_min, p_max; |
197 |
| - |
198 |
| --- GET DIAGNOSTICS p_total = ROW_COUNT; |
199 |
| --- RETURN; |
200 |
| --- END |
201 |
| --- $$ |
202 |
| --- LANGUAGE plpgsql; |
203 |
| - |
204 | 143 | /*
|
205 | 144 | * Copy rows to partitions
|
206 | 145 | */
|
207 | 146 | CREATE OR REPLACE FUNCTION @ [email protected]_data(
|
208 |
| - parent_relid REGCLASS, |
209 |
| - OUT p_total BIGINT) |
| 147 | + p_relation regclass |
| 148 | + , p_min ANYELEMENT DEFAULT NULL::text |
| 149 | + , p_max ANYELEMENT DEFAULT NULL::text |
| 150 | + , p_limit INT DEFAULT NULL |
| 151 | + , OUT p_total BIGINT) |
210 | 152 | AS
|
211 | 153 | $$
|
212 | 154 | DECLARE
|
213 |
| - relname TEXT; |
214 |
| - rec RECORD; |
215 |
| - cnt BIGINT := 0; |
216 |
| - |
| 155 | + v_attr TEXT; |
| 156 | + v_limit_clause TEXT := ''; |
| 157 | + v_where_clause TEXT := ''; |
217 | 158 | BEGIN
|
218 |
| - p_total := 0; |
219 |
| - |
220 |
| - /* Create partitions and copy rest of the data */ |
221 |
| - EXECUTE format('WITH part_data AS (DELETE FROM ONLY %1$s RETURNING *) |
222 |
| - INSERT INTO %1$s SELECT * FROM part_data', |
223 |
| - @ [email protected]_schema_qualified_name(parent_relid)); |
224 |
| - |
225 |
| - /* Get number of inserted rows */ |
226 |
| - GET DIAGNOSTICS p_total = ROW_COUNT; |
227 |
| - RETURN; |
| 159 | + SELECT attname INTO v_attr |
| 160 | + FROM @ [email protected]_config WHERE partrel = p_relation; |
| 161 | + |
| 162 | + PERFORM @ [email protected]_capture(); |
| 163 | + |
| 164 | + p_total := 0; |
| 165 | + |
| 166 | + /* Format LIMIT clause if needed */ |
| 167 | + IF NOT p_limit IS NULL THEN |
| 168 | + v_limit_clause := format('LIMIT %s', p_limit); |
| 169 | + END IF; |
| 170 | + |
| 171 | + /* Format WHERE clause if needed */ |
| 172 | + IF NOT p_min IS NULL THEN |
| 173 | + v_where_clause := format('%1$s >= $1', v_attr); |
| 174 | + END IF; |
| 175 | + |
| 176 | + IF NOT p_max IS NULL THEN |
| 177 | + IF NOT p_min IS NULL THEN |
| 178 | + v_where_clause := v_where_clause || ' AND '; |
| 179 | + END IF; |
| 180 | + v_where_clause := v_where_clause || format('%1$s < $2', v_attr); |
| 181 | + END IF; |
| 182 | + |
| 183 | + IF v_where_clause != '' THEN |
| 184 | + v_where_clause := 'WHERE ' || v_where_clause; |
| 185 | + END IF; |
| 186 | + |
| 187 | + /* Lock rows and copy data */ |
| 188 | + RAISE NOTICE 'Copying data to partitions...'; |
| 189 | + EXECUTE format(' |
| 190 | + WITH data AS ( |
| 191 | + DELETE FROM ONLY %1$s WHERE ctid IN ( |
| 192 | + SELECT ctid FROM ONLY %1$s %2$s %3$s FOR UPDATE NOWAIT |
| 193 | + ) RETURNING *) |
| 194 | + INSERT INTO %1$s SELECT * FROM data' |
| 195 | + , p_relation, v_where_clause, v_limit_clause) |
| 196 | + USING p_min, p_max; |
| 197 | + |
| 198 | + GET DIAGNOSTICS p_total = ROW_COUNT; |
| 199 | + RETURN; |
228 | 200 | END
|
229 | 201 | $$
|
230 | 202 | LANGUAGE plpgsql;
|
231 | 203 |
|
| 204 | +/* |
| 205 | + * Copy rows to partitions |
| 206 | + */ |
| 207 | +-- CREATE OR REPLACE FUNCTION @[email protected]_data( |
| 208 | +-- parent_relid REGCLASS, |
| 209 | +-- OUT p_total BIGINT) |
| 210 | +-- AS |
| 211 | +-- $$ |
| 212 | +-- DECLARE |
| 213 | +-- relname TEXT; |
| 214 | +-- rec RECORD; |
| 215 | +-- cnt BIGINT := 0; |
| 216 | + |
| 217 | +-- BEGIN |
| 218 | +-- p_total := 0; |
| 219 | + |
| 220 | +-- /* Create partitions and copy rest of the data */ |
| 221 | +-- EXECUTE format('WITH part_data AS (DELETE FROM ONLY %1$s RETURNING *) |
| 222 | +-- INSERT INTO %1$s SELECT * FROM part_data', |
| 223 | +-- @[email protected]_schema_qualified_name(parent_relid)); |
| 224 | + |
| 225 | +-- /* Get number of inserted rows */ |
| 226 | +-- GET DIAGNOSTICS p_total = ROW_COUNT; |
| 227 | +-- RETURN; |
| 228 | +-- END |
| 229 | +-- $$ |
| 230 | +-- LANGUAGE plpgsql; |
| 231 | + |
232 | 232 | /*
|
233 | 233 | * Disable pathman partitioning for specified relation
|
234 | 234 | */
|
|
0 commit comments