mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-10-08 10:56:51 +02:00
c2fe139c20
Too allow table accesses to be not directly dependent on heap, several new abstractions are needed. Specifically: 1) Heap scans need to be generalized into table scans. Do this by introducing TableScanDesc, which will be the "base class" for individual AMs. This contains the AM independent fields from HeapScanDesc. The previous heap_{beginscan,rescan,endscan} et al. have been replaced with a table_ version. There's no direct replacement for heap_getnext(), as that returned a HeapTuple, which is undesirable for a other AMs. Instead there's table_scan_getnextslot(). But note that heap_getnext() lives on, it's still used widely to access catalog tables. This is achieved by new scan_begin, scan_end, scan_rescan, scan_getnextslot callbacks. 2) The portion of parallel scans that's shared between backends need to be able to do so without the user doing per-AM work. To achieve that new parallelscan_{estimate, initialize, reinitialize} callbacks are introduced, which operate on a new ParallelTableScanDesc, which again can be subclassed by AMs. As it is likely that several AMs are going to be block oriented, block oriented callbacks that can be shared between such AMs are provided and used by heap. table_block_parallelscan_{estimate, intiialize, reinitialize} as callbacks, and table_block_parallelscan_{nextpage, init} for use in AMs. These operate on a ParallelBlockTableScanDesc. 3) Index scans need to be able to access tables to return a tuple, and there needs to be state across individual accesses to the heap to store state like buffers. That's now handled by introducing a sort-of-scan IndexFetchTable, which again is intended to be subclassed by individual AMs (for heap IndexFetchHeap). The relevant callbacks for an AM are index_fetch_{end, begin, reset} to create the necessary state, and index_fetch_tuple to retrieve an indexed tuple. Note that index_fetch_tuple implementations need to be smarter than just blindly fetching the tuples for AMs that have optimizations similar to heap's HOT - the currently alive tuple in the update chain needs to be fetched if appropriate. Similar to table_scan_getnextslot(), it's undesirable to continue to return HeapTuples. Thus index_fetch_heap (might want to rename that later) now accepts a slot as an argument. Core code doesn't have a lot of call sites performing index scans without going through the systable_* API (in contrast to loads of heap_getnext calls and working directly with HeapTuples). Index scans now store the result of a search in IndexScanDesc->xs_heaptid, rather than xs_ctup->t_self. As the target is not generally a HeapTuple anymore that seems cleaner. To be able to sensible adapt code to use the above, two further callbacks have been introduced: a) slot_callbacks returns a TupleTableSlotOps* suitable for creating slots capable of holding a tuple of the AMs type. table_slot_callbacks() and table_slot_create() are based upon that, but have additional logic to deal with views, foreign tables, etc. While this change could have been done separately, nearly all the call sites that needed to be adapted for the rest of this commit also would have been needed to be adapted for table_slot_callbacks(), making separation not worthwhile. b) tuple_satisfies_snapshot checks whether the tuple in a slot is currently visible according to a snapshot. That's required as a few places now don't have a buffer + HeapTuple around, but a slot (which in heap's case internally has that information). Additionally a few infrastructure changes were needed: I) SysScanDesc, as used by systable_{beginscan, getnext} et al. now internally uses a slot to keep track of tuples. While systable_getnext() still returns HeapTuples, and will so for the foreseeable future, the index API (see 1) above) now only deals with slots. The remainder, and largest part, of this commit is then adjusting all scans in postgres to use the new APIs. Author: Andres Freund, Haribabu Kommi, Alvaro Herrera Discussion: https://postgr.es/m/20180703070645.wchpu5muyto5n647@alap3.anarazel.de https://postgr.es/m/20160812231527.GA690404@alvherre.pgsql
214 lines
5.7 KiB
C
214 lines
5.7 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* pg_conversion.c
|
|
* routines to support manipulation of the pg_conversion relation
|
|
*
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
*
|
|
* IDENTIFICATION
|
|
* src/backend/catalog/pg_conversion.c
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#include "postgres.h"
|
|
|
|
#include "access/heapam.h"
|
|
#include "access/htup_details.h"
|
|
#include "access/sysattr.h"
|
|
#include "access/tableam.h"
|
|
#include "catalog/catalog.h"
|
|
#include "catalog/dependency.h"
|
|
#include "catalog/indexing.h"
|
|
#include "catalog/objectaccess.h"
|
|
#include "catalog/pg_conversion.h"
|
|
#include "catalog/pg_namespace.h"
|
|
#include "catalog/pg_proc.h"
|
|
#include "mb/pg_wchar.h"
|
|
#include "utils/builtins.h"
|
|
#include "utils/catcache.h"
|
|
#include "utils/fmgroids.h"
|
|
#include "utils/rel.h"
|
|
#include "utils/syscache.h"
|
|
|
|
/*
|
|
* ConversionCreate
|
|
*
|
|
* Add a new tuple to pg_conversion.
|
|
*/
|
|
ObjectAddress
|
|
ConversionCreate(const char *conname, Oid connamespace,
|
|
Oid conowner,
|
|
int32 conforencoding, int32 contoencoding,
|
|
Oid conproc, bool def)
|
|
{
|
|
int i;
|
|
Relation rel;
|
|
TupleDesc tupDesc;
|
|
HeapTuple tup;
|
|
Oid oid;
|
|
bool nulls[Natts_pg_conversion];
|
|
Datum values[Natts_pg_conversion];
|
|
NameData cname;
|
|
ObjectAddress myself,
|
|
referenced;
|
|
|
|
/* sanity checks */
|
|
if (!conname)
|
|
elog(ERROR, "no conversion name supplied");
|
|
|
|
/* make sure there is no existing conversion of same name */
|
|
if (SearchSysCacheExists2(CONNAMENSP,
|
|
PointerGetDatum(conname),
|
|
ObjectIdGetDatum(connamespace)))
|
|
ereport(ERROR,
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
errmsg("conversion \"%s\" already exists", conname)));
|
|
|
|
if (def)
|
|
{
|
|
/*
|
|
* make sure there is no existing default <for encoding><to encoding>
|
|
* pair in this name space
|
|
*/
|
|
if (FindDefaultConversion(connamespace,
|
|
conforencoding,
|
|
contoencoding))
|
|
ereport(ERROR,
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
errmsg("default conversion for %s to %s already exists",
|
|
pg_encoding_to_char(conforencoding),
|
|
pg_encoding_to_char(contoencoding))));
|
|
}
|
|
|
|
/* open pg_conversion */
|
|
rel = table_open(ConversionRelationId, RowExclusiveLock);
|
|
tupDesc = rel->rd_att;
|
|
|
|
/* initialize nulls and values */
|
|
for (i = 0; i < Natts_pg_conversion; i++)
|
|
{
|
|
nulls[i] = false;
|
|
values[i] = (Datum) NULL;
|
|
}
|
|
|
|
/* form a tuple */
|
|
namestrcpy(&cname, conname);
|
|
oid = GetNewOidWithIndex(rel, ConversionOidIndexId,
|
|
Anum_pg_conversion_oid);
|
|
values[Anum_pg_conversion_oid - 1] = ObjectIdGetDatum(oid);
|
|
values[Anum_pg_conversion_conname - 1] = NameGetDatum(&cname);
|
|
values[Anum_pg_conversion_connamespace - 1] = ObjectIdGetDatum(connamespace);
|
|
values[Anum_pg_conversion_conowner - 1] = ObjectIdGetDatum(conowner);
|
|
values[Anum_pg_conversion_conforencoding - 1] = Int32GetDatum(conforencoding);
|
|
values[Anum_pg_conversion_contoencoding - 1] = Int32GetDatum(contoencoding);
|
|
values[Anum_pg_conversion_conproc - 1] = ObjectIdGetDatum(conproc);
|
|
values[Anum_pg_conversion_condefault - 1] = BoolGetDatum(def);
|
|
|
|
tup = heap_form_tuple(tupDesc, values, nulls);
|
|
|
|
/* insert a new tuple */
|
|
CatalogTupleInsert(rel, tup);
|
|
|
|
myself.classId = ConversionRelationId;
|
|
myself.objectId = oid;
|
|
myself.objectSubId = 0;
|
|
|
|
/* create dependency on conversion procedure */
|
|
referenced.classId = ProcedureRelationId;
|
|
referenced.objectId = conproc;
|
|
referenced.objectSubId = 0;
|
|
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
|
|
|
|
/* create dependency on namespace */
|
|
referenced.classId = NamespaceRelationId;
|
|
referenced.objectId = connamespace;
|
|
referenced.objectSubId = 0;
|
|
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
|
|
|
|
/* create dependency on owner */
|
|
recordDependencyOnOwner(ConversionRelationId, oid, conowner);
|
|
|
|
/* dependency on extension */
|
|
recordDependencyOnCurrentExtension(&myself, false);
|
|
|
|
/* Post creation hook for new conversion */
|
|
InvokeObjectPostCreateHook(ConversionRelationId, oid, 0);
|
|
|
|
heap_freetuple(tup);
|
|
table_close(rel, RowExclusiveLock);
|
|
|
|
return myself;
|
|
}
|
|
|
|
/*
|
|
* RemoveConversionById
|
|
*
|
|
* Remove a tuple from pg_conversion by Oid. This function is solely
|
|
* called inside catalog/dependency.c
|
|
*/
|
|
void
|
|
RemoveConversionById(Oid conversionOid)
|
|
{
|
|
Relation rel;
|
|
HeapTuple tuple;
|
|
TableScanDesc scan;
|
|
ScanKeyData scanKeyData;
|
|
|
|
ScanKeyInit(&scanKeyData,
|
|
Anum_pg_conversion_oid,
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
ObjectIdGetDatum(conversionOid));
|
|
|
|
/* open pg_conversion */
|
|
rel = table_open(ConversionRelationId, RowExclusiveLock);
|
|
|
|
scan = table_beginscan_catalog(rel, 1, &scanKeyData);
|
|
|
|
/* search for the target tuple */
|
|
if (HeapTupleIsValid(tuple = heap_getnext(scan, ForwardScanDirection)))
|
|
CatalogTupleDelete(rel, &tuple->t_self);
|
|
else
|
|
elog(ERROR, "could not find tuple for conversion %u", conversionOid);
|
|
table_endscan(scan);
|
|
table_close(rel, RowExclusiveLock);
|
|
}
|
|
|
|
/*
|
|
* FindDefaultConversion
|
|
*
|
|
* Find "default" conversion proc by for_encoding and to_encoding in the
|
|
* given namespace.
|
|
*
|
|
* If found, returns the procedure's oid, otherwise InvalidOid. Note that
|
|
* you get the procedure's OID not the conversion's OID!
|
|
*/
|
|
Oid
|
|
FindDefaultConversion(Oid name_space, int32 for_encoding, int32 to_encoding)
|
|
{
|
|
CatCList *catlist;
|
|
HeapTuple tuple;
|
|
Form_pg_conversion body;
|
|
Oid proc = InvalidOid;
|
|
int i;
|
|
|
|
catlist = SearchSysCacheList3(CONDEFAULT,
|
|
ObjectIdGetDatum(name_space),
|
|
Int32GetDatum(for_encoding),
|
|
Int32GetDatum(to_encoding));
|
|
|
|
for (i = 0; i < catlist->n_members; i++)
|
|
{
|
|
tuple = &catlist->members[i]->tuple;
|
|
body = (Form_pg_conversion) GETSTRUCT(tuple);
|
|
if (body->condefault)
|
|
{
|
|
proc = body->conproc;
|
|
break;
|
|
}
|
|
}
|
|
ReleaseSysCacheList(catlist);
|
|
return proc;
|
|
}
|