<commit_before>/*-------------------------------------------------------------------------
 *
 * temprel.c
 *	  POSTGRES temporary relation handling
 *
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.28 2000/10/11 21:28:19 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */

/*
 * This implements temp tables by modifying the relname cache lookups
 * of pg_class.
 *
 * When a temp table is created, normal entries are made for it in pg_class,
 * pg_type, etc using a unique "physical" relation name.  We also make an
 * entry in the temp table list maintained by this module.  Subsequently,
 * relname lookups are filtered through the temp table list, and attempts
 * to look up a temp table name are changed to look up the physical name.
 * This allows temp table names to mask a regular table of the same name
 * for the duration of the session.  The temp table list is also used
 * to drop the underlying physical relations at session shutdown.
 */

#include <sys/types.h>

#include "postgres.h"

#include "catalog/heap.h"
#include "catalog/index.h"
#include "miscadmin.h"
#include "utils/catcache.h"
#include "utils/temprel.h"


/* ----------------
 *		global variables
 * ----------------
 */

static List *temp_rels = NIL;

typedef struct TempTable
{
	char	   *user_relname;	/* logical name of temp table */
	char	   *relname;		/* underlying unique name */
	Oid			relid;			/* needed properties of rel */
	char		relkind;
	TransactionId xid;			/* xact in which temp tab was created */
} TempTable;


/*
 * Create a temp-relation list entry given the logical temp table name
 * and the already-created pg_class tuple for the underlying relation.
 *
 * NB: we assume a check has already been made for a duplicate logical name.
 */
void
create_temp_relation(const char *relname, HeapTuple pg_class_tuple)
{
	Form_pg_class pg_class_form = (Form_pg_class) GETSTRUCT(pg_class_tuple);
	MemoryContext oldcxt;
	TempTable  *temp_rel;

	oldcxt = MemoryContextSwitchTo(CacheMemoryContext);

	temp_rel = (TempTable *) palloc(sizeof(TempTable));
	temp_rel->user_relname = (char *) palloc(NAMEDATALEN);
	temp_rel->relname = (char *) palloc(NAMEDATALEN);

	StrNCpy(temp_rel->user_relname, relname, NAMEDATALEN);
	StrNCpy(temp_rel->relname, NameStr(pg_class_form->relname), NAMEDATALEN);
	temp_rel->relid = pg_class_tuple->t_data->t_oid;
	temp_rel->relkind = pg_class_form->relkind;
	temp_rel->xid = GetCurrentTransactionId();

	temp_rels = lcons(temp_rel, temp_rels);

	MemoryContextSwitchTo(oldcxt);
}

/*
 * Remove underlying relations for all temp rels at backend shutdown.
 */
void
remove_all_temp_relations(void)
{
	AbortOutOfAnyTransaction();
	StartTransactionCommand();

	while (temp_rels != NIL)
	{
		char		relname[NAMEDATALEN];
		TempTable  *temp_rel = (TempTable *) lfirst(temp_rels);

		if (temp_rel->relkind != RELKIND_INDEX)
		{
			/* safe from deallocation */
			strcpy(relname, temp_rel->user_relname);
			heap_drop_with_catalog(relname, allowSystemTableMods);
		}
		else
			index_drop(temp_rel->relid);
		CommandCounterIncrement();
	}
	CommitTransactionCommand();
}

/*
 * Remove a temp relation map entry (part of DROP TABLE on a temp table)
 *
 * we don't have the relname for indexes, so we just pass the oid<commit_msg>Improve comments.<commit_after>/*-------------------------------------------------------------------------
 *
 * temprel.c
 *	  POSTGRES temporary relation handling
 *
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.29 2000/10/19 23:06:24 tgl Exp $
 *
 *-------------------------------------------------------------------------
 */

/*
 * This implements temp tables by modifying the relname cache lookups
 * of pg_class.
 *
 * When a temp table is created, normal entries are made for it in pg_class,
 * pg_type, etc using a unique "physical" relation name.  We also make an
 * entry in the temp table list maintained by this module.  Subsequently,
 * relname lookups are filtered through the temp table list, and attempts
 * to look up a temp table name are changed to look up the physical name.
 * This allows temp table names to mask a regular table of the same name
 * for the duration of the session.  The temp table list is also used
 * to drop the underlying physical relations at session shutdown.
 */

#include <sys/types.h>

#include "postgres.h"

#include "catalog/heap.h"
#include "catalog/index.h"
#include "miscadmin.h"
#include "utils/catcache.h"
#include "utils/temprel.h"


/* ----------------
 *		global variables
 * ----------------
 */

static List *temp_rels = NIL;

typedef struct TempTable
{
	char	   *user_relname;	/* logical name of temp table */
	char	   *relname;		/* underlying unique name */
	Oid			relid;			/* needed properties of rel */
	char		relkind;
	TransactionId xid;			/* xact in which temp tab was created */
} TempTable;


/*
 * Create a temp-relation list entry given the logical temp table name
 * and the already-created pg_class tuple for the underlying relation.
 *
 * NB: we assume a check has already been made for a duplicate logical name.
 */
void
create_temp_relation(const char *relname, HeapTuple pg_class_tuple)
{
	Form_pg_class pg_class_form = (Form_pg_class) GETSTRUCT(pg_class_tuple);
	MemoryContext oldcxt;
	TempTable  *temp_rel;

	oldcxt = MemoryContextSwitchTo(CacheMemoryContext);

	temp_rel = (TempTable *) palloc(sizeof(TempTable));
	temp_rel->user_relname = (char *) palloc(NAMEDATALEN);
	temp_rel->relname = (char *) palloc(NAMEDATALEN);

	StrNCpy(temp_rel->user_relname, relname, NAMEDATALEN);
	StrNCpy(temp_rel->relname, NameStr(pg_class_form->relname), NAMEDATALEN);
	temp_rel->relid = pg_class_tuple->t_data->t_oid;
	temp_rel->relkind = pg_class_form->relkind;
	temp_rel->xid = GetCurrentTransactionId();

	temp_rels = lcons(temp_rel, temp_rels);

	MemoryContextSwitchTo(oldcxt);
}

/*
 * Remove underlying relations for all temp rels at backend shutdown.
 */
void
remove_all_temp_relations(void)
{
	/* skip xact start overhead if nothing to do */
	if (temp_rels == NIL)
		return;

	AbortOutOfAnyTransaction();
	StartTransactionCommand();

	/*
	 * The way this works is that each time through the loop, we delete
	 * the frontmost entry.  The DROP will call remove_temp_rel_by_relid()
	 * as a side effect, thereby removing the entry in the temp_rels list.
	 * So this is not an infinite loop, even though it looks like one.
	 */
	while (temp_rels != NIL)
	{
		TempTable  *temp_rel = (TempTable *) lfirst(temp_rels);

		if (temp_rel->relkind != RELKIND_INDEX)
		{
			char		relname[NAMEDATALEN];

			/* safe from deallocation */
			strcpy(relname, temp_rel->user_relname);
			heap_drop_with_catalog(relname, allowSystemTableMods);
		}
		else
			index_drop(temp_rel->relid);
		/* advance cmd counter to make catalog changes visible */
		CommandCounterIncrement();
	}

	CommitTransactionCommand();
}

/*
 * Remove a temp relation map entry (part of DROP TABLE on a temp table)
 *
 * we don't have the relname for indexes, so we just pass the oid
{'fraction_non_alphanumeric': 0.14048008728859793, 'fraction_numerical': 0.007910529187124931, 'mean_word_length': 3.295840656121851, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6592736', 'n_tokens_mistral': 2428, 'n_tokens_neox': 2285, 'n_words': 732}