Fix corruption of pg_shdepend when copying deps from template database
authorMichael Paquier <[email protected]>
Thu, 21 Oct 2021 01:39:01 +0000 (10:39 +0900)
committerMichael Paquier <[email protected]>
Thu, 21 Oct 2021 01:39:01 +0000 (10:39 +0900)
Using for a new database a template database with shared dependencies
that need to be copied over was causing a corruption of pg_shdepend
because of an off-by-one computation error of the index number used for
the values inserted with a slot.

Issue introduced by e3931d0.  Monitoring the rest of the code, there are
no similar mistakes.

Reported-by: Sven Klemm
Author: Aleksander Alekseev
Reviewed-by: Daniel Gustafsson, Michael Paquier
Discussion: https://postgr.es/m/CAJ7c6TP0AowkUgNL6zcAK-s5HYsVHVBRWfu69FRubPpfwZGM9A@mail.gmail.com
Backpatch-through: 14

src/backend/catalog/pg_shdepend.c

index 4b676f560786cdeeedeee99b555c042dcd167967..56a9a7662fd4f0279efbb5591405fc2d3536317c 100644 (file)
@@ -905,13 +905,13 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId)
 
                shdep = (Form_pg_shdepend) GETSTRUCT(tup);
 
-               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_dbid] = ObjectIdGetDatum(newDbId);
-               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_classid] = shdep->classid;
-               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objid] = shdep->objid;
-               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objsubid] = shdep->objsubid;
-               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refclassid] = shdep->refclassid;
-               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refobjid] = shdep->refobjid;
-               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_deptype] = shdep->deptype;
+               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(newDbId);
+               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_classid - 1] = shdep->classid;
+               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objid - 1] = shdep->objid;
+               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objsubid - 1] = shdep->objsubid;
+               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refclassid - 1] = shdep->refclassid;
+               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refobjid - 1] = shdep->refobjid;
+               slot[slot_stored_count]->tts_values[Anum_pg_shdepend_deptype - 1] = shdep->deptype;
 
                ExecStoreVirtualTuple(slot[slot_stored_count]);
                slot_stored_count++;