8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.82 2000/07/03 23:09:33 wieck Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.83 2000/07/04 06:11:27 tgl Exp $
12
12
*
13
13
* NOTES
14
14
* The PerformAddAttribute() code, like most of the relation
21
21
22
22
#include "catalog/catalog.h"
23
23
#include "catalog/catname.h"
24
+ #include "catalog/index.h"
24
25
#include "catalog/indexing.h"
25
26
#include "catalog/pg_attrdef.h"
27
+ #include "catalog/pg_opclass.h"
26
28
#include "commands/command.h"
27
29
#include "executor/spi.h"
28
30
#include "catalog/heap.h"
@@ -1184,22 +1186,18 @@ AlterTableCreateToastTable(const char *relationName)
1184
1186
Form_pg_attribute * att ;
1185
1187
Relation class_rel ;
1186
1188
Relation ridescs [Num_pg_class_indices ];
1187
- Oid toast_relid = 2 ;
1188
- Oid toast_idxid = 2 ;
1189
+ Oid toast_relid ;
1190
+ Oid toast_idxid ;
1189
1191
bool has_toastable_attrs = false;
1190
- bool old_allow ;
1191
1192
int i ;
1192
-
1193
1193
char toast_relname [NAMEDATALEN ];
1194
1194
char toast_idxname [NAMEDATALEN ];
1195
- char tmp_query [1024 ];
1196
1195
Relation toast_rel ;
1196
+ AttrNumber attNums [1 ];
1197
+ Oid classObjectId [1 ];
1197
1198
1198
1199
/*
1199
- * permissions checking. this would normally be done in utility.c,
1200
- * but this particular routine is recursive.
1201
- *
1202
- * normally, only the owner of a class can change its schema.
1200
+ * permissions checking. XXX exactly what is appropriate here?
1203
1201
*/
1204
1202
/*
1205
1203
if (!allowSystemTableMods && IsSystemRelationName(relationName))
@@ -1215,7 +1213,7 @@ AlterTableCreateToastTable(const char *relationName)
1215
1213
* Grab an exclusive lock on the target table, which we will NOT
1216
1214
* release until end of transaction.
1217
1215
*/
1218
- rel = heap_openr (relationName , RowExclusiveLock );
1216
+ rel = heap_openr (relationName , AccessExclusiveLock );
1219
1217
myrelid = RelationGetRelid (rel );
1220
1218
1221
1219
/*
@@ -1240,8 +1238,8 @@ AlterTableCreateToastTable(const char *relationName)
1240
1238
* Get the pg_class tuple for the relation
1241
1239
*/
1242
1240
reltup = SearchSysCacheTuple (RELNAME ,
1243
- PointerGetDatum (relationName ),
1244
- 0 , 0 , 0 );
1241
+ PointerGetDatum (relationName ),
1242
+ 0 , 0 , 0 );
1245
1243
1246
1244
if (!HeapTupleIsValid (reltup ))
1247
1245
elog (ERROR , "ALTER TABLE: relation \"%s\" not found" ,
@@ -1261,26 +1259,43 @@ AlterTableCreateToastTable(const char *relationName)
1261
1259
relationName );
1262
1260
1263
1261
/*
1264
- * Create the toast table and it's index
1265
- * This is bad and ugly, because we need to override
1266
- * allowSystemTableMods in order to keep the toast
1267
- * table- and index-name out of the users namespace.
1262
+ * Create the toast table and its index
1268
1263
*/
1269
- sprintf (toast_relname , "pg_toast_%d" , myrelid );
1270
- sprintf (toast_idxname , "pg_toast_%d_idx" , myrelid );
1271
-
1272
- old_allow = allowSystemTableMods ;
1273
- allowSystemTableMods = true;
1274
-
1275
- sprintf (tmp_query , "create table \"%s\" (chunk_id oid, chunk_seq int4, chunk_data text)" ,
1276
- toast_relname );
1277
- pg_exec_query_dest (tmp_query , None , CurrentMemoryContext );
1278
-
1279
- sprintf (tmp_query , "create index \"%s\" on \"%s\" (chunk_id)" ,
1280
- toast_idxname , toast_relname );
1281
- pg_exec_query_dest (tmp_query , None , CurrentMemoryContext );
1282
-
1283
- allowSystemTableMods = old_allow ;
1264
+ sprintf (toast_relname , "pg_toast_%u" , myrelid );
1265
+ sprintf (toast_idxname , "pg_toast_%u_idx" , myrelid );
1266
+
1267
+ /* this is pretty painful... need a tuple descriptor */
1268
+ tupdesc = CreateTemplateTupleDesc (3 );
1269
+ TupleDescInitEntry (tupdesc , (AttrNumber ) 1 ,
1270
+ "chunk_id" ,
1271
+ OIDOID ,
1272
+ -1 , 0 , false);
1273
+ TupleDescInitEntry (tupdesc , (AttrNumber ) 2 ,
1274
+ "chunk_seq" ,
1275
+ INT4OID ,
1276
+ -1 , 0 , false);
1277
+ TupleDescInitEntry (tupdesc , (AttrNumber ) 3 ,
1278
+ "chunk_data" ,
1279
+ TEXTOID , /* XXX wouldn't BYTEAOID be better? */
1280
+ -1 , 0 , false);
1281
+
1282
+ /* XXX use RELKIND_TOASTVALUE here? */
1283
+ /* XXX what if owning relation is temp? need we mark toasttable too? */
1284
+ heap_create_with_catalog (toast_relname , tupdesc , RELKIND_RELATION ,
1285
+ false, true);
1286
+
1287
+ /* make the toast relation visible, else index creation will fail */
1288
+ CommandCounterIncrement ();
1289
+
1290
+ /* create index on chunk_id */
1291
+ attNums [0 ] = 1 ;
1292
+ classObjectId [0 ] = OID_OPS_OID ;
1293
+ index_create (toast_relname , toast_idxname , NULL , NULL , BTREE_AM_OID ,
1294
+ 1 , attNums , classObjectId ,
1295
+ (Node * ) NULL , false, false, false, true);
1296
+
1297
+ /* make the index visible in this transaction */
1298
+ CommandCounterIncrement ();
1284
1299
1285
1300
/*
1286
1301
* Get the OIDs of the newly created objects
@@ -1318,8 +1333,8 @@ AlterTableCreateToastTable(const char *relationName)
1318
1333
1319
1334
heap_freetuple (reltup );
1320
1335
1336
+ heap_close (class_rel , RowExclusiveLock );
1321
1337
heap_close (rel , NoLock );
1322
- heap_close (class_rel , NoLock );
1323
1338
}
1324
1339
1325
1340
0 commit comments