pgsql: Build in some knowledge about foreign-key relationships in the c - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Build in some knowledge about foreign-key relationships in the c
Date
Msg-id E1l73uF-00025d-3u@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Build in some knowledge about foreign-key relationships in the catalogs.

This follows in the spirit of commit dfb75e478, which created primary
key and uniqueness constraints to improve the visibility of constraints
imposed on the system catalogs.  While our catalogs contain many
foreign-key-like relationships, they don't quite follow SQL semantics,
in that the convention for an omitted reference is to write zero not
NULL.  Plus, we have some cases in which there are arrays each of whose
elements is supposed to be an FK reference; SQL has no way to model that.
So we can't create actual foreign key constraints to describe the
situation.  Nonetheless, we can collect and use knowledge about these
relationships.

This patch therefore adds annotations to the catalog header files to
declare foreign-key relationships.  (The BKI_LOOKUP annotations cover
simple cases, but we weren't previously distinguishing which such
columns are allowed to contain zeroes; we also need new markings for
multi-column FK references.)  Then, Catalog.pm and genbki.pl are
taught to collect this information into a table in a new generated
header "system_fk_info.h".  The only user of that at the moment is
a new SQL function pg_get_catalog_foreign_keys(), which exposes the
table to SQL.  The oidjoins regression test is rewritten to use
pg_get_catalog_foreign_keys() to find out which columns to check.
Aside from removing the need for manual maintenance of that test
script, this allows it to cover numerous relationships that were not
checked by the old implementation based on findoidjoins.  (As of this
commit, 217 relationships are checked by the test, versus 181 before.)

Discussion: https://postgr.es/m/3240355.1612129197@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/62f34097c88433ef1f3de604714fe7e7024f2fdf

Modified Files
--------------
doc/src/sgml/bki.sgml                         |   25 +-
doc/src/sgml/func.sgml                        |   32 +
src/backend/catalog/.gitignore                |    1 +
src/backend/catalog/Catalog.pm                |   28 +-
src/backend/catalog/Makefile                  |    2 +-
src/backend/catalog/genbki.pl                 |  123 +-
src/backend/utils/adt/misc.c                  |   80 ++
src/include/Makefile                          |    5 +-
src/include/catalog/.gitignore                |    1 +
src/include/catalog/catversion.h              |    2 +-
src/include/catalog/genbki.h                  |   36 +-
src/include/catalog/pg_aggregate.h            |   18 +-
src/include/catalog/pg_amop.h                 |    2 +-
src/include/catalog/pg_attrdef.h              |    5 +-
src/include/catalog/pg_attribute.h            |   14 +-
src/include/catalog/pg_auth_members.h         |    6 +-
src/include/catalog/pg_cast.h                 |    2 +-
src/include/catalog/pg_class.h                |   22 +-
src/include/catalog/pg_collation.dat          |   15 +-
src/include/catalog/pg_collation.h            |    5 +-
src/include/catalog/pg_constraint.h           |   29 +-
src/include/catalog/pg_conversion.h           |    4 +-
src/include/catalog/pg_database.h             |    2 +-
src/include/catalog/pg_db_role_setting.h      |    7 +-
src/include/catalog/pg_default_acl.h          |    6 +-
src/include/catalog/pg_depend.h               |    6 +-
src/include/catalog/pg_description.h          |    3 +
src/include/catalog/pg_enum.h                 |    2 +-
src/include/catalog/pg_event_trigger.h        |    5 +-
src/include/catalog/pg_extension.h            |    8 +-
src/include/catalog/pg_foreign_data_wrapper.h |    9 +-
src/include/catalog/pg_foreign_server.h       |    4 +-
src/include/catalog/pg_foreign_table.h        |    4 +-
src/include/catalog/pg_index.h                |   12 +-
src/include/catalog/pg_inherits.h             |    4 +-
src/include/catalog/pg_init_privs.h           |    3 +-
src/include/catalog/pg_language.h             |    8 +-
src/include/catalog/pg_largeobject.h          |    3 +-
src/include/catalog/pg_largeobject_metadata.h |    3 +-
src/include/catalog/pg_namespace.dat          |    6 +-
src/include/catalog/pg_namespace.h            |    2 +-
src/include/catalog/pg_opclass.h              |    8 +-
src/include/catalog/pg_operator.h             |   22 +-
src/include/catalog/pg_opfamily.h             |    4 +-
src/include/catalog/pg_partitioned_table.h    |   17 +-
src/include/catalog/pg_policy.h               |    7 +-
src/include/catalog/pg_proc.dat               |   12 +-
src/include/catalog/pg_proc.h                 |   12 +-
src/include/catalog/pg_publication.h          |    2 +-
src/include/catalog/pg_publication_rel.h      |    4 +-
src/include/catalog/pg_range.h                |    6 +-
src/include/catalog/pg_rewrite.h              |    2 +-
src/include/catalog/pg_seclabel.h             |    3 +-
src/include/catalog/pg_sequence.h             |    4 +-
src/include/catalog/pg_shdepend.h             |    9 +-
src/include/catalog/pg_shdescription.h        |    3 +
src/include/catalog/pg_shseclabel.h           |    3 +-
src/include/catalog/pg_statistic.h            |   25 +-
src/include/catalog/pg_statistic_ext.h        |   10 +-
src/include/catalog/pg_statistic_ext_data.h   |    3 +-
src/include/catalog/pg_subscription.h         |    5 +-
src/include/catalog/pg_subscription_rel.h     |    4 +-
src/include/catalog/pg_tablespace.dat         |    6 +-
src/include/catalog/pg_tablespace.h           |    2 +-
src/include/catalog/pg_transform.h            |    8 +-
src/include/catalog/pg_trigger.h              |   21 +-
src/include/catalog/pg_ts_config.h            |    4 +-
src/include/catalog/pg_ts_dict.h              |    4 +-
src/include/catalog/pg_ts_parser.h            |    4 +-
src/include/catalog/pg_ts_template.h          |    4 +-
src/include/catalog/pg_type.h                 |   26 +-
src/include/catalog/pg_user_mapping.h         |    8 +-
src/test/regress/expected/oidjoins.out        | 1713 ++++---------------------
src/test/regress/parallel_schedule            |    5 +-
src/test/regress/serial_schedule              |    2 +-
src/test/regress/sql/oidjoins.sql             |  772 +----------
src/tools/msvc/Solution.pm                    |    3 +
src/tools/msvc/clean.bat                      |    2 +
78 files changed, 901 insertions(+), 2402 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Doc: consistently identify OID catalog columns that can be zero.
Next
From: Tom Lane
Date:
Subject: pgsql: Retire findoidjoins.