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

Commit c076f3d

Browse files
committed
Remove pg_constraint.conincluding
This column was added in commit 8224de4 ("Indexes with INCLUDE columns and their support in B-tree") to ease writing the ruleutils.c supporting code for that feature, but it turns out to be unnecessary -- we can do the same thing with just one more syscache lookup. Even the documentation for the new column being removed in this commit is awkward. Discussion: https://postgr.es/m/20180902165018.33otxftp3olgtu4t@alvherre.pgsql
1 parent 4ddd8f5 commit c076f3d

File tree

7 files changed

+71
-73
lines changed

7 files changed

+71
-73
lines changed

doc/src/sgml/catalogs.sgml

-8
Original file line numberDiff line numberDiff line change
@@ -2373,14 +2373,6 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
23732373
triggers), list of the constrained columns</entry>
23742374
</row>
23752375

2376-
<row>
2377-
<entry><structfield>conincluding</structfield></entry>
2378-
<entry><type>int2[]</type></entry>
2379-
<entry><literal><link linkend="catalog-pg-attribute"><structname>pg_attribute</structname></link>.attnum</literal></entry>
2380-
<entry>List of the non-constrained columns which are included into
2381-
the same index as the constrained columns</entry>
2382-
</row>
2383-
23842376
<row>
23852377
<entry><structfield>confkey</structfield></entry>
23862378
<entry><type>int2[]</type></entry>

src/backend/catalog/pg_constraint.c

-21
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ CreateConstraintEntry(const char *constraintName,
8585
bool nulls[Natts_pg_constraint];
8686
Datum values[Natts_pg_constraint];
8787
ArrayType *conkeyArray;
88-
ArrayType *conincludingArray;
8988
ArrayType *confkeyArray;
9089
ArrayType *conpfeqopArray;
9190
ArrayType *conppeqopArray;
@@ -116,21 +115,6 @@ CreateConstraintEntry(const char *constraintName,
116115
else
117116
conkeyArray = NULL;
118117

119-
if (constraintNTotalKeys > constraintNKeys)
120-
{
121-
Datum *conincluding;
122-
int j = 0;
123-
int constraintNIncludedKeys = constraintNTotalKeys - constraintNKeys;
124-
125-
conincluding = (Datum *) palloc(constraintNIncludedKeys * sizeof(Datum));
126-
for (i = constraintNKeys; i < constraintNTotalKeys; i++)
127-
conincluding[j++] = Int16GetDatum(constraintKey[i]);
128-
conincludingArray = construct_array(conincluding, constraintNIncludedKeys,
129-
INT2OID, 2, true, 's');
130-
}
131-
else
132-
conincludingArray = NULL;
133-
134118
if (foreignNKeys > 0)
135119
{
136120
Datum *fkdatums;
@@ -204,11 +188,6 @@ CreateConstraintEntry(const char *constraintName,
204188
else
205189
nulls[Anum_pg_constraint_conkey - 1] = true;
206190

207-
if (conincludingArray)
208-
values[Anum_pg_constraint_conincluding - 1] = PointerGetDatum(conincludingArray);
209-
else
210-
nulls[Anum_pg_constraint_conincluding - 1] = true;
211-
212191
if (confkeyArray)
213192
values[Anum_pg_constraint_confkey - 1] = PointerGetDatum(confkeyArray);
214193
else

src/backend/utils/adt/ruleutils.c

+45-12
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ static char *deparse_expression_pretty(Node *expr, List *dpcontext,
315315
static char *pg_get_viewdef_worker(Oid viewoid,
316316
int prettyFlags, int wrapColumn);
317317
static char *pg_get_triggerdef_worker(Oid trigid, bool pretty);
318-
static void decompile_column_index_array(Datum column_index_array, Oid relId,
318+
static int decompile_column_index_array(Datum column_index_array, Oid relId,
319319
StringInfo buf);
320320
static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);
321321
static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
@@ -2055,6 +2055,8 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
20552055
Datum val;
20562056
bool isnull;
20572057
Oid indexId;
2058+
int keyatts;
2059+
HeapTuple indtup;
20582060

20592061
/* Start off the constraint definition */
20602062
if (conForm->contype == CONSTRAINT_PRIMARY)
@@ -2069,24 +2071,52 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
20692071
elog(ERROR, "null conkey for constraint %u",
20702072
constraintId);
20712073

2072-
decompile_column_index_array(val, conForm->conrelid, &buf);
2074+
keyatts = decompile_column_index_array(val, conForm->conrelid, &buf);
20732075

20742076
appendStringInfoChar(&buf, ')');
20752077

2076-
/* Fetch and build including column list */
2077-
isnull = true;
2078-
val = SysCacheGetAttr(CONSTROID, tup,
2079-
Anum_pg_constraint_conincluding, &isnull);
2080-
if (!isnull)
2078+
indexId = get_constraint_index(constraintId);
2079+
2080+
/* Build including column list (from pg_index.indkeys) */
2081+
indtup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId));
2082+
if (!HeapTupleIsValid(indtup))
2083+
elog(ERROR, "cache lookup failed for index %u", indexId);
2084+
val = SysCacheGetAttr(INDEXRELID, indtup,
2085+
Anum_pg_index_indnatts, &isnull);
2086+
if (isnull)
2087+
elog(ERROR, "null indnatts for index %u", indexId);
2088+
if (DatumGetInt32(val) > keyatts)
20812089
{
2090+
Datum cols;
2091+
Datum *keys;
2092+
int nKeys;
2093+
int j;
2094+
20822095
appendStringInfoString(&buf, " INCLUDE (");
20832096

2084-
decompile_column_index_array(val, conForm->conrelid, &buf);
2097+
cols = SysCacheGetAttr(INDEXRELID, indtup,
2098+
Anum_pg_index_indkey, &isnull);
2099+
if (isnull)
2100+
elog(ERROR, "null indkey for index %u", indexId);
2101+
2102+
deconstruct_array(DatumGetArrayTypeP(cols),
2103+
INT2OID, 2, true, 's',
2104+
&keys, NULL, &nKeys);
2105+
2106+
for (j = keyatts; j < nKeys; j++)
2107+
{
2108+
char *colName;
2109+
2110+
colName = get_attname(conForm->conrelid,
2111+
DatumGetInt16(keys[j]), false);
2112+
if (j > keyatts)
2113+
appendStringInfoString(&buf, ", ");
2114+
appendStringInfoString(&buf, quote_identifier(colName));
2115+
}
20852116

20862117
appendStringInfoChar(&buf, ')');
20872118
}
2088-
2089-
indexId = get_constraint_index(constraintId);
2119+
ReleaseSysCache(indtup);
20902120

20912121
/* XXX why do we only print these bits if fullCommand? */
20922122
if (fullCommand && OidIsValid(indexId))
@@ -2232,9 +2262,10 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
22322262

22332263
/*
22342264
* Convert an int16[] Datum into a comma-separated list of column names
2235-
* for the indicated relation; append the list to buf.
2265+
* for the indicated relation; append the list to buf. Returns the number
2266+
* of keys.
22362267
*/
2237-
static void
2268+
static int
22382269
decompile_column_index_array(Datum column_index_array, Oid relId,
22392270
StringInfo buf)
22402271
{
@@ -2258,6 +2289,8 @@ decompile_column_index_array(Datum column_index_array, Oid relId,
22582289
else
22592290
appendStringInfo(buf, ", %s", quote_identifier(colName));
22602291
}
2292+
2293+
return nKeys;
22612294
}
22622295

22632296

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201808271
56+
#define CATALOG_VERSION_NO 201809031
5757

5858
#endif

src/include/catalog/pg_constraint.h

-6
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ CATALOG(pg_constraint,2606,ConstraintRelationId)
105105
*/
106106
int16 conkey[1];
107107

108-
/*
109-
* Columns of conrelid that the constraint does not apply to, but are
110-
* included into the same index as the key columns
111-
*/
112-
int16 conincluding[1];
113-
114108
/*
115109
* If a foreign key, the referenced columns of confrelid
116110
*/

src/test/regress/expected/index_including.out

+20-20
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, i
9494
covering | 4 | 2 | t | f | 1 2 3 4 | 1978 1978
9595
(1 row)
9696

97-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
98-
pg_get_constraintdef | conname | conkey | conincluding
99-
----------------------------------+----------+--------+--------------
100-
UNIQUE (c1, c2) INCLUDE (c3, c4) | covering | {1,2} | {3,4}
97+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
98+
pg_get_constraintdef | conname | conkey
99+
----------------------------------+----------+--------
100+
UNIQUE (c1, c2) INCLUDE (c3, c4) | covering | {1,2}
101101
(1 row)
102102

103103
-- ensure that constraint works
@@ -113,10 +113,10 @@ SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, i
113113
covering | 4 | 2 | t | t | 1 2 3 4 | 1978 1978
114114
(1 row)
115115

116-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
117-
pg_get_constraintdef | conname | conkey | conincluding
118-
---------------------------------------+----------+--------+--------------
119-
PRIMARY KEY (c1, c2) INCLUDE (c3, c4) | covering | {1,2} | {3,4}
116+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
117+
pg_get_constraintdef | conname | conkey
118+
---------------------------------------+----------+--------
119+
PRIMARY KEY (c1, c2) INCLUDE (c3, c4) | covering | {1,2}
120120
(1 row)
121121

122122
-- ensure that constraint works
@@ -136,10 +136,10 @@ SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, i
136136
tbl_c1_c2_c3_c4_key | 4 | 2 | t | f | 1 2 3 4 | 1978 1978
137137
(1 row)
138138

139-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
140-
pg_get_constraintdef | conname | conkey | conincluding
141-
----------------------------------+---------------------+--------+--------------
142-
UNIQUE (c1, c2) INCLUDE (c3, c4) | tbl_c1_c2_c3_c4_key | {1,2} | {3,4}
139+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
140+
pg_get_constraintdef | conname | conkey
141+
----------------------------------+---------------------+--------
142+
UNIQUE (c1, c2) INCLUDE (c3, c4) | tbl_c1_c2_c3_c4_key | {1,2}
143143
(1 row)
144144

145145
-- ensure that constraint works
@@ -155,10 +155,10 @@ SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, i
155155
tbl_pkey | 4 | 2 | t | t | 1 2 3 4 | 1978 1978
156156
(1 row)
157157

158-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
159-
pg_get_constraintdef | conname | conkey | conincluding
160-
---------------------------------------+----------+--------+--------------
161-
PRIMARY KEY (c1, c2) INCLUDE (c3, c4) | tbl_pkey | {1,2} | {3,4}
158+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
159+
pg_get_constraintdef | conname | conkey
160+
---------------------------------------+----------+--------
161+
PRIMARY KEY (c1, c2) INCLUDE (c3, c4) | tbl_pkey | {1,2}
162162
(1 row)
163163

164164
-- ensure that constraint works
@@ -178,10 +178,10 @@ SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, i
178178
tbl_c1_c3_c4_excl | 3 | 1 | f | f | 1 3 4 | 1978
179179
(1 row)
180180

181-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
182-
pg_get_constraintdef | conname | conkey | conincluding
183-
--------------------------------------------------+-------------------+--------+--------------
184-
EXCLUDE USING btree (c1 WITH =) INCLUDE (c3, c4) | tbl_c1_c3_c4_excl | {1} | {3,4}
181+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
182+
pg_get_constraintdef | conname | conkey
183+
--------------------------------------------------+-------------------+--------
184+
EXCLUDE USING btree (c1 WITH =) INCLUDE (c3, c4) | tbl_c1_c3_c4_excl | {1}
185185
(1 row)
186186

187187
-- ensure that constraint works

src/test/regress/sql/index_including.sql

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ ALTER TABLE tbl_include_box_pk add PRIMARY KEY (c1, c2) INCLUDE (c3, c4);
6060
CREATE TABLE tbl (c1 int,c2 int, c3 int, c4 box,
6161
CONSTRAINT covering UNIQUE(c1,c2) INCLUDE(c3,c4));
6262
SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid;
63-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
63+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
6464
-- ensure that constraint works
6565
INSERT INTO tbl SELECT 1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x;
6666
DROP TABLE tbl;
6767

6868
CREATE TABLE tbl (c1 int,c2 int, c3 int, c4 box,
6969
CONSTRAINT covering PRIMARY KEY(c1,c2) INCLUDE(c3,c4));
7070
SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid;
71-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
71+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
7272
-- ensure that constraint works
7373
INSERT INTO tbl SELECT 1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x;
7474
INSERT INTO tbl SELECT 1, NULL, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x;
@@ -78,15 +78,15 @@ DROP TABLE tbl;
7878
CREATE TABLE tbl (c1 int,c2 int, c3 int, c4 box,
7979
UNIQUE(c1,c2) INCLUDE(c3,c4));
8080
SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid;
81-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
81+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
8282
-- ensure that constraint works
8383
INSERT INTO tbl SELECT 1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x;
8484
DROP TABLE tbl;
8585

8686
CREATE TABLE tbl (c1 int,c2 int, c3 int, c4 box,
8787
PRIMARY KEY(c1,c2) INCLUDE(c3,c4));
8888
SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid;
89-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
89+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
9090
-- ensure that constraint works
9191
INSERT INTO tbl SELECT 1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x;
9292
INSERT INTO tbl SELECT 1, NULL, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x;
@@ -96,7 +96,7 @@ DROP TABLE tbl;
9696
CREATE TABLE tbl (c1 int,c2 int, c3 int, c4 box,
9797
EXCLUDE USING btree (c1 WITH =) INCLUDE(c3,c4));
9898
SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid;
99-
SELECT pg_get_constraintdef(oid), conname, conkey, conincluding FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
99+
SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid;
100100
-- ensure that constraint works
101101
INSERT INTO tbl SELECT 1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x;
102102
INSERT INTO tbl SELECT x, 2*x, NULL, NULL FROM generate_series(1,10) AS x;

0 commit comments

Comments
 (0)