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:
Tom Lane
2020-03-04 10:34:25 -05:00
parent 0ad6f848ee
commit 3ed2005ff5
76 changed files with 341 additions and 309 deletions

View File

@ -20,12 +20,12 @@
* Information about one column of a tuple being toasted.
*
* NOTE: toast_action[i] can have these values:
* ' ' default handling
* 'p' already processed --- don't touch it
* 'x' incompressible, but OK to move off
* ' ' default handling
* TYPSTORAGE_PLAIN already processed --- don't touch it
* TYPSTORAGE_EXTENDED incompressible, but OK to move off
*
* NOTE: toast_attr[i].tai_size is only made valid for varlena attributes with
* toast_action[i] different from 'p'.
* toast_action[i] different from TYPSTORAGE_PLAIN.
*/
typedef struct
{

View File

@ -14,6 +14,8 @@
#ifndef TUPMACS_H
#define TUPMACS_H
#include "catalog/pg_type_d.h" /* for TYPALIGN macros */
/*
* Check a tuple's null bitmap to determine whether the attribute is null.
@ -145,11 +147,11 @@
*/
#define att_align_nominal(cur_offset, attalign) \
( \
((attalign) == 'i') ? INTALIGN(cur_offset) : \
(((attalign) == 'c') ? (uintptr_t) (cur_offset) : \
(((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \
((attalign) == TYPALIGN_INT) ? INTALIGN(cur_offset) : \
(((attalign) == TYPALIGN_CHAR) ? (uintptr_t) (cur_offset) : \
(((attalign) == TYPALIGN_DOUBLE) ? DOUBLEALIGN(cur_offset) : \
( \
AssertMacro((attalign) == 's'), \
AssertMacro((attalign) == TYPALIGN_SHORT), \
SHORTALIGN(cur_offset) \
))) \
)

View File

@ -110,14 +110,7 @@ CATALOG(pg_attribute,1249,AttributeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(75,
/*----------
* attstorage tells for VARLENA attributes, what the heap access
* methods can do to it if a given tuple doesn't fit into a page.
* Possible values are
* 'p': Value must be stored plain always
* 'e': Value can be stored in "secondary" relation (if relation
* has one, see pg_class.reltoastrelid)
* 'm': Value can be stored compressed inline
* 'x': Value can be stored compressed inline or in "secondary"
* Note that 'm' fields can also be moved out to secondary storage,
* but only as a last resort ('e' and 'x' fields are moved first).
* Possible values are as for pg_type.typstorage (see TYPSTORAGE macros).
*----------
*/
char attstorage;

View File

@ -155,6 +155,7 @@ CATALOG(pg_type,1247,TypeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(71,TypeRelati
* 's' = SHORT alignment (2 bytes on most machines).
* 'i' = INT alignment (4 bytes on most machines).
* 'd' = DOUBLE alignment (8 bytes on many machines, but by no means all).
* (Use the TYPALIGN macros below for these.)
*
* See include/access/tupmacs.h for the macros that compute these
* alignment requirements. Note also that we allow the nominal alignment
@ -176,6 +177,10 @@ CATALOG(pg_type,1247,TypeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(71,TypeRelati
* 'e' EXTERNAL external storage possible, don't try to compress
* 'x' EXTENDED try to compress and store external if required
* 'm' MAIN like 'x' but try to keep in main tuple
* (Use the TYPSTORAGE macros below for these.)
*
* Note that 'm' fields can also be moved out to secondary storage,
* but only as a last resort ('e' and 'x' fields are moved first).
* ----------------
*/
char typstorage BKI_DEFAULT(p) BKI_ARRAY_DEFAULT(x);
@ -278,6 +283,16 @@ typedef FormData_pg_type *Form_pg_type;
#define TYPCATEGORY_BITSTRING 'V' /* er ... "varbit"? */
#define TYPCATEGORY_UNKNOWN 'X'
#define TYPALIGN_CHAR 'c' /* char alignment (i.e. unaligned) */
#define TYPALIGN_SHORT 's' /* short alignment (typically 2 bytes) */
#define TYPALIGN_INT 'i' /* int alignment (typically 4 bytes) */
#define TYPALIGN_DOUBLE 'd' /* double alignment (often 8 bytes) */
#define TYPSTORAGE_PLAIN 'p' /* type not prepared for toasting */
#define TYPSTORAGE_EXTERNAL 'e' /* toastable, don't try to compress */
#define TYPSTORAGE_EXTENDED 'x' /* fully toastable */
#define TYPSTORAGE_MAIN 'm' /* like 'x' but try to store inline */
/* Is a type OID a polymorphic pseudotype? (Beware of multiple evaluation) */
#define IsPolymorphicType(typid) \
((typid) == ANYELEMENTOID || \

View File

@ -186,6 +186,6 @@ extern Oid get_index_column_opclass(Oid index_oid, int attno);
/* type_is_array_domain accepts both plain arrays and domains over arrays */
#define type_is_array_domain(typid) (get_base_element_type(typid) != InvalidOid)
#define TypeIsToastable(typid) (get_typstorage(typid) != 'p')
#define TypeIsToastable(typid) (get_typstorage(typid) != TYPSTORAGE_PLAIN)
#endif /* LSYSCACHE_H */