* Open a temporary file that will disappear when we close it. * * This routine takes care of generating an appropriate tempfile name. * There's no need to pass in fileFlags or fileMode either, since only * one setting makes any sense for a temp file. * * Unless interXact is true, the file is remembered by CurrentResourceOwner * to ensure it's closed and deleted when it's no longer needed, t
| 1659 | * string is embedded in the file name. It can be NULL. |
| 1660 | */ |
| 1661 | File |
| 1662 | OpenTemporaryFile(bool interXact, const char *filePrefix) |
| 1663 | { |
| 1664 | File file = 0; |
| 1665 | |
| 1666 | /* |
| 1667 | * Make sure the current resource owner has space for this File before we |
| 1668 | * open it, if we'll be registering it below. |
| 1669 | */ |
| 1670 | if (!interXact) |
| 1671 | ResourceOwnerEnlargeFiles(CurrentResourceOwner); |
| 1672 | |
| 1673 | /* |
| 1674 | * If some temp tablespace(s) have been given to us, try to use the next |
| 1675 | * one. If a given tablespace can't be found, we silently fall back to |
| 1676 | * the database's default tablespace. |
| 1677 | * |
| 1678 | * BUT: if the temp file is slated to outlive the current transaction, |
| 1679 | * force it into the database's default tablespace, so that it will not |
| 1680 | * pose a threat to possible tablespace drop attempts. |
| 1681 | */ |
| 1682 | if (numTempTableSpaces > 0 && !interXact) |
| 1683 | { |
| 1684 | Oid tblspcOid = GetNextTempTableSpace(); |
| 1685 | |
| 1686 | if (OidIsValid(tblspcOid)) |
| 1687 | file = OpenTemporaryFileInTablespace(tblspcOid, |
| 1688 | false, /* rejectError */ |
| 1689 | filePrefix, |
| 1690 | true, /* makenameunique */ |
| 1691 | true); /* create */ |
| 1692 | } |
| 1693 | |
| 1694 | /* |
| 1695 | * If not, or if tablespace is bad, create in database's default |
| 1696 | * tablespace. MyDatabaseTableSpace should normally be set before we get |
| 1697 | * here, but just in case it isn't, fall back to pg_default tablespace. |
| 1698 | */ |
| 1699 | if (file <= 0) |
| 1700 | file = OpenTemporaryFileInTablespace(MyDatabaseTableSpace ? |
| 1701 | MyDatabaseTableSpace : |
| 1702 | DEFAULTTABLESPACE_OID, |
| 1703 | true, |
| 1704 | filePrefix, |
| 1705 | true, /* makenameunique */ |
| 1706 | true); /* create */ |
| 1707 | |
| 1708 | /* Mark it for deletion at close and temporary file size limit */ |
| 1709 | VfdCache[file].fdstate |= FD_DELETE_AT_CLOSE | FD_TEMP_FILE_LIMIT; |
| 1710 | |
| 1711 | /* Register it with the current resource owner */ |
| 1712 | if (!interXact) |
| 1713 | RegisterTemporaryFile(file); |
| 1714 | |
| 1715 | SIMPLE_FAULT_INJECTOR("after_open_temp_file"); |
| 1716 | return file; |
| 1717 | } |
| 1718 |
no test coverage detected