Remove custom Constraint node read/write implementations

This is part of an effort to reduce the number of special cases in the
automatically generated node support functions.

Allegedly, only certain fields of the Constraint node are valid based
on contype.  But this has historically not been kept up to date in the
read/write functions.  The Constraint node is only used for debugging
DDL statements, so there are no strong requirements for its output,
and there is no enforcement for its correctness.  (There was no read
support before a6bc3301925.)  Commits e7a552f303 and abf46ad9c7 are
examples of where omissions were fixed.

This patch just removes the custom read/write implementations for the
Constraint node type.  Now we just output all the fields, which is a
bit more than before, but at least we don't have to maintain these
functions anymore.  Also, we lose the string representation of the
contype field, but for this marginal use case that seems tolerable.
This patch also changes the documentation of the Constraint struct to
put less emphasis on grouping fields by constraint type but rather
document for each field how it's used.

Reviewed-by: Paul Jungwirth <pj@illuminatedcomputing.com>
Discussion: https://www.postgresql.org/message-id/flat/4b27fc50-8cd6-46f5-ab20-88dbaadca645@eisentraut.org
This commit is contained in:
Peter Eisentraut 2024-02-22 07:07:12 +01:00
parent 801792e528
commit fbc93b8b5f
3 changed files with 13 additions and 298 deletions

View File

@ -704,134 +704,6 @@ _outA_Const(StringInfo str, const A_Const *node)
WRITE_LOCATION_FIELD(location);
}
static void
_outConstraint(StringInfo str, const Constraint *node)
{
WRITE_NODE_TYPE("CONSTRAINT");
WRITE_STRING_FIELD(conname);
WRITE_BOOL_FIELD(deferrable);
WRITE_BOOL_FIELD(initdeferred);
WRITE_LOCATION_FIELD(location);
appendStringInfoString(str, " :contype ");
switch (node->contype)
{
case CONSTR_NULL:
appendStringInfoString(str, "NULL");
break;
case CONSTR_NOTNULL:
appendStringInfoString(str, "NOT_NULL");
WRITE_NODE_FIELD(keys);
WRITE_INT_FIELD(inhcount);
WRITE_BOOL_FIELD(is_no_inherit);
WRITE_BOOL_FIELD(skip_validation);
WRITE_BOOL_FIELD(initially_valid);
break;
case CONSTR_DEFAULT:
appendStringInfoString(str, "DEFAULT");
WRITE_NODE_FIELD(raw_expr);
WRITE_STRING_FIELD(cooked_expr);
break;
case CONSTR_IDENTITY:
appendStringInfoString(str, "IDENTITY");
WRITE_NODE_FIELD(options);
WRITE_CHAR_FIELD(generated_when);
break;
case CONSTR_GENERATED:
appendStringInfoString(str, "GENERATED");
WRITE_NODE_FIELD(raw_expr);
WRITE_STRING_FIELD(cooked_expr);
WRITE_CHAR_FIELD(generated_when);
break;
case CONSTR_CHECK:
appendStringInfoString(str, "CHECK");
WRITE_BOOL_FIELD(is_no_inherit);
WRITE_NODE_FIELD(raw_expr);
WRITE_STRING_FIELD(cooked_expr);
WRITE_BOOL_FIELD(skip_validation);
WRITE_BOOL_FIELD(initially_valid);
break;
case CONSTR_PRIMARY:
appendStringInfoString(str, "PRIMARY_KEY");
WRITE_NODE_FIELD(keys);
WRITE_BOOL_FIELD(without_overlaps);
WRITE_NODE_FIELD(including);
WRITE_NODE_FIELD(options);
WRITE_STRING_FIELD(indexname);
WRITE_STRING_FIELD(indexspace);
WRITE_BOOL_FIELD(reset_default_tblspc);
/* access_method and where_clause not currently used */
break;
case CONSTR_UNIQUE:
appendStringInfoString(str, "UNIQUE");
WRITE_BOOL_FIELD(nulls_not_distinct);
WRITE_NODE_FIELD(keys);
WRITE_BOOL_FIELD(without_overlaps);
WRITE_NODE_FIELD(including);
WRITE_NODE_FIELD(options);
WRITE_STRING_FIELD(indexname);
WRITE_STRING_FIELD(indexspace);
WRITE_BOOL_FIELD(reset_default_tblspc);
/* access_method and where_clause not currently used */
break;
case CONSTR_EXCLUSION:
appendStringInfoString(str, "EXCLUSION");
WRITE_NODE_FIELD(exclusions);
WRITE_NODE_FIELD(including);
WRITE_NODE_FIELD(options);
WRITE_STRING_FIELD(indexname);
WRITE_STRING_FIELD(indexspace);
WRITE_BOOL_FIELD(reset_default_tblspc);
WRITE_STRING_FIELD(access_method);
WRITE_NODE_FIELD(where_clause);
break;
case CONSTR_FOREIGN:
appendStringInfoString(str, "FOREIGN_KEY");
WRITE_NODE_FIELD(pktable);
WRITE_NODE_FIELD(fk_attrs);
WRITE_NODE_FIELD(pk_attrs);
WRITE_CHAR_FIELD(fk_matchtype);
WRITE_CHAR_FIELD(fk_upd_action);
WRITE_CHAR_FIELD(fk_del_action);
WRITE_NODE_FIELD(fk_del_set_cols);
WRITE_NODE_FIELD(old_conpfeqop);
WRITE_OID_FIELD(old_pktable_oid);
WRITE_BOOL_FIELD(skip_validation);
WRITE_BOOL_FIELD(initially_valid);
break;
case CONSTR_ATTR_DEFERRABLE:
appendStringInfoString(str, "ATTR_DEFERRABLE");
break;
case CONSTR_ATTR_NOT_DEFERRABLE:
appendStringInfoString(str, "ATTR_NOT_DEFERRABLE");
break;
case CONSTR_ATTR_DEFERRED:
appendStringInfoString(str, "ATTR_DEFERRED");
break;
case CONSTR_ATTR_IMMEDIATE:
appendStringInfoString(str, "ATTR_IMMEDIATE");
break;
default:
elog(ERROR, "unrecognized ConstrType: %d", (int) node->contype);
break;
}
}
/*
* outNode -

View File

@ -343,151 +343,6 @@ _readA_Const(void)
READ_DONE();
}
/*
* _readConstraint
*/
static Constraint *
_readConstraint(void)
{
READ_LOCALS(Constraint);
READ_STRING_FIELD(conname);
READ_BOOL_FIELD(deferrable);
READ_BOOL_FIELD(initdeferred);
READ_LOCATION_FIELD(location);
token = pg_strtok(&length); /* skip :contype */
token = pg_strtok(&length); /* get field value */
if (length == 4 && strncmp(token, "NULL", 4) == 0)
local_node->contype = CONSTR_NULL;
else if (length == 8 && strncmp(token, "NOT_NULL", 8) == 0)
local_node->contype = CONSTR_NOTNULL;
else if (length == 7 && strncmp(token, "DEFAULT", 7) == 0)
local_node->contype = CONSTR_DEFAULT;
else if (length == 8 && strncmp(token, "IDENTITY", 8) == 0)
local_node->contype = CONSTR_IDENTITY;
else if (length == 9 && strncmp(token, "GENERATED", 9) == 0)
local_node->contype = CONSTR_GENERATED;
else if (length == 5 && strncmp(token, "CHECK", 5) == 0)
local_node->contype = CONSTR_CHECK;
else if (length == 11 && strncmp(token, "PRIMARY_KEY", 11) == 0)
local_node->contype = CONSTR_PRIMARY;
else if (length == 6 && strncmp(token, "UNIQUE", 6) == 0)
local_node->contype = CONSTR_UNIQUE;
else if (length == 9 && strncmp(token, "EXCLUSION", 9) == 0)
local_node->contype = CONSTR_EXCLUSION;
else if (length == 11 && strncmp(token, "FOREIGN_KEY", 11) == 0)
local_node->contype = CONSTR_FOREIGN;
else if (length == 15 && strncmp(token, "ATTR_DEFERRABLE", 15) == 0)
local_node->contype = CONSTR_ATTR_DEFERRABLE;
else if (length == 19 && strncmp(token, "ATTR_NOT_DEFERRABLE", 19) == 0)
local_node->contype = CONSTR_ATTR_NOT_DEFERRABLE;
else if (length == 13 && strncmp(token, "ATTR_DEFERRED", 13) == 0)
local_node->contype = CONSTR_ATTR_DEFERRED;
else if (length == 14 && strncmp(token, "ATTR_IMMEDIATE", 14) == 0)
local_node->contype = CONSTR_ATTR_IMMEDIATE;
switch (local_node->contype)
{
case CONSTR_NULL:
/* no extra fields */
break;
case CONSTR_NOTNULL:
READ_NODE_FIELD(keys);
READ_INT_FIELD(inhcount);
READ_BOOL_FIELD(is_no_inherit);
READ_BOOL_FIELD(skip_validation);
READ_BOOL_FIELD(initially_valid);
break;
case CONSTR_DEFAULT:
READ_NODE_FIELD(raw_expr);
READ_STRING_FIELD(cooked_expr);
break;
case CONSTR_IDENTITY:
READ_NODE_FIELD(options);
READ_CHAR_FIELD(generated_when);
break;
case CONSTR_GENERATED:
READ_NODE_FIELD(raw_expr);
READ_STRING_FIELD(cooked_expr);
READ_CHAR_FIELD(generated_when);
break;
case CONSTR_CHECK:
READ_BOOL_FIELD(is_no_inherit);
READ_NODE_FIELD(raw_expr);
READ_STRING_FIELD(cooked_expr);
READ_BOOL_FIELD(skip_validation);
READ_BOOL_FIELD(initially_valid);
break;
case CONSTR_PRIMARY:
READ_NODE_FIELD(keys);
READ_BOOL_FIELD(without_overlaps);
READ_NODE_FIELD(including);
READ_NODE_FIELD(options);
READ_STRING_FIELD(indexname);
READ_STRING_FIELD(indexspace);
READ_BOOL_FIELD(reset_default_tblspc);
/* access_method and where_clause not currently used */
break;
case CONSTR_UNIQUE:
READ_BOOL_FIELD(nulls_not_distinct);
READ_NODE_FIELD(keys);
READ_BOOL_FIELD(without_overlaps);
READ_NODE_FIELD(including);
READ_NODE_FIELD(options);
READ_STRING_FIELD(indexname);
READ_STRING_FIELD(indexspace);
READ_BOOL_FIELD(reset_default_tblspc);
/* access_method and where_clause not currently used */
break;
case CONSTR_EXCLUSION:
READ_NODE_FIELD(exclusions);
READ_NODE_FIELD(including);
READ_NODE_FIELD(options);
READ_STRING_FIELD(indexname);
READ_STRING_FIELD(indexspace);
READ_BOOL_FIELD(reset_default_tblspc);
READ_STRING_FIELD(access_method);
READ_NODE_FIELD(where_clause);
break;
case CONSTR_FOREIGN:
READ_NODE_FIELD(pktable);
READ_NODE_FIELD(fk_attrs);
READ_NODE_FIELD(pk_attrs);
READ_CHAR_FIELD(fk_matchtype);
READ_CHAR_FIELD(fk_upd_action);
READ_CHAR_FIELD(fk_del_action);
READ_NODE_FIELD(fk_del_set_cols);
READ_NODE_FIELD(old_conpfeqop);
READ_OID_FIELD(old_pktable_oid);
READ_BOOL_FIELD(skip_validation);
READ_BOOL_FIELD(initially_valid);
break;
case CONSTR_ATTR_DEFERRABLE:
case CONSTR_ATTR_NOT_DEFERRABLE:
case CONSTR_ATTR_DEFERRED:
case CONSTR_ATTR_IMMEDIATE:
/* no extra fields */
break;
default:
elog(ERROR, "unrecognized ConstrType: %d", (int) local_node->contype);
break;
}
READ_DONE();
}
static RangeTblEntry *
_readRangeTblEntry(void)
{

View File

@ -2566,44 +2566,34 @@ typedef enum ConstrType /* types of constraints */
typedef struct Constraint
{
pg_node_attr(custom_read_write)
NodeTag type;
ConstrType contype; /* see above */
/* Fields used for most/all constraint types: */
char *conname; /* Constraint name, or NULL if unnamed */
bool deferrable; /* DEFERRABLE? */
bool initdeferred; /* INITIALLY DEFERRED? */
int location; /* token location, or -1 if unknown */
/* Fields used for constraints with expressions (CHECK and DEFAULT): */
bool skip_validation; /* skip validation of existing rows? */
bool initially_valid; /* mark the new constraint as valid? */
bool is_no_inherit; /* is constraint non-inheritable? */
Node *raw_expr; /* expr, as untransformed parse tree */
char *cooked_expr; /* expr, as nodeToString representation */
Node *raw_expr; /* CHECK or DEFAULT expression, as
* untransformed parse tree */
char *cooked_expr; /* CHECK or DEFAULT expression, as
* nodeToString representation */
char generated_when; /* ALWAYS or BY DEFAULT */
/* Fields used for "raw" NOT NULL constraints: */
int inhcount; /* initial inheritance count to apply */
/* Fields used for unique constraints (UNIQUE and PRIMARY KEY): */
int inhcount; /* initial inheritance count to apply, for
* "raw" NOT NULL constraints */
bool nulls_not_distinct; /* null treatment for UNIQUE constraints */
List *keys; /* String nodes naming referenced key
* column(s); also used for NOT NULL */
* column(s); for UNIQUE/PK/NOT NULL */
bool without_overlaps; /* WITHOUT OVERLAPS specified */
List *including; /* String nodes naming referenced nonkey
* column(s) */
/* Fields used for EXCLUSION constraints: */
List *exclusions; /* list of (IndexElem, operator name) pairs */
/* Fields used for index constraints (UNIQUE, PRIMARY KEY, EXCLUSION): */
* column(s); for UNIQUE/PK */
List *exclusions; /* list of (IndexElem, operator name) pairs;
* for exclusion constraints */
List *options; /* options from WITH clause */
char *indexname; /* existing index to use; otherwise NULL */
char *indexspace; /* index tablespace; NULL for default */
bool reset_default_tblspc; /* reset default_tablespace prior to
* creating the index */
/* These could be, but currently are not, used for UNIQUE/PKEY: */
char *access_method; /* index access method; NULL for default */
Node *where_clause; /* partial index predicate */
@ -2619,9 +2609,7 @@ typedef struct Constraint
Oid old_pktable_oid; /* pg_constraint.confrelid of my former
* self */
/* Fields used for constraints that allow a NOT VALID specification */
bool skip_validation; /* skip validation of existing rows? */
bool initially_valid; /* mark the new constraint as valid? */
int location; /* token location, or -1 if unknown */
} Constraint;
/* ----------------------