Skip to content

Commit 608bae3

Browse files
committed
Add psql, pg_dump and pg_upgrade support
Signed-off-by: Ildus Kurbangaliev <i.kurbangaliev@gmail.com>
1 parent 7a1865c commit 608bae3

File tree

13 files changed

+440
-42
lines changed

13 files changed

+440
-42
lines changed

src/backend/commands/compressioncmds.c

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
#include "utils/syscache.h"
3737
#include "utils/snapmgr.h"
3838

39+
/* Set by pg_upgrade_support functions */
40+
Oid binary_upgrade_next_attr_compression_oid = InvalidOid;
41+
3942
/*
4043
* When conditions of compression satisfies one if builtin attribute
4144
* compresssion tuples the compressed attribute will be linked to
@@ -129,11 +132,12 @@ lookup_attribute_compression(Oid attrelid, AttrNumber attnum,
129132
tup_amoid;
130133
Datum values[Natts_pg_attr_compression];
131134
bool nulls[Natts_pg_attr_compression];
135+
char *amname;
132136

133137
heap_deform_tuple(tuple, RelationGetDescr(rel), values, nulls);
134138
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);
137141

138142
if (previous_amoids)
139143
*previous_amoids = list_append_unique_oid(*previous_amoids, tup_amoid);
@@ -150,17 +154,15 @@ lookup_attribute_compression(Oid attrelid, AttrNumber attnum,
150154
if (DatumGetPointer(acoptions) == NULL)
151155
result = acoid;
152156
}
153-
else
157+
else if (DatumGetPointer(acoptions) != NULL)
154158
{
155159
bool equal;
156160

157161
/* check if arrays for WITH options are equal */
158162
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+
164166
if (equal)
165167
result = acoid;
166168
}
@@ -227,6 +229,16 @@ CreateAttributeCompression(Form_pg_attribute att,
227229
/* Try to find builtin compression first */
228230
acoid = lookup_attribute_compression(0, 0, amoid, arropt, NULL);
229231

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+
230242
/*
231243
* attrelid will be invalid on CREATE TABLE, no need for table rewrite
232244
* check.
@@ -252,16 +264,10 @@ CreateAttributeCompression(Form_pg_attribute att,
252264
*/
253265
if (need_rewrite != NULL)
254266
{
255-
/* no rewrite by default */
256-
*need_rewrite = false;
257-
258267
Assert(preserved_amoids != NULL);
259268

260269
if (compression->preserve == NIL)
261-
{
262-
Assert(!IsBinaryUpgrade);
263270
*need_rewrite = true;
264-
}
265271
else
266272
{
267273
ListCell *cell;
@@ -294,7 +300,7 @@ CreateAttributeCompression(Form_pg_attribute att,
294300
* In binary upgrade list will not be free since it contains
295301
* Oid of builtin compression access method.
296302
*/
297-
if (!IsBinaryUpgrade && list_length(previous_amoids) != 0)
303+
if (list_length(previous_amoids) != 0)
298304
*need_rewrite = true;
299305
}
300306
}
@@ -303,9 +309,6 @@ CreateAttributeCompression(Form_pg_attribute att,
303309
list_free(previous_amoids);
304310
}
305311

306-
if (IsBinaryUpgrade && !OidIsValid(acoid))
307-
elog(ERROR, "could not restore attribute compression data");
308-
309312
/* Return Oid if we already found identical compression on this column */
310313
if (OidIsValid(acoid))
311314
{
@@ -315,6 +318,7 @@ CreateAttributeCompression(Form_pg_attribute att,
315318
return acoid;
316319
}
317320

321+
add_tuple:
318322
/* Initialize buffers for new tuple values */
319323
memset(values, 0, sizeof(values));
320324
memset(nulls, false, sizeof(nulls));
@@ -323,13 +327,27 @@ CreateAttributeCompression(Form_pg_attribute att,
323327

324328
rel = heap_open(AttrCompressionRelationId, RowExclusiveLock);
325329

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+
328346
if (acoid < FirstNormalObjectId)
329347
{
330-
/* this is database initialization */
348+
/* this is built-in attribute compression */
331349
heap_close(rel, RowExclusiveLock);
332-
return DefaultCompressionOid;
350+
return acoid;
333351
}
334352

335353
/* we need routine only to call cmcheck function */
@@ -390,8 +408,8 @@ RemoveAttributeCompression(Oid acoid)
390408
/*
391409
* CleanupAttributeCompression
392410
*
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.
395413
*/
396414
void
397415
CleanupAttributeCompression(Oid relid, AttrNumber attnum, List *keepAmOids)
@@ -419,9 +437,7 @@ CleanupAttributeCompression(Oid relid, AttrNumber attnum, List *keepAmOids)
419437
ReleaseSysCache(attrtuple);
420438

421439
Assert(relid > 0 && attnum > 0);
422-
423-
if (IsBinaryUpgrade)
424-
goto builtin_removal;
440+
Assert(!IsBinaryUpgrade);
425441

426442
rel = heap_open(AttrCompressionRelationId, RowExclusiveLock);
427443

@@ -438,7 +454,10 @@ CleanupAttributeCompression(Oid relid, AttrNumber attnum, List *keepAmOids)
438454
scan = systable_beginscan(rel, AttrCompressionRelidAttnumIndexId,
439455
true, NULL, 2, key);
440456

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+
*/
442461
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
443462
{
444463
Form_pg_attr_compression acform;
@@ -460,7 +479,10 @@ CleanupAttributeCompression(Oid relid, AttrNumber attnum, List *keepAmOids)
460479
systable_endscan(scan);
461480
heap_close(rel, RowExclusiveLock);
462481

463-
/* Now remove dependencies */
482+
/*
483+
* Now remove dependencies between attribute compression (dependent)
484+
* and column.
485+
*/
464486
rel = heap_open(DependRelationId, RowExclusiveLock);
465487
foreach(lc, removed)
466488
{

src/backend/commands/tablecmds.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -831,10 +831,10 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
831831
if (colDef->generated)
832832
attr->attgenerated = colDef->generated;
833833

834-
if (relkind == RELKIND_RELATION || relkind == RELKIND_PARTITIONED_TABLE)
834+
if (!IsBinaryUpgrade &&
835+
(relkind == RELKIND_RELATION || relkind == RELKIND_PARTITIONED_TABLE))
835836
attr->attcompression = CreateAttributeCompression(attr,
836-
colDef->compression,
837-
NULL, NULL);
837+
colDef->compression, NULL, NULL);
838838
else
839839
attr->attcompression = InvalidOid;
840840
}
@@ -14432,14 +14432,6 @@ ATExecSetCompression(AlteredTableInfo *tab,
1443214432
/* make changes visible */
1443314433
CommandCounterIncrement();
1443414434

14435-
/*
14436-
* Normally cleanup is done in rewrite but in binary upgrade we should do
14437-
* it explicitly.
14438-
*/
14439-
if (IsBinaryUpgrade)
14440-
CleanupAttributeCompression(RelationGetRelid(rel),
14441-
attnum, preserved_amoids);
14442-
1444314435
ObjectAddressSet(address, AttrCompressionRelationId, acoid);
1444414436
return address;
1444514437
}

src/backend/utils/adt/pg_upgrade_support.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ binary_upgrade_set_next_pg_authid_oid(PG_FUNCTION_ARGS)
116116
PG_RETURN_VOID();
117117
}
118118

119+
Datum
120+
binary_upgrade_set_next_attr_compression_oid(PG_FUNCTION_ARGS)
121+
{
122+
Oid acoid = PG_GETARG_OID(0);
123+
124+
CHECK_IS_BINARY_UPGRADE;
125+
binary_upgrade_next_attr_compression_oid = acoid;
126+
PG_RETURN_VOID();
127+
}
128+
119129
Datum
120130
binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS)
121131
{

src/bin/pg_dump/pg_backup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ typedef struct _restoreOptions
7878
int no_publications; /* Skip publication entries */
7979
int no_security_labels; /* Skip security label entries */
8080
int no_subscriptions; /* Skip subscription entries */
81+
int no_compression_methods; /* Skip compression methods */
8182
int strict_names;
8283

8384
const char *filename;
@@ -150,6 +151,7 @@ typedef struct _dumpOptions
150151
int no_security_labels;
151152
int no_publications;
152153
int no_subscriptions;
154+
int no_compression_methods;
153155
int no_synchronized_snapshots;
154156
int no_unlogged_table_data;
155157
int serializable_deferrable;

0 commit comments

Comments
 (0)