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

Commit 3e66019

Browse files
committed
Assert that we don't insert nulls into attnotnull catalog columns.
The executor checks for this error, and so does the bootstrap catalog loader, but we never checked for it in retail catalog manipulations. The folly of that has now been exposed, so let's add assertions checking it. Checking in CatalogTupleInsert[WithInfo] and CatalogTupleUpdate[WithInfo] should be enough to cover this. Back-patch to v10; the aforesaid functions didn't exist before that, and it didn't seem worth adapting the patch to the oldest branches. But given the risk of JIT crashes, I think we certainly need this as far back as v11. Pre-v13, we have to explicitly exclude pg_subscription.subslotname and pg_subscription_rel.srsublsn from the checks, since they are mismarked. (Even if we change our mind about applying BKI_FORCE_NULL in the branch tips, it doesn't seem wise to have assertions that would fire in existing databases.) Discussion: https://postgr.es/m/298837.1595196283@sss.pgh.pa.us
1 parent c273d9d commit 3e66019

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

doc/src/sgml/bki.sgml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@
122122
if they are fixed-width and are not preceded by any nullable column.
123123
Where this rule is inadequate, you can force correct marking by using
124124
<literal>BKI_FORCE_NOT_NULL</literal>
125-
and <literal>BKI_FORCE_NULL</literal> annotations as needed. But note
126-
that <literal>NOT NULL</literal> constraints are only enforced in the
127-
executor, not against tuples that are generated by random C code,
128-
so care is still needed when manually creating or updating catalog rows.
125+
and <literal>BKI_FORCE_NULL</literal> annotations as needed.
129126
</para>
130127

131128
<para>

src/backend/catalog/indexing.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,43 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
167167
ExecDropSingleTupleTableSlot(slot);
168168
}
169169

170+
/*
171+
* Subroutine to verify that catalog constraints are honored.
172+
*
173+
* Tuples inserted via CatalogTupleInsert/CatalogTupleUpdate are generally
174+
* "hand made", so that it's possible that they fail to satisfy constraints
175+
* that would be checked if they were being inserted by the executor. That's
176+
* a coding error, so we only bother to check for it in assert-enabled builds.
177+
*/
178+
#ifdef USE_ASSERT_CHECKING
179+
180+
static void
181+
CatalogTupleCheckConstraints(Relation heapRel, HeapTuple tup)
182+
{
183+
/*
184+
* Currently, the only constraints implemented for system catalogs are
185+
* attnotnull constraints.
186+
*/
187+
if (HeapTupleHasNulls(tup))
188+
{
189+
TupleDesc tupdesc = RelationGetDescr(heapRel);
190+
bits8 *bp = tup->t_data->t_bits;
191+
192+
for (int attnum = 0; attnum < tupdesc->natts; attnum++)
193+
{
194+
Form_pg_attribute thisatt = TupleDescAttr(tupdesc, attnum);
195+
196+
Assert(!(thisatt->attnotnull && att_isnull(attnum, bp)));
197+
}
198+
}
199+
}
200+
201+
#else /* !USE_ASSERT_CHECKING */
202+
203+
#define CatalogTupleCheckConstraints(heapRel, tup) ((void) 0)
204+
205+
#endif /* USE_ASSERT_CHECKING */
206+
170207
/*
171208
* CatalogTupleInsert - do heap and indexing work for a new catalog tuple
172209
*
@@ -184,6 +221,8 @@ CatalogTupleInsert(Relation heapRel, HeapTuple tup)
184221
{
185222
CatalogIndexState indstate;
186223

224+
CatalogTupleCheckConstraints(heapRel, tup);
225+
187226
indstate = CatalogOpenIndexes(heapRel);
188227

189228
simple_heap_insert(heapRel, tup);
@@ -204,6 +243,8 @@ void
204243
CatalogTupleInsertWithInfo(Relation heapRel, HeapTuple tup,
205244
CatalogIndexState indstate)
206245
{
246+
CatalogTupleCheckConstraints(heapRel, tup);
247+
207248
simple_heap_insert(heapRel, tup);
208249

209250
CatalogIndexInsert(indstate, tup);
@@ -225,6 +266,8 @@ CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
225266
{
226267
CatalogIndexState indstate;
227268

269+
CatalogTupleCheckConstraints(heapRel, tup);
270+
228271
indstate = CatalogOpenIndexes(heapRel);
229272

230273
simple_heap_update(heapRel, otid, tup);
@@ -245,6 +288,8 @@ void
245288
CatalogTupleUpdateWithInfo(Relation heapRel, ItemPointer otid, HeapTuple tup,
246289
CatalogIndexState indstate)
247290
{
291+
CatalogTupleCheckConstraints(heapRel, tup);
292+
248293
simple_heap_update(heapRel, otid, tup);
249294

250295
CatalogIndexInsert(indstate, tup);

0 commit comments

Comments
 (0)