36
36
#include "utils/syscache.h"
37
37
#include "utils/snapmgr.h"
38
38
39
+ /* Set by pg_upgrade_support functions */
40
+ Oid binary_upgrade_next_attr_compression_oid = InvalidOid ;
41
+
39
42
/*
40
43
* When conditions of compression satisfies one if builtin attribute
41
44
* compresssion tuples the compressed attribute will be linked to
@@ -129,11 +132,12 @@ lookup_attribute_compression(Oid attrelid, AttrNumber attnum,
129
132
tup_amoid ;
130
133
Datum values [Natts_pg_attr_compression ];
131
134
bool nulls [Natts_pg_attr_compression ];
135
+ char * amname ;
132
136
133
137
heap_deform_tuple (tuple , RelationGetDescr (rel ), values , nulls );
134
138
acoid = DatumGetObjectId (values [Anum_pg_attr_compression_acoid - 1 ]);
135
- tup_amoid = get_am_oid (
136
- NameStr ( * DatumGetName ( values [ Anum_pg_attr_compression_acname - 1 ])) , false);
139
+ amname = NameStr ( * DatumGetName ( values [ Anum_pg_attr_compression_acname - 1 ]));
140
+ tup_amoid = get_am_oid ( amname , false);
137
141
138
142
if (previous_amoids )
139
143
* previous_amoids = list_append_unique_oid (* previous_amoids , tup_amoid );
@@ -150,17 +154,15 @@ lookup_attribute_compression(Oid attrelid, AttrNumber attnum,
150
154
if (DatumGetPointer (acoptions ) == NULL )
151
155
result = acoid ;
152
156
}
153
- else
157
+ else if ( DatumGetPointer ( acoptions ) != NULL )
154
158
{
155
159
bool equal ;
156
160
157
161
/* check if arrays for WITH options are equal */
158
162
equal = DatumGetBool (CallerFInfoFunctionCall2 (
159
- array_eq ,
160
- & arrayeq_info ,
161
- InvalidOid ,
162
- acoptions ,
163
- values [Anum_pg_attr_compression_acoptions - 1 ]));
163
+ array_eq , & arrayeq_info , InvalidOid , acoptions ,
164
+ values [Anum_pg_attr_compression_acoptions - 1 ]));
165
+
164
166
if (equal )
165
167
result = acoid ;
166
168
}
@@ -227,6 +229,16 @@ CreateAttributeCompression(Form_pg_attribute att,
227
229
/* Try to find builtin compression first */
228
230
acoid = lookup_attribute_compression (0 , 0 , amoid , arropt , NULL );
229
231
232
+ /* no rewrite by default */
233
+ if (need_rewrite != NULL )
234
+ * need_rewrite = false;
235
+
236
+ if (IsBinaryUpgrade )
237
+ {
238
+ /* Skip the rewrite checks and searching of identical compression */
239
+ goto add_tuple ;
240
+ }
241
+
230
242
/*
231
243
* attrelid will be invalid on CREATE TABLE, no need for table rewrite
232
244
* check.
@@ -252,16 +264,10 @@ CreateAttributeCompression(Form_pg_attribute att,
252
264
*/
253
265
if (need_rewrite != NULL )
254
266
{
255
- /* no rewrite by default */
256
- * need_rewrite = false;
257
-
258
267
Assert (preserved_amoids != NULL );
259
268
260
269
if (compression -> preserve == NIL )
261
- {
262
- Assert (!IsBinaryUpgrade );
263
270
* need_rewrite = true;
264
- }
265
271
else
266
272
{
267
273
ListCell * cell ;
@@ -294,7 +300,7 @@ CreateAttributeCompression(Form_pg_attribute att,
294
300
* In binary upgrade list will not be free since it contains
295
301
* Oid of builtin compression access method.
296
302
*/
297
- if (! IsBinaryUpgrade && list_length (previous_amoids ) != 0 )
303
+ if (list_length (previous_amoids ) != 0 )
298
304
* need_rewrite = true;
299
305
}
300
306
}
@@ -303,9 +309,6 @@ CreateAttributeCompression(Form_pg_attribute att,
303
309
list_free (previous_amoids );
304
310
}
305
311
306
- if (IsBinaryUpgrade && !OidIsValid (acoid ))
307
- elog (ERROR , "could not restore attribute compression data" );
308
-
309
312
/* Return Oid if we already found identical compression on this column */
310
313
if (OidIsValid (acoid ))
311
314
{
@@ -315,6 +318,7 @@ CreateAttributeCompression(Form_pg_attribute att,
315
318
return acoid ;
316
319
}
317
320
321
+ add_tuple :
318
322
/* Initialize buffers for new tuple values */
319
323
memset (values , 0 , sizeof (values ));
320
324
memset (nulls , false, sizeof (nulls ));
@@ -323,13 +327,27 @@ CreateAttributeCompression(Form_pg_attribute att,
323
327
324
328
rel = heap_open (AttrCompressionRelationId , RowExclusiveLock );
325
329
326
- acoid = GetNewOidWithIndex (rel , AttrCompressionIndexId ,
327
- Anum_pg_attr_compression_acoid );
330
+ if (IsBinaryUpgrade )
331
+ {
332
+ /* acoid should be found in some cases */
333
+ if (binary_upgrade_next_attr_compression_oid < FirstNormalObjectId &&
334
+ (!OidIsValid (acoid ) || binary_upgrade_next_attr_compression_oid != acoid ))
335
+ elog (ERROR , "could not link to built-in attribute compression" );
336
+
337
+ acoid = binary_upgrade_next_attr_compression_oid ;
338
+ }
339
+ else
340
+ {
341
+ acoid = GetNewOidWithIndex (rel , AttrCompressionIndexId ,
342
+ Anum_pg_attr_compression_acoid );
343
+
344
+ }
345
+
328
346
if (acoid < FirstNormalObjectId )
329
347
{
330
- /* this is database initialization */
348
+ /* this is built-in attribute compression */
331
349
heap_close (rel , RowExclusiveLock );
332
- return DefaultCompressionOid ;
350
+ return acoid ;
333
351
}
334
352
335
353
/* we need routine only to call cmcheck function */
@@ -390,8 +408,8 @@ RemoveAttributeCompression(Oid acoid)
390
408
/*
391
409
* CleanupAttributeCompression
392
410
*
393
- * Remove entries in pg_attr_compression except current attribute compression
394
- * and related with specified list of access methods.
411
+ * Remove entries in pg_attr_compression of the column except current
412
+ * attribute compression and related with specified list of access methods.
395
413
*/
396
414
void
397
415
CleanupAttributeCompression (Oid relid , AttrNumber attnum , List * keepAmOids )
@@ -419,9 +437,7 @@ CleanupAttributeCompression(Oid relid, AttrNumber attnum, List *keepAmOids)
419
437
ReleaseSysCache (attrtuple );
420
438
421
439
Assert (relid > 0 && attnum > 0 );
422
-
423
- if (IsBinaryUpgrade )
424
- goto builtin_removal ;
440
+ Assert (!IsBinaryUpgrade );
425
441
426
442
rel = heap_open (AttrCompressionRelationId , RowExclusiveLock );
427
443
@@ -438,7 +454,10 @@ CleanupAttributeCompression(Oid relid, AttrNumber attnum, List *keepAmOids)
438
454
scan = systable_beginscan (rel , AttrCompressionRelidAttnumIndexId ,
439
455
true, NULL , 2 , key );
440
456
441
- /* Remove attribute compression tuples and collect removed Oids to list */
457
+ /*
458
+ * Remove attribute compression tuples and collect removed Oids
459
+ * to list.
460
+ */
442
461
while (HeapTupleIsValid (tuple = systable_getnext (scan )))
443
462
{
444
463
Form_pg_attr_compression acform ;
@@ -460,7 +479,10 @@ CleanupAttributeCompression(Oid relid, AttrNumber attnum, List *keepAmOids)
460
479
systable_endscan (scan );
461
480
heap_close (rel , RowExclusiveLock );
462
481
463
- /* Now remove dependencies */
482
+ /*
483
+ * Now remove dependencies between attribute compression (dependent)
484
+ * and column.
485
+ */
464
486
rel = heap_open (DependRelationId , RowExclusiveLock );
465
487
foreach (lc , removed )
466
488
{
0 commit comments