Skip to content

Commit 6666185

Browse files
committed
Use an indexscan not a heapscan to search pg_index in get_pkey_attnames.
Noted while looking for heapscans that might need to start from block zero.
1 parent d3b1b1f commit 6666185

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

contrib/dblink/dblink.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Darko Prenosil <Darko.Prenosil@finteh.hr>
99
* Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
1010
*
11-
* $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.68 2008/01/03 21:27:59 tgl Exp $
11+
* $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.69 2008/01/14 02:49:47 tgl Exp $
1212
* Copyright (c) 2001-2008, PostgreSQL Global Development Group
1313
* ALL RIGHTS RESERVED;
1414
*
@@ -38,8 +38,10 @@
3838
#include "fmgr.h"
3939
#include "funcapi.h"
4040
#include "miscadmin.h"
41+
#include "access/genam.h"
4142
#include "access/heapam.h"
4243
#include "access/tupdesc.h"
44+
#include "catalog/indexing.h"
4345
#include "catalog/namespace.h"
4446
#include "catalog/pg_index.h"
4547
#include "catalog/pg_type.h"
@@ -1662,15 +1664,18 @@ static char **
16621664
get_pkey_attnames(Oid relid, int16 *numatts)
16631665
{
16641666
Relation indexRelation;
1665-
ScanKeyData entry;
1666-
HeapScanDesc scan;
1667+
ScanKeyData skey;
1668+
SysScanDesc scan;
16671669
HeapTuple indexTuple;
16681670
int i;
16691671
char **result = NULL;
16701672
Relation rel;
16711673
TupleDesc tupdesc;
16721674
AclResult aclresult;
16731675

1676+
/* initialize numatts to 0 in case no primary key exists */
1677+
*numatts = 0;
1678+
16741679
/* open relation using relid, check permissions, get tupdesc */
16751680
rel = relation_open(relid, AccessShareLock);
16761681

@@ -1682,23 +1687,22 @@ get_pkey_attnames(Oid relid, int16 *numatts)
16821687

16831688
tupdesc = rel->rd_att;
16841689

1685-
/* initialize numatts to 0 in case no primary key exists */
1686-
*numatts = 0;
1687-
1688-
/* use relid to get all related indexes */
1690+
/* Prepare to scan pg_index for entries having indrelid = this rel. */
16891691
indexRelation = heap_open(IndexRelationId, AccessShareLock);
1690-
ScanKeyInit(&entry,
1692+
ScanKeyInit(&skey,
16911693
Anum_pg_index_indrelid,
16921694
BTEqualStrategyNumber, F_OIDEQ,
16931695
ObjectIdGetDatum(relid));
1694-
scan = heap_beginscan(indexRelation, SnapshotNow, 1, &entry);
16951696

1696-
while ((indexTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1697+
scan = systable_beginscan(indexRelation, IndexIndrelidIndexId, true,
1698+
SnapshotNow, 1, &skey);
1699+
1700+
while (HeapTupleIsValid(indexTuple = systable_getnext(scan)))
16971701
{
16981702
Form_pg_index index = (Form_pg_index) GETSTRUCT(indexTuple);
16991703

17001704
/* we're only interested if it is the primary key */
1701-
if (index->indisprimary == TRUE)
1705+
if (index->indisprimary)
17021706
{
17031707
*numatts = index->indnatts;
17041708
if (*numatts > 0)
@@ -1711,7 +1715,8 @@ get_pkey_attnames(Oid relid, int16 *numatts)
17111715
break;
17121716
}
17131717
}
1714-
heap_endscan(scan);
1718+
1719+
systable_endscan(scan);
17151720
heap_close(indexRelation, AccessShareLock);
17161721
relation_close(rel, AccessShareLock);
17171722

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy