Skip to content

Commit fb71329

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 ae3d40b commit fb71329

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/backend/catalog/indexing.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "access/htup_details.h"
1919
#include "catalog/index.h"
2020
#include "catalog/indexing.h"
21+
#include "catalog/pg_subscription.h"
22+
#include "catalog/pg_subscription_rel.h"
2123
#include "executor/executor.h"
2224
#include "utils/rel.h"
2325

@@ -163,6 +165,53 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
163165
ExecDropSingleTupleTableSlot(slot);
164166
}
165167

168+
/*
169+
* Subroutine to verify that catalog constraints are honored.
170+
*
171+
* Tuples inserted via CatalogTupleInsert/CatalogTupleUpdate are generally
172+
* "hand made", so that it's possible that they fail to satisfy constraints
173+
* that would be checked if they were being inserted by the executor. That's
174+
* a coding error, so we only bother to check for it in assert-enabled builds.
175+
*/
176+
#ifdef USE_ASSERT_CHECKING
177+
178+
static void
179+
CatalogTupleCheckConstraints(Relation heapRel, HeapTuple tup)
180+
{
181+
/*
182+
* Currently, the only constraints implemented for system catalogs are
183+
* attnotnull constraints.
184+
*/
185+
if (HeapTupleHasNulls(tup))
186+
{
187+
TupleDesc tupdesc = RelationGetDescr(heapRel);
188+
bits8 *bp = tup->t_data->t_bits;
189+
190+
for (int attnum = 0; attnum < tupdesc->natts; attnum++)
191+
{
192+
Form_pg_attribute thisatt = TupleDescAttr(tupdesc, attnum);
193+
194+
/*
195+
* Through an embarrassing oversight, pre-v13 installations have
196+
* pg_subscription.subslotname and pg_subscription_rel.srsublsn
197+
* marked as attnotnull, which they should not be. Ignore those
198+
* flags.
199+
*/
200+
Assert(!(thisatt->attnotnull && att_isnull(attnum, bp) &&
201+
!((thisatt->attrelid == SubscriptionRelationId &&
202+
thisatt->attnum == Anum_pg_subscription_subslotname) ||
203+
(thisatt->attrelid == SubscriptionRelRelationId &&
204+
thisatt->attnum == Anum_pg_subscription_rel_srsublsn))));
205+
}
206+
}
207+
}
208+
209+
#else /* !USE_ASSERT_CHECKING */
210+
211+
#define CatalogTupleCheckConstraints(heapRel, tup) ((void) 0)
212+
213+
#endif /* USE_ASSERT_CHECKING */
214+
166215
/*
167216
* CatalogTupleInsert - do heap and indexing work for a new catalog tuple
168217
*
@@ -181,6 +230,8 @@ CatalogTupleInsert(Relation heapRel, HeapTuple tup)
181230
CatalogIndexState indstate;
182231
Oid oid;
183232

233+
CatalogTupleCheckConstraints(heapRel, tup);
234+
184235
indstate = CatalogOpenIndexes(heapRel);
185236

186237
oid = simple_heap_insert(heapRel, tup);
@@ -205,6 +256,8 @@ CatalogTupleInsertWithInfo(Relation heapRel, HeapTuple tup,
205256
{
206257
Oid oid;
207258

259+
CatalogTupleCheckConstraints(heapRel, tup);
260+
208261
oid = simple_heap_insert(heapRel, tup);
209262

210263
CatalogIndexInsert(indstate, tup);
@@ -228,6 +281,8 @@ CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
228281
{
229282
CatalogIndexState indstate;
230283

284+
CatalogTupleCheckConstraints(heapRel, tup);
285+
231286
indstate = CatalogOpenIndexes(heapRel);
232287

233288
simple_heap_update(heapRel, otid, tup);
@@ -248,6 +303,8 @@ void
248303
CatalogTupleUpdateWithInfo(Relation heapRel, ItemPointer otid, HeapTuple tup,
249304
CatalogIndexState indstate)
250305
{
306+
CatalogTupleCheckConstraints(heapRel, tup);
307+
251308
simple_heap_update(heapRel, otid, tup);
252309

253310
CatalogIndexInsert(indstate, tup);

0 commit comments

Comments
 (0)