Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0c6828f

Browse files
committed
Add PublicationTable and PublicationRelInfo structs
These encapsulate a relation when referred from replication DDL. Currently they don't do anything useful (they're just wrappers around RangeVar and Relation respectively) but in the future they'll be used to carry column lists. Extracted from a larger patch by Rahila Syed. Author: Rahila Syed <rahilasyed90@gmail.com> Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Tomas Vondra <tomas.vondra@enterprisedb.com> Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Discussion: https://postgr.es/m/CAH2L28vddB_NFdRVpuyRBJEBWjz4BSyTB=_ektNRH8NJ1jf95g@mail.gmail.com
1 parent 89dba59 commit 0c6828f

File tree

9 files changed

+95
-31
lines changed

9 files changed

+95
-31
lines changed

src/backend/catalog/pg_publication.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ pg_relation_is_publishable(PG_FUNCTION_ARGS)
141141
* Insert new publication / relation mapping.
142142
*/
143143
ObjectAddress
144-
publication_add_relation(Oid pubid, Relation targetrel,
144+
publication_add_relation(Oid pubid, PublicationRelInfo *targetrel,
145145
bool if_not_exists)
146146
{
147147
Relation rel;
148148
HeapTuple tup;
149149
Datum values[Natts_pg_publication_rel];
150150
bool nulls[Natts_pg_publication_rel];
151-
Oid relid = RelationGetRelid(targetrel);
151+
Oid relid = RelationGetRelid(targetrel->relation);
152152
Oid prrelid;
153153
Publication *pub = GetPublication(pubid);
154154
ObjectAddress myself,
@@ -172,10 +172,10 @@ publication_add_relation(Oid pubid, Relation targetrel,
172172
ereport(ERROR,
173173
(errcode(ERRCODE_DUPLICATE_OBJECT),
174174
errmsg("relation \"%s\" is already member of publication \"%s\"",
175-
RelationGetRelationName(targetrel), pub->name)));
175+
RelationGetRelationName(targetrel->relation), pub->name)));
176176
}
177177

178-
check_publication_add_relation(targetrel);
178+
check_publication_add_relation(targetrel->relation);
179179

180180
/* Form a tuple. */
181181
memset(values, 0, sizeof(values));
@@ -209,7 +209,7 @@ publication_add_relation(Oid pubid, Relation targetrel,
209209
table_close(rel, RowExclusiveLock);
210210

211211
/* Invalidate relcache so that publication info is rebuilt. */
212-
CacheInvalidateRelcache(targetrel);
212+
CacheInvalidateRelcache(targetrel->relation);
213213

214214
return myself;
215215
}

src/backend/commands/publicationcmds.c

+34-19
Original file line numberDiff line numberDiff line change
@@ -393,21 +393,28 @@ AlterPublicationTables(AlterPublicationStmt *stmt, Relation rel,
393393

394394
foreach(newlc, rels)
395395
{
396-
Relation newrel = (Relation) lfirst(newlc);
396+
PublicationRelInfo *newpubrel;
397397

398-
if (RelationGetRelid(newrel) == oldrelid)
398+
newpubrel = (PublicationRelInfo *) lfirst(newlc);
399+
if (RelationGetRelid(newpubrel->relation) == oldrelid)
399400
{
400401
found = true;
401402
break;
402403
}
403404
}
404-
405+
/* Not yet in the list, open it and add to the list */
405406
if (!found)
406407
{
407-
Relation oldrel = table_open(oldrelid,
408-
ShareUpdateExclusiveLock);
408+
Relation oldrel;
409+
PublicationRelInfo *pubrel;
410+
411+
/* Wrap relation into PublicationRelInfo */
412+
oldrel = table_open(oldrelid, ShareUpdateExclusiveLock);
413+
414+
pubrel = palloc(sizeof(PublicationRelInfo));
415+
pubrel->relation = oldrel;
409416

410-
delrels = lappend(delrels, oldrel);
417+
delrels = lappend(delrels, pubrel);
411418
}
412419
}
413420

@@ -498,9 +505,9 @@ RemovePublicationRelById(Oid proid)
498505
}
499506

500507
/*
501-
* Open relations specified by a RangeVar list.
502-
* The returned tables are locked in ShareUpdateExclusiveLock mode in order to
503-
* add them to a publication.
508+
* Open relations specified by a PublicationTable list.
509+
* In the returned list of PublicationRelInfo, tables are locked
510+
* in ShareUpdateExclusiveLock mode in order to add them to a publication.
504511
*/
505512
static List *
506513
OpenTableList(List *tables)
@@ -514,15 +521,16 @@ OpenTableList(List *tables)
514521
*/
515522
foreach(lc, tables)
516523
{
517-
RangeVar *rv = lfirst_node(RangeVar, lc);
518-
bool recurse = rv->inh;
524+
PublicationTable *t = lfirst_node(PublicationTable, lc);
525+
bool recurse = t->relation->inh;
519526
Relation rel;
520527
Oid myrelid;
528+
PublicationRelInfo *pub_rel;
521529

522530
/* Allow query cancel in case this takes a long time */
523531
CHECK_FOR_INTERRUPTS();
524532

525-
rel = table_openrv(rv, ShareUpdateExclusiveLock);
533+
rel = table_openrv(t->relation, ShareUpdateExclusiveLock);
526534
myrelid = RelationGetRelid(rel);
527535

528536
/*
@@ -538,7 +546,9 @@ OpenTableList(List *tables)
538546
continue;
539547
}
540548

541-
rels = lappend(rels, rel);
549+
pub_rel = palloc(sizeof(PublicationRelInfo));
550+
pub_rel->relation = rel;
551+
rels = lappend(rels, pub_rel);
542552
relids = lappend_oid(relids, myrelid);
543553

544554
/*
@@ -571,7 +581,9 @@ OpenTableList(List *tables)
571581

572582
/* find_all_inheritors already got lock */
573583
rel = table_open(childrelid, NoLock);
574-
rels = lappend(rels, rel);
584+
pub_rel = palloc(sizeof(PublicationRelInfo));
585+
pub_rel->relation = rel;
586+
rels = lappend(rels, pub_rel);
575587
relids = lappend_oid(relids, childrelid);
576588
}
577589
}
@@ -592,9 +604,10 @@ CloseTableList(List *rels)
592604

593605
foreach(lc, rels)
594606
{
595-
Relation rel = (Relation) lfirst(lc);
607+
PublicationRelInfo *pub_rel;
596608

597-
table_close(rel, NoLock);
609+
pub_rel = (PublicationRelInfo *) lfirst(lc);
610+
table_close(pub_rel->relation, NoLock);
598611
}
599612
}
600613

@@ -611,15 +624,16 @@ PublicationAddTables(Oid pubid, List *rels, bool if_not_exists,
611624

612625
foreach(lc, rels)
613626
{
614-
Relation rel = (Relation) lfirst(lc);
627+
PublicationRelInfo *pub_rel = (PublicationRelInfo *) lfirst(lc);
628+
Relation rel = pub_rel->relation;
615629
ObjectAddress obj;
616630

617631
/* Must be owner of the table or superuser. */
618632
if (!pg_class_ownercheck(RelationGetRelid(rel), GetUserId()))
619633
aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(rel->rd_rel->relkind),
620634
RelationGetRelationName(rel));
621635

622-
obj = publication_add_relation(pubid, rel, if_not_exists);
636+
obj = publication_add_relation(pubid, pub_rel, if_not_exists);
623637
if (stmt)
624638
{
625639
EventTriggerCollectSimpleCommand(obj, InvalidObjectAddress,
@@ -643,7 +657,8 @@ PublicationDropTables(Oid pubid, List *rels, bool missing_ok)
643657

644658
foreach(lc, rels)
645659
{
646-
Relation rel = (Relation) lfirst(lc);
660+
PublicationRelInfo *pubrel = (PublicationRelInfo *) lfirst(lc);
661+
Relation rel = pubrel->relation;
647662
Oid relid = RelationGetRelid(rel);
648663

649664
prid = GetSysCacheOid2(PUBLICATIONRELMAP, Anum_pg_publication_rel_oid,

src/backend/nodes/copyfuncs.c

+12
Original file line numberDiff line numberDiff line change
@@ -4939,6 +4939,15 @@ _copyForeignKeyCacheInfo(const ForeignKeyCacheInfo *from)
49394939
return newnode;
49404940
}
49414941

4942+
static PublicationTable *
4943+
_copyPublicationTable(const PublicationTable *from)
4944+
{
4945+
PublicationTable *newnode = makeNode(PublicationTable);
4946+
4947+
COPY_NODE_FIELD(relation);
4948+
4949+
return newnode;
4950+
}
49424951

49434952
/*
49444953
* copyObjectImpl -- implementation of copyObject(); see nodes/nodes.h
@@ -5854,6 +5863,9 @@ copyObjectImpl(const void *from)
58545863
case T_PartitionCmd:
58555864
retval = _copyPartitionCmd(from);
58565865
break;
5866+
case T_PublicationTable:
5867+
retval = _copyPublicationTable(from);
5868+
break;
58575869

58585870
/*
58595871
* MISCELLANEOUS NODES

src/backend/nodes/equalfuncs.c

+11
Original file line numberDiff line numberDiff line change
@@ -3114,6 +3114,14 @@ _equalValue(const Value *a, const Value *b)
31143114
return true;
31153115
}
31163116

3117+
static bool
3118+
_equalPublicationTable(const PublicationTable *a, const PublicationTable *b)
3119+
{
3120+
COMPARE_NODE_FIELD(relation);
3121+
3122+
return true;
3123+
}
3124+
31173125
/*
31183126
* equal
31193127
* returns whether two nodes are equal
@@ -3862,6 +3870,9 @@ equal(const void *a, const void *b)
38623870
case T_PartitionCmd:
38633871
retval = _equalPartitionCmd(a, b);
38643872
break;
3873+
case T_PublicationTable:
3874+
retval = _equalPublicationTable(a, b);
3875+
break;
38653876

38663877
default:
38673878
elog(ERROR, "unrecognized node type: %d",

src/backend/parser/gram.y

+20-6
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,14 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
426426
transform_element_list transform_type_list
427427
TriggerTransitions TriggerReferencing
428428
vacuum_relation_list opt_vacuum_relation_list
429-
drop_option_list
429+
drop_option_list publication_table_list
430430

431431
%type <node> opt_routine_body
432432
%type <groupclause> group_clause
433433
%type <list> group_by_list
434434
%type <node> group_by_item empty_grouping_set rollup_clause cube_clause
435435
%type <node> grouping_sets_clause
436-
%type <node> opt_publication_for_tables publication_for_tables
436+
%type <node> opt_publication_for_tables publication_for_tables publication_table
437437

438438
%type <list> opt_fdw_options fdw_options
439439
%type <defelt> fdw_option
@@ -9620,7 +9620,7 @@ opt_publication_for_tables:
96209620
;
96219621

96229622
publication_for_tables:
9623-
FOR TABLE relation_expr_list
9623+
FOR TABLE publication_table_list
96249624
{
96259625
$$ = (Node *) $3;
96269626
}
@@ -9630,6 +9630,20 @@ publication_for_tables:
96309630
}
96319631
;
96329632

9633+
publication_table_list:
9634+
publication_table
9635+
{ $$ = list_make1($1); }
9636+
| publication_table_list ',' publication_table
9637+
{ $$ = lappend($1, $3); }
9638+
;
9639+
9640+
publication_table: relation_expr
9641+
{
9642+
PublicationTable *n = makeNode(PublicationTable);
9643+
n->relation = $1;
9644+
$$ = (Node *) n;
9645+
}
9646+
;
96339647

96349648
/*****************************************************************************
96359649
*
@@ -9651,23 +9665,23 @@ AlterPublicationStmt:
96519665
n->options = $5;
96529666
$$ = (Node *)n;
96539667
}
9654-
| ALTER PUBLICATION name ADD_P TABLE relation_expr_list
9668+
| ALTER PUBLICATION name ADD_P TABLE publication_table_list
96559669
{
96569670
AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
96579671
n->pubname = $3;
96589672
n->tables = $6;
96599673
n->tableAction = DEFELEM_ADD;
96609674
$$ = (Node *)n;
96619675
}
9662-
| ALTER PUBLICATION name SET TABLE relation_expr_list
9676+
| ALTER PUBLICATION name SET TABLE publication_table_list
96639677
{
96649678
AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
96659679
n->pubname = $3;
96669680
n->tables = $6;
96679681
n->tableAction = DEFELEM_SET;
96689682
$$ = (Node *)n;
96699683
}
9670-
| ALTER PUBLICATION name DROP TABLE relation_expr_list
9684+
| ALTER PUBLICATION name DROP TABLE publication_table_list
96719685
{
96729686
AlterPublicationStmt *n = makeNode(AlterPublicationStmt);
96739687
n->pubname = $3;

src/include/catalog/pg_publication.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ typedef struct Publication
8383
PublicationActions pubactions;
8484
} Publication;
8585

86+
typedef struct PublicationRelInfo
87+
{
88+
Relation relation;
89+
} PublicationRelInfo;
90+
8691
extern Publication *GetPublication(Oid pubid);
8792
extern Publication *GetPublicationByName(const char *pubname, bool missing_ok);
8893
extern List *GetRelationPublications(Oid relid);
@@ -108,7 +113,7 @@ extern List *GetAllTablesPublications(void);
108113
extern List *GetAllTablesPublicationRelations(bool pubviaroot);
109114

110115
extern bool is_publishable_relation(Relation rel);
111-
extern ObjectAddress publication_add_relation(Oid pubid, Relation targetrel,
116+
extern ObjectAddress publication_add_relation(Oid pubid, PublicationRelInfo *targetrel,
112117
bool if_not_exists);
113118

114119
extern Oid get_publication_oid(const char *pubname, bool missing_ok);

src/include/nodes/nodes.h

+1
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ typedef enum NodeTag
490490
T_PartitionRangeDatum,
491491
T_PartitionCmd,
492492
T_VacuumRelation,
493+
T_PublicationTable,
493494

494495
/*
495496
* TAGS FOR REPLICATION GRAMMAR PARSE NODES (replnodes.h)

src/include/nodes/parsenodes.h

+5
Original file line numberDiff line numberDiff line change
@@ -3624,6 +3624,11 @@ typedef struct AlterTSConfigurationStmt
36243624
bool missing_ok; /* for DROP - skip error if missing? */
36253625
} AlterTSConfigurationStmt;
36263626

3627+
typedef struct PublicationTable
3628+
{
3629+
NodeTag type;
3630+
RangeVar *relation; /* relation to be published */
3631+
} PublicationTable;
36273632

36283633
typedef struct CreatePublicationStmt
36293634
{

src/tools/pgindent/typedefs.list

+1
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,7 @@ PublicationActions
20472047
PublicationInfo
20482048
PublicationPartOpt
20492049
PublicationRelInfo
2050+
PublicationTable
20502051
PullFilter
20512052
PullFilterOps
20522053
PushFilter

0 commit comments

Comments
 (0)