13
13
* Portions Copyright (c) 1994, Regents of the University of California
14
14
*
15
15
* IDENTIFICATION
16
- * $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.96 2007/04/20 02:37:37 tgl Exp $
16
+ * $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.97 2007/07/25 22:16:18 tgl Exp $
17
17
*
18
18
*-------------------------------------------------------------------------
19
19
*/
@@ -152,6 +152,9 @@ static List *overrideStack = NIL;
152
152
* in a particular backend session (this happens when a CREATE TEMP TABLE
153
153
* command is first executed). Thereafter it's the OID of the temp namespace.
154
154
*
155
+ * myTempToastNamespace is the OID of the namespace for my temp tables' toast
156
+ * tables. It is set when myTempNamespace is, and is InvalidOid before that.
157
+ *
155
158
* myTempNamespaceSubID shows whether we've created the TEMP namespace in the
156
159
* current subtransaction. The flag propagates up the subtransaction tree,
157
160
* so the main transaction will correctly recognize the flag if all
@@ -161,6 +164,8 @@ static List *overrideStack = NIL;
161
164
*/
162
165
static Oid myTempNamespace = InvalidOid ;
163
166
167
+ static Oid myTempToastNamespace = InvalidOid ;
168
+
164
169
static SubTransactionId myTempNamespaceSubID = InvalidSubTransactionId ;
165
170
166
171
/*
@@ -1599,39 +1604,78 @@ isTempNamespace(Oid namespaceId)
1599
1604
return false;
1600
1605
}
1601
1606
1607
+ /*
1608
+ * isTempToastNamespace - is the given namespace my temporary-toast-table
1609
+ * namespace?
1610
+ */
1611
+ bool
1612
+ isTempToastNamespace (Oid namespaceId )
1613
+ {
1614
+ if (OidIsValid (myTempToastNamespace ) && myTempToastNamespace == namespaceId )
1615
+ return true;
1616
+ return false;
1617
+ }
1618
+
1619
+ /*
1620
+ * isTempOrToastNamespace - is the given namespace my temporary-table
1621
+ * namespace or my temporary-toast-table namespace?
1622
+ */
1623
+ bool
1624
+ isTempOrToastNamespace (Oid namespaceId )
1625
+ {
1626
+ if (OidIsValid (myTempNamespace ) &&
1627
+ (myTempNamespace == namespaceId || myTempToastNamespace == namespaceId ))
1628
+ return true;
1629
+ return false;
1630
+ }
1631
+
1602
1632
/*
1603
1633
* isAnyTempNamespace - is the given namespace a temporary-table namespace
1604
- * (either my own, or another backend's)?
1634
+ * (either my own, or another backend's)? Temporary-toast-table namespaces
1635
+ * are included, too.
1605
1636
*/
1606
1637
bool
1607
1638
isAnyTempNamespace (Oid namespaceId )
1608
1639
{
1609
1640
bool result ;
1610
1641
char * nspname ;
1611
1642
1612
- /* If the namespace name starts with "pg_temp_", say "true " */
1643
+ /* True if the namespace name starts with "pg_temp_" or "pg_toast_temp_ " */
1613
1644
nspname = get_namespace_name (namespaceId );
1614
1645
if (!nspname )
1615
1646
return false; /* no such namespace? */
1616
- result = (strncmp (nspname , "pg_temp_" , 8 ) == 0 );
1647
+ result = (strncmp (nspname , "pg_temp_" , 8 ) == 0 ) ||
1648
+ (strncmp (nspname , "pg_toast_temp_" , 14 ) == 0 );
1617
1649
pfree (nspname );
1618
1650
return result ;
1619
1651
}
1620
1652
1621
1653
/*
1622
1654
* isOtherTempNamespace - is the given namespace some other backend's
1623
- * temporary-table namespace?
1655
+ * temporary-table namespace (including temporary-toast-table namespaces) ?
1624
1656
*/
1625
1657
bool
1626
1658
isOtherTempNamespace (Oid namespaceId )
1627
1659
{
1628
1660
/* If it's my own temp namespace, say "false" */
1629
- if (isTempNamespace (namespaceId ))
1661
+ if (isTempOrToastNamespace (namespaceId ))
1630
1662
return false;
1631
- /* Else, if the namespace name starts with "pg_temp_" , say "true" */
1663
+ /* Else, if it's any temp namespace , say "true" */
1632
1664
return isAnyTempNamespace (namespaceId );
1633
1665
}
1634
1666
1667
+ /*
1668
+ * GetTempToastNamespace - get the OID of my temporary-toast-table namespace,
1669
+ * which must already be assigned. (This is only used when creating a toast
1670
+ * table for a temp table, so we must have already done InitTempTableNamespace)
1671
+ */
1672
+ Oid
1673
+ GetTempToastNamespace (void )
1674
+ {
1675
+ Assert (OidIsValid (myTempToastNamespace ));
1676
+ return myTempToastNamespace ;
1677
+ }
1678
+
1635
1679
1636
1680
/*
1637
1681
* GetOverrideSearchPath - fetch current search path definition in form
@@ -2006,6 +2050,7 @@ InitTempTableNamespace(void)
2006
2050
{
2007
2051
char namespaceName [NAMEDATALEN ];
2008
2052
Oid namespaceId ;
2053
+ Oid toastspaceId ;
2009
2054
2010
2055
Assert (!OidIsValid (myTempNamespace ));
2011
2056
@@ -2054,12 +2099,31 @@ InitTempTableNamespace(void)
2054
2099
RemoveTempRelations (namespaceId );
2055
2100
}
2056
2101
2102
+ /*
2103
+ * If the corresponding temp-table namespace doesn't exist yet, create it.
2104
+ * (We assume there is no need to clean it out if it does exist, since
2105
+ * dropping a parent table should make its toast table go away.)
2106
+ */
2107
+ snprintf (namespaceName , sizeof (namespaceName ), "pg_toast_temp_%d" ,
2108
+ MyBackendId );
2109
+
2110
+ toastspaceId = GetSysCacheOid (NAMESPACENAME ,
2111
+ CStringGetDatum (namespaceName ),
2112
+ 0 , 0 , 0 );
2113
+ if (!OidIsValid (toastspaceId ))
2114
+ {
2115
+ toastspaceId = NamespaceCreate (namespaceName , BOOTSTRAP_SUPERUSERID );
2116
+ /* Advance command counter to make namespace visible */
2117
+ CommandCounterIncrement ();
2118
+ }
2119
+
2057
2120
/*
2058
2121
* Okay, we've prepared the temp namespace ... but it's not committed yet,
2059
2122
* so all our work could be undone by transaction rollback. Set flag for
2060
2123
* AtEOXact_Namespace to know what to do.
2061
2124
*/
2062
2125
myTempNamespace = namespaceId ;
2126
+ myTempToastNamespace = toastspaceId ;
2063
2127
2064
2128
/* It should not be done already. */
2065
2129
AssertState (myTempNamespaceSubID == InvalidSubTransactionId );
@@ -2089,6 +2153,7 @@ AtEOXact_Namespace(bool isCommit)
2089
2153
else
2090
2154
{
2091
2155
myTempNamespace = InvalidOid ;
2156
+ myTempToastNamespace = InvalidOid ;
2092
2157
baseSearchPathValid = false; /* need to rebuild list */
2093
2158
}
2094
2159
myTempNamespaceSubID = InvalidSubTransactionId ;
@@ -2140,6 +2205,7 @@ AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid,
2140
2205
myTempNamespaceSubID = InvalidSubTransactionId ;
2141
2206
/* TEMP namespace creation failed, so reset state */
2142
2207
myTempNamespace = InvalidOid ;
2208
+ myTempToastNamespace = InvalidOid ;
2143
2209
baseSearchPathValid = false; /* need to rebuild list */
2144
2210
}
2145
2211
}
0 commit comments