mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Introduce macros for typalign and typstorage constants.
Our usual practice for "poor man's enum" catalog columns is to define macros for the possible values and use those, not literal constants, in C code. But for some reason lost in the mists of time, this was never done for typalign/attalign or typstorage/attstorage. It's never too late to make it better though, so let's do that. The reason I got interested in this right now is the need to duplicate some uses of the TYPSTORAGE constants in an upcoming ALTER TYPE patch. But in general, this sort of change aids greppability and readability, so it's a good idea even without any specific motivation. I may have missed a few places that could be converted, and it's even more likely that pending patches will re-introduce some hard-coded references. But that's not fatal --- there's no expectation that we'd actually change any of these values. We can clean up stragglers over time. Discussion: https://postgr.es/m/16457.1583189537@sss.pgh.pa.us
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
* that have been put into a tuple but never sent to disk. Hopefully there
|
||||
* are few such places.
|
||||
*
|
||||
* Varlenas still have alignment 'i' (or 'd') in pg_type/pg_attribute, since
|
||||
* Varlenas still have alignment INT (or DOUBLE) in pg_type/pg_attribute, since
|
||||
* that's the normal requirement for the untoasted format. But we ignore that
|
||||
* for the 1-byte-header format. This means that the actual start position
|
||||
* of a varlena datum may vary depending on which format it has. To determine
|
||||
@@ -39,7 +39,7 @@
|
||||
* catalog, this is now risky: it's only safe if the preceding field is
|
||||
* word-aligned, so that there will never be any padding.
|
||||
*
|
||||
* We don't pack varlenas whose attstorage is 'p', since the data type
|
||||
* We don't pack varlenas whose attstorage is PLAIN, since the data type
|
||||
* isn't expecting to have to detoast values. This is used in particular
|
||||
* by oidvector and int2vector, which are used in the system catalogs
|
||||
* and we'd like to still refer to them via C struct offsets.
|
||||
@@ -66,10 +66,10 @@
|
||||
|
||||
/* Does att's datatype allow packing into the 1-byte-header varlena format? */
|
||||
#define ATT_IS_PACKABLE(att) \
|
||||
((att)->attlen == -1 && (att)->attstorage != 'p')
|
||||
((att)->attlen == -1 && (att)->attstorage != TYPSTORAGE_PLAIN)
|
||||
/* Use this if it's already known varlena */
|
||||
#define VARLENA_ATT_IS_PACKABLE(att) \
|
||||
((att)->attstorage != 'p')
|
||||
((att)->attstorage != TYPSTORAGE_PLAIN)
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
@@ -274,7 +274,7 @@ fill_val(Form_pg_attribute att,
|
||||
{
|
||||
/* cstring ... never needs alignment */
|
||||
*infomask |= HEAP_HASVARWIDTH;
|
||||
Assert(att->attalign == 'c');
|
||||
Assert(att->attalign == TYPALIGN_CHAR);
|
||||
data_length = strlen(DatumGetCString(datum)) + 1;
|
||||
memcpy(data, DatumGetPointer(datum), data_length);
|
||||
}
|
||||
|
||||
@@ -100,7 +100,8 @@ index_form_tuple(TupleDesc tupleDescriptor,
|
||||
*/
|
||||
if (!VARATT_IS_EXTENDED(DatumGetPointer(untoasted_values[i])) &&
|
||||
VARSIZE(DatumGetPointer(untoasted_values[i])) > TOAST_INDEX_TARGET &&
|
||||
(att->attstorage == 'x' || att->attstorage == 'm'))
|
||||
(att->attstorage == TYPSTORAGE_EXTENDED ||
|
||||
att->attstorage == TYPSTORAGE_MAIN))
|
||||
{
|
||||
Datum cvalue = toast_compress_datum(untoasted_values[i]);
|
||||
|
||||
|
||||
@@ -892,7 +892,7 @@ transformRelOptions(Datum oldOptions, List *defList, const char *namspace,
|
||||
int noldoptions;
|
||||
int i;
|
||||
|
||||
deconstruct_array(array, TEXTOID, -1, false, 'i',
|
||||
deconstruct_array(array, TEXTOID, -1, false, TYPALIGN_INT,
|
||||
&oldoptions, NULL, &noldoptions);
|
||||
|
||||
for (i = 0; i < noldoptions; i++)
|
||||
@@ -1060,7 +1060,7 @@ untransformRelOptions(Datum options)
|
||||
|
||||
array = DatumGetArrayTypeP(options);
|
||||
|
||||
deconstruct_array(array, TEXTOID, -1, false, 'i',
|
||||
deconstruct_array(array, TEXTOID, -1, false, TYPALIGN_INT,
|
||||
&optiondatums, NULL, &noptions);
|
||||
|
||||
for (i = 0; i < noptions; i++)
|
||||
@@ -1201,7 +1201,7 @@ parseRelOptions(Datum options, bool validate, relopt_kind kind,
|
||||
Datum *optiondatums;
|
||||
int noptions;
|
||||
|
||||
deconstruct_array(array, TEXTOID, -1, false, 'i',
|
||||
deconstruct_array(array, TEXTOID, -1, false, TYPALIGN_INT,
|
||||
&optiondatums, NULL, &noptions);
|
||||
|
||||
for (i = 0; i < noptions; i++)
|
||||
|
||||
@@ -725,32 +725,32 @@ TupleDescInitBuiltinEntry(TupleDesc desc,
|
||||
case TEXTARRAYOID:
|
||||
att->attlen = -1;
|
||||
att->attbyval = false;
|
||||
att->attalign = 'i';
|
||||
att->attstorage = 'x';
|
||||
att->attalign = TYPALIGN_INT;
|
||||
att->attstorage = TYPSTORAGE_EXTENDED;
|
||||
att->attcollation = DEFAULT_COLLATION_OID;
|
||||
break;
|
||||
|
||||
case BOOLOID:
|
||||
att->attlen = 1;
|
||||
att->attbyval = true;
|
||||
att->attalign = 'c';
|
||||
att->attstorage = 'p';
|
||||
att->attalign = TYPALIGN_CHAR;
|
||||
att->attstorage = TYPSTORAGE_PLAIN;
|
||||
att->attcollation = InvalidOid;
|
||||
break;
|
||||
|
||||
case INT4OID:
|
||||
att->attlen = 4;
|
||||
att->attbyval = true;
|
||||
att->attalign = 'i';
|
||||
att->attstorage = 'p';
|
||||
att->attalign = TYPALIGN_INT;
|
||||
att->attstorage = TYPSTORAGE_PLAIN;
|
||||
att->attcollation = InvalidOid;
|
||||
break;
|
||||
|
||||
case INT8OID:
|
||||
att->attlen = 8;
|
||||
att->attbyval = FLOAT8PASSBYVAL;
|
||||
att->attalign = 'd';
|
||||
att->attstorage = 'p';
|
||||
att->attalign = TYPALIGN_DOUBLE;
|
||||
att->attstorage = TYPSTORAGE_PLAIN;
|
||||
att->attcollation = InvalidOid;
|
||||
break;
|
||||
|
||||
|
||||
@@ -2023,7 +2023,7 @@ heapam_relation_needs_toast_table(Relation rel)
|
||||
maxlength_unknown = true;
|
||||
else
|
||||
data_length += maxlen;
|
||||
if (att->attstorage != 'p')
|
||||
if (att->attstorage != TYPSTORAGE_PLAIN)
|
||||
has_toastable_attrs = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,11 +159,12 @@ heap_toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup,
|
||||
/* ----------
|
||||
* Compress and/or save external until data fits into target length
|
||||
*
|
||||
* 1: Inline compress attributes with attstorage 'x', and store very
|
||||
* large attributes with attstorage 'x' or 'e' external immediately
|
||||
* 2: Store attributes with attstorage 'x' or 'e' external
|
||||
* 3: Inline compress attributes with attstorage 'm'
|
||||
* 4: Store attributes with attstorage 'm' external
|
||||
* 1: Inline compress attributes with attstorage EXTENDED, and store very
|
||||
* large attributes with attstorage EXTENDED or EXTERNAL external
|
||||
* immediately
|
||||
* 2: Store attributes with attstorage EXTENDED or EXTERNAL external
|
||||
* 3: Inline compress attributes with attstorage MAIN
|
||||
* 4: Store attributes with attstorage MAIN external
|
||||
* ----------
|
||||
*/
|
||||
|
||||
@@ -176,8 +177,9 @@ heap_toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup,
|
||||
maxDataLen = RelationGetToastTupleTarget(rel, TOAST_TUPLE_TARGET) - hoff;
|
||||
|
||||
/*
|
||||
* Look for attributes with attstorage 'x' to compress. Also find large
|
||||
* attributes with attstorage 'x' or 'e', and store them external.
|
||||
* Look for attributes with attstorage EXTENDED to compress. Also find
|
||||
* large attributes with attstorage EXTENDED or EXTERNAL, and store them
|
||||
* external.
|
||||
*/
|
||||
while (heap_compute_data_size(tupleDesc,
|
||||
toast_values, toast_isnull) > maxDataLen)
|
||||
@@ -189,13 +191,16 @@ heap_toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup,
|
||||
break;
|
||||
|
||||
/*
|
||||
* Attempt to compress it inline, if it has attstorage 'x'
|
||||
* Attempt to compress it inline, if it has attstorage EXTENDED
|
||||
*/
|
||||
if (TupleDescAttr(tupleDesc, biggest_attno)->attstorage == 'x')
|
||||
if (TupleDescAttr(tupleDesc, biggest_attno)->attstorage == TYPSTORAGE_EXTENDED)
|
||||
toast_tuple_try_compression(&ttc, biggest_attno);
|
||||
else
|
||||
{
|
||||
/* has attstorage 'e', ignore on subsequent compression passes */
|
||||
/*
|
||||
* has attstorage EXTERNAL, ignore on subsequent compression
|
||||
* passes
|
||||
*/
|
||||
toast_attr[biggest_attno].tai_colflags |= TOASTCOL_INCOMPRESSIBLE;
|
||||
}
|
||||
|
||||
@@ -213,9 +218,9 @@ heap_toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup,
|
||||
}
|
||||
|
||||
/*
|
||||
* Second we look for attributes of attstorage 'x' or 'e' that are still
|
||||
* inline, and make them external. But skip this if there's no toast
|
||||
* table to push them to.
|
||||
* Second we look for attributes of attstorage EXTENDED or EXTERNAL that
|
||||
* are still inline, and make them external. But skip this if there's no
|
||||
* toast table to push them to.
|
||||
*/
|
||||
while (heap_compute_data_size(tupleDesc,
|
||||
toast_values, toast_isnull) > maxDataLen &&
|
||||
@@ -230,7 +235,7 @@ heap_toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup,
|
||||
}
|
||||
|
||||
/*
|
||||
* Round 3 - this time we take attributes with storage 'm' into
|
||||
* Round 3 - this time we take attributes with storage MAIN into
|
||||
* compression
|
||||
*/
|
||||
while (heap_compute_data_size(tupleDesc,
|
||||
@@ -246,8 +251,8 @@ heap_toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup,
|
||||
}
|
||||
|
||||
/*
|
||||
* Finally we store attributes of type 'm' externally. At this point we
|
||||
* increase the target tuple size, so that 'm' attributes aren't stored
|
||||
* Finally we store attributes of type MAIN externally. At this point we
|
||||
* increase the target tuple size, so that MAIN attributes aren't stored
|
||||
* externally unless really necessary.
|
||||
*/
|
||||
maxDataLen = TOAST_TUPLE_TARGET_MAIN - hoff;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* Copyright (c) 2000-2020, PostgreSQL Global Development Group
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* src/backend/access/common/toast_helper.c
|
||||
* src/backend/access/table/toast_helper.c
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -18,6 +18,8 @@
|
||||
#include "access/table.h"
|
||||
#include "access/toast_helper.h"
|
||||
#include "access/toast_internals.h"
|
||||
#include "catalog/pg_type_d.h"
|
||||
|
||||
|
||||
/*
|
||||
* Prepare to TOAST a tuple.
|
||||
@@ -120,7 +122,7 @@ toast_tuple_init(ToastTupleContext *ttc)
|
||||
/*
|
||||
* If the table's attribute says PLAIN always, force it so.
|
||||
*/
|
||||
if (att->attstorage == 'p')
|
||||
if (att->attstorage == TYPSTORAGE_PLAIN)
|
||||
ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
|
||||
|
||||
/*
|
||||
@@ -134,7 +136,7 @@ toast_tuple_init(ToastTupleContext *ttc)
|
||||
if (VARATT_IS_EXTERNAL(new_value))
|
||||
{
|
||||
ttc->ttc_attr[i].tai_oldexternal = new_value;
|
||||
if (att->attstorage == 'p')
|
||||
if (att->attstorage == TYPSTORAGE_PLAIN)
|
||||
new_value = detoast_attr(new_value);
|
||||
else
|
||||
new_value = detoast_external_attr(new_value);
|
||||
@@ -165,8 +167,8 @@ toast_tuple_init(ToastTupleContext *ttc)
|
||||
* for_compression flag is passed as true, it must also not be marked
|
||||
* TOASTCOL_INCOMPRESSIBLE.
|
||||
*
|
||||
* The column must have attstorage 'e' or 'x' if check_main is false, and
|
||||
* must have attstorage 'm' if check_main is true.
|
||||
* The column must have attstorage EXTERNAL or EXTENDED if check_main is
|
||||
* false, and must have attstorage MAIN if check_main is true.
|
||||
*
|
||||
* The column must have a minimum size of MAXALIGN(TOAST_POINTER_SIZE);
|
||||
* if not, no benefit is to be expected by compressing it.
|
||||
@@ -195,13 +197,14 @@ toast_tuple_find_biggest_attribute(ToastTupleContext *ttc,
|
||||
if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
|
||||
continue;
|
||||
if (VARATT_IS_EXTERNAL(DatumGetPointer(ttc->ttc_values[i])))
|
||||
continue; /* can't happen, toast_action would be 'p' */
|
||||
continue; /* can't happen, toast_action would be PLAIN */
|
||||
if (for_compression &&
|
||||
VARATT_IS_COMPRESSED(DatumGetPointer(ttc->ttc_values[i])))
|
||||
continue;
|
||||
if (check_main && att->attstorage != 'm')
|
||||
if (check_main && att->attstorage != TYPSTORAGE_MAIN)
|
||||
continue;
|
||||
if (!check_main && att->attstorage != 'x' && att->attstorage != 'e')
|
||||
if (!check_main && att->attstorage != TYPSTORAGE_EXTENDED &&
|
||||
att->attstorage != TYPSTORAGE_EXTERNAL)
|
||||
continue;
|
||||
|
||||
if (ttc->ttc_attr[i].tai_size > biggest_size)
|
||||
|
||||
Reference in New Issue
Block a user