Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fbc93b8

Browse files
committed
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 a6bc330.) Commits e7a552f and abf46ad 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
1 parent 801792e commit fbc93b8

File tree

3 files changed

+13
-298
lines changed

3 files changed

+13
-298
lines changed

src/backend/nodes/outfuncs.c

-128
Original file line numberDiff line numberDiff line change
@@ -704,134 +704,6 @@ _outA_Const(StringInfo str, const A_Const *node)
704704
WRITE_LOCATION_FIELD(location);
705705
}
706706

707-
static void
708-
_outConstraint(StringInfo str, const Constraint *node)
709-
{
710-
WRITE_NODE_TYPE("CONSTRAINT");
711-
712-
WRITE_STRING_FIELD(conname);
713-
WRITE_BOOL_FIELD(deferrable);
714-
WRITE_BOOL_FIELD(initdeferred);
715-
WRITE_LOCATION_FIELD(location);
716-
717-
appendStringInfoString(str, " :contype ");
718-
switch (node->contype)
719-
{
720-
case CONSTR_NULL:
721-
appendStringInfoString(str, "NULL");
722-
break;
723-
724-
case CONSTR_NOTNULL:
725-
appendStringInfoString(str, "NOT_NULL");
726-
WRITE_NODE_FIELD(keys);
727-
WRITE_INT_FIELD(inhcount);
728-
WRITE_BOOL_FIELD(is_no_inherit);
729-
WRITE_BOOL_FIELD(skip_validation);
730-
WRITE_BOOL_FIELD(initially_valid);
731-
break;
732-
733-
case CONSTR_DEFAULT:
734-
appendStringInfoString(str, "DEFAULT");
735-
WRITE_NODE_FIELD(raw_expr);
736-
WRITE_STRING_FIELD(cooked_expr);
737-
break;
738-
739-
case CONSTR_IDENTITY:
740-
appendStringInfoString(str, "IDENTITY");
741-
WRITE_NODE_FIELD(options);
742-
WRITE_CHAR_FIELD(generated_when);
743-
break;
744-
745-
case CONSTR_GENERATED:
746-
appendStringInfoString(str, "GENERATED");
747-
WRITE_NODE_FIELD(raw_expr);
748-
WRITE_STRING_FIELD(cooked_expr);
749-
WRITE_CHAR_FIELD(generated_when);
750-
break;
751-
752-
case CONSTR_CHECK:
753-
appendStringInfoString(str, "CHECK");
754-
WRITE_BOOL_FIELD(is_no_inherit);
755-
WRITE_NODE_FIELD(raw_expr);
756-
WRITE_STRING_FIELD(cooked_expr);
757-
WRITE_BOOL_FIELD(skip_validation);
758-
WRITE_BOOL_FIELD(initially_valid);
759-
break;
760-
761-
case CONSTR_PRIMARY:
762-
appendStringInfoString(str, "PRIMARY_KEY");
763-
WRITE_NODE_FIELD(keys);
764-
WRITE_BOOL_FIELD(without_overlaps);
765-
WRITE_NODE_FIELD(including);
766-
WRITE_NODE_FIELD(options);
767-
WRITE_STRING_FIELD(indexname);
768-
WRITE_STRING_FIELD(indexspace);
769-
WRITE_BOOL_FIELD(reset_default_tblspc);
770-
/* access_method and where_clause not currently used */
771-
break;
772-
773-
case CONSTR_UNIQUE:
774-
appendStringInfoString(str, "UNIQUE");
775-
WRITE_BOOL_FIELD(nulls_not_distinct);
776-
WRITE_NODE_FIELD(keys);
777-
WRITE_BOOL_FIELD(without_overlaps);
778-
WRITE_NODE_FIELD(including);
779-
WRITE_NODE_FIELD(options);
780-
WRITE_STRING_FIELD(indexname);
781-
WRITE_STRING_FIELD(indexspace);
782-
WRITE_BOOL_FIELD(reset_default_tblspc);
783-
/* access_method and where_clause not currently used */
784-
break;
785-
786-
case CONSTR_EXCLUSION:
787-
appendStringInfoString(str, "EXCLUSION");
788-
WRITE_NODE_FIELD(exclusions);
789-
WRITE_NODE_FIELD(including);
790-
WRITE_NODE_FIELD(options);
791-
WRITE_STRING_FIELD(indexname);
792-
WRITE_STRING_FIELD(indexspace);
793-
WRITE_BOOL_FIELD(reset_default_tblspc);
794-
WRITE_STRING_FIELD(access_method);
795-
WRITE_NODE_FIELD(where_clause);
796-
break;
797-
798-
case CONSTR_FOREIGN:
799-
appendStringInfoString(str, "FOREIGN_KEY");
800-
WRITE_NODE_FIELD(pktable);
801-
WRITE_NODE_FIELD(fk_attrs);
802-
WRITE_NODE_FIELD(pk_attrs);
803-
WRITE_CHAR_FIELD(fk_matchtype);
804-
WRITE_CHAR_FIELD(fk_upd_action);
805-
WRITE_CHAR_FIELD(fk_del_action);
806-
WRITE_NODE_FIELD(fk_del_set_cols);
807-
WRITE_NODE_FIELD(old_conpfeqop);
808-
WRITE_OID_FIELD(old_pktable_oid);
809-
WRITE_BOOL_FIELD(skip_validation);
810-
WRITE_BOOL_FIELD(initially_valid);
811-
break;
812-
813-
case CONSTR_ATTR_DEFERRABLE:
814-
appendStringInfoString(str, "ATTR_DEFERRABLE");
815-
break;
816-
817-
case CONSTR_ATTR_NOT_DEFERRABLE:
818-
appendStringInfoString(str, "ATTR_NOT_DEFERRABLE");
819-
break;
820-
821-
case CONSTR_ATTR_DEFERRED:
822-
appendStringInfoString(str, "ATTR_DEFERRED");
823-
break;
824-
825-
case CONSTR_ATTR_IMMEDIATE:
826-
appendStringInfoString(str, "ATTR_IMMEDIATE");
827-
break;
828-
829-
default:
830-
elog(ERROR, "unrecognized ConstrType: %d", (int) node->contype);
831-
break;
832-
}
833-
}
834-
835707

836708
/*
837709
* outNode -

src/backend/nodes/readfuncs.c

-145
Original file line numberDiff line numberDiff line change
@@ -343,151 +343,6 @@ _readA_Const(void)
343343
READ_DONE();
344344
}
345345

346-
/*
347-
* _readConstraint
348-
*/
349-
static Constraint *
350-
_readConstraint(void)
351-
{
352-
READ_LOCALS(Constraint);
353-
354-
READ_STRING_FIELD(conname);
355-
READ_BOOL_FIELD(deferrable);
356-
READ_BOOL_FIELD(initdeferred);
357-
READ_LOCATION_FIELD(location);
358-
359-
token = pg_strtok(&length); /* skip :contype */
360-
token = pg_strtok(&length); /* get field value */
361-
if (length == 4 && strncmp(token, "NULL", 4) == 0)
362-
local_node->contype = CONSTR_NULL;
363-
else if (length == 8 && strncmp(token, "NOT_NULL", 8) == 0)
364-
local_node->contype = CONSTR_NOTNULL;
365-
else if (length == 7 && strncmp(token, "DEFAULT", 7) == 0)
366-
local_node->contype = CONSTR_DEFAULT;
367-
else if (length == 8 && strncmp(token, "IDENTITY", 8) == 0)
368-
local_node->contype = CONSTR_IDENTITY;
369-
else if (length == 9 && strncmp(token, "GENERATED", 9) == 0)
370-
local_node->contype = CONSTR_GENERATED;
371-
else if (length == 5 && strncmp(token, "CHECK", 5) == 0)
372-
local_node->contype = CONSTR_CHECK;
373-
else if (length == 11 && strncmp(token, "PRIMARY_KEY", 11) == 0)
374-
local_node->contype = CONSTR_PRIMARY;
375-
else if (length == 6 && strncmp(token, "UNIQUE", 6) == 0)
376-
local_node->contype = CONSTR_UNIQUE;
377-
else if (length == 9 && strncmp(token, "EXCLUSION", 9) == 0)
378-
local_node->contype = CONSTR_EXCLUSION;
379-
else if (length == 11 && strncmp(token, "FOREIGN_KEY", 11) == 0)
380-
local_node->contype = CONSTR_FOREIGN;
381-
else if (length == 15 && strncmp(token, "ATTR_DEFERRABLE", 15) == 0)
382-
local_node->contype = CONSTR_ATTR_DEFERRABLE;
383-
else if (length == 19 && strncmp(token, "ATTR_NOT_DEFERRABLE", 19) == 0)
384-
local_node->contype = CONSTR_ATTR_NOT_DEFERRABLE;
385-
else if (length == 13 && strncmp(token, "ATTR_DEFERRED", 13) == 0)
386-
local_node->contype = CONSTR_ATTR_DEFERRED;
387-
else if (length == 14 && strncmp(token, "ATTR_IMMEDIATE", 14) == 0)
388-
local_node->contype = CONSTR_ATTR_IMMEDIATE;
389-
390-
switch (local_node->contype)
391-
{
392-
case CONSTR_NULL:
393-
/* no extra fields */
394-
break;
395-
396-
case CONSTR_NOTNULL:
397-
READ_NODE_FIELD(keys);
398-
READ_INT_FIELD(inhcount);
399-
READ_BOOL_FIELD(is_no_inherit);
400-
READ_BOOL_FIELD(skip_validation);
401-
READ_BOOL_FIELD(initially_valid);
402-
break;
403-
404-
case CONSTR_DEFAULT:
405-
READ_NODE_FIELD(raw_expr);
406-
READ_STRING_FIELD(cooked_expr);
407-
break;
408-
409-
case CONSTR_IDENTITY:
410-
READ_NODE_FIELD(options);
411-
READ_CHAR_FIELD(generated_when);
412-
break;
413-
414-
case CONSTR_GENERATED:
415-
READ_NODE_FIELD(raw_expr);
416-
READ_STRING_FIELD(cooked_expr);
417-
READ_CHAR_FIELD(generated_when);
418-
break;
419-
420-
case CONSTR_CHECK:
421-
READ_BOOL_FIELD(is_no_inherit);
422-
READ_NODE_FIELD(raw_expr);
423-
READ_STRING_FIELD(cooked_expr);
424-
READ_BOOL_FIELD(skip_validation);
425-
READ_BOOL_FIELD(initially_valid);
426-
break;
427-
428-
case CONSTR_PRIMARY:
429-
READ_NODE_FIELD(keys);
430-
READ_BOOL_FIELD(without_overlaps);
431-
READ_NODE_FIELD(including);
432-
READ_NODE_FIELD(options);
433-
READ_STRING_FIELD(indexname);
434-
READ_STRING_FIELD(indexspace);
435-
READ_BOOL_FIELD(reset_default_tblspc);
436-
/* access_method and where_clause not currently used */
437-
break;
438-
439-
case CONSTR_UNIQUE:
440-
READ_BOOL_FIELD(nulls_not_distinct);
441-
READ_NODE_FIELD(keys);
442-
READ_BOOL_FIELD(without_overlaps);
443-
READ_NODE_FIELD(including);
444-
READ_NODE_FIELD(options);
445-
READ_STRING_FIELD(indexname);
446-
READ_STRING_FIELD(indexspace);
447-
READ_BOOL_FIELD(reset_default_tblspc);
448-
/* access_method and where_clause not currently used */
449-
break;
450-
451-
case CONSTR_EXCLUSION:
452-
READ_NODE_FIELD(exclusions);
453-
READ_NODE_FIELD(including);
454-
READ_NODE_FIELD(options);
455-
READ_STRING_FIELD(indexname);
456-
READ_STRING_FIELD(indexspace);
457-
READ_BOOL_FIELD(reset_default_tblspc);
458-
READ_STRING_FIELD(access_method);
459-
READ_NODE_FIELD(where_clause);
460-
break;
461-
462-
case CONSTR_FOREIGN:
463-
READ_NODE_FIELD(pktable);
464-
READ_NODE_FIELD(fk_attrs);
465-
READ_NODE_FIELD(pk_attrs);
466-
READ_CHAR_FIELD(fk_matchtype);
467-
READ_CHAR_FIELD(fk_upd_action);
468-
READ_CHAR_FIELD(fk_del_action);
469-
READ_NODE_FIELD(fk_del_set_cols);
470-
READ_NODE_FIELD(old_conpfeqop);
471-
READ_OID_FIELD(old_pktable_oid);
472-
READ_BOOL_FIELD(skip_validation);
473-
READ_BOOL_FIELD(initially_valid);
474-
break;
475-
476-
case CONSTR_ATTR_DEFERRABLE:
477-
case CONSTR_ATTR_NOT_DEFERRABLE:
478-
case CONSTR_ATTR_DEFERRED:
479-
case CONSTR_ATTR_IMMEDIATE:
480-
/* no extra fields */
481-
break;
482-
483-
default:
484-
elog(ERROR, "unrecognized ConstrType: %d", (int) local_node->contype);
485-
break;
486-
}
487-
488-
READ_DONE();
489-
}
490-
491346
static RangeTblEntry *
492347
_readRangeTblEntry(void)
493348
{

src/include/nodes/parsenodes.h

+13-25
Original file line numberDiff line numberDiff line change
@@ -2566,44 +2566,34 @@ typedef enum ConstrType /* types of constraints */
25662566

25672567
typedef struct Constraint
25682568
{
2569-
pg_node_attr(custom_read_write)
2570-
25712569
NodeTag type;
25722570
ConstrType contype; /* see above */
2573-
2574-
/* Fields used for most/all constraint types: */
25752571
char *conname; /* Constraint name, or NULL if unnamed */
25762572
bool deferrable; /* DEFERRABLE? */
25772573
bool initdeferred; /* INITIALLY DEFERRED? */
2578-
int location; /* token location, or -1 if unknown */
2579-
2580-
/* Fields used for constraints with expressions (CHECK and DEFAULT): */
2574+
bool skip_validation; /* skip validation of existing rows? */
2575+
bool initially_valid; /* mark the new constraint as valid? */
25812576
bool is_no_inherit; /* is constraint non-inheritable? */
2582-
Node *raw_expr; /* expr, as untransformed parse tree */
2583-
char *cooked_expr; /* expr, as nodeToString representation */
2577+
Node *raw_expr; /* CHECK or DEFAULT expression, as
2578+
* untransformed parse tree */
2579+
char *cooked_expr; /* CHECK or DEFAULT expression, as
2580+
* nodeToString representation */
25842581
char generated_when; /* ALWAYS or BY DEFAULT */
2585-
2586-
/* Fields used for "raw" NOT NULL constraints: */
2587-
int inhcount; /* initial inheritance count to apply */
2588-
2589-
/* Fields used for unique constraints (UNIQUE and PRIMARY KEY): */
2582+
int inhcount; /* initial inheritance count to apply, for
2583+
* "raw" NOT NULL constraints */
25902584
bool nulls_not_distinct; /* null treatment for UNIQUE constraints */
25912585
List *keys; /* String nodes naming referenced key
2592-
* column(s); also used for NOT NULL */
2586+
* column(s); for UNIQUE/PK/NOT NULL */
25932587
bool without_overlaps; /* WITHOUT OVERLAPS specified */
25942588
List *including; /* String nodes naming referenced nonkey
2595-
* column(s) */
2596-
2597-
/* Fields used for EXCLUSION constraints: */
2598-
List *exclusions; /* list of (IndexElem, operator name) pairs */
2599-
2600-
/* Fields used for index constraints (UNIQUE, PRIMARY KEY, EXCLUSION): */
2589+
* column(s); for UNIQUE/PK */
2590+
List *exclusions; /* list of (IndexElem, operator name) pairs;
2591+
* for exclusion constraints */
26012592
List *options; /* options from WITH clause */
26022593
char *indexname; /* existing index to use; otherwise NULL */
26032594
char *indexspace; /* index tablespace; NULL for default */
26042595
bool reset_default_tblspc; /* reset default_tablespace prior to
26052596
* creating the index */
2606-
/* These could be, but currently are not, used for UNIQUE/PKEY: */
26072597
char *access_method; /* index access method; NULL for default */
26082598
Node *where_clause; /* partial index predicate */
26092599

@@ -2619,9 +2609,7 @@ typedef struct Constraint
26192609
Oid old_pktable_oid; /* pg_constraint.confrelid of my former
26202610
* self */
26212611

2622-
/* Fields used for constraints that allow a NOT VALID specification */
2623-
bool skip_validation; /* skip validation of existing rows? */
2624-
bool initially_valid; /* mark the new constraint as valid? */
2612+
int location; /* token location, or -1 if unknown */
26252613
} Constraint;
26262614

26272615
/* ----------------------

0 commit comments

Comments
 (0)