Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3e66019

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 catalogloader, but we never checked for it in retail catalog manipulations.The folly of that has now been exposed, so let's add assertionschecking it. Checking in CatalogTupleInsert[WithInfo] andCatalogTupleUpdate[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 thisas far back as v11.Pre-v13, we have to explicitly exclude pg_subscription.subslotnameand pg_subscription_rel.srsublsn from the checks, since they aremismarked. (Even if we change our mind about applying BKI_FORCE_NULLin the branch tips, it doesn't seem wise to have assertions thatwould fire in existing databases.)Discussion:https://postgr.es/m/298837.1595196283@sss.pgh.pa.us
1 parentc273d9d commit3e66019

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+
#ifdefUSE_ASSERT_CHECKING
179+
180+
staticvoid
181+
CatalogTupleCheckConstraints(RelationheapRel,HeapTupletup)
182+
{
183+
/*
184+
* Currently, the only constraints implemented for system catalogs are
185+
* attnotnull constraints.
186+
*/
187+
if (HeapTupleHasNulls(tup))
188+
{
189+
TupleDesctupdesc=RelationGetDescr(heapRel);
190+
bits8*bp=tup->t_data->t_bits;
191+
192+
for (intattnum=0;attnum<tupdesc->natts;attnum++)
193+
{
194+
Form_pg_attributethisatt=TupleDescAttr(tupdesc,attnum);
195+
196+
Assert(!(thisatt->attnotnull&&att_isnull(attnum,bp)));
197+
}
198+
}
199+
}
200+
201+
#else/* !USE_ASSERT_CHECKING */
202+
203+
#defineCatalogTupleCheckConstraints(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
CatalogIndexStateindstate;
186223

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

189228
simple_heap_insert(heapRel,tup);
@@ -204,6 +243,8 @@ void
204243
CatalogTupleInsertWithInfo(RelationheapRel,HeapTupletup,
205244
CatalogIndexStateindstate)
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
CatalogIndexStateindstate;
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(RelationheapRel,ItemPointerotid,HeapTupletup,
246289
CatalogIndexStateindstate)
247290
{
291+
CatalogTupleCheckConstraints(heapRel,tup);
292+
248293
simple_heap_update(heapRel,otid,tup);
249294

250295
CatalogIndexInsert(indstate,tup);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp